[go: nahoru, domu]

blob: d428822d61c370d75650c1165fa15482cc2f7345 [file] [log] [blame]
michaeln10e5fc352017-02-07 02:07:581// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "storage/browser/quota/quota_settings.h"
6
7#include <algorithm>
8
9#include "base/metrics/histogram_macros.h"
michaelnfa4c89402017-04-11 02:36:2010#include "base/rand_util.h"
michaeln10e5fc352017-02-07 02:07:5811#include "base/sys_info.h"
12
13#define UMA_HISTOGRAM_MBYTES(name, sample) \
14 UMA_HISTOGRAM_CUSTOM_COUNTS((name), static_cast<int>((sample) / kMBytes), 1, \
15 10 * 1024 * 1024 /* 10TB */, 100)
16
17namespace storage {
18
michaelnfa4c89402017-04-11 02:36:2019namespace {
20
21// Skews |value| by +/- |percent|.
22int64_t RandomizeByPercent(int64_t value, int percent) {
23 double random_percent = (base::RandDouble() - 0.5) * percent * 2;
24 return value + (value * (random_percent / 100.0));
25}
26
27} // anonymous namespace
28
michaeln10e5fc352017-02-07 02:07:5829base::Optional<storage::QuotaSettings> CalculateNominalDynamicSettings(
30 const base::FilePath& partition_path,
31 bool is_incognito) {
32 const int64_t kMBytes = 1024 * 1024;
michaelnfa4c89402017-04-11 02:36:2033 const int kRandomizedPercentage = 10;
michaeln10e5fc352017-02-07 02:07:5834
35 if (is_incognito) {
michaelnfa4c89402017-04-11 02:36:2036 // The incognito pool size is a fraction of the amount of system memory,
37 // and the amount is capped to a hard limit.
38 const double kIncognitoPoolSizeRatio = 0.1; // 10%
39 const int64_t kMaxIncognitoPoolSize = 300 * kMBytes;
40
michaeln10e5fc352017-02-07 02:07:5841 storage::QuotaSettings settings;
michaelnfa4c89402017-04-11 02:36:2042 settings.pool_size = std::min(
43 RandomizeByPercent(kMaxIncognitoPoolSize, kRandomizedPercentage),
44 static_cast<int64_t>(base::SysInfo::AmountOfPhysicalMemory() *
45 kIncognitoPoolSizeRatio));
michaeln10e5fc352017-02-07 02:07:5846 settings.per_host_quota = settings.pool_size / 3;
michaelnfa4c89402017-04-11 02:36:2047 settings.session_only_per_host_quota = settings.per_host_quota;
michaeln10e5fc352017-02-07 02:07:5848 settings.refresh_interval = base::TimeDelta::Max();
49 return settings;
50 }
51
52 // The fraction of the device's storage the browser is willing to
53 // use for temporary storage, this is applied after adjusting the
54 // total to take os_accomodation into account.
55 const double kTemporaryPoolSizeRatio = 1.0 / 3.0; // 33%
56
57 // The fraction of the device's storage the browser attempts to
58 // keep free.
59 const double kShouldRemainAvailableRatio = 0.1; // 10%
60
61 // The fraction of the device's storage the browser attempts to
62 // keep free at all costs.
63 const double kMustRemainAvailableRatio = 0.01; // 1%
64
65 // Determines the portion of the temp pool that can be
66 // utilized by a single host (ie. 5 for 20%).
67 const int kPerHostTemporaryPortion = 5;
68
michaelnfa4c89402017-04-11 02:36:2069 // SessionOnly (or ephemeral) origins are allotted a fraction of what
70 // normal origins are provided, and the amount is capped to a hard limit.
71 const double kSessionOnlyHostQuotaRatio = 0.1; // 10%
72 const int64_t kMaxSessionOnlyHostQuota = 300 * kMBytes;
73
michaeln10e5fc352017-02-07 02:07:5874 // os_accomodation is an estimate of how much storage is needed for
75 // the os and essential application code outside of the browser.
76 const int64_t kDefaultOSAccomodation =
77#if defined(OS_ANDROID)
78 1000 * kMBytes;
79#elif defined(OS_CHROMEOS)
80 1000 * kMBytes;
81#elif defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX)
82 10000 * kMBytes;
83#else
84#error "Port: Need to define an OS accomodation value for unknown OS."
85#endif
86
87 storage::QuotaSettings settings;
88
89 int64_t total = base::SysInfo::AmountOfTotalDiskSpace(partition_path);
90 if (total == -1) {
91 LOG(ERROR) << "Unable to compute QuotaSettings.";
92 return base::nullopt;
93 }
94
95 // If our hardcoded OS accomodation is too large for the volume size, define
96 // the value as a fraction of the total volume size instead.
97 int64_t os_accomodation =
98 std::min(kDefaultOSAccomodation, static_cast<int64_t>(total * 0.8));
99 UMA_HISTOGRAM_MBYTES("Quota.OSAccomodationDelta",
100 kDefaultOSAccomodation - os_accomodation);
101
102 int64_t adjusted_total = total - os_accomodation;
103 int64_t pool_size = adjusted_total * kTemporaryPoolSizeRatio;
104
105 settings.pool_size = pool_size;
106 settings.should_remain_available = total * kShouldRemainAvailableRatio;
107 settings.must_remain_available = total * kMustRemainAvailableRatio;
108 settings.per_host_quota = pool_size / kPerHostTemporaryPortion;
michaelnfa4c89402017-04-11 02:36:20109 settings.session_only_per_host_quota = std::min(
110 RandomizeByPercent(kMaxSessionOnlyHostQuota, kRandomizedPercentage),
111 static_cast<int64_t>(settings.per_host_quota *
112 kSessionOnlyHostQuotaRatio));
michaeln10e5fc352017-02-07 02:07:58113 settings.refresh_interval = base::TimeDelta::FromSeconds(60);
114 return settings;
115}
116
117} // namespace