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

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

Issue 12150004: Category group support/Renamings. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebased, fixed last nit. Created 7 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 (c) 2012 The Chromium Authors. All rights reserved. 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 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 5
6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_ 6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_
7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ 7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 long long as_int; 60 long long as_int;
61 double as_double; 61 double as_double;
62 const void* as_pointer; 62 const void* as_pointer;
63 const char* as_string; 63 const char* as_string;
64 }; 64 };
65 65
66 TraceEvent(); 66 TraceEvent();
67 TraceEvent(int thread_id, 67 TraceEvent(int thread_id,
68 TimeTicks timestamp, 68 TimeTicks timestamp,
69 char phase, 69 char phase,
70 const unsigned char* category_enabled, 70 const unsigned char* category_group_enabled,
71 const char* name, 71 const char* name,
72 unsigned long long id, 72 unsigned long long id,
73 int num_args, 73 int num_args,
74 const char** arg_names, 74 const char** arg_names,
75 const unsigned char* arg_types, 75 const unsigned char* arg_types,
76 const unsigned long long* arg_values, 76 const unsigned long long* arg_values,
77 unsigned char flags); 77 unsigned char flags);
78 ~TraceEvent(); 78 ~TraceEvent();
79 79
80 // Serialize event data to JSON 80 // Serialize event data to JSON
81 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, 81 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events,
82 size_t start, 82 size_t start,
83 size_t count, 83 size_t count,
84 std::string* out); 84 std::string* out);
85 void AppendAsJSON(std::string* out) const; 85 void AppendAsJSON(std::string* out) const;
86 86
87 static void AppendValueAsJSON(unsigned char type, 87 static void AppendValueAsJSON(unsigned char type,
88 TraceValue value, 88 TraceValue value,
89 std::string* out); 89 std::string* out);
90 90
91 TimeTicks timestamp() const { return timestamp_; } 91 TimeTicks timestamp() const { return timestamp_; }
92 92
93 // Exposed for unittesting: 93 // Exposed for unittesting:
94 94
95 const base::RefCountedString* parameter_copy_storage() const { 95 const base::RefCountedString* parameter_copy_storage() const {
96 return parameter_copy_storage_.get(); 96 return parameter_copy_storage_.get();
97 } 97 }
98 98
99 const unsigned char* category_enabled() const { return category_enabled_; } 99 const unsigned char* category_group_enabled() const {
100 return category_group_enabled_;
101 }
102
100 const char* name() const { return name_; } 103 const char* name() const { return name_; }
101 104
102 private: 105 private:
103 // Note: these are ordered by size (largest first) for optimal packing. 106 // Note: these are ordered by size (largest first) for optimal packing.
104 TimeTicks timestamp_; 107 TimeTicks timestamp_;
105 // id_ can be used to store phase-specific data. 108 // id_ can be used to store phase-specific data.
106 unsigned long long id_; 109 unsigned long long id_;
107 TraceValue arg_values_[kTraceMaxNumArgs]; 110 TraceValue arg_values_[kTraceMaxNumArgs];
108 const char* arg_names_[kTraceMaxNumArgs]; 111 const char* arg_names_[kTraceMaxNumArgs];
109 const unsigned char* category_enabled_; 112 const unsigned char* category_group_enabled_;
110 const char* name_; 113 const char* name_;
111 scoped_refptr<base::RefCountedString> parameter_copy_storage_; 114 scoped_refptr<base::RefCountedString> parameter_copy_storage_;
112 int thread_id_; 115 int thread_id_;
113 char phase_; 116 char phase_;
114 unsigned char flags_; 117 unsigned char flags_;
115 unsigned char arg_types_[kTraceMaxNumArgs]; 118 unsigned char arg_types_[kTraceMaxNumArgs];
116 }; 119 };
117 120
118 // TraceBuffer holds the events as they are collected. 121 // TraceBuffer holds the events as they are collected.
119 class BASE_EXPORT TraceBuffer { 122 class BASE_EXPORT TraceBuffer {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 169
167 // When all fragments have been added, call Finish to complete the JSON 170 // When all fragments have been added, call Finish to complete the JSON
168 // formatted output. 171 // formatted output.
169 void Finish(); 172 void Finish();
170 173
171 private: 174 private:
172 OutputCallback output_callback_; 175 OutputCallback output_callback_;
173 bool append_comma_; 176 bool append_comma_;
174 }; 177 };
175 178
179 class BASE_EXPORT CategoryFilter {
180 public:
181 // The default category filter, used when none is provided.
182 // Allows all categories through, except if they end in the suffix 'Debug' or
183 // 'Test'.
184 static const char* kDefaultCategoryFilterString;
185
186 // |filter_string| is a comma-delimited list of category wildcards.
187 // A category can have an optional '-' prefix to make it an excluded category.
188 // All the same rules apply above, so for example, having both included and
189 // excluded categories in the same list would not be supported.
190 //
191 // Example: CategoryFilter"test_MyTest*");
192 // Example: CategoryFilter("test_MyTest*,test_OtherStuff");
193 // Example: CategoryFilter("-excluded_category1,-excluded_category2");
194 // Example: CategoryFilter("-*,webkit"); would disable everything but webkit.
195 // Example: CategoryFilter("-webkit"); would enable everything but webkit.
196 explicit CategoryFilter(const std::string& filter_string);
197
198 CategoryFilter(const CategoryFilter& cf);
199
200 CategoryFilter& operator=(const CategoryFilter& rhs);
201
202 // Writes the string representation of the CategoryFilter. This is a comma
203 // separated string, similar in nature to the one used to determine
204 // enabled/disabled category patterns, except here there is an arbitrary
205 // order, included categories go first, then excluded categories. Excluded
206 // categories are distinguished from included categories by the prefix '-'.
207 std::string ToString() const;
208
209 // Determines whether category group would be enabled or
210 // disabled by this category filter.
211 bool IsCategoryGroupEnabled(const char* category_group) const;
212
213 // Merges nested_filter with the current CategoryFilter
214 void Merge(const CategoryFilter& nested_filter);
215
216 // Determines whether or not we have explicitly allowed category patterns.
217 bool HasIncludedPatterns() const;
218
219 // Clears both included/excluded pattern lists. This would be equivalent to
220 // creating a CategoryFilter with an empty string, through the constructor.
221 // i.e: CategoryFilter("").
222 //
223 // When using an empty filter, all categories are considered included as we
224 // are not excluding anything.
225 void Clear();
226
227 static bool IsEmptyOrContainsLeadingOrTrailingWhitespace(
228 const std::string str);
jar (doing other things) 2013/03/31 15:14:08 nit: should this be a const ref?
rterrazas 2013/04/15 01:41:11 Done.
229
230 private:
231 void Initialize(const std::string& filter_string);
232 void WriteString(std::string* out, bool included) const;
233
234 std::vector<std::string> included_;
235 std::vector<std::string> excluded_;
236 };
jar (doing other things) 2013/03/31 15:14:08 nit: can we have a DISALLOW_COPY_AND_ASSIGN, or is
rterrazas 2013/04/15 01:41:11 Nope, it's created/copied in other classes.
237
176 class TraceSamplingThread; 238 class TraceSamplingThread;
177 239
178 class BASE_EXPORT TraceLog { 240 class BASE_EXPORT TraceLog {
179 public: 241 public:
180 // Notification is a mask of one or more of the following events. 242 // Notification is a mask of one or more of the following events.
181 enum Notification { 243 enum Notification {
182 // The trace buffer does not flush dynamically, so when it fills up, 244 // The trace buffer does not flush dynamically, so when it fills up,
183 // subsequent trace events will be dropped. This callback is generated when 245 // subsequent trace events will be dropped. This callback is generated when
184 // the trace buffer is full. The callback must be thread safe. 246 // the trace buffer is full. The callback must be thread safe.
185 TRACE_BUFFER_FULL = 1 << 0, 247 TRACE_BUFFER_FULL = 1 << 0,
(...skipping 13 matching lines...) Expand all
199 // Enable the sampling profiler. 261 // Enable the sampling profiler.
200 ENABLE_SAMPLING = 1 << 2 262 ENABLE_SAMPLING = 1 << 2
201 }; 263 };
202 264
203 static TraceLog* GetInstance(); 265 static TraceLog* GetInstance();
204 266
205 // Convert the given string to trace options. Defaults to RECORD_UNTIL_FULL if 267 // Convert the given string to trace options. Defaults to RECORD_UNTIL_FULL if
206 // the string does not provide valid options. 268 // the string does not provide valid options.
207 static Options TraceOptionsFromString(const std::string& str); 269 static Options TraceOptionsFromString(const std::string& str);
208 270
209 // Get set of known categories. This can change as new code paths are reached. 271 // Get set of known category groups. This can change as new code paths are
210 // The known categories are inserted into |categories|. 272 // reached. The known category groups are inserted into |category_groups|.
211 void GetKnownCategories(std::vector<std::string>* categories); 273 void GetKnownCategoryGroups(std::vector<std::string>* category_groups);
212 274
213 // Enable tracing for provided list of categories. If tracing is already 275 // Retrieves the current filter used to enable categories. Only
214 // enabled, this method does nothing -- changing categories during trace is
215 // not supported.
216 // If both included_categories and excluded_categories are empty,
217 // all categories are traced.
218 // Else if included_categories is non-empty, only those are traced.
219 // Else if excluded_categories is non-empty, everything but those are traced.
220 // Wildcards * and ? are supported (see MatchPattern in string_util.h).
221 void SetEnabled(const std::vector<std::string>& included_categories,
222 const std::vector<std::string>& excluded_categories,
223 Options options);
224
225 // |categories| is a comma-delimited list of category wildcards.
226 // A category can have an optional '-' prefix to make it an excluded category.
227 // All the same rules apply above, so for example, having both included and
228 // excluded categories in the same list would not be supported.
229 //
230 // Example: SetEnabled("test_MyTest*");
231 // Example: SetEnabled("test_MyTest*,test_OtherStuff");
232 // Example: SetEnabled("-excluded_category1,-excluded_category2");
233 void SetEnabled(const std::string& categories, Options options);
234
235 // Retieves the categories set via a prior call to SetEnabled(). Only
236 // meaningful if |IsEnabled()| is true. 276 // meaningful if |IsEnabled()| is true.
237 void GetEnabledTraceCategories(std::vector<std::string>* included_out, 277 const CategoryFilter& GetCurrentCategoryFilter();
238 std::vector<std::string>* excluded_out);
239 278
240 Options trace_options() const { return trace_options_; } 279 Options trace_options() const { return trace_options_; }
241 280
281 // Enables tracing. See CategoryFilter comments for details
282 // on how to control what categories will be traced.
283 void SetEnabled(const CategoryFilter& category_filter, Options options);
284
242 // Disable tracing for all categories. 285 // Disable tracing for all categories.
243 void SetDisabled(); 286 void SetDisabled();
244 // Helper method to enable/disable tracing for all categories.
245 void SetEnabled(bool enabled, Options options);
246 bool IsEnabled() { return !!enable_count_; } 287 bool IsEnabled() { return !!enable_count_; }
247 288
248 #if defined(OS_ANDROID) 289 #if defined(OS_ANDROID)
249 void StartATrace(); 290 void StartATrace();
250 void StopATrace(); 291 void StopATrace();
251 #endif 292 #endif
252 293
253 // Enabled state listeners give a callback when tracing is enabled or 294 // Enabled state listeners give a callback when tracing is enabled or
254 // disabled. This can be used to tie into other library's tracing systems 295 // disabled. This can be used to tie into other library's tracing systems
255 // on-demand. 296 // on-demand.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 338
298 // Flush all collected events to the given output callback. The callback will 339 // Flush all collected events to the given output callback. The callback will
299 // be called one or more times with IPC-bite-size chunks. The string format is 340 // be called one or more times with IPC-bite-size chunks. The string format is
300 // undefined. Use TraceResultBuffer to convert one or more trace strings to 341 // undefined. Use TraceResultBuffer to convert one or more trace strings to
301 // JSON. 342 // JSON.
302 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)> 343 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)>
303 OutputCallback; 344 OutputCallback;
304 void Flush(const OutputCallback& cb); 345 void Flush(const OutputCallback& cb);
305 346
306 // Called by TRACE_EVENT* macros, don't call this directly. 347 // Called by TRACE_EVENT* macros, don't call this directly.
307 static const unsigned char* GetCategoryEnabled(const char* name); 348 // The name parameter is a category group for example:
308 static const char* GetCategoryName(const unsigned char* category_enabled); 349 // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent")
350 static const unsigned char* GetCategoryGroupEnabled(const char* name);
351 static const char* GetCategoryGroupName(
352 const unsigned char* category_group_enabled);
309 353
310 // Called by TRACE_EVENT* macros, don't call this directly. 354 // Called by TRACE_EVENT* macros, don't call this directly.
311 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied 355 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied
312 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. 356 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above.
313 void AddTraceEvent(char phase, 357 void AddTraceEvent(char phase,
314 const unsigned char* category_enabled, 358 const unsigned char* category_group_enabled,
315 const char* name, 359 const char* category_group,
316 unsigned long long id, 360 unsigned long long id,
317 int num_args, 361 int num_args,
318 const char** arg_names, 362 const char** arg_names,
319 const unsigned char* arg_types, 363 const unsigned char* arg_types,
320 const unsigned long long* arg_values, 364 const unsigned long long* arg_values,
321 unsigned char flags); 365 unsigned char flags);
322 void AddTraceEventWithThreadIdAndTimestamp( 366 void AddTraceEventWithThreadIdAndTimestamp(
323 char phase, 367 char phase,
324 const unsigned char* category_enabled, 368 const unsigned char* category_enabled,
325 const char* name, 369 const char* name,
326 unsigned long long id, 370 unsigned long long id,
327 int thread_id, 371 int thread_id,
328 const TimeTicks& timestamp, 372 const TimeTicks& timestamp,
329 int num_args, 373 int num_args,
330 const char** arg_names, 374 const char** arg_names,
331 const unsigned char* arg_types, 375 const unsigned char* arg_types,
332 const unsigned long long* arg_values, 376 const unsigned long long* arg_values,
333 unsigned char flags); 377 unsigned char flags);
334 static void AddTraceEventEtw(char phase, 378 static void AddTraceEventEtw(char phase,
335 const char* name, 379 const char* category_group,
336 const void* id, 380 const void* id,
337 const char* extra); 381 const char* extra);
338 static void AddTraceEventEtw(char phase, 382 static void AddTraceEventEtw(char phase,
339 const char* name, 383 const char* category_group,
340 const void* id, 384 const void* id,
341 const std::string& extra); 385 const std::string& extra);
342 386
343 // For every matching event, a notification will be fired. NOTE: the 387 // For every matching event, a notification will be fired. NOTE: the
344 // notification will fire for each matching event that has already occurred 388 // notification will fire for each matching event that has already occurred
345 // since tracing was started (including before tracing if the process was 389 // since tracing was started (including before tracing if the process was
346 // started with tracing turned on). 390 // started with tracing turned on).
347 void SetWatchEvent(const std::string& category_name, 391 void SetWatchEvent(const std::string& category_name,
348 const std::string& event_name); 392 const std::string& event_name);
349 // Cancel the watch event. If tracing is enabled, this may race with the 393 // Cancel the watch event. If tracing is enabled, this may race with the
(...skipping 22 matching lines...) Expand all
372 416
373 // Allow setting an offset between the current TimeTicks time and the time 417 // Allow setting an offset between the current TimeTicks time and the time
374 // that should be reported. 418 // that should be reported.
375 void SetTimeOffset(TimeDelta offset); 419 void SetTimeOffset(TimeDelta offset);
376 420
377 private: 421 private:
378 // This allows constructor and destructor to be private and usable only 422 // This allows constructor and destructor to be private and usable only
379 // by the Singleton class. 423 // by the Singleton class.
380 friend struct StaticMemorySingletonTraits<TraceLog>; 424 friend struct StaticMemorySingletonTraits<TraceLog>;
381 425
382 // The pointer returned from GetCategoryEnabledInternal() points to a value 426 // Enable/disable each category group based on the current category_filter_.
383 // with zero or more of the following bits. Used in this class only. 427 // If the category group contains a category that matches an included category
428 // pattern, that category group will be enabled.
429 void EnableIncludedCategoryGroups();
430 void EnableIncludedCategoryGroup(int category_index);
431
432 // The pointer returned from GetCategoryGroupEnabledInternal() points to a
433 // value with zero or more of the following bits. Used in this class only.
384 // The TRACE_EVENT macros should only use the value as a bool. 434 // The TRACE_EVENT macros should only use the value as a bool.
385 enum CategoryEnabledFlags { 435 enum CategoryEnabledFlags {
386 // Normal enabled flag for categories enabled with Enable(). 436 // Normal enabled flag for categories enabled with Enable().
387 CATEGORY_ENABLED = 1 << 0, 437 CATEGORY_ENABLED = 1 << 0,
388 // On Android if ATrace is enabled, all categories will have this bit. 438 // On Android if ATrace is enabled, all categories will have this bit.
389 // Not used on other platforms. 439 // Not used on other platforms.
390 ATRACE_ENABLED = 1 << 1 440 ATRACE_ENABLED = 1 << 1
391 }; 441 };
392 442
393 // Helper class for managing notification_thread_count_ and running 443 // Helper class for managing notification_thread_count_ and running
(...skipping 14 matching lines...) Expand all
408 inline void SendNotificationIfAny(); 458 inline void SendNotificationIfAny();
409 459
410 private: 460 private:
411 TraceLog* trace_log_; 461 TraceLog* trace_log_;
412 NotificationCallback callback_copy_; 462 NotificationCallback callback_copy_;
413 int notification_; 463 int notification_;
414 }; 464 };
415 465
416 TraceLog(); 466 TraceLog();
417 ~TraceLog(); 467 ~TraceLog();
418 const unsigned char* GetCategoryEnabledInternal(const char* name); 468 const unsigned char* GetCategoryGroupEnabledInternal(const char* name);
419 void AddThreadNameMetadataEvents(); 469 void AddThreadNameMetadataEvents();
420 470
421 #if defined(OS_ANDROID) 471 #if defined(OS_ANDROID)
422 void SendToATrace(char phase, 472 void SendToATrace(char phase,
423 const char* category, 473 const char* category_group,
424 const char* name, 474 const char* name,
425 unsigned long long id, 475 unsigned long long id,
426 int num_args, 476 int num_args,
427 const char** arg_names, 477 const char** arg_names,
428 const unsigned char* arg_types, 478 const unsigned char* arg_types,
429 const unsigned long long* arg_values, 479 const unsigned long long* arg_values,
430 unsigned char flags); 480 unsigned char flags);
431 static void ApplyATraceEnabledFlag(unsigned char* category_enabled); 481 static void ApplyATraceEnabledFlag(unsigned char* category_group_enabled);
432 #endif 482 #endif
433 483
434 TraceBuffer* GetTraceBuffer(); 484 TraceBuffer* GetTraceBuffer();
435 485
436 // TODO(nduca): switch to per-thread trace buffers to reduce thread 486 // TODO(nduca): switch to per-thread trace buffers to reduce thread
437 // synchronization. 487 // synchronization.
438 // This lock protects TraceLog member accesses from arbitrary threads. 488 // This lock protects TraceLog member accesses from arbitrary threads.
439 Lock lock_; 489 Lock lock_;
440 int enable_count_; 490 int enable_count_;
441 NotificationCallback notification_callback_; 491 NotificationCallback notification_callback_;
442 scoped_ptr<TraceBuffer> logged_events_; 492 scoped_ptr<TraceBuffer> logged_events_;
443 EventCallback event_callback_; 493 EventCallback event_callback_;
444 std::vector<std::string> included_categories_;
445 std::vector<std::string> excluded_categories_;
446 bool dispatching_to_observer_list_; 494 bool dispatching_to_observer_list_;
447 ObserverList<EnabledStateChangedObserver> enabled_state_observer_list_; 495 ObserverList<EnabledStateChangedObserver> enabled_state_observer_list_;
448 496
449 base::hash_map<int, std::string> thread_names_; 497 base::hash_map<int, std::string> thread_names_;
450 498
451 // XORed with TraceID to make it unlikely to collide with other processes. 499 // XORed with TraceID to make it unlikely to collide with other processes.
452 unsigned long long process_id_hash_; 500 unsigned long long process_id_hash_;
453 501
454 int process_id_; 502 int process_id_;
455 503
456 TimeDelta time_offset_; 504 TimeDelta time_offset_;
457 505
458 // Allow tests to wake up when certain events occur. 506 // Allow tests to wake up when certain events occur.
459 const unsigned char* watch_category_; 507 const unsigned char* watch_category_;
460 std::string watch_event_name_; 508 std::string watch_event_name_;
461 509
462 Options trace_options_; 510 Options trace_options_;
463 511
464 // Sampling thread handles. 512 // Sampling thread handles.
465 scoped_ptr<TraceSamplingThread> sampling_thread_; 513 scoped_ptr<TraceSamplingThread> sampling_thread_;
466 PlatformThreadHandle sampling_thread_handle_; 514 PlatformThreadHandle sampling_thread_handle_;
467 515
516 CategoryFilter category_filter_;
517
468 DISALLOW_COPY_AND_ASSIGN(TraceLog); 518 DISALLOW_COPY_AND_ASSIGN(TraceLog);
469 }; 519 };
470 520
471 } // namespace debug 521 } // namespace debug
472 } // namespace base 522 } // namespace base
473 523
474 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ 524 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698