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

Side by Side Diff: base/trace_event/memory_dump_scheduler.h

Issue 2743993004: [memory-infra] Make MemoryDumpScheduler non-thread safe singleton (Closed)
Patch Set: . Created 3 years, 9 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
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 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H 5 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H 6 #define BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H
7 7
8 #include <memory>
9
8 #include "base/base_export.h" 10 #include "base/base_export.h"
9 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
10 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/singleton.h"
Primiano Tucci (use gerrit) 2017/03/16 19:01:36 these days you don't need this boiler plate anymor
ssid 2017/03/22 01:32:58 Thanks! fixed.
11 #include "base/timer/timer.h" 14 #include "base/timer/timer.h"
12 #include "base/trace_event/memory_dump_request_args.h" 15 #include "base/trace_event/memory_dump_request_args.h"
13 16
14 namespace base { 17 namespace base {
15 class SingleThreadTaskRunner; 18 class SingleThreadTaskRunner;
16 19
17 namespace trace_event { 20 namespace trace_event {
18 21
19 class MemoryDumpManager; 22 class MemoryDumpManager;
20 23
21 // Schedules global dump requests based on the triggers added. 24 // Schedules global dump requests based on the triggers added. The methods of
25 // this class are NOT thread safe and the client has to take care of invoking
26 // all the methods of the class safely.
22 class BASE_EXPORT MemoryDumpScheduler { 27 class BASE_EXPORT MemoryDumpScheduler {
23 public: 28 public:
24 MemoryDumpScheduler( 29 static MemoryDumpScheduler* GetInstance();
25 MemoryDumpManager* mdm_, 30
26 scoped_refptr<SingleThreadTaskRunner> polling_task_runner); 31 // Initializes the scheduler. NOT thread safe.
27 ~MemoryDumpScheduler(); 32 void Setup(MemoryDumpManager* mdm_,
33 scoped_refptr<SingleThreadTaskRunner> polling_task_runner);
28 34
29 // Adds triggers for scheduling global dumps. Both periodic and peak triggers 35 // 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 36 // 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 37 // 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 38 // at most one. All intervals should be an integeral multiple of the smallest
33 // interval specified. 39 // interval specified. NOT thread safe.
34 void AddTrigger(MemoryDumpType trigger_type, 40 void AddTrigger(MemoryDumpType trigger_type,
35 MemoryDumpLevelOfDetail level_of_detail, 41 MemoryDumpLevelOfDetail level_of_detail,
36 uint32_t min_time_between_dumps_ms); 42 uint32_t min_time_between_dumps_ms);
37 43
38 // Starts periodic dumps. 44 // Starts periodic dumps. NOT thread safe and triggers must be added before
39 void NotifyPeriodicTriggerSupported(); 45 // enabling.
46 void EnablePeriodicTriggerIfNeeded();
40 47
41 // Starts polling memory total. 48 // Starts polling memory total. NOT thread safe and triggers must be added
42 void NotifyPollingSupported(); 49 // before enabling.
50 void EnablePollingIfNeeded();
43 51
44 // Resets time for triggering dump to account for minimum time between the 52 // Resets time for triggering dump to account for minimum time between the
45 // dumps. 53 // dumps. NOT thread safe.
46 void NotifyDumpTriggered(); 54 void NotifyDumpTriggered();
47 55
48 // Disables all triggers. 56 // Disables all triggers. NOT thread safe. This should be called before
57 // polling thread is stopped to stop polling cleanly.
49 void DisableAllTriggers(); 58 void DisableAllTriggers();
50 59
51 private: 60 private:
61 friend struct DefaultSingletonTraits<MemoryDumpScheduler>;
52 friend class MemoryDumpManagerTest; 62 friend class MemoryDumpManagerTest;
53 friend class MemoryDumpSchedulerPollingTest; 63 friend class MemoryDumpSchedulerPollingTest;
54 FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest, TestPollingOnDumpThread); 64 FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest, TestPollingOnDumpThread);
55 65
56 // Helper class to schdule periodic memory dumps. 66 // Helper class to schdule periodic memory dumps.
57 struct PeriodicTriggerState { 67 struct PeriodicTriggerState {
58 PeriodicTriggerState(); 68 PeriodicTriggerState();
59 ~PeriodicTriggerState(); 69 ~PeriodicTriggerState();
60 70
61 bool is_configured; 71 bool is_configured;
(...skipping 12 matching lines...) Expand all
74 84
75 struct PollingTriggerState { 85 struct PollingTriggerState {
76 enum State { 86 enum State {
77 CONFIGURED, // Polling trigger was added. 87 CONFIGURED, // Polling trigger was added.
78 ENABLED, // Polling is running. 88 ENABLED, // Polling is running.
79 DISABLED // Polling is disabled. 89 DISABLED // Polling is disabled.
80 }; 90 };
81 91
82 static const uint32_t kMaxNumMemorySamples = 50; 92 static const uint32_t kMaxNumMemorySamples = 50;
83 93
84 explicit PollingTriggerState( 94 PollingTriggerState();
85 scoped_refptr<SingleThreadTaskRunner> polling_task_runner);
86 ~PollingTriggerState(); 95 ~PollingTriggerState();
87 96
88 // Helper to clear the tracked memory totals and poll count from last dump. 97 // Helper to clear the tracked memory totals and poll count from last dump.
89 void ResetTotals(); 98 void ResetTotals();
90 99
91 State current_state; 100 State current_state;
92 MemoryDumpLevelOfDetail level_of_detail; 101 MemoryDumpLevelOfDetail level_of_detail;
93 102
94 scoped_refptr<SingleThreadTaskRunner> polling_task_runner;
95 uint32_t polling_interval_ms; 103 uint32_t polling_interval_ms;
96 104
97 // Minimum numer of polls after the last dump at which next dump can be 105 // Minimum numer of polls after the last dump at which next dump can be
98 // triggered. 106 // triggered.
99 int min_polls_between_dumps; 107 int min_polls_between_dumps;
100 int num_polls_from_last_dump; 108 int num_polls_from_last_dump;
101 109
102 uint64_t last_dump_memory_total; 110 uint64_t last_dump_memory_total;
103 int64_t memory_increase_threshold; 111 int64_t memory_increase_threshold;
104 uint64_t last_memory_totals_kb[kMaxNumMemorySamples]; 112 uint64_t last_memory_totals_kb[kMaxNumMemorySamples];
105 uint32_t last_memory_totals_kb_index; 113 uint32_t last_memory_totals_kb_index;
106 114
107 DISALLOW_COPY_AND_ASSIGN(PollingTriggerState); 115 DISALLOW_COPY_AND_ASSIGN(PollingTriggerState);
108 }; 116 };
109 117
110 // Helper to set polling disabled on the polling thread. 118 MemoryDumpScheduler();
111 void DisablePolling(); 119 ~MemoryDumpScheduler();
120
121 // Helper to set polling disabled.
122 void DisablePollingOnPollingThread();
112 123
113 // Periodically called by the timer. 124 // Periodically called by the timer.
114 void RequestPeriodicGlobalDump(); 125 void RequestPeriodicGlobalDump();
115 126
116 // Called for polling memory usage and trigger dumps if peak is detected. 127 // Called for polling memory usage and trigger dumps if peak is detected.
117 void PollMemoryOnPollingThread(); 128 void PollMemoryOnPollingThread();
118 129
119 // Returns true if peak memory value is detected. 130 // Returns true if peak memory value is detected.
120 bool ShouldTriggerDump(uint64_t current_memory_total); 131 bool ShouldTriggerDump(uint64_t current_memory_total);
121 132
122 // Helper to detect peaks in memory usage. 133 // Helper to detect peaks in memory usage.
123 bool IsCurrentSamplePeak(uint64_t current_memory_total); 134 bool IsCurrentSamplePeak(uint64_t current_memory_total);
124 135
125 // Must be set before enabling tracing. 136 // Must be set before enabling tracing.
126 static void SetPollingIntervalForTesting(uint32_t interval); 137 static void SetPollingIntervalForTesting(uint32_t interval);
127 138
128 // True if periodic dumping is enabled. 139 // True if periodic dumping is enabled.
129 bool IsPeriodicTimerRunningForTesting(); 140 bool IsPeriodicTimerRunningForTesting();
130 141
131 MemoryDumpManager* mdm_; 142 MemoryDumpManager* mdm_;
132 143
133 PeriodicTriggerState periodic_state_; 144 // Accessed on the thread of the client before enabling and only accessed on
134 PollingTriggerState polling_state_; 145 // the thread that called "EnablePeriodicTriggersIfNeeded()" after enabling.
146 std::unique_ptr<PeriodicTriggerState> periodic_state_;
147
148 // Accessed on the thread of the client before enabling and only accessed on
149 // the polling thread after enabling.
150 std::unique_ptr<PollingTriggerState> polling_state_;
151
152 // Accessed on the thread of the client only.
153 scoped_refptr<SingleThreadTaskRunner> polling_task_runner_;
154
155 // True when the scheduler is setup. Accessed on the thread of client only.
156 bool is_setup_;
135 157
136 DISALLOW_COPY_AND_ASSIGN(MemoryDumpScheduler); 158 DISALLOW_COPY_AND_ASSIGN(MemoryDumpScheduler);
137 }; 159 };
138 160
139 } // namespace trace_event 161 } // namespace trace_event
140 } // namespace base 162 } // namespace base
141 163
142 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H 164 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698