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