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 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H | |
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H | |
7 | |
8 #include "base/base_export.h" | |
9 #include "base/gtest_prod_util.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/timer/timer.h" | |
12 #include "base/trace_event/memory_dump_request_args.h" | |
13 | |
14 namespace base { | |
15 class SingleThreadTaskRunner; | |
16 | |
17 namespace trace_event { | |
18 | |
19 class MemoryDumpManager; | |
20 | |
21 // Schedules global dump requests based on the triggers added. | |
22 class BASE_EXPORT MemoryDumpScheduler { | |
23 public: | |
24 MemoryDumpScheduler( | |
25 MemoryDumpManager* mdm_, | |
26 scoped_refptr<SingleThreadTaskRunner> polling_task_runner); | |
27 ~MemoryDumpScheduler(); | |
28 | |
29 // Adds triggers for scheduling global dumps. Both periodic and peak triggers | |
30 // cannot be added together. At the moment the periodic support is limited to | |
31 // at most one periodic trigger per dump mode and peak triggers are limited to | |
32 // at most one. All intervals should be an integeral multiple of the smallest | |
33 // interval specified. | |
34 void AddTrigger(MemoryDumpType trigger_type, | |
35 MemoryDumpLevelOfDetail level_of_detail, | |
36 uint32_t min_time_between_dumps_ms); | |
37 | |
38 // Starts periodic dumps. | |
39 void NotifyPeriodicTriggerSupported(); | |
Primiano Tucci (use gerrit)
2017/01/26 02:52:23
thinking more, should these methods just be called
ssid
2017/01/26 21:48:57
Yes I was thinking the same. But felt enable is a
| |
40 | |
41 // Starts polling memory total. | |
42 void NotifyPollingSupported(); | |
43 | |
44 // Disables all triggers. | |
45 void DisableAllTriggers(); | |
46 | |
47 private: | |
48 friend class MemoryDumpManagerTest; | |
49 FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest, TestPollingOnDumpThread); | |
50 | |
51 // Helper class to schdule periodic memory dumps. | |
52 class PeriodicDumpScheduler { | |
Primiano Tucci (use gerrit)
2017/01/26 02:52:23
aren't these sub-classes a bit too much overkill?
ssid
2017/01/26 21:48:57
The main reason for having them is because I wante
| |
53 public: | |
54 explicit PeriodicDumpScheduler(MemoryDumpManager* mdm); | |
55 ~PeriodicDumpScheduler(); | |
56 | |
57 // Adds periodic dump time periods. | |
58 void AddTrigger(MemoryDumpLevelOfDetail level_of_detail, | |
59 uint32_t min_time_between_dumps_ms); | |
60 | |
61 // Starts periodic timer on notification. | |
62 void NotifySupported(); | |
Primiano Tucci (use gerrit)
2017/01/26 02:52:23
just Enable()
ssid
2017/01/26 21:48:57
Same reply as above. Enable() does not enable unle
| |
63 | |
64 // Disables periodic timer. | |
65 void Disable(); | |
66 | |
67 bool IsTimerRunning() { return timer_.IsRunning(); } | |
68 | |
69 // Periodically called by the timer. | |
70 void RequestPeriodicGlobalDump(); | |
71 | |
72 // True if periodic scheduler is enabled in this session. | |
73 bool is_setup() const { return is_setup_; } | |
74 | |
75 private: | |
76 MemoryDumpManager* mdm_; | |
77 | |
78 bool is_setup_; | |
79 | |
80 // Timer for periodic dumps. | |
81 RepeatingTimer timer_; | |
82 uint32_t dump_count_; | |
83 uint32_t min_timer_period_ms_; | |
84 uint32_t light_dumps_rate_; | |
85 uint32_t heavy_dumps_rate_; | |
86 | |
87 uint32_t light_dump_period_ms = 0; | |
88 uint32_t heavy_dump_period_ms = 0; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(PeriodicDumpScheduler); | |
91 }; | |
92 | |
93 class PeakDumpScheduler { | |
94 public: | |
95 PeakDumpScheduler( | |
96 MemoryDumpManager* mdm, | |
97 scoped_refptr<SingleThreadTaskRunner> polling_task_runner); | |
98 ~PeakDumpScheduler(); | |
99 | |
100 // Sets up peak dump triggers for global memory dumps according to | |
101 // the trigger. | |
102 void AddTrigger(MemoryDumpLevelOfDetail level_of_detail, | |
103 uint32_t min_time_between_dumps_ms); | |
104 | |
105 // Called for polling memory usage and trigger dumps if peak is detected. | |
106 void PollMemoryOnPollingThread(); | |
107 | |
108 // Starts polling for peak detection. | |
109 void NotifySupported(); | |
Primiano Tucci (use gerrit)
2017/01/26 02:52:23
ditto
| |
110 | |
111 // Disables polling for peak detection. | |
112 void Disable(); | |
113 | |
114 // True if peak dumps scheduler is enabled in this session. | |
115 bool is_setup() const { return is_setup_; } | |
116 | |
117 private: | |
118 // Returns true if peak memory value is detected. | |
119 bool ShouldTriggerDump(uint64_t current_memory_total); | |
120 | |
121 MemoryDumpManager* mdm_; | |
122 | |
123 bool is_setup_; | |
124 MemoryDumpLevelOfDetail dump_mode_; | |
125 | |
126 scoped_refptr<SingleThreadTaskRunner> polling_task_runner_; | |
127 uint32_t polling_interval_ = 0; | |
128 | |
129 int min_polls_between_dumps_ = 0; | |
130 int num_polls_from_last_dump_ = 0; | |
131 | |
132 // Threshold on increase in memory from last dump beyond which a new dump | |
133 // must be triggered. | |
134 int64_t memory_increase_threshold_ = 50 * 1024 * 1024; // 50MiB | |
135 uint64_t last_dump_memory_total_ = 0; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(PeakDumpScheduler); | |
138 }; | |
139 | |
140 // Must be set before enabling tracing. | |
141 static void SetPollingIntervalForTesting(uint32_t interval); | |
142 | |
143 bool IsPeriodicTimerRunningForTesting(); | |
144 | |
145 MemoryDumpManager* mdm_; | |
146 | |
147 PeriodicDumpScheduler periodic_dump_scheduler_; | |
148 PeakDumpScheduler peak_dump_scheduler_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(MemoryDumpScheduler); | |
151 }; | |
152 | |
153 } // namespace trace_event | |
154 } // namespace base | |
155 | |
156 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H | |
OLD | NEW |