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

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

Issue 2452063003: tracing: split out the CategoryRegistry from the TraceLog (Closed)
Patch Set: fix test checks Created 4 years, 1 month 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 2016 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_TRACE_CATEGORY_H_
6 #define BASE_TRACE_EVENT_TRACE_CATEGORY_H_
7
8 #include <stdint.h>
9
10 namespace base {
11 namespace trace_event {
12
13 // Captures the state of an invidivual trace category. Nothing except tracing
14 // internals (e.g., TraceLog) is supposed to have non-const Category pointers.
15 class TraceCategory {
16 public:
17 // The TRACE_EVENT macros should only use this value as a bool.
18 // These enum values are effectively a public API and third_party projects
19 // depend on their value. Hence, never remove or recycle existing bits, unless
20 // you are sure that all the third-party projects that depend on this have
21 // been updated.
22 enum StateFlags : uint8_t {
23 ENABLED_FOR_RECORDING = 1 << 0,
24
25 // Not used anymore.
26 DEPRECATED_ENABLED_FOR_MONITORING = 1 << 1,
27 DEPRECATED_ENABLED_FOR_EVENT_CALLBACK = 1 << 2,
28
29 ENABLED_FOR_ETW_EXPORT = 1 << 3,
30 ENABLED_FOR_FILTERING = 1 << 4
31 };
32
33 TraceCategory() : TraceCategory(nullptr) {}
34 TraceCategory(const char* name)
35 : state_(0), enabled_filters_(0), name_(name) {
36 static_assert(
37 offsetof(TraceCategory, state_) == 0,
38 "|state_| must be the first field of the TraceCategory class.");
39 }
40
41 bool is_valid() const { return name_ != nullptr; }
42 void set_name(const char* name) { name_ = name; }
43 const char* name() const {
44 DCHECK(is_valid());
45 return name_;
46 }
47
48 // TODO(primiano): This is an intermediate solution to deal with the fact that
49 // today TRACE_EVENT* macros cache the state ptr. They should just cache the
50 // full TraceCategory ptr, which is immutable, and use these helper function
51 // here. This will get rid of the need of this awkward ptr getter completely.
52 const uint8_t* state_ptr() const {
53 return const_cast<const uint8_t*>(&state_);
54 }
55
56 bool is_enabled() const { return state_ != 0; }
57 bool is(StateFlags flag) const { return (state_ & flag) != 0; }
58 void set_state(uint8_t state) { state_ = state; }
59 void clear_state_flag(StateFlags flag) { state_ &= ~flag; }
60 void set_state_flag(StateFlags flag) { state_ |= flag; }
61
62 bool is_filter_enabled(size_t index) const {
63 DCHECK(index < sizeof(enabled_filters_) * 8);
64 return (enabled_filters_ & (1 << index)) != 0;
65 }
66 uint16_t enabled_filters() const { return enabled_filters_; }
67 void set_enabled_filters(uint16_t enabled_filters) {
68 enabled_filters_ = enabled_filters;
69 }
70
71 void reset_for_testing() {
72 state_ = 0;
73 enabled_filters_ = 0;
74 }
75
76 private:
77 // The enabled state. TRACE_EVENT* macros should capture the event if any of
78 // the flags here are set. This state is racy by design: this is polled by
79 // TRACE_EVENTx macros in a lot of fast-paths all over the codebase. We don't
80 // want to slow them down with any kind of barrier. This state is effectively
81 // mutated when starting/stopping tracing, in which case don't care if we miss
82 // some events.
83 volatile uint8_t state_;
84
85 // When ENABLED_FOR_FILTERING is set, this contains a bitmap to the
86 // coressponding filter (see event_filters.h).
87 uint16_t enabled_filters_;
88
89 // TraceCategory group names are long lived static strings.
90 const char* name_;
91
92 DISALLOW_COPY_AND_ASSIGN(TraceCategory);
93 };
94
95 } // namespace trace_event
96 } // namespace base
97
98 #endif // BASE_TRACE_EVENT_TRACE_CATEGORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698