| Index: base/trace_event/pseudo_stack.h
|
| diff --git a/base/trace_event/pseudo_stack.h b/base/trace_event/pseudo_stack.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8c1c83763855387558c7dc283b739b6c946b3ba0
|
| --- /dev/null
|
| +++ b/base/trace_event/pseudo_stack.h
|
| @@ -0,0 +1,81 @@
|
| +// Copyright (c) 2015 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_TRACE_EVENT_PSEUDO_STACK_H_
|
| +#define BASE_TRACE_EVENT_PSEUDO_STACK_H_
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/base_export.h"
|
| +#include "base/trace_event/trace_log.h"
|
| +
|
| +namespace base {
|
| +namespace trace_event {
|
| +
|
| +struct PseudoStackFrame {
|
| + const char* name;
|
| + // TODO(ruuda): Add metadata fields so we can add more context to the stack
|
| + // frames. For instance, having TaskQueueManager::PostTask at the top of the
|
| + // pseudo stack is not very helpful, but the scheduler has valuable metadata
|
| + // like the location that the task was posted from.
|
| +};
|
| +
|
| +// A stack that is kept per thread in TLS to keep track of trace events. Unlike
|
| +// |std::stack| it allows iterating the stack and it guards for underflow.
|
| +// Furthermore it has a generation to prevent mixing stacks from different
|
| +// traces.
|
| +class BASE_EXPORT PseudoStack {
|
| + public:
|
| + typedef std::vector<PseudoStackFrame>::const_reverse_iterator ConstIterator;
|
| +
|
| + PseudoStack(int32_t generation);
|
| + ~PseudoStack();
|
| +
|
| + inline ConstIterator Top() const { return stack_.rbegin(); }
|
| + inline ConstIterator Bottom() const { return stack_.rend(); }
|
| +
|
| + inline void Push(PseudoStackFrame frame) { stack_.push_back(frame); }
|
| + inline void Pop() {
|
| + if (!stack_.empty())
|
| + stack_.pop_back();
|
| + }
|
| +
|
| + // Returns the thread-local pseudo stack for the current generation, creating
|
| + // it if it does not exist.
|
| + static PseudoStack* GetInstance();
|
| +
|
| + // The pseudo stack is only valid when its generation matches the current
|
| + // generation. A new generation starts when tracing is enabled, and ends when
|
| + // tracing is disabled.
|
| + inline int32_t generation() const { return generation_; }
|
| +
|
| + private:
|
| + std::vector<PseudoStackFrame> stack_;
|
| + const int32_t generation_;
|
| +};
|
| +
|
| +class BASE_EXPORT PseudoStackController final
|
| + : public TraceLog::EnabledStateObserver {
|
| + public:
|
| + PseudoStackController();
|
| + ~PseudoStackController() override;
|
| +
|
| + void OnTraceLogEnabled() override;
|
| + void OnTraceLogDisabled() override;
|
| +
|
| + // This has no memory barriers for performance reasons. A little lag after
|
| + // enabling or disabling capturing is not a problem, and pop needs to deal
|
| + // with underflow either way.
|
| + static bool capture_enabled() { return capture_enabled_; }
|
| +
|
| + private:
|
| + // This is a static member instead of a member of a singleton, to avoid the
|
| + // extra indirection.
|
| + static bool capture_enabled_;
|
| +};
|
| +
|
| +} // namespace trace_event
|
| +} // namespace base
|
| +
|
| +#endif // BASE_TRACE_EVENT_PSEUDO_STACK_H_
|
|
|