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

Side by Side Diff: content/browser/trace_controller.h

Issue 9694028: Add a Content API around TracingController. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix linux Created 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 CONTENT_BROWSER_TRACE_CONTROLLER_H_
6 #define CONTENT_BROWSER_TRACE_CONTROLLER_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/debug/trace_event.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/memory/singleton.h"
15 #include "content/common/content_export.h"
16
17 class CommandLine;
18 class TraceMessageFilter;
19
20 // Objects interested in receiving trace data derive from TraceSubscriber.
21 // See also: trace_message_filter.h
22 // See also: child_trace_message_filter.h
23 class CONTENT_EXPORT TraceSubscriber {
24 public:
25 // Called once after TraceController::EndTracingAsync.
26 virtual void OnEndTracingComplete() = 0;
27 // Called 0 or more times between TraceController::BeginTracing and
28 // OnEndTracingComplete. Use base::debug::TraceResultBuffer to convert one or
29 // more trace fragments to JSON.
30 virtual void OnTraceDataCollected(
31 const scoped_refptr<base::RefCountedString>& trace_fragment) = 0;
32 // Called once after TraceController::GetKnownCategoriesAsync.
33 virtual void OnKnownCategoriesCollected(
34 const std::set<std::string>& known_categories);
35 virtual void OnTraceBufferPercentFullReply(float percent_full);
36
37 protected:
38 virtual ~TraceSubscriber();
39 };
40
41 // TraceController is used on the browser processes to enable/disable
42 // trace status and collect trace data. Only the browser UI thread is allowed
43 // to interact with the TraceController object. All calls on the TraceSubscriber
44 // happen on the UI thread.
45 class CONTENT_EXPORT TraceController {
46 public:
47 static TraceController* GetInstance();
48
49 // Called on the main thread of the browser process to initialize
50 // startup tracing.
51 void InitStartupTracing(const CommandLine& command_line);
52
53 // Get set of known categories. This can change as new code paths are reached.
54 // If true is returned, subscriber->OnKnownCategoriesCollected will be called
55 // when once the categories are retrieved from child processes.
56 bool GetKnownCategoriesAsync(TraceSubscriber* subscriber);
57
58 // Called by browser process to start tracing events on all processes.
59 //
60 // Currently only one subscriber is allowed at a time.
61 // Tracing begins immediately locally, and asynchronously on child processes
62 // as soon as they receive the BeginTracing request.
63 // By default, all categories are traced except those matching "test_*".
64 //
65 // If BeginTracing was already called previously,
66 // or if an EndTracingAsync is pending,
67 // or if another subscriber is tracing,
68 // BeginTracing will return false meaning it failed.
69 bool BeginTracing(TraceSubscriber* subscriber);
70
71 // Same as above, but specifies which categories to trace.
72 // If both included_categories and excluded_categories are empty,
73 // all categories are traced.
74 // Else if included_categories is non-empty, only those are traced.
75 // Else if excluded_categories is non-empty, everything but those are traced.
76 bool BeginTracing(TraceSubscriber* subscriber,
77 const std::vector<std::string>& included_categories,
78 const std::vector<std::string>& excluded_categories);
79
80 // |categories| is a comma-delimited list of category wildcards.
81 // A category can have an optional '-' prefix to make it an excluded category.
82 // All the same rules apply above, so for example, having both included and
83 // excluded categories in the same list would not be supported.
84 //
85 // Example: BeginTracing("test_MyTest*");
86 // Example: BeginTracing("test_MyTest*,test_OtherStuff");
87 // Example: BeginTracing("-excluded_category1,-excluded_category2");
88 bool BeginTracing(TraceSubscriber* subscriber, const std::string& categories);
89
90 // Called by browser process to stop tracing events on all processes.
91 //
92 // Child processes typically are caching trace data and only rarely flush
93 // and send trace data back to the browser process. That is because it may be
94 // an expensive operation to send the trace data over IPC, and we would like
95 // to avoid much runtime overhead of tracing. So, to end tracing, we must
96 // asynchronously ask all child processes to flush any pending trace data.
97 //
98 // Once all child processes have acked the EndTracing request,
99 // TraceSubscriber will be called with OnEndTracingComplete.
100 //
101 // If a previous call to EndTracingAsync is already pending,
102 // or if another subscriber is tracing,
103 // EndTracingAsync will return false meaning it failed.
104 bool EndTracingAsync(TraceSubscriber* subscriber);
105
106 // Get the maximum across processes of trace buffer percent full state.
107 // When the TraceBufferPercentFull value is determined,
108 // subscriber->OnTraceBufferPercentFullReply is called.
109 // When any child process reaches 100% full, the TraceController will end
110 // tracing, and call TraceSubscriber::OnEndTracingComplete.
111 // GetTraceBufferPercentFullAsync fails in the following conditions:
112 // trace is ending or disabled;
113 // a previous call to GetTraceBufferPercentFullAsync is pending; or
114 // the caller is not the current subscriber.
115 bool GetTraceBufferPercentFullAsync(TraceSubscriber* subscriber);
116
117 // Cancel the subscriber so that it will not be called when EndTracingAsync is
118 // acked by all child processes. This will also call EndTracingAsync
119 // internally if necessary.
120 // Safe to call even if caller is not the current subscriber.
121 void CancelSubscriber(TraceSubscriber* subscriber);
122
123 private:
124 typedef std::set<scoped_refptr<TraceMessageFilter> > FilterMap;
125
126 friend struct DefaultSingletonTraits<TraceController>;
127 friend class TraceMessageFilter;
128
129 TraceController();
130 ~TraceController();
131
132 bool is_tracing_enabled() const {
133 return can_end_tracing();
134 }
135
136 bool can_end_tracing() const {
137 return is_tracing_ && pending_end_ack_count_ == 0;
138 }
139
140 // Can get Buffer Percent Full
141 bool can_get_buffer_percent_full() const {
142 return is_tracing_ &&
143 pending_end_ack_count_ == 0 &&
144 pending_bpf_ack_count_ == 0;
145 }
146
147 bool can_begin_tracing(TraceSubscriber* subscriber) const {
148 return !is_tracing_ &&
149 (subscriber_ == NULL || subscriber == subscriber_);
150 }
151
152 // Methods for use by TraceMessageFilter.
153
154 void AddFilter(TraceMessageFilter* filter);
155 void RemoveFilter(TraceMessageFilter* filter);
156 void OnTracingBegan(TraceSubscriber* subscriber);
157 void OnEndTracingAck(const std::vector<std::string>& known_categories);
158 void OnTraceDataCollected(
159 const scoped_refptr<base::RefCountedString>& events_str_ptr);
160 void OnTraceBufferFull();
161 void OnTraceBufferPercentFullReply(float percent_full);
162
163 FilterMap filters_;
164 TraceSubscriber* subscriber_;
165 // Pending acks for EndTracingAsync:
166 int pending_end_ack_count_;
167 // Pending acks for GetTraceBufferPercentFullAsync:
168 int pending_bpf_ack_count_;
169 float maximum_bpf_;
170 bool is_tracing_;
171 bool is_get_categories_;
172 std::set<std::string> known_categories_;
173 std::vector<std::string> included_categories_;
174 std::vector<std::string> excluded_categories_;
175
176 DISALLOW_COPY_AND_ASSIGN(TraceController);
177 };
178
179 #endif // CONTENT_BROWSER_TRACE_CONTROLLER_H_
180
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698