Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3687)

Unified Diff: base/debug/blame_context.h

Issue 1447563002: Implement frame attribution (FrameBlamer) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/debug/blame_context.h
diff --git a/base/debug/blame_context.h b/base/debug/blame_context.h
new file mode 100644
index 0000000000000000000000000000000000000000..9e3588619c66de242629762770a0fab51a715552
--- /dev/null
+++ b/base/debug/blame_context.h
@@ -0,0 +1,160 @@
+// 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_DEBUG_BLAME_CONTEXT_H_
+#define BASE_DEBUG_BLAME_CONTEXT_H_
+
+#include <inttypes.h>
+
+#include "base/macros.h"
+#include "base/strings/stringprintf.h"
+#include "base/trace_event/trace_event.h"
+#include "base/trace_event/trace_event_argument.h"
+
+namespace base {
+namespace debug {
+
+// A blame context represents a logical unit to which we want to attribute
+// different costs (CPU, network usage, memory, etc.). An example of a blame
+// context is an <iframe> element on a web page. Different subsystems can
+// "enter" and "leave" blame contexts to indicate that they are doing work which
+// should be accounted against this blame context.
+//
+// Each blame context can optionally have a parent blame context, forming a
+// blame context tree. When work is attributed to a particular blame context, it
+// is considered to count against all of that context's children too. This is
+// useful when work cannot be exactly attributed into a more specific context.
+// For example, Javascript garbage collection generally needs to inspect all
+// objects on a page instead looking at each <iframe> individually. In this case
+// the work should be attributed to a blame context which is the parent of all
+// <iframe> blame contexts.
+
+// Abstract blame context base class which can be used for attributing work.
+class BlameContextBase {
+ public:
+ // Indicate that the current thread is now doing work which should count
+ // against this blame context.
+ virtual void Enter() = 0;
+
+ // Leave and stop doing work for a previously entered blame context. If
+ // another blame context of the same type was entered prior to this one, it
+ // becomes the active blame context for this thread again.
+ virtual void Leave() = 0;
+
+ protected:
+ BlameContextBase() {}
+ virtual ~BlameContextBase() {}
+
+ DISALLOW_COPY_AND_ASSIGN(BlameContextBase);
+};
+
+// A concrete instance of a blame context which emits blame context trace
+// events. |category| is the tracing category this context belongs to and |name|
+// identifies the type of the blame context. |scope| defines the name space for
+// the |id| parameter (identical ids in different scopes are considered separate
+// blame context objects). Each string parameter must be a pointer to a constant
+// string with application lifetime.
+//
+// Note that the blame context is templatized to work around a trace event
+// limitation which prohibits a single trace event macro instantiation to be
+// used with more than one trace category.
+// TODO(skyostil): De-templatize this class.
+template <const char* category, const char* name, const char* scope>
+class BlameContext : public BlameContextBase,
+ public trace_event::TraceLog::EnabledStateObserver {
+ public:
+ // Construct a blame context with the given |id| belonging to the |scope|
+ // namespace. The blame context will not have a parent.
+ explicit BlameContext(int64_t id)
+ : id_(id), parent_id_(0), parent_scope_(nullptr) {
+ Initialize();
+ }
+
+ // Construct a blame context with the given |id| belonging to the |scope|
+ // namespace. |parent_context| identifies the parent for this blame context.
+ // It must belong to the same blame context tree as this context, i.e., |name|
+ // must equal |parent_name|.
+ template <const char* parent_category,
+ const char* parent_name,
+ const char* parent_scope>
+ BlameContext(int64_t id,
+ const BlameContext<parent_category, parent_name, parent_scope>&
+ parent_context)
+ : id_(id), parent_id_(parent_context.id()), parent_scope_(parent_scope) {
+ DCHECK(!strcmp(name, parent_name))
+ << "parent blame context must be of the same type";
+ Initialize();
+ }
+
+ // BlameContextBase implementation:
+ void Enter() override {
+ TRACE_EVENT_ENTER_CONTEXT(category, name, TRACE_ID_WITH_SCOPE(scope, id_));
+ }
+
+ void Leave() override {
+ TRACE_EVENT_LEAVE_CONTEXT(category, name, TRACE_ID_WITH_SCOPE(scope, id_));
+ }
+
+ // Record a snapshot of the blame context. This is normally only needed if a
+ // blame context subclass defines custom properties (see AsValueInto) and one
+ // or more of the properties have changed.
+ void TakeSnapshot() {
+ bool is_tracing = false;
+ TRACE_EVENT_CATEGORY_GROUP_ENABLED(category, &is_tracing);
+ if (!is_tracing)
+ return;
+ scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue);
+ AsValueInto(snapshot.get());
+ scoped_ptr<trace_event::ConvertableToTraceFormat> snapshot_data(
+ std::move(snapshot));
+ TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category, name,
+ TRACE_ID_WITH_SCOPE(scope, id_),
+ std::move(snapshot_data));
+ }
+
+ int64_t id() const { return id_; }
+
+ // trace_event::TraceLog::EnabledStateObserver implementation:
+ void OnTraceLogEnabled() override { TakeSnapshot(); }
+
+ void OnTraceLogDisabled() override {}
+
+ protected:
+ virtual ~BlameContext() {
+ TRACE_EVENT_OBJECT_DELETED_WITH_ID(category, name,
+ TRACE_ID_WITH_SCOPE(scope, id_));
+ trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
+ }
+
+ // Serialize the properties of this blame context into |state|. Subclasses can
+ // override this method to record additional properties (e.g, the URL for an
+ // <iframe> blame context). Note that an overriden implementation must still
+ // call this base method.
+ virtual void AsValueInto(trace_event::TracedValue* state) {
+ if (!parent_id_)
+ return;
+ state->BeginDictionary("parent");
+ state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_));
+ state->SetString("scope", parent_scope_);
+ state->EndDictionary();
+ }
+
+ private:
+ void Initialize() {
+ TRACE_EVENT_OBJECT_CREATED_WITH_ID(category, name,
+ TRACE_ID_WITH_SCOPE(scope, id_));
+ trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this);
+ }
+
+ const int64_t id_;
+ const int64_t parent_id_;
+ const char* parent_scope_;
+
+ DISALLOW_COPY_AND_ASSIGN(BlameContext);
+};
+
+} // namespace debug
+} // namespace base
+
+#endif // BASE_DEBUG_BLAME_CONTEXT_H_

Powered by Google App Engine
This is Rietveld 408576698