Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(755)

Side by Side Diff: chrome/browser/experiments/memory_ablation_experiment.cc

Issue 2810833002: Add memory ablation experiment. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
Primiano Tucci (use gerrit) 2017/04/10 18:05:04 do you need a class at all here? It seems all this
DmitrySkiba 2017/04/10 19:31:00 I think class is more flexible, for example we mig
Primiano Tucci (use gerrit) 2017/04/10 19:40:11 A good rule of thumb is don't introduce unnecessar
DmitrySkiba 2017/04/10 19:51:41 I mean, we might end up doing those things in this
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/metrics/field_trial_params.h"
10 #include "base/process/process_metrics.h"
11
12 const base::Feature kMemoryAblationFeature{"MemoryAblation",
13 base::FEATURE_DISABLED_BY_DEFAULT};
14
15 const char kMemoryAblationFeatureSizeParam[] = "Size";
16
17 constexpr size_t kMemoryAblationDelaySeconds = 5;
18
19 MemoryAblationExperiment::MemoryAblationExperiment()
20 : memory_(nullptr, &free) {}
21
22 MemoryAblationExperiment::~MemoryAblationExperiment() {}
23
24 MemoryAblationExperiment* MemoryAblationExperiment::GetInstance() {
25 static auto* instance = new MemoryAblationExperiment();
26 return instance;
27 }
28
29 void MemoryAblationExperiment::MaybeStart(
30 scoped_refptr<base::SequencedTaskRunner> task_runner) {
31 int size = base::GetFieldTrialParamByFeatureAsInt(
32 kMemoryAblationFeature, kMemoryAblationFeatureSizeParam,
33 0 /* default value */);
34 if (size > 0) {
35 GetInstance()->Start(task_runner, size);
36 }
37 }
38
39 void MemoryAblationExperiment::Start(
40 scoped_refptr<base::SequencedTaskRunner> task_runner,
41 size_t memory_size) {
42 task_runner->PostDelayedTask(
43 FROM_HERE,
44 base::Bind(&MemoryAblationExperiment::AllocateMemory,
45 base::Unretained(this), memory_size),
46 base::TimeDelta::FromSeconds(kMemoryAblationDelaySeconds));
47 }
48
49 void MemoryAblationExperiment::AllocateMemory(size_t size) {
50 memory_size_ = size;
51 memory_.reset(static_cast<uint8_t*>(malloc(size)));
52 TouchMemory();
53 }
54
55 void MemoryAblationExperiment::TouchMemory() {
56 if (memory_) {
57 size_t page_size = base::GetPageSize();
58 for (size_t i = 0; i < memory_size_; i += page_size) {
59 memory_[i]++;
Primiano Tucci (use gerrit) 2017/04/10 18:05:04 two things here: 1. touching uninitialized memory
DmitrySkiba 2017/04/10 19:31:00 Yup, definitely UB. Fixed.
60 }
61 }
62 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698