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

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

Issue 11823016: Trace category groups and category filter. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Category group and category filter basics. Created 7 years, 10 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
« no previous file with comments | « no previous file | base/debug/trace_event_impl.cc » ('j') | base/debug/trace_event_impl.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "build/build_config.h" 9 #include "build/build_config.h"
10 10
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 // When all fragments have been added, call Finish to complete the JSON 151 // When all fragments have been added, call Finish to complete the JSON
152 // formatted output. 152 // formatted output.
153 void Finish(); 153 void Finish();
154 154
155 private: 155 private:
156 OutputCallback output_callback_; 156 OutputCallback output_callback_;
157 bool append_comma_; 157 bool append_comma_;
158 }; 158 };
159 159
160 class BASE_EXPORT CategoryFilter {
161 // The default category filter, used when none is provided.
162 // Allows all categories through, except if they end in the suffix 'Debug' or
163 // 'Test'.
164 static const char* kDefaultCategoryFilterString;
165
166 public:
167 // Constructs the default category filter. This is equivalent to
168 // CategoryFilter(CategoryFilter::kDefaultCategoryFilterString);
169 CategoryFilter();
170
171 // |filter_string| is a comma-delimited list of category wildcards.
172 // A category can have an optional '-' prefix to make it an excluded category.
173 // All the same rules apply above, so for example, having both included and
174 // excluded categories in the same list would not be supported.
175 //
176 // Example: SetEnabled("test_MyTest*");
177 // Example: SetEnabled("test_MyTest*,test_OtherStuff");
178 // Example: SetEnabled("-excluded_category1,-excluded_category2");
179 CategoryFilter(const std::string& filter_string);
180
181 // Writes the string representation of the CategoryFilter. This is a comma
182 // separated string, similar in nature to the one used to determine
183 // enabled/disabled category patterns, except here there is an arbitrary
184 // order, included categories go first, then excluded categories. Excluded
185 // categories are distinguished from included categories by the prefix '-'.
186 void ToString(std::string* filter_string) const;
187
188 // Determines whether category group would be enabled or
189 // disabled by this filter.
190 bool IsCategoryGroupEnabled(const char* category_group) const;
191
192 // Merges nested_filter with the current CategoryFilter
193 void Merge(const CategoryFilter& nested_filter);
194
195 // Determines whether or not we have included category patterns.
196 bool HasIncludedCategories() const;
197
198 // Clears both included/excluded pattern lists.
199 void Clear();
200
201 private:
202 void Initialize(const std::string& filter_string);
203 void WriteString(std::string* out, bool included) const;
204
205 std::vector<std::string> included_;
206 std::vector<std::string> excluded_;
207
208 };
160 209
161 class BASE_EXPORT TraceLog { 210 class BASE_EXPORT TraceLog {
162 public: 211 public:
163 // Notification is a mask of one or more of the following events. 212 // Notification is a mask of one or more of the following events.
164 enum Notification { 213 enum Notification {
165 // The trace buffer does not flush dynamically, so when it fills up, 214 // The trace buffer does not flush dynamically, so when it fills up,
166 // subsequent trace events will be dropped. This callback is generated when 215 // subsequent trace events will be dropped. This callback is generated when
167 // the trace buffer is full. The callback must be thread safe. 216 // the trace buffer is full. The callback must be thread safe.
168 TRACE_BUFFER_FULL = 1 << 0, 217 TRACE_BUFFER_FULL = 1 << 0,
169 // A subscribed trace-event occurred. 218 // A subscribed trace-event occurred.
170 EVENT_WATCH_NOTIFICATION = 1 << 1 219 EVENT_WATCH_NOTIFICATION = 1 << 1
171 }; 220 };
172 221
173 static TraceLog* GetInstance(); 222 static TraceLog* GetInstance();
174 223
175 // Get set of known categories. This can change as new code paths are reached. 224 // Get set of known category groups. This can change as new code paths are
176 // The known categories are inserted into |categories|. 225 // reached. The known category groups are inserted into |category_groups|.
177 void GetKnownCategories(std::vector<std::string>* categories); 226 void GetKnownCategories(std::vector<std::string>* category_groups);
178 227
179 // Enable tracing for provided list of categories. If tracing is already 228 // Enables tracing. See CategoryFilter comments for details
180 // enabled, this method does nothing -- changing categories during trace is 229 // on how to control what categories will be traced.
181 // not supported. 230 void SetEnabled(const CategoryFilter& category_filter);
182 // If both included_categories and excluded_categories are empty,
183 // all categories are traced.
184 // Else if included_categories is non-empty, only those are traced.
185 // Else if excluded_categories is non-empty, everything but those are traced.
186 // Wildcards * and ? are supported (see MatchPattern in string_util.h).
187 void SetEnabled(const std::vector<std::string>& included_categories,
188 const std::vector<std::string>& excluded_categories);
189
190 // |categories| is a comma-delimited list of category wildcards.
191 // A category can have an optional '-' prefix to make it an excluded category.
192 // All the same rules apply above, so for example, having both included and
193 // excluded categories in the same list would not be supported.
194 //
195 // Example: SetEnabled("test_MyTest*");
196 // Example: SetEnabled("test_MyTest*,test_OtherStuff");
197 // Example: SetEnabled("-excluded_category1,-excluded_category2");
198 void SetEnabled(const std::string& categories);
199
200 // Retieves the categories set via a prior call to SetEnabled(). Only
201 // meaningful if |IsEnabled()| is true.
202 void GetEnabledTraceCategories(std::vector<std::string>* included_out,
203 std::vector<std::string>* excluded_out);
204 231
205 // Disable tracing for all categories. 232 // Disable tracing for all categories.
206 void SetDisabled(); 233 void SetDisabled();
234
235 // Retieves the current filter used to enable categories. Only
236 // meaningful if |IsEnabled()| is true.
237 CategoryFilter& GetCurrentCategoryFilter();
238
207 // Helper method to enable/disable tracing for all categories. 239 // Helper method to enable/disable tracing for all categories.
208 void SetEnabled(bool enabled);
209 bool IsEnabled() { return !!enable_count_; } 240 bool IsEnabled() { return !!enable_count_; }
210 241
211 #if defined(OS_ANDROID) 242 #if defined(OS_ANDROID)
212 void StartATrace(); 243 void StartATrace();
213 void StopATrace(); 244 void StopATrace();
214 #endif 245 #endif
215 246
216 // Enabled state listeners give a callback when tracing is enabled or 247 // Enabled state listeners give a callback when tracing is enabled or
217 // disabled. This can be used to tie into other library's tracing systems 248 // disabled. This can be used to tie into other library's tracing systems
218 // on-demand. 249 // on-demand.
(...skipping 25 matching lines...) Expand all
244 275
245 // Flush all collected events to the given output callback. The callback will 276 // Flush all collected events to the given output callback. The callback will
246 // be called one or more times with IPC-bite-size chunks. The string format is 277 // be called one or more times with IPC-bite-size chunks. The string format is
247 // undefined. Use TraceResultBuffer to convert one or more trace strings to 278 // undefined. Use TraceResultBuffer to convert one or more trace strings to
248 // JSON. 279 // JSON.
249 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)> 280 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)>
250 OutputCallback; 281 OutputCallback;
251 void Flush(const OutputCallback& cb); 282 void Flush(const OutputCallback& cb);
252 283
253 // Called by TRACE_EVENT* macros, don't call this directly. 284 // Called by TRACE_EVENT* macros, don't call this directly.
285 // The name parameter is a category group for example:
286 // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent")
254 static const unsigned char* GetCategoryEnabled(const char* name); 287 static const unsigned char* GetCategoryEnabled(const char* name);
255 static const char* GetCategoryName(const unsigned char* category_enabled); 288 static const char* GetCategoryName(const unsigned char* category_enabled);
256 289
257 // Called by TRACE_EVENT* macros, don't call this directly. 290 // Called by TRACE_EVENT* macros, don't call this directly.
258 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied 291 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied
259 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. 292 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above.
260 void AddTraceEvent(char phase, 293 void AddTraceEvent(char phase,
261 const unsigned char* category_enabled, 294 const unsigned char* category_enabled,
262 const char* name, 295 const char* name,
263 unsigned long long id, 296 unsigned long long id,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 351
319 // Allow setting an offset between the current TimeTicks time and the time 352 // Allow setting an offset between the current TimeTicks time and the time
320 // that should be reported. 353 // that should be reported.
321 void SetTimeOffset(TimeDelta offset); 354 void SetTimeOffset(TimeDelta offset);
322 355
323 private: 356 private:
324 // This allows constructor and destructor to be private and usable only 357 // This allows constructor and destructor to be private and usable only
325 // by the Singleton class. 358 // by the Singleton class.
326 friend struct StaticMemorySingletonTraits<TraceLog>; 359 friend struct StaticMemorySingletonTraits<TraceLog>;
327 360
328 // The pointer returned from GetCategoryEnabledInternal() points to a value 361 // Enable/disable each category group based on the given |category_filter|.
329 // with zero or more of the following bits. Used in this class only. 362 // If the category group contains a category that matches an included category
363 // pattern, that category group will be enabled.
364 static void EnableIncludedCategoryGroups(
365 const CategoryFilter& category_filter);
366 static void EnableIncludedCategoryGroup(int category_index,
367 const CategoryFilter& category_filter);
368
369 // The pointer returned from GetCategoryGroupEnabledInternal() points to a
370 // value with zero or more of the following bits. Used in this class only.
330 // The TRACE_EVENT macros should only use the value as a bool. 371 // The TRACE_EVENT macros should only use the value as a bool.
331 enum CategoryEnabledFlags { 372 enum CategoryEnabledFlags {
332 // Normal enabled flag for categories enabled with Enable(). 373 // Normal enabled flag for categories enabled with Enable().
333 CATEGORY_ENABLED = 1 << 0, 374 CATEGORY_ENABLED = 1 << 0,
334 // On Android if ATrace is enabled, all categories will have this bit. 375 // On Android if ATrace is enabled, all categories will have this bit.
335 // Not used on other platforms. 376 // Not used on other platforms.
336 ATRACE_ENABLED = 1 << 1 377 ATRACE_ENABLED = 1 << 1
337 }; 378 };
338 379
339 // Helper class for managing notification_thread_count_ and running 380 // Helper class for managing notification_thread_count_ and running
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 static void ApplyATraceEnabledFlag(unsigned char* category_enabled); 416 static void ApplyATraceEnabledFlag(unsigned char* category_enabled);
376 #endif 417 #endif
377 418
378 // TODO(nduca): switch to per-thread trace buffers to reduce thread 419 // TODO(nduca): switch to per-thread trace buffers to reduce thread
379 // synchronization. 420 // synchronization.
380 // This lock protects TraceLog member accesses from arbitrary threads. 421 // This lock protects TraceLog member accesses from arbitrary threads.
381 Lock lock_; 422 Lock lock_;
382 int enable_count_; 423 int enable_count_;
383 NotificationCallback notification_callback_; 424 NotificationCallback notification_callback_;
384 std::vector<TraceEvent> logged_events_; 425 std::vector<TraceEvent> logged_events_;
385 std::vector<std::string> included_categories_; 426 CategoryFilter category_filter_;
386 std::vector<std::string> excluded_categories_;
387 bool dispatching_to_observer_list_; 427 bool dispatching_to_observer_list_;
388 ObserverList<EnabledStateChangedObserver> enabled_state_observer_list_; 428 ObserverList<EnabledStateChangedObserver> enabled_state_observer_list_;
389 429
390 base::hash_map<int, std::string> thread_names_; 430 base::hash_map<int, std::string> thread_names_;
391 431
392 // XORed with TraceID to make it unlikely to collide with other processes. 432 // XORed with TraceID to make it unlikely to collide with other processes.
393 unsigned long long process_id_hash_; 433 unsigned long long process_id_hash_;
394 434
395 int process_id_; 435 int process_id_;
396 436
397 TimeDelta time_offset_; 437 TimeDelta time_offset_;
398 438
399 // Allow tests to wake up when certain events occur. 439 // Allow tests to wake up when certain events occur.
400 const unsigned char* watch_category_; 440 const unsigned char* watch_category_;
401 std::string watch_event_name_; 441 std::string watch_event_name_;
402 442
403 DISALLOW_COPY_AND_ASSIGN(TraceLog); 443 DISALLOW_COPY_AND_ASSIGN(TraceLog);
404 }; 444 };
405 445
406 } // namespace debug 446 } // namespace debug
407 } // namespace base 447 } // namespace base
408 448
409 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ 449 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_
OLDNEW
« no previous file with comments | « no previous file | base/debug/trace_event_impl.cc » ('j') | base/debug/trace_event_impl.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698