blame_context.h 5.1 KB

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