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

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

Issue 2786373002: memory-infra: Add peak-detector skeleton. (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 #ifndef BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_
6 #define BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_
7
8 #include <stdint.h>
9
10 #include <memory>
11 #include <vector>
12
13 #include "base/base_export.h"
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/sequence_checker.h"
18
19 namespace base {
20
21 class SequencedTaskRunner;
22
23 namespace trace_event {
24
25 struct MemoryDumpProviderInfo;
26
27 // This class is NOT thread-safe, the public methods must be called on the same
28 // thread, which does not have to be |task_runner| on which the polling tasks
ssid 2017/03/31 18:03:35 How are you planning to ensure this? Which thread
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 Oh you are right. Nah there is no need to post, be
29 // run (see Start()).
ssid 2017/03/31 18:03:35 Start() does not have any documentation.
Primiano Tucci (use gerrit) 2017/03/31 20:01:55 now it does :)
30 class BASE_EXPORT MemoryPeakDetector {
31 public:
32 using OnPeakDetectedCallback = RepeatingClosure;
33 using DumpProvidersList = std::vector<scoped_refptr<MemoryDumpProviderInfo>>;
34 using GetDumpProvidersFunction = RepeatingCallback<void(DumpProvidersList*)>;
35
36 enum State {
ssid 2017/03/31 18:03:35 I think "State" is a confusing name here. Especial
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 It's going to be better once the polling part of t
37 NOT_INITIALIZED = 0, // Before Initialize()
38 DISABLED, // Before Start() or after Stop().
39 ENABLED, // After Start() but no dump_providers_ are available.
40 RUNNING // After Start(). The PollMemoryAndDetectPeak() task is scheduled.
41 };
42
43 static MemoryPeakDetector* GetInstance();
44
45 // GetDumpProvidersFunction: is the function that will be invoked to get
46 // an updated list of polling-capable dump providers. This is really just
47 // MemoryDumpManager::GetDumpProvidersForPolling, but this extra level of
48 // indirection allows easier testing.
49 // SequencedTaskRunner: the task runner where PollMemoryAndDetectPeak() will
50 // be periodically called.
51 // OnPeakDetectedCallback: a callback that will be invoked (on the same
52 // task runner) when a memory peak is detected.
53 void Initialize(const GetDumpProvidersFunction&,
54 const scoped_refptr<SequencedTaskRunner>&,
ssid 2017/03/31 18:03:35 Is there any advantage of using sequenced task run
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 nah just being future proof in case, in future, we
55 const OnPeakDetectedCallback&);
ssid 2017/03/31 18:03:35 Maybe this callback shouldn't be part of Initializ
Primiano Tucci (use gerrit) 2017/03/31 20:01:55 Isn't the callback to be always the same though (e
ssid 2017/04/03 17:55:27 hm callback can be different. for instance for slo
Primiano Tucci (use gerrit) 2017/04/03 20:28:15 I think we should deal with that within the callba
56 void TearDownForTesting();
57
58 void Start();
59 void Stop();
60
61 // Used by MemoryDumpManager to notify that the list of polling-capable dump
62 // providers has changed. The peak detector will reload the list on the next
63 // polling task.
64 void NotifyMemoryDumpProvidersChanged();
65
66 private:
67 friend class MemoryPeakDetectorTest;
68
69 MemoryPeakDetector();
70 ~MemoryPeakDetector();
71
72 // All these methods are always called on the |task_runner_|.
73 void StartInternal();
74 void StopInternal();
75 void ReloadDumpProvidersAndStartPollingIfNeeded();
76 void PollMemoryAndDetectPeak();
77
78 // It is safe to call these testing method only on the |task_runner_|.
79 State state_for_testing() const { return state_; }
80 uint32_t poll_tasks_count_for_testing() const { return poll_tasks_count_; }
81
82 // The task runner where all the internal calls are posted onto. This field
83 // must be accessed only by the thread that owns the class instance and NOT by
84 // by the posted tasks that run on the |task_runner_| itself.
85 scoped_refptr<SequencedTaskRunner> task_runner_;
86 SequenceChecker sequence_checker_;
87
88 // After the Initialize() call, the fields below, must be accessed only from
89 // the |task_runner_|.
90
91 // Bound function to get an updated list of polling-capable dump providers.
92 GetDumpProvidersFunction get_dump_providers_function_;
93
94 // The callback to invoke when peaks are detected.
95 OnPeakDetectedCallback on_peak_detected_callback_;
96
97 // List of polling-aware dump providers to invoke upon each poll.
98 DumpProvidersList dump_providers_;
99
100 State state_;
101
102 uint32_t polling_interval_ms_;
103
104 uint32_t poll_tasks_count_; // for testing,
105
106 DISALLOW_COPY_AND_ASSIGN(MemoryPeakDetector);
107 };
108
109 } // namespace trace_event
110 } // namespace base
111
112 #endif // BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698