| Index: base/trace_event/trace_category.h
|
| diff --git a/base/trace_event/trace_category.h b/base/trace_event/trace_category.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3d8afa3433cc9a9eee3fa4ce67d85a2453864f53
|
| --- /dev/null
|
| +++ b/base/trace_event/trace_category.h
|
| @@ -0,0 +1,98 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef BASE_TRACE_EVENT_TRACE_CATEGORY_H_
|
| +#define BASE_TRACE_EVENT_TRACE_CATEGORY_H_
|
| +
|
| +#include <stdint.h>
|
| +
|
| +namespace base {
|
| +namespace trace_event {
|
| +
|
| +// Captures the state of an invidivual trace category. Nothing except tracing
|
| +// internals (e.g., TraceLog) is supposed to have non-const Category pointers.
|
| +class TraceCategory {
|
| + public:
|
| + // The TRACE_EVENT macros should only use this value as a bool.
|
| + // These enum values are effectively a public API and third_party projects
|
| + // depend on their value. Hence, never remove or recycle existing bits, unless
|
| + // you are sure that all the third-party projects that depend on this have
|
| + // been updated.
|
| + enum StateFlags : uint8_t {
|
| + ENABLED_FOR_RECORDING = 1 << 0,
|
| +
|
| + // Not used anymore.
|
| + DEPRECATED_ENABLED_FOR_MONITORING = 1 << 1,
|
| + DEPRECATED_ENABLED_FOR_EVENT_CALLBACK = 1 << 2,
|
| +
|
| + ENABLED_FOR_ETW_EXPORT = 1 << 3,
|
| + ENABLED_FOR_FILTERING = 1 << 4
|
| + };
|
| +
|
| + TraceCategory() : TraceCategory(nullptr) {}
|
| + TraceCategory(const char* name)
|
| + : state_(0), enabled_filters_(0), name_(name) {
|
| + static_assert(
|
| + offsetof(TraceCategory, state_) == 0,
|
| + "|state_| must be the first field of the TraceCategory class.");
|
| + }
|
| +
|
| + bool is_valid() const { return name_ != nullptr; }
|
| + void set_name(const char* name) { name_ = name; }
|
| + const char* name() const {
|
| + DCHECK(is_valid());
|
| + return name_;
|
| + }
|
| +
|
| + // TODO(primiano): This is an intermediate solution to deal with the fact that
|
| + // today TRACE_EVENT* macros cache the state ptr. They should just cache the
|
| + // full TraceCategory ptr, which is immutable, and use these helper function
|
| + // here. This will get rid of the need of this awkward ptr getter completely.
|
| + const uint8_t* state_ptr() const {
|
| + return const_cast<const uint8_t*>(&state_);
|
| + }
|
| +
|
| + bool is_enabled() const { return state_ != 0; }
|
| + bool is(StateFlags flag) const { return (state_ & flag) != 0; }
|
| + void set_state(uint8_t state) { state_ = state; }
|
| + void clear_state_flag(StateFlags flag) { state_ &= ~flag; }
|
| + void set_state_flag(StateFlags flag) { state_ |= flag; }
|
| +
|
| + bool is_filter_enabled(size_t index) const {
|
| + DCHECK(index < sizeof(enabled_filters_) * 8);
|
| + return (enabled_filters_ & (1 << index)) != 0;
|
| + }
|
| + uint16_t enabled_filters() const { return enabled_filters_; }
|
| + void set_enabled_filters(uint16_t enabled_filters) {
|
| + enabled_filters_ = enabled_filters;
|
| + }
|
| +
|
| + void reset_for_testing() {
|
| + state_ = 0;
|
| + enabled_filters_ = 0;
|
| + }
|
| +
|
| + private:
|
| + // The enabled state. TRACE_EVENT* macros should capture the event if any of
|
| + // the flags here are set. This state is racy by design: this is polled by
|
| + // TRACE_EVENTx macros in a lot of fast-paths all over the codebase. We don't
|
| + // want to slow them down with any kind of barrier. This state is effectively
|
| + // mutated when starting/stopping tracing, in which case don't care if we miss
|
| + // some events.
|
| + volatile uint8_t state_;
|
| +
|
| + // When ENABLED_FOR_FILTERING is set, this contains a bitmap to the
|
| + // coressponding filter (see event_filters.h).
|
| + uint16_t enabled_filters_;
|
| +
|
| + // TraceCategory group names are long lived static strings.
|
| + const char* name_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TraceCategory);
|
| +};
|
| +
|
| +} // namespace trace_event
|
| +} // namespace base
|
| +
|
| +#endif // BASE_TRACE_EVENT_TRACE_CATEGORY_H_
|
|
|