OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/experiments/memory_ablation_experiment.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/debug/alias.h" |
| 10 #include "base/metrics/field_trial_params.h" |
| 11 #include "base/process/process_metrics.h" |
| 12 #include "base/sequenced_task_runner.h" |
| 13 |
| 14 const base::Feature kMemoryAblationFeature{"MemoryAblation", |
| 15 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 16 |
| 17 const char kMemoryAblationFeatureSizeParam[] = "Size"; |
| 18 |
| 19 constexpr size_t kMemoryAblationDelaySeconds = 5; |
| 20 |
| 21 MemoryAblationExperiment::MemoryAblationExperiment() {} |
| 22 |
| 23 MemoryAblationExperiment::~MemoryAblationExperiment() {} |
| 24 |
| 25 MemoryAblationExperiment* MemoryAblationExperiment::GetInstance() { |
| 26 static auto* instance = new MemoryAblationExperiment(); |
| 27 return instance; |
| 28 } |
| 29 |
| 30 void MemoryAblationExperiment::MaybeStart( |
| 31 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| 32 int size = base::GetFieldTrialParamByFeatureAsInt( |
| 33 kMemoryAblationFeature, kMemoryAblationFeatureSizeParam, |
| 34 0 /* default value */); |
| 35 if (size > 0) { |
| 36 GetInstance()->Start(task_runner, size); |
| 37 } |
| 38 } |
| 39 |
| 40 void MemoryAblationExperiment::Start( |
| 41 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 42 size_t memory_size) { |
| 43 task_runner->PostDelayedTask( |
| 44 FROM_HERE, |
| 45 base::Bind(&MemoryAblationExperiment::AllocateMemory, |
| 46 base::Unretained(this), memory_size), |
| 47 base::TimeDelta::FromSeconds(kMemoryAblationDelaySeconds)); |
| 48 } |
| 49 |
| 50 void MemoryAblationExperiment::AllocateMemory(size_t size) { |
| 51 memory_size_ = size; |
| 52 memory_.reset(new uint8_t[size]); |
| 53 TouchMemory(); |
| 54 } |
| 55 |
| 56 void MemoryAblationExperiment::TouchMemory() { |
| 57 if (memory_) { |
| 58 size_t page_size = base::GetPageSize(); |
| 59 auto* memory = static_cast<volatile uint8_t*>(memory_.get()); |
| 60 for (size_t i = 0; i < memory_size_; i += page_size) { |
| 61 memory[i] = i; |
| 62 } |
| 63 base::debug::Alias(memory_.get()); |
| 64 } |
| 65 } |
OLD | NEW |