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

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

Issue 6862002: Merge gpu_trace_event back into base/debug/trace_event (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing latest siggi nits Created 9 years, 7 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // Trace events to track application performance. Events consist of a name 5 // Trace events are for tracking application performance.
6 // a type (BEGIN, END or INSTANT), a tracking id and extra string data.
7 // In addition, the current process id, thread id, a timestamp down to the
8 // microsecond and a file and line number of the calling location.
9 // 6 //
10 // The current implementation logs these events into a log file of the form 7 // Events are issued against categories. Whereas LOG's
11 // trace_<pid>.log where it's designed to be post-processed to generate a 8 // categories are statically defined, TRACE categories are created
12 // trace report. In the future, it may use another mechansim to facilitate 9 // implicitly with a string. For example:
13 // real-time analysis. 10 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent")
11 //
12 // Events can be INSTANT, or can be pairs of BEGIN and END:
13 // TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly")
14 // doSomethingCostly()
15 // TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly")
16 //
17 // A common use case is to trace entire function scopes. This
18 // issues a trace BEGIN and END automatically:
19 // void doSomethingCostly() {
20 // TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly");
21 // ...
22 // }
23 //
24 // Additional parameters can be associated with an event:
25 // void doSomethingCostly2(int howMuch) {
26 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
27 // "howMuch", StringPrintf("%i", howMuch).c_str());
28 // ...
29 // }
30 //
31 // The trace system will automatically add to this information the
32 // current process id, thread id, a timestamp down to the
33 // microsecond, as well as the file and line number of the calling location.
34 //
35 // By default, trace collection is compiled in, but turned off at runtime.
36 // Collecting trace data is the responsibility of the embedding
37 // application. In Chrome's case, navigating to about:gpu will turn on
38 // tracing and display data collected across all active processes.
14 39
15 #ifndef BASE_DEBUG_TRACE_EVENT_H_ 40 #ifndef BASE_DEBUG_TRACE_EVENT_H_
16 #define BASE_DEBUG_TRACE_EVENT_H_ 41 #define BASE_DEBUG_TRACE_EVENT_H_
17 #pragma once 42 #pragma once
18 43
19 #include "build/build_config.h" 44 #include "build/build_config.h"
20 45
21 #if defined(OS_WIN)
22 // On Windows we always pull in an alternative implementation
23 // which logs to Event Tracing for Windows.
24 //
25 // Note that the Windows implementation is always enabled, irrespective the
26 // value of the CHROMIUM_ENABLE_TRACE_EVENT define. The Windows implementation
27 // is controlled by Event Tracing for Windows, which will turn tracing on only
28 // if there is someone listening for the events it generates.
29 #include "base/debug/trace_event_win.h"
30 #else // defined(OS_WIN)
31
32 #include <string> 46 #include <string>
33 47
34 #include "base/memory/scoped_ptr.h" 48 #include "base/callback.h"
35 #include "base/memory/singleton.h" 49 #include "base/memory/singleton.h"
36 #include "base/synchronization/lock.h" 50 #include "base/string_util.h"
37 #include "base/time.h"
38 #include "base/timer.h" 51 #include "base/timer.h"
39 52 #include <vector>
Sigurður Ásgeirsson 2011/05/05 14:58:56 nit: not your doing, but std includes precede our
scheib 2011/05/05 16:50:28 Done.
40 #ifndef CHROMIUM_ENABLE_TRACE_EVENT 53
41 #define TRACE_EVENT_BEGIN(name, id, extra) ((void) 0) 54 // Older style trace macros with explicit id and extra data
42 #define TRACE_EVENT_END(name, id, extra) ((void) 0) 55 // Only these macros result in publishing data to ETW as currently implemented.
43 #define TRACE_EVENT_INSTANT(name, id, extra) ((void) 0) 56 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \
44 57 base::debug::TraceLog::AddTraceEventEtw( \
45 #else // CHROMIUM_ENABLE_TRACE_EVENT 58 base::debug::TRACE_EVENT_PHASE_BEGIN, \
46 // Use the following macros rather than using the TraceLog class directly as the 59 __FILE__, __LINE__, name, reinterpret_cast<const void*>(id), extra);
47 // underlying implementation may change in the future. Here's a sample usage: 60
48 // TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation); 61 #define TRACE_EVENT_END_ETW(name, id, extra) \
49 // RunScript(script); 62 base::debug::TraceLog::AddTraceEventEtw( \
50 // TRACE_EVENT_END("v8.run", documentId, scriptLocation); 63 base::debug::TRACE_EVENT_PHASE_END, \
51 64 __FILE__, __LINE__, name, reinterpret_cast<const void*>(id), extra);
52 // Record that an event (of name, id) has begun. All BEGIN events should have 65
53 // corresponding END events with a matching (name, id). 66 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \
54 #define TRACE_EVENT_BEGIN(name, id, extra) \ 67 base::debug::TraceLog::AddTraceEventEtw( \
55 base::debug::TraceLog::GetInstance()->Trace( \ 68 base::debug::TRACE_EVENT_PHASE_INSTANT, \
56 name, \ 69 __FILE__, __LINE__, name, reinterpret_cast<const void*>(id), extra);
57 base::debug::TraceLog::EVENT_BEGIN, \ 70
58 reinterpret_cast<const void*>(id), \ 71
59 extra, \ 72 // Implementation detail: trace event macros create temporary variables
60 __FILE__, \ 73 // to keep instrumentation overhead low. These macros give each temporary
61 __LINE__) 74 // variable a unique name based on the line number to prevent name collissions.
62 75 #define TRACE_EVENT_UNIQUE_IDENTIFIER3(a,b) a##b
63 // Record that an event (of name, id) has ended. All END events should have 76 #define TRACE_EVENT_UNIQUE_IDENTIFIER2(a,b) \
64 // corresponding BEGIN events with a matching (name, id). 77 TRACE_EVENT_UNIQUE_IDENTIFIER3(a,b)
65 #define TRACE_EVENT_END(name, id, extra) \ 78 #define TRACE_EVENT_UNIQUE_IDENTIFIER(name_prefix) \
66 base::debug::TraceLog::GetInstance()->Trace( \ 79 TRACE_EVENT_UNIQUE_IDENTIFIER2(name_prefix, __LINE__)
67 name, \ 80
68 base::debug::TraceLog::EVENT_END, \ 81 // Records a pair of begin and end events called "name" for the current
69 reinterpret_cast<const void*>(id), \ 82 // scope, with 0, 1 or 2 associated arguments. If the category is not
70 extra, \ 83 // enabled, then this does nothing.
71 __FILE__, \ 84 // - category and name strings must have application lifetime (statics or
72 __LINE__) 85 // literals). They may not include " chars.
73 86 #define TRACE_EVENT0(category, name) \
74 // Record that an event (of name, id) with no duration has happened. 87 TRACE_EVENT1(category, name, NULL, 0)
75 #define TRACE_EVENT_INSTANT(name, id, extra) \ 88 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \
76 base::debug::TraceLog::GetInstance()->Trace( \ 89 TRACE_EVENT2(category, name, arg1_name, arg1_val, NULL, 0)
77 name, \ 90 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
78 base::debug::TraceLog::EVENT_INSTANT, \ 91 static const base::debug::TraceCategory* \
79 reinterpret_cast<const void*>(id), \ 92 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic) = \
80 extra, \ 93 base::debug::TraceLog::GetCategory(category); \
81 __FILE__, \ 94 if (TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic)->enabled) { \
82 __LINE__) 95 base::debug::TraceLog::GetInstance()->AddTraceEvent( \
83 #endif // CHROMIUM_ENABLE_TRACE_EVENT 96 base::debug::TRACE_EVENT_PHASE_BEGIN, \
97 __FILE__, __LINE__, \
98 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic), \
99 name, \
100 arg1_name, arg1_val, \
101 arg2_name, arg2_val); \
102 } \
103 base::debug::internal::TraceEndOnScopeClose \
104 TRACE_EVENT_UNIQUE_IDENTIFIER(profileScope) ( \
105 __FILE__, __LINE__, \
106 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic), name);
107
108 // Records a single event called "name" immediately, with 0, 1 or 2
109 // associated arguments. If the category is not enabled, then this
110 // does nothing.
111 // - category and name strings must have application lifetime (statics or
112 // literals). They may not include " chars.
113 #define TRACE_EVENT_INSTANT0(category, name) \
114 TRACE_EVENT_INSTANT1(category, name, NULL, 0)
115 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
116 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, NULL, 0)
117 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
118 arg2_name, arg2_val) \
119 static const base::debug::TraceCategory* \
120 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic) = \
121 base::debug::TraceLog::GetCategory(category); \
122 if (TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic)->enabled) { \
123 base::debug::TraceLog::GetInstance()->AddTraceEvent( \
124 base::debug::TRACE_EVENT_PHASE_INSTANT, \
125 __FILE__, __LINE__, \
126 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic), \
127 name, \
128 arg1_name, arg1_val, \
129 arg2_name, arg2_val); \
130 }
131
132 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
133 // associated arguments. If the category is not enabled, then this
134 // does nothing.
135 // - category and name strings must have application lifetime (statics or
136 // literals). They may not include " chars.
137 #define TRACE_EVENT_BEGIN0(category, name) \
138 TRACE_EVENT_BEGIN1(category, name, NULL, 0)
139 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \
140 TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, NULL, 0)
141 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \
142 arg2_name, arg2_val) \
143 static const base::debug::TraceCategory* \
144 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic) = \
145 base::debug::TraceLog::GetCategory(category); \
146 if (TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic)->enabled) { \
147 base::debug::TraceLog::GetInstance()->AddTraceEvent( \
148 base::debug::TRACE_EVENT_PHASE_BEGIN, \
149 __FILE__, __LINE__, \
150 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic), \
151 name, \
152 arg1_name, arg1_val, \
153 arg2_name, arg2_val); \
154 }
155
156 // Records a single END event for "name" immediately. If the category
157 // is not enabled, then this does nothing.
158 // - category and name strings must have application lifetime (statics or
159 // literals). They may not include " chars.
160 #define TRACE_EVENT_END0(category, name) \
161 TRACE_EVENT_END1(category, name, NULL, 0)
162 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \
163 TRACE_EVENT_END2(category, name, arg1_name, arg1_val, NULL, 0)
164 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \
165 arg2_name, arg2_val) \
166 static const base::debug::TraceCategory* \
167 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic) = \
168 base::debug::TraceLog::GetCategory(category); \
169 if (TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic)->enabled) { \
170 base::debug::TraceLog::GetInstance()->AddTraceEvent( \
171 base::debug::TRACE_EVENT_PHASE_END, \
172 __FILE__, __LINE__, \
173 TRACE_EVENT_UNIQUE_IDENTIFIER(catstatic), \
174 name, \
175 arg1_name, arg1_val, \
176 arg2_name, arg2_val); \
177 }
84 178
85 namespace base { 179 namespace base {
86 180
87 class ProcessMetrics;
88
89 namespace debug { 181 namespace debug {
90 182
183 // Categories allow enabling/disabling of streams of trace events
184 struct TraceCategory {
185 const char* name;
186 volatile bool enabled;
187 };
188
189 const size_t kTraceMaxNumArgs = 2;
190
191 enum TraceEventPhase {
192 TRACE_EVENT_PHASE_BEGIN,
193 TRACE_EVENT_PHASE_END,
194 TRACE_EVENT_PHASE_INSTANT
195 };
196
197 // Simple union of values. This is much lighter weight than base::Value, which
198 // requires dynamic allocation and a vtable. To keep the trace runtime overhead
199 // low, we want constant size storage here.
200 class TraceValue {
201 public:
202 enum Type {
203 TRACE_TYPE_UNDEFINED,
204 TRACE_TYPE_BOOL,
205 TRACE_TYPE_UINT,
206 TRACE_TYPE_INT,
207 TRACE_TYPE_DOUBLE,
208 TRACE_TYPE_POINTER,
209 TRACE_TYPE_STRING
210 };
211
212 TraceValue() : type_(TRACE_TYPE_UNDEFINED) {
213 value_.as_uint = 0ull;
214 }
215 TraceValue(bool rhs) : type_(TRACE_TYPE_BOOL) {
216 value_.as_bool = rhs;
217 }
218 TraceValue(uint64 rhs) : type_(TRACE_TYPE_UINT) {
219 value_.as_uint = rhs;
220 }
221 TraceValue(uint32 rhs) : type_(TRACE_TYPE_UINT) {
222 value_.as_uint = rhs;
223 }
224 TraceValue(uint16 rhs) : type_(TRACE_TYPE_UINT) {
225 value_.as_uint = rhs;
226 }
227 TraceValue(uint8 rhs) : type_(TRACE_TYPE_UINT) {
228 value_.as_uint = rhs;
229 }
230 TraceValue(int64 rhs) : type_(TRACE_TYPE_INT) {
231 value_.as_int = rhs;
232 }
233 TraceValue(int32 rhs) : type_(TRACE_TYPE_INT) {
234 value_.as_int = rhs;
235 }
236 TraceValue(int16 rhs) : type_(TRACE_TYPE_INT) {
237 value_.as_int = rhs;
238 }
239 TraceValue(int8 rhs) : type_(TRACE_TYPE_INT) {
240 value_.as_int = rhs;
241 }
242 TraceValue(double rhs) : type_(TRACE_TYPE_DOUBLE) {
243 value_.as_double = rhs;
244 }
245 TraceValue(const void* rhs) : type_(TRACE_TYPE_POINTER) {
246 value_.as_pointer = rhs;
247 }
248 TraceValue(const char* rhs) : type_(TRACE_TYPE_STRING) {
249 value_.as_string_refptr = new RefCountedString();
250 value_.as_string_refptr->AddRef();
251 DCHECK(value_.as_string_refptr->HasOneRef());
252 value_.as_string_refptr->data = rhs;
253 }
254 TraceValue(const TraceValue& rhs) : type_(TRACE_TYPE_UNDEFINED) {
255 operator=(rhs);
256 }
257 ~TraceValue() {
258 Destroy();
259 }
260
261 TraceValue& operator=(const TraceValue& rhs);
262 bool operator!=(const TraceValue& rhs) const {
263 return !operator==(rhs);
264 }
265
266 void Destroy();
267
268 void AppendAsJSON(std::string* out) const;
269
270 Type type() const {
271 return type_;
272 }
273 uint64 as_uint() const {
274 return value_.as_uint;
275 }
276 bool as_bool() const {
277 return value_.as_bool;
278 }
279 int64 as_int() const {
280 return value_.as_int;
281 }
282 double as_double() const {
283 return value_.as_double;
284 }
285 const void* as_pointer() const {
286 return value_.as_pointer;
287 }
288 const char* as_string() const {
289 return value_.as_string_refptr->data.c_str();
290 }
291
292 typedef RefCountedData<std::string> RefCountedString;
293
294 private:
295 union Value {
296 bool as_bool;
297 uint64 as_uint;
298 int64 as_int;
299 double as_double;
300 const void* as_pointer;
301 RefCountedString* as_string_refptr;
302 };
303
304 bool operator==(const TraceValue& rhs) const;
305
306 Type type_;
307 Value value_;
308 };
309
310 // Output records are "Events" and can be obtained via the
311 // OutputCallback whenever the tracing system decides to flush. This
312 // can happen at any time, on any thread, or you can programatically
313 // force it to happen.
314 struct TraceEvent {
315 TraceEvent();
316 ~TraceEvent();
317
318 // Serialize event data to JSON
319 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events,
320 size_t start,
321 size_t count,
322 std::string* out);
323 void AppendAsJSON(std::string* out) const;
324
325
326 unsigned long process_id;
327 unsigned long thread_id;
328 TimeTicks timestamp;
329 TraceEventPhase phase;
330 const TraceCategory* category;
331 const char* name;
332 const char* arg_names[kTraceMaxNumArgs];
333 TraceValue arg_values[kTraceMaxNumArgs];
334 };
335
336
91 class BASE_API TraceLog { 337 class BASE_API TraceLog {
92 public: 338 public:
93 enum EventType {
94 EVENT_BEGIN,
95 EVENT_END,
96 EVENT_INSTANT
97 };
98
99 static TraceLog* GetInstance(); 339 static TraceLog* GetInstance();
100 340
101 // Is tracing currently enabled. 341 // Global enable of tracing. Currently enables all categories or not.
102 static bool IsTracing(); 342 // TODO(scheib): Replace with an Enable/DisableCategory() that
103 // Start logging trace events. 343 // implicitly controls the global logging state.
104 static bool StartTracing(); 344 void SetEnabled(bool enabled);
105 // Stop logging trace events. 345 bool IsEnabled() { return enabled_; }
106 static void StopTracing(); 346
107 347 float GetBufferPercentFull() const;
108 // Log a trace event of (name, type, id) with the optional extra string. 348
109 void Trace(const std::string& name, 349 // When enough events are collected, they are handed (in bulk) to
110 EventType type, 350 // the output callback. If no callback is set, the output will be
111 const void* id, 351 // silently dropped. The callback must be thread safe.
112 const std::wstring& extra, 352 typedef RefCountedData<std::string> RefCountedString;
113 const char* file, 353 typedef base::Callback<void(RefCountedString*)> OutputCallback;
114 int line); 354 void SetOutputCallback(const OutputCallback& cb);
115 void Trace(const std::string& name, 355
116 EventType type, 356 // The trace buffer does not flush dynamically, so when it fills up,
117 const void* id, 357 // subsequent trace events will be dropped. This callback is generated when
118 const std::string& extra, 358 // the trace buffer is full. The callback must be thread safe.
119 const char* file, 359 typedef base::Callback<void(void)> BufferFullCallback;
120 int line); 360 void SetBufferFullCallback(const BufferFullCallback& cb);
361
362 // Flushes all logged data to the callback.
363 void Flush();
364
365 // Called by TRACE_EVENT* macros, don't call this directly.
366 static const TraceCategory* GetCategory(const char* name);
367
368 // Called by TRACE_EVENT* macros, don't call this directly.
369 void AddTraceEvent(TraceEventPhase phase,
370 const char* file, int line,
371 const TraceCategory* category,
372 const char* name,
373 const char* arg1_name, TraceValue arg1_val,
374 const char* arg2_name, TraceValue arg2_val);
375 static void AddTraceEventEtw(TraceEventPhase phase,
376 const char* file, int line,
377 const char* name,
378 const void* id,
379 const char* extra);
380 static void AddTraceEventEtw(TraceEventPhase phase,
381 const char* file, int line,
382 const char* name,
383 const void* id,
384 const std::string& extra) {
385 AddTraceEventEtw(phase, file, line, name, id, extra.c_str());
386 }
387
388 // Exposed for unittesting only, allows resurrecting our
389 // singleton instance post-AtExit processing.
390 static void Resurrect();
121 391
122 private: 392 private:
123 // This allows constructor and destructor to be private and usable only 393 // This allows constructor and destructor to be private and usable only
124 // by the Singleton class. 394 // by the Singleton class.
125 friend struct DefaultSingletonTraits<TraceLog>; 395 friend struct StaticMemorySingletonTraits<TraceLog>;
126 396
127 TraceLog(); 397 TraceLog();
128 ~TraceLog(); 398 ~TraceLog();
129 bool OpenLogFile(); 399 const TraceCategory* GetCategoryInternal(const char* name);
130 void CloseLogFile(); 400
131 bool Start(); 401 // TODO(nduca): switch to per-thread trace buffers to reduce thread
132 void Stop(); 402 // synchronization.
133 void Heartbeat(); 403 Lock lock_;
134 void Log(const std::string& msg);
135
136 bool enabled_; 404 bool enabled_;
137 FILE* log_file_; 405 OutputCallback output_callback_;
138 base::Lock file_lock_; 406 BufferFullCallback buffer_full_callback_;
139 TimeTicks trace_start_time_; 407 std::vector<TraceEvent> logged_events_;
140 scoped_ptr<base::ProcessMetrics> process_metrics_; 408
141 RepeatingTimer<TraceLog> timer_; 409 DISALLOW_COPY_AND_ASSIGN(TraceLog);
142 }; 410 };
411
412 namespace internal {
413
414 // Used by TRACE_EVENTx macro. Do not use directly.
415 class TraceEndOnScopeClose {
416 public:
417 TraceEndOnScopeClose(const char* file, int line,
418 const TraceCategory* category,
419 const char* name)
420 : file_(file)
421 , line_(line)
422 , category_(category)
423 , name_(name) { }
424
425 ~TraceEndOnScopeClose() {
426 if (category_->enabled)
427 base::debug::TraceLog::GetInstance()->AddTraceEvent(
428 base::debug::TRACE_EVENT_PHASE_END,
429 file_, line_,
430 category_,
431 name_,
432 NULL, 0, NULL, 0);
433 }
434
435 private:
436 const char* file_;
437 int line_;
438 const TraceCategory* category_;
439 const char* name_;
440 };
441
442 } // namespace internal
143 443
144 } // namespace debug 444 } // namespace debug
145 } // namespace base 445 } // namespace base
146 446
147 #endif // defined(OS_WIN)
148
149 #endif // BASE_DEBUG_TRACE_EVENT_H_ 447 #endif // BASE_DEBUG_TRACE_EVENT_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/debug/trace_event.cc » ('j') | base/debug/trace_event.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698