forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_util.cc
213 lines (177 loc) · 7.68 KB
/
cache_util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/disk_cache/cache_util.h"
#include <limits>
#include "base/bind.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
namespace {
const int kMaxOldFolders = 100;
// Returns a fully qualified name from path and name, using a given name prefix
// and index number. For instance, if the arguments are "/foo", "bar" and 5, it
// will return "/foo/old_bar_005".
base::FilePath GetPrefixedName(const base::FilePath& path,
const std::string& name,
int index) {
std::string tmp = base::StringPrintf("%s%s_%03d", "old_",
name.c_str(), index);
return path.AppendASCII(tmp);
}
// This is a simple callback to cleanup old caches.
void CleanupCallback(const base::FilePath& path, const std::string& name) {
for (int i = 0; i < kMaxOldFolders; i++) {
base::FilePath to_delete = GetPrefixedName(path, name, i);
disk_cache::DeleteCache(to_delete, true);
}
}
// Returns a full path to rename the current cache, in order to delete it. path
// is the current folder location, and name is the current folder name.
base::FilePath GetTempCacheName(const base::FilePath& path,
const std::string& name) {
// We'll attempt to have up to kMaxOldFolders folders for deletion.
for (int i = 0; i < kMaxOldFolders; i++) {
base::FilePath to_delete = GetPrefixedName(path, name, i);
if (!base::PathExists(to_delete))
return to_delete;
}
return base::FilePath();
}
int64_t PreferredCacheSizeInternal(int64_t available) {
using disk_cache::kDefaultCacheSize;
// Return 80% of the available space if there is not enough space to use
// kDefaultCacheSize.
if (available < kDefaultCacheSize * 10 / 8)
return available * 8 / 10;
// Return kDefaultCacheSize if it uses 10% to 80% of the available space.
if (available < kDefaultCacheSize * 10)
return kDefaultCacheSize;
// Return 10% of the available space if the target size
// (2.5 * kDefaultCacheSize) is more than 10%.
if (available < static_cast<int64_t>(kDefaultCacheSize) * 25)
return available / 10;
// Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1%
// of the available space.
if (available < static_cast<int64_t>(kDefaultCacheSize) * 250)
return kDefaultCacheSize * 5 / 2;
// Return 1% of the available space.
return available / 100;
}
} // namespace
namespace disk_cache {
const int kDefaultCacheSize = 80 * 1024 * 1024;
const base::Feature kChangeDiskCacheSizeExperiment{
"ChangeDiskCacheSize", base::FEATURE_DISABLED_BY_DEFAULT};
void DeleteCache(const base::FilePath& path, bool remove_folder) {
if (remove_folder) {
if (!base::DeletePathRecursively(path))
LOG(WARNING) << "Unable to delete cache folder.";
return;
}
base::FileEnumerator iter(
path,
/* recursive */ false,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
for (base::FilePath file = iter.Next(); !file.value().empty();
file = iter.Next()) {
if (!base::DeletePathRecursively(file)) {
LOG(WARNING) << "Unable to delete cache.";
return;
}
}
}
// In order to process a potentially large number of files, we'll rename the
// cache directory to old_ + original_name + number, (located on the same parent
// directory), and use a worker thread to delete all the files on all the stale
// cache directories. The whole process can still fail if we are not able to
// rename the cache directory (for instance due to a sharing violation), and in
// that case a cache for this profile (on the desired path) cannot be created.
bool DelayedCacheCleanup(const base::FilePath& full_path) {
// GetTempCacheName() and MoveCache() use synchronous file operations.
base::ThreadRestrictions::ScopedAllowIO allow_io;
// We can exit early if nothing was done/the directory is empty.
if (base::IsDirectoryEmpty(full_path))
return true;
base::FilePath current_path = full_path.StripTrailingSeparators();
base::FilePath path = current_path.DirName();
base::FilePath name = current_path.BaseName();
#if defined(OS_WIN)
// We created this file so it should only contain ASCII.
std::string name_str = base::WideToASCII(name.value());
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
std::string name_str = name.value();
#endif
base::FilePath to_delete = GetTempCacheName(path, name_str);
if (to_delete.empty()) {
LOG(ERROR) << "Unable to get another cache folder";
return false;
}
if (!disk_cache::MoveCache(full_path, to_delete)) {
LOG(ERROR) << "Unable to move cache folder " << full_path.value() << " to "
<< to_delete.value();
return false;
}
base::ThreadPool::PostTask(FROM_HERE,
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&CleanupCallback, path, name_str));
return true;
}
// Returns the preferred maximum number of bytes for the cache given the
// number of available bytes.
int PreferredCacheSize(int64_t available, net::CacheType type) {
// Percent of cache size to use, relative to the default size. "100" means to
// use 100% of the default size.
int percent_relative_size = 100;
if (base::FeatureList::IsEnabled(
disk_cache::kChangeDiskCacheSizeExperiment) &&
type == net::DISK_CACHE) {
percent_relative_size = base::GetFieldTrialParamByFeatureAsInt(
disk_cache::kChangeDiskCacheSizeExperiment, "percent_relative_size",
100 /* default value */);
}
// Cap scaling, as a safety check, to avoid overflow.
if (percent_relative_size > 400)
percent_relative_size = 400;
else if (percent_relative_size < 100)
percent_relative_size = 100;
int64_t scaled_default_disk_cache_size =
(static_cast<int64_t>(disk_cache::kDefaultCacheSize) *
percent_relative_size) /
100;
if (available < 0)
return static_cast<int32_t>(scaled_default_disk_cache_size);
int64_t preferred_cache_size = PreferredCacheSizeInternal(available);
// If the preferred cache size is less than 20% of the available space, scale
// for the field trial, capping the scaled value at 20% of the available
// space.
if (preferred_cache_size < available / 5) {
preferred_cache_size = std::min(
(preferred_cache_size * percent_relative_size) / 100, available / 5);
}
// Limit cache size to somewhat less than kint32max to avoid potential
// integer overflows in cache backend implementations.
//
// Note: the 4x limit is of course far below that; historically it came
// from the blockfile backend with the following explanation:
// "Let's not use more than the default size while we tune-up the performance
// of bigger caches. "
int64_t size_limit = scaled_default_disk_cache_size * 4;
// Native code entries can be large, so we would like a larger cache.
// Make the size limit 50% larger in that case.
if (type == net::GENERATED_NATIVE_CODE_CACHE) {
size_limit = (size_limit / 2) * 3;
}
DCHECK_LT(size_limit, std::numeric_limits<int32_t>::max());
return static_cast<int32_t>(std::min(preferred_cache_size, size_limit));
}
} // namespace disk_cache