OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/experiments/memory_ablation_experiment.h" | 5 #include "chrome/browser/experiments/memory_ablation_experiment.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <limits> | 8 #include <limits> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/debug/alias.h" | 12 #include "base/debug/alias.h" |
12 #include "base/metrics/field_trial_params.h" | 13 #include "base/metrics/field_trial_params.h" |
13 #include "base/process/process_metrics.h" | 14 #include "base/process/process_metrics.h" |
14 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
15 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
16 | 17 |
17 const base::Feature kMemoryAblationFeature{"MemoryAblation", | 18 const base::Feature kMemoryAblationFeature{"MemoryAblation", |
18 base::FEATURE_DISABLED_BY_DEFAULT}; | 19 base::FEATURE_DISABLED_BY_DEFAULT}; |
19 | 20 |
20 const char kMemoryAblationFeatureSizeParam[] = "Size"; | 21 const char kMemoryAblationFeatureSizeParam[] = "Size"; |
21 const char kMemoryAblationFeatureMinRAMParam[] = "MinRAM"; | 22 const char kMemoryAblationFeatureMinRAMParam[] = "MinRAM"; |
22 const char kMemoryAblationFeatureMaxRAMParam[] = "MaxRAM"; | 23 const char kMemoryAblationFeatureMaxRAMParam[] = "MaxRAM"; |
23 | 24 |
24 constexpr size_t kMemoryAblationDelaySeconds = 5; | 25 // Since MaybeStart() is called during startup, we delay the allocation |
26 // to avoid slowing things down. | |
27 constexpr size_t kAllocationDelaySeconds = 5; | |
28 | |
29 // We need to touch allocated memory to "dirty" it. However, touching | |
30 // large chunks of memory (even a single byte per page) can be costly | |
31 // (~60ms per 10MiB on Nexus 5). So we touch in chunks to allow other | |
32 // things to happen in between. With the following values we'll touch | |
33 // 2MiB per second, spending ~10% of 250ms time slice per chunk. | |
34 constexpr size_t kTouchDelayMilliseconds = 250; | |
35 constexpr size_t kTouchChunkSize = 512 * 1024; | |
25 | 36 |
26 MemoryAblationExperiment::MemoryAblationExperiment() {} | 37 MemoryAblationExperiment::MemoryAblationExperiment() {} |
27 | 38 |
28 MemoryAblationExperiment::~MemoryAblationExperiment() {} | 39 MemoryAblationExperiment::~MemoryAblationExperiment() {} |
29 | 40 |
30 MemoryAblationExperiment* MemoryAblationExperiment::GetInstance() { | 41 MemoryAblationExperiment* MemoryAblationExperiment::GetInstance() { |
31 static auto* instance = new MemoryAblationExperiment(); | 42 static auto* instance = new MemoryAblationExperiment(); |
32 return instance; | 43 return instance; |
33 } | 44 } |
34 | 45 |
(...skipping 14 matching lines...) Expand all Loading... | |
49 kMemoryAblationFeature, kMemoryAblationFeatureSizeParam, | 60 kMemoryAblationFeature, kMemoryAblationFeatureSizeParam, |
50 0 /* default value */); | 61 0 /* default value */); |
51 if (size > 0) { | 62 if (size > 0) { |
52 GetInstance()->Start(task_runner, size); | 63 GetInstance()->Start(task_runner, size); |
53 } | 64 } |
54 } | 65 } |
55 | 66 |
56 void MemoryAblationExperiment::Start( | 67 void MemoryAblationExperiment::Start( |
57 scoped_refptr<base::SequencedTaskRunner> task_runner, | 68 scoped_refptr<base::SequencedTaskRunner> task_runner, |
58 size_t memory_size) { | 69 size_t memory_size) { |
59 task_runner->PostDelayedTask( | 70 DCHECK(task_runner_ == nullptr) << "Already started"; |
71 task_runner_ = task_runner; | |
72 task_runner_->PostDelayedTask( | |
60 FROM_HERE, | 73 FROM_HERE, |
61 base::BindOnce(&MemoryAblationExperiment::AllocateMemory, | 74 base::BindOnce(&MemoryAblationExperiment::AllocateMemory, |
62 base::Unretained(this), memory_size), | 75 base::Unretained(this), memory_size), |
63 base::TimeDelta::FromSeconds(kMemoryAblationDelaySeconds)); | 76 base::TimeDelta::FromSeconds(kAllocationDelaySeconds)); |
64 } | 77 } |
65 | 78 |
66 void MemoryAblationExperiment::AllocateMemory(size_t size) { | 79 void MemoryAblationExperiment::AllocateMemory(size_t size) { |
67 memory_size_ = size; | 80 memory_size_ = size; |
68 memory_.reset(new uint8_t[size]); | 81 memory_.reset(new uint8_t[size]); |
69 TouchMemory(); | 82 ScheduleTouchMemory(0); |
70 } | 83 } |
71 | 84 |
72 void MemoryAblationExperiment::TouchMemory() { | 85 void MemoryAblationExperiment::TouchMemory(size_t offset) { |
73 if (memory_) { | 86 if (memory_) { |
74 size_t page_size = base::GetPageSize(); | 87 size_t page_size = base::GetPageSize(); |
75 auto* memory = static_cast<volatile uint8_t*>(memory_.get()); | 88 auto* memory = static_cast<volatile uint8_t*>(memory_.get()); |
76 for (size_t i = 0; i < memory_size_; i += page_size) { | 89 size_t max_offset = std::min(offset + kTouchChunkSize, memory_size_); |
77 memory[i] = i; | 90 for (; offset < max_offset; offset += page_size) { |
91 memory[offset] = static_cast<uint8_t>(offset); | |
78 } | 92 } |
79 base::debug::Alias(memory_.get()); | 93 base::debug::Alias(memory_.get()); |
94 if (offset < memory_size_) { | |
95 ScheduleTouchMemory(offset); | |
96 } | |
80 } | 97 } |
81 } | 98 } |
99 | |
100 void MemoryAblationExperiment::ScheduleTouchMemory(size_t offset) { | |
101 task_runner_->PostDelayedTask( | |
brettw
2017/05/19 20:41:48
Can you comment here that this task is a singleton
| |
102 FROM_HERE, | |
103 base::BindOnce(&MemoryAblationExperiment::TouchMemory, | |
104 base::Unretained(this), offset), | |
105 base::TimeDelta::FromMilliseconds(kTouchDelayMilliseconds)); | |
106 } | |
OLD | NEW |