OLD | NEW |
(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_BLAME_CONTEXT_H_ |
| 6 #define BASE_TRACE_EVENT_BLAME_CONTEXT_H_ |
| 7 |
| 8 #include <inttypes.h> |
| 9 |
| 10 #include "base/base_export.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/trace_event/trace_log.h" |
| 13 |
| 14 namespace base { |
| 15 namespace trace_event { |
| 16 class TracedValue; |
| 17 } |
| 18 |
| 19 namespace trace_event { |
| 20 |
| 21 // A blame context represents a logical unit to which we want to attribute |
| 22 // different costs (e.g., CPU, network, or memory usage). An example of a blame |
| 23 // context is an <iframe> element on a web page. Different subsystems can |
| 24 // "enter" and "leave" blame contexts to indicate that they are doing work which |
| 25 // should be accounted against this blame context. |
| 26 // |
| 27 // A blame context can optionally have a parent context, forming a blame context |
| 28 // tree. When work is attributed to a particular blame context, it is considered |
| 29 // to count against all of that context's children too. This is useful when work |
| 30 // cannot be exactly attributed into a more specific context. For example, |
| 31 // Javascript garbage collection generally needs to inspect all objects on a |
| 32 // page instead looking at each <iframe> individually. In this case the work |
| 33 // should be attributed to a blame context which is the parent of all <iframe> |
| 34 // blame contexts. |
| 35 class BASE_EXPORT BlameContext |
| 36 : public trace_event::TraceLog::EnabledStateObserver { |
| 37 public: |
| 38 // Construct a blame context belonging to the blame context tree |name|, using |
| 39 // the tracing category |category|, identified by |id| from the |scope| |
| 40 // namespace. |type| identifies the type of this object snapshot in the blame |
| 41 // context tree. |parent_context| is the parent of this blame context or |
| 42 // null. Note that all strings must have application lifetime. |
| 43 // |
| 44 // For example, a blame context which represents a specific <iframe> in a |
| 45 // browser frame tree could be specified with: |
| 46 // |
| 47 // category="blink", |
| 48 // name="FrameTree", |
| 49 // type="IFrame", |
| 50 // scope="IFrameIdentifier", |
| 51 // id=1234. |
| 52 // |
| 53 // Each <iframe> blame context could have another <iframe> context as a |
| 54 // parent, or a top-level context which represents the entire browser: |
| 55 // |
| 56 // category="blink", |
| 57 // name="FrameTree", |
| 58 // type="Browser", |
| 59 // scope="BrowserIdentifier", |
| 60 // id=1. |
| 61 // |
| 62 // Note that the |name| property is identical, signifying that both context |
| 63 // types are part of the same tree. |
| 64 // |
| 65 BlameContext(const char* category, |
| 66 const char* name, |
| 67 const char* type, |
| 68 const char* scope, |
| 69 int64_t id, |
| 70 const BlameContext* parent_context); |
| 71 ~BlameContext() override; |
| 72 |
| 73 // Initialize the blame context, automatically taking a snapshot if tracing is |
| 74 // enabled. Must be called before any other methods on this class. |
| 75 void Initialize(); |
| 76 |
| 77 // Indicate that the current thread is now doing work which should count |
| 78 // against this blame context. |
| 79 void Enter(); |
| 80 |
| 81 // Leave and stop doing work for a previously entered blame context. If |
| 82 // another blame context belongin to the same tree was entered prior to this |
| 83 // one, it becomes the active blame context for this thread again. |
| 84 void Leave(); |
| 85 |
| 86 // Record a snapshot of the blame context. This is normally only needed if a |
| 87 // blame context subclass defines custom properties (see AsValueInto) and one |
| 88 // or more of those properties have changed. |
| 89 void TakeSnapshot(); |
| 90 |
| 91 const char* category() const { return category_; } |
| 92 const char* name() const { return name_; } |
| 93 const char* type() const { return type_; } |
| 94 const char* scope() const { return scope_; } |
| 95 int64_t id() const { return id_; } |
| 96 |
| 97 // trace_event::TraceLog::EnabledStateObserver implementation: |
| 98 void OnTraceLogEnabled() override; |
| 99 void OnTraceLogDisabled() override; |
| 100 |
| 101 protected: |
| 102 // Serialize the properties of this blame context into |state|. Subclasses can |
| 103 // override this method to record additional properties (e.g, the URL for an |
| 104 // <iframe> blame context). Note that an overridden implementation must still |
| 105 // call this base method. |
| 106 virtual void AsValueInto(trace_event::TracedValue* state); |
| 107 |
| 108 private: |
| 109 bool WasInitialized() const; |
| 110 |
| 111 // The following string pointers have application lifetime. |
| 112 const char* category_; |
| 113 const char* name_; |
| 114 const char* type_; |
| 115 const char* scope_; |
| 116 const int64_t id_; |
| 117 |
| 118 const char* parent_scope_; |
| 119 const int64_t parent_id_; |
| 120 |
| 121 const unsigned char* category_group_enabled_; |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(BlameContext); |
| 124 }; |
| 125 |
| 126 } // namespace trace_event |
| 127 } // namespace base |
| 128 |
| 129 #endif // BASE_TRACE_EVENT_BLAME_CONTEXT_H_ |
OLD | NEW |