• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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 #include "base/check.h"
11 
12 namespace base {
13 namespace trace_event {
14 
15 // Captures the state of an invidivual trace category. Nothing except tracing
16 // internals (e.g., TraceLog) is supposed to have non-const Category pointers.
17 struct TraceCategory {
18   // The TRACE_EVENT macros should only use this value as a bool.
19   // These enum values are effectively a public API and third_party projects
20   // depend on their value. Hence, never remove or recycle existing bits, unless
21   // you are sure that all the third-party projects that depend on this have
22   // been updated.
23   enum StateFlags : uint8_t {
24     ENABLED_FOR_RECORDING = 1 << 0,
25 
26     // Not used anymore.
27     DEPRECATED_ENABLED_FOR_MONITORING = 1 << 1,
28     DEPRECATED_ENABLED_FOR_EVENT_CALLBACK = 1 << 2,
29 
30     ENABLED_FOR_ETW_EXPORT = 1 << 3,
31     ENABLED_FOR_FILTERING = 1 << 4
32   };
33 
FromStatePtrTraceCategory34   static const TraceCategory* FromStatePtr(const uint8_t* state_ptr) {
35     static_assert(
36         offsetof(TraceCategory, state_) == 0,
37         "|state_| must be the first field of the TraceCategory class.");
38     return reinterpret_cast<const TraceCategory*>(state_ptr);
39   }
40 
is_validTraceCategory41   bool is_valid() const { return name_ != nullptr; }
set_nameTraceCategory42   void set_name(const char* name) { name_ = name; }
nameTraceCategory43   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.
state_ptrTraceCategory52   constexpr const uint8_t* state_ptr() const {
53     return const_cast<const uint8_t*>(&state_);
54   }
55 
stateTraceCategory56   uint8_t state() const {
57     return *const_cast<volatile const uint8_t*>(&state_);
58   }
59 
is_enabledTraceCategory60   bool is_enabled() const { return state() != 0; }
61 
set_stateTraceCategory62   void set_state(uint8_t state) {
63     *const_cast<volatile uint8_t*>(&state_) = state;
64   }
65 
clear_state_flagTraceCategory66   void clear_state_flag(StateFlags flag) { set_state(state() & (~flag)); }
set_state_flagTraceCategory67   void set_state_flag(StateFlags flag) { set_state(state() | flag); }
68 
enabled_filtersTraceCategory69   uint32_t enabled_filters() const {
70     return *const_cast<volatile const uint32_t*>(&enabled_filters_);
71   }
72 
is_filter_enabledTraceCategory73   bool is_filter_enabled(size_t index) const {
74     DCHECK(index < sizeof(enabled_filters_) * 8);
75     return (enabled_filters() & (1 << index)) != 0;
76   }
77 
set_enabled_filtersTraceCategory78   void set_enabled_filters(uint32_t enabled_filters) {
79     *const_cast<volatile uint32_t*>(&enabled_filters_) = enabled_filters;
80   }
81 
reset_for_testingTraceCategory82   void reset_for_testing() {
83     set_state(0);
84     set_enabled_filters(0);
85   }
86 
87   // These fields should not be accessed directly, not even by tracing code.
88   // The only reason why these are not private is because it makes it impossible
89   // to have a global array of TraceCategory in category_registry.cc without
90   // creating initializers. See discussion on goo.gl/qhZN94 and
91   // crbug.com/{660967,660828}.
92 
93   // The enabled state. TRACE_EVENT* macros will capture events if any of the
94   // flags here are set. Since TRACE_EVENTx macros are used in a lot of
95   // fast-paths, accesses to this field are non-barriered and racy by design.
96   // This field is mutated when starting/stopping tracing and we don't care
97   // about missing some events.
98   uint8_t state_;
99 
100   // When ENABLED_FOR_FILTERING is set, this contains a bitmap to the
101   // corresponding filter (see event_filters.h).
102   uint32_t enabled_filters_;
103 
104   // TraceCategory group names are long lived static strings.
105   const char* name_;
106 };
107 
108 }  // namespace trace_event
109 }  // namespace base
110 
111 #endif  // BASE_TRACE_EVENT_TRACE_CATEGORY_H_
112