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

Unified Diff: base/debug/blame_context.h

Issue 1776673002: base: Add blame context (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Bring back the base. Created 4 years, 9 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..9e54ce0c96051f76760271b70cba64dc390224d4
--- /dev/null
+++ b/base/debug/blame_context.h
@@ -0,0 +1,126 @@
+// 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/base_export.h"
+#include "base/macros.h"
+#include "base/trace_event/trace_event.h"
+
+namespace base {
+namespace trace_event {
+class TracedValue;
+}
+
+namespace debug {
+
+// Abstract blame context base class which can be used for attributing work.
+class BASE_EXPORT 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 blame context represents a logical unit to which we want to attribute
+// different costs (e.g., CPU, network, or memory usage). 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 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
chiniforooshan 2016/03/10 16:18:55 Oh, does this attributing work to all children wor
Sami 2016/03/10 17:59:02 I commented on the doc but I'll repeat it here too
+// 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.
+class BASE_EXPORT BlameContext
+ : public BlameContextBase,
+ public trace_event::TraceLog::EnabledStateObserver {
+ public:
+ // Construct a blame context of type |name|, belonging to the tracing category
+ // |category|, identified by |id| from to the |scope| namespace. This blame
+ // context will not have a parent. Note that all strings must have application
+ // lifetime.
+ BlameContext(const char* category,
+ const char* name,
+ const char* scope,
+ int64_t id);
+
+ // Construct a blame context of type |name|, belonging to the tracing category
+ // |category|, identified by |id| from 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_context.name()|. Note that all strings must have application
+ // lifetime.
+ BlameContext(const char* category,
+ const char* name,
+ const char* scope,
+ int64_t id,
+ const BlameContext& parent_context);
+
+ // BlameContextBase implementation:
+ void Enter() override;
+ void Leave() override;
+
+ // 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 those properties have changed.
+ void TakeSnapshot();
+
+ const char* category() const { return category_; }
+ const char* name() const { return name_; }
+ const char* scope() const { return scope_; }
+ int64_t id() const { return id_; }
+
+ // trace_event::TraceLog::EnabledStateObserver implementation:
+ void OnTraceLogEnabled() override;
+ void OnTraceLogDisabled() override;
+
+ protected:
+ ~BlameContext() override;
+
+ // 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 overridden implementation must still
+ // call this base method.
+ virtual void AsValueInto(trace_event::TracedValue* state);
+
+ private:
+ void Initialize();
+
+ // The following string pointers have application lifetime.
+ const char* category_;
+ const char* name_;
+ const char* scope_;
+ const int64_t id_;
+
+ const char* parent_scope_;
+ const int64_t parent_id_;
+
+ const unsigned char* category_group_enabled_;
+
+ DISALLOW_COPY_AND_ASSIGN(BlameContext);
+};
+
+} // namespace debug
+} // namespace base
+
+#endif // BASE_DEBUG_BLAME_CONTEXT_H_
« no previous file with comments | « base/base.gypi ('k') | base/debug/blame_context.cc » ('j') | base/debug/blame_context.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698