trace_category.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef BASE_TRACE_EVENT_TRACE_CATEGORY_H_
  5. #define BASE_TRACE_EVENT_TRACE_CATEGORY_H_
  6. #include <stdint.h>
  7. #include "base/check.h"
  8. namespace base {
  9. namespace trace_event {
  10. // Captures the state of an invidivual trace category. Nothing except tracing
  11. // internals (e.g., TraceLog) is supposed to have non-const Category pointers.
  12. struct TraceCategory {
  13. // The TRACE_EVENT macros should only use this value as a bool.
  14. // These enum values are effectively a public API and third_party projects
  15. // depend on their value. Hence, never remove or recycle existing bits, unless
  16. // you are sure that all the third-party projects that depend on this have
  17. // been updated.
  18. enum StateFlags : uint8_t {
  19. ENABLED_FOR_RECORDING = 1 << 0,
  20. // Not used anymore.
  21. DEPRECATED_ENABLED_FOR_MONITORING = 1 << 1,
  22. DEPRECATED_ENABLED_FOR_EVENT_CALLBACK = 1 << 2,
  23. ENABLED_FOR_ETW_EXPORT = 1 << 3,
  24. ENABLED_FOR_FILTERING = 1 << 4
  25. };
  26. static const TraceCategory* FromStatePtr(const uint8_t* state_ptr) {
  27. static_assert(
  28. offsetof(TraceCategory, state_) == 0,
  29. "|state_| must be the first field of the TraceCategory class.");
  30. return reinterpret_cast<const TraceCategory*>(state_ptr);
  31. }
  32. bool is_valid() const { return name_ != nullptr; }
  33. void set_name(const char* name) { name_ = name; }
  34. const char* name() const {
  35. DCHECK(is_valid());
  36. return name_;
  37. }
  38. // TODO(primiano): This is an intermediate solution to deal with the fact that
  39. // today TRACE_EVENT* macros cache the state ptr. They should just cache the
  40. // full TraceCategory ptr, which is immutable, and use these helper function
  41. // here. This will get rid of the need of this awkward ptr getter completely.
  42. constexpr const uint8_t* state_ptr() const {
  43. return const_cast<const uint8_t*>(&state_);
  44. }
  45. uint8_t state() const {
  46. return *const_cast<volatile const uint8_t*>(&state_);
  47. }
  48. bool is_enabled() const { return state() != 0; }
  49. void set_state(uint8_t state) {
  50. *const_cast<volatile uint8_t*>(&state_) = state;
  51. }
  52. void clear_state_flag(StateFlags flag) { set_state(state() & (~flag)); }
  53. void set_state_flag(StateFlags flag) { set_state(state() | flag); }
  54. uint32_t enabled_filters() const {
  55. return *const_cast<volatile const uint32_t*>(&enabled_filters_);
  56. }
  57. bool is_filter_enabled(size_t index) const {
  58. DCHECK(index < sizeof(enabled_filters_) * 8);
  59. return (enabled_filters() & (1 << index)) != 0;
  60. }
  61. void set_enabled_filters(uint32_t enabled_filters) {
  62. *const_cast<volatile uint32_t*>(&enabled_filters_) = enabled_filters;
  63. }
  64. void reset_for_testing() {
  65. set_state(0);
  66. set_enabled_filters(0);
  67. }
  68. // These fields should not be accessed directly, not even by tracing code.
  69. // The only reason why these are not private is because it makes it impossible
  70. // to have a global array of TraceCategory in category_registry.cc without
  71. // creating initializers. See discussion on goo.gl/qhZN94 and
  72. // crbug.com/{660967,660828}.
  73. // The enabled state. TRACE_EVENT* macros will capture events if any of the
  74. // flags here are set. Since TRACE_EVENTx macros are used in a lot of
  75. // fast-paths, accesses to this field are non-barriered and racy by design.
  76. // This field is mutated when starting/stopping tracing and we don't care
  77. // about missing some events.
  78. uint8_t state_;
  79. // When ENABLED_FOR_FILTERING is set, this contains a bitmap to the
  80. // corresponding filter (see event_filters.h).
  81. uint32_t enabled_filters_;
  82. // TraceCategory group names are long lived static strings.
  83. const char* name_;
  84. };
  85. } // namespace trace_event
  86. } // namespace base
  87. #endif // BASE_TRACE_EVENT_TRACE_CATEGORY_H_