| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TRACE_EVENT_BLAME_CONTEXT_H_ | 5 #ifndef BASE_TRACE_EVENT_BLAME_CONTEXT_H_ |
| 6 #define BASE_TRACE_EVENT_BLAME_CONTEXT_H_ | 6 #define BASE_TRACE_EVENT_BLAME_CONTEXT_H_ |
| 7 | 7 |
| 8 #include <inttypes.h> | 8 #include <inttypes.h> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" |
| 12 #include "base/trace_event/trace_log.h" | 13 #include "base/trace_event/trace_log.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 namespace trace_event { | 16 namespace trace_event { |
| 16 class TracedValue; | 17 class TracedValue; |
| 17 } | 18 } |
| 18 | 19 |
| 19 namespace trace_event { | 20 namespace trace_event { |
| 20 | 21 |
| 21 // A blame context represents a logical unit to which we want to attribute | 22 // 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 // 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 // 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 // "enter" and "leave" blame contexts to indicate that they are doing work which |
| 25 // should be accounted against this blame context. | 26 // should be accounted against this blame context. |
| 26 // | 27 // |
| 27 // A blame context can optionally have a parent context, forming a blame context | 28 // 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 // 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 // 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 // cannot be exactly attributed into a more specific context. For example, |
| 31 // Javascript garbage collection generally needs to inspect all objects on a | 32 // 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 // 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 // should be attributed to a blame context which is the parent of all <iframe> |
| 34 // blame contexts. | 35 // blame contexts. |
| 36 // |
| 37 // |
| 38 // *** THREAD SAFETY *** |
| 39 // |
| 40 // All member functions of BlameContext, except those marked with "ThreadSafe", |
| 41 // must be called from the same thread where the instance is constructed. |
| 42 // |
| 43 // Any subclass overriding OnTraceLogEnabled()/Disabled() must perform thread |
| 44 // checking and make sure that any call to a other non-ThreadSafe function from |
| 45 // a different thread is done as a task posted to task_runner(). |
| 46 // |
| 47 // Any subclass must call UnregisterFromTraceLog() at the beginning of its dtor |
| 48 // to prevent access from the TraceLog when the instance is being destructed. |
| 35 class BASE_EXPORT BlameContext | 49 class BASE_EXPORT BlameContext |
| 36 : public trace_event::TraceLog::EnabledStateObserver { | 50 : public trace_event::TraceLog::EnabledStateObserver { |
| 37 public: | 51 public: |
| 38 // Construct a blame context belonging to the blame context tree |name|, using | 52 // Construct a blame context belonging to the blame context tree |name|, using |
| 39 // the tracing category |category|, identified by |id| from the |scope| | 53 // the tracing category |category|, identified by |id| from the |scope| |
| 40 // namespace. |type| identifies the type of this object snapshot in the blame | 54 // 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 | 55 // context tree. |parent_context| is the parent of this blame context or |
| 42 // null. Note that all strings must have application lifetime. | 56 // null. Note that all strings must have application lifetime. |
| 43 // | 57 // |
| 44 // For example, a blame context which represents a specific <iframe> in a | 58 // For example, a blame context which represents a specific <iframe> in a |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // blame context subclass defines custom properties (see AsValueInto) and one | 101 // blame context subclass defines custom properties (see AsValueInto) and one |
| 88 // or more of those properties have changed. | 102 // or more of those properties have changed. |
| 89 void TakeSnapshot(); | 103 void TakeSnapshot(); |
| 90 | 104 |
| 91 const char* category() const { return category_; } | 105 const char* category() const { return category_; } |
| 92 const char* name() const { return name_; } | 106 const char* name() const { return name_; } |
| 93 const char* type() const { return type_; } | 107 const char* type() const { return type_; } |
| 94 const char* scope() const { return scope_; } | 108 const char* scope() const { return scope_; } |
| 95 int64_t id() const { return id_; } | 109 int64_t id() const { return id_; } |
| 96 | 110 |
| 111 // ThreadSafe |
| 97 // trace_event::TraceLog::EnabledStateObserver implementation: | 112 // trace_event::TraceLog::EnabledStateObserver implementation: |
| 98 void OnTraceLogEnabled() override; | 113 void OnTraceLogEnabled() override; |
| 99 void OnTraceLogDisabled() override; | 114 void OnTraceLogDisabled() override; |
| 100 | 115 |
| 101 protected: | 116 protected: |
| 102 // Serialize the properties of this blame context into |state|. Subclasses can | 117 // 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 | 118 // override this method to record additional properties (e.g, the URL for an |
| 104 // <iframe> blame context). Note that an overridden implementation must still | 119 // <iframe> blame context). Note that an overridden implementation must still |
| 105 // call this base method. | 120 // call this base method. |
| 106 virtual void AsValueInto(trace_event::TracedValue* state); | 121 virtual void AsValueInto(trace_event::TracedValue* state); |
| 107 | 122 |
| 123 // NOTE: Call this function at the beginning of any subclass's destructor to |
| 124 // prevent access from the TraceLog when the instance is being destructed. |
| 125 void UnregisterFromTraceLog(); |
| 126 |
| 127 // ThreadSafe |
| 128 // Allow subclasses to access task runner and weak pointers so that they can |
| 129 // override OnTraceLogEnabled()/Disabled() without breaking thread safety. |
| 130 SingleThreadTaskRunner* task_runner() { return task_runner_.get(); } |
| 131 WeakPtr<BlameContext> get_weak() { return weak_factory_.GetWeakPtr(); } |
| 132 |
| 108 private: | 133 private: |
| 109 bool WasInitialized() const; | 134 bool WasInitialized() const; |
| 110 | 135 |
| 111 // The following string pointers have application lifetime. | 136 // The following string pointers have application lifetime. |
| 112 const char* category_; | 137 const char* category_; |
| 113 const char* name_; | 138 const char* name_; |
| 114 const char* type_; | 139 const char* type_; |
| 115 const char* scope_; | 140 const char* scope_; |
| 116 const int64_t id_; | 141 const int64_t id_; |
| 117 | 142 |
| 118 const char* parent_scope_; | 143 const char* parent_scope_; |
| 119 const int64_t parent_id_; | 144 const int64_t parent_id_; |
| 120 | 145 |
| 121 const unsigned char* category_group_enabled_; | 146 const unsigned char* category_group_enabled_; |
| 122 | 147 |
| 148 // Indicator of whether the instance is registered at the TraceLog. |
| 149 bool registered_; |
| 150 |
| 151 ThreadChecker thread_checker_; |
| 152 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
| 153 WeakPtrFactory<BlameContext> weak_factory_; |
| 154 |
| 123 DISALLOW_COPY_AND_ASSIGN(BlameContext); | 155 DISALLOW_COPY_AND_ASSIGN(BlameContext); |
| 124 }; | 156 }; |
| 125 | 157 |
| 126 } // namespace trace_event | 158 } // namespace trace_event |
| 127 } // namespace base | 159 } // namespace base |
| 128 | 160 |
| 129 #endif // BASE_TRACE_EVENT_BLAME_CONTEXT_H_ | 161 #endif // BASE_TRACE_EVENT_BLAME_CONTEXT_H_ |
| OLD | NEW |