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 "base/trace_event/memory_dump_scheduler.h"
6
7 #include <algorithm>
8 #include <limits>
9
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/threading/sequenced_task_runner_handle.h"
13
14 namespace base {
15 namespace trace_event {
16
17 // static
GetInstance()18 MemoryDumpScheduler* MemoryDumpScheduler::GetInstance() {
19 static MemoryDumpScheduler* instance = new MemoryDumpScheduler();
20 return instance;
21 }
22
MemoryDumpScheduler()23 MemoryDumpScheduler::MemoryDumpScheduler() : period_ms_(0), generation_(0) {}
~MemoryDumpScheduler()24 MemoryDumpScheduler::~MemoryDumpScheduler() {
25 // Hit only in tests. Check that tests don't leave without stopping.
26 DCHECK(!is_enabled_for_testing());
27 }
28
Start(MemoryDumpScheduler::Config config,scoped_refptr<SequencedTaskRunner> task_runner)29 void MemoryDumpScheduler::Start(
30 MemoryDumpScheduler::Config config,
31 scoped_refptr<SequencedTaskRunner> task_runner) {
32 DCHECK(!task_runner_);
33 task_runner_ = task_runner;
34 task_runner->PostTask(FROM_HERE, BindOnce(&MemoryDumpScheduler::StartInternal,
35 Unretained(this), config));
36 }
37
Stop()38 void MemoryDumpScheduler::Stop() {
39 if (!task_runner_)
40 return;
41 task_runner_->PostTask(FROM_HERE, BindOnce(&MemoryDumpScheduler::StopInternal,
42 Unretained(this)));
43 task_runner_ = nullptr;
44 }
45
StartInternal(MemoryDumpScheduler::Config config)46 void MemoryDumpScheduler::StartInternal(MemoryDumpScheduler::Config config) {
47 uint32_t light_dump_period_ms = 0;
48 uint32_t heavy_dump_period_ms = 0;
49 uint32_t min_period_ms = std::numeric_limits<uint32_t>::max();
50 for (const Config::Trigger& trigger : config.triggers) {
51 DCHECK_GT(trigger.period_ms, 0u);
52 switch (trigger.level_of_detail) {
53 case MemoryDumpLevelOfDetail::BACKGROUND:
54 break;
55 case MemoryDumpLevelOfDetail::LIGHT:
56 DCHECK_EQ(0u, light_dump_period_ms);
57 light_dump_period_ms = trigger.period_ms;
58 break;
59 case MemoryDumpLevelOfDetail::DETAILED:
60 DCHECK_EQ(0u, heavy_dump_period_ms);
61 heavy_dump_period_ms = trigger.period_ms;
62 break;
63 }
64 min_period_ms = std::min(min_period_ms, trigger.period_ms);
65 }
66
67 DCHECK_EQ(0u, light_dump_period_ms % min_period_ms);
68 DCHECK_EQ(0u, heavy_dump_period_ms % min_period_ms);
69 DCHECK(!config.callback.is_null());
70 callback_ = config.callback;
71 period_ms_ = min_period_ms;
72 tick_count_ = 0;
73 light_dump_rate_ = light_dump_period_ms / min_period_ms;
74 heavy_dump_rate_ = heavy_dump_period_ms / min_period_ms;
75
76 // Trigger the first dump after 200ms.
77 // TODO(lalitm): this is a tempoarary hack to delay the first scheduled dump
78 // so that the child processes get tracing enabled notification via IPC.
79 // See crbug.com/770151.
80 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
81 FROM_HERE,
82 BindOnce(&MemoryDumpScheduler::Tick, Unretained(this), ++generation_),
83 TimeDelta::FromMilliseconds(200));
84 }
85
StopInternal()86 void MemoryDumpScheduler::StopInternal() {
87 period_ms_ = 0;
88 generation_++;
89 callback_.Reset();
90 }
91
Tick(uint32_t expected_generation)92 void MemoryDumpScheduler::Tick(uint32_t expected_generation) {
93 if (period_ms_ == 0 || generation_ != expected_generation)
94 return;
95
96 MemoryDumpLevelOfDetail level_of_detail = MemoryDumpLevelOfDetail::BACKGROUND;
97 if (light_dump_rate_ > 0 && tick_count_ % light_dump_rate_ == 0)
98 level_of_detail = MemoryDumpLevelOfDetail::LIGHT;
99 if (heavy_dump_rate_ > 0 && tick_count_ % heavy_dump_rate_ == 0)
100 level_of_detail = MemoryDumpLevelOfDetail::DETAILED;
101 tick_count_++;
102
103 callback_.Run(level_of_detail);
104
105 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
106 FROM_HERE,
107 BindOnce(&MemoryDumpScheduler::Tick, Unretained(this),
108 expected_generation),
109 TimeDelta::FromMilliseconds(period_ms_));
110 }
111
112 MemoryDumpScheduler::Config::Config() = default;
113 MemoryDumpScheduler::Config::~Config() = default;
114 MemoryDumpScheduler::Config::Config(const MemoryDumpScheduler::Config&) =
115 default;
116
117 } // namespace trace_event
118 } // namespace base
119