Index: base/metrics/activity_tracker.h |
diff --git a/base/metrics/activity_tracker.h b/base/metrics/activity_tracker.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..169ea18870b19d0368be6c2b0c05fcd91cae1dd0 |
--- /dev/null |
+++ b/base/metrics/activity_tracker.h |
@@ -0,0 +1,221 @@ |
+// 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_METRICS_ACTIVITY_TRACKER_H_ |
+#define BASE_METRICS_ACTIVITY_TRACKER_H_ |
+ |
+#include <atomic> |
+#include <memory> |
+ |
+#include "base/base_export.h" |
+#include "base/feature_list.h" |
+#include "base/files/file_path.h" |
+#include "base/metrics/persistent_memory_allocator.h" |
+#include "base/synchronization/lock.h" |
+#include "base/threading/thread_checker.h" |
+#include "base/threading/thread_local_storage.h" |
+ |
+namespace base { |
+ |
+struct PendingTask; |
+ |
+class Lock; |
+class MemoryMappedFile; |
+ |
+// Feature definition for enabling persistent activity tracking. |
+BASE_EXPORT extern const Feature kPersistentActivityTrackingFeature; |
+ |
+// This class manages tracking a stack of activities for a single thread in |
+// a persistent manner. However, in order to support an operational mode where |
+// another thread is analyzing this data in real-time, atomic operations are |
+// used where necessary to guarantee a consistent view from the outside. |
+class BASE_EXPORT ThreadActivityTracker { |
manzagop (departed)
2016/05/20 13:55:42
If this is a stack, then does the interface need a
|
+ public: |
+ enum ActivityType : uint8_t { |
+ ACT_TASK, |
+ ACT_LOCK, |
+ ACT_EVENT, |
+ }; |
+ |
+ struct StackEntry { |
+ int64_t time_ticks; |
+ uint8_t activity_type; |
+ intptr_t source_address; |
+ intptr_t method_address; |
+ uint64_t sequence_id; |
+ }; |
+ |
+ class BASE_EXPORT ScopedActivity { |
+ public: |
+ ScopedActivity(ThreadActivityTracker* tracker, |
+ const void* source, |
+ ActivityType activity, |
+ intptr_t method, |
+ uint64_t sequence) |
+ : tracker_(tracker), source_(source) { |
+ if (tracker_) |
+ tracker_->RecordStart(source, activity, method, sequence); |
+ } |
+ ~ScopedActivity() { |
+ if (tracker_) |
+ tracker_->RecordFinish(source_); |
+ } |
+ |
+ private: |
+ ThreadActivityTracker* const tracker_; |
+ const void* const source_; |
+ }; |
+ |
+ // A ThreadActivityTracker runs on top of memory that is managed externally. |
+ ThreadActivityTracker(void* base, size_t size); |
+ virtual ~ThreadActivityTracker(); |
+ |
+ // Indicate that a method of the given (arbitrary) identifier has started. |
+ void RecordStart(const void* source, |
+ ActivityType activity, |
+ intptr_t method, |
+ uint64_t sequence); |
+ |
+ // Indicate that a method of the given (arbitrary) identifier has finished. |
+ void RecordFinish(const void* source); |
+ |
+ // Gets a copy of the current stack contents. The return value is the current |
+ // depth of the stack which may be greater than the number of StackEntry |
+ // records returned. If so, the returned stack has the "base" of the stack |
+ // with later entries omitted. |
+ uint32_t CopyStack(std::vector<StackEntry>* stack); |
+ |
+ // Returns whether the current data is valid or not. Fetching a copy of the |
+ // stack will return nothing if the data is not valid. |
+ bool is_valid() { return valid_; } |
+ |
+ private: |
+ friend class Iterator; |
+ |
+ struct Header; |
+ |
+ Header* const header_; |
+ StackEntry* const stack_; |
+ const uint32_t slots_; |
+ |
+ bool valid_ = false; |
+ |
+ base::ThreadChecker thread_checker_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ThreadActivityTracker); |
+}; |
+ |
+class BASE_EXPORT GlobalActivityTracker { |
manzagop (departed)
2016/05/20 13:55:42
TrackerManager or some such to make it clearer thi
|
+ public: |
+ class BASE_EXPORT ScopedThreadActivity |
+ : public ThreadActivityTracker::ScopedActivity { |
+ public: |
+ ScopedThreadActivity(const void* source, |
+ ThreadActivityTracker::ActivityType activity, |
+ intptr_t method, |
+ uint64_t sequence) |
+ : ThreadActivityTracker::ScopedActivity(GetOrCreateTracker(), |
+ source, |
+ activity, |
+ method, |
+ sequence) {} |
+ |
+ private: |
+ static ThreadActivityTracker* GetOrCreateTracker() { |
+ GlobalActivityTracker* global_tracker = Get(); |
+ if (!global_tracker) |
+ return nullptr; |
+ return global_tracker->GetOrCreateTrackerForCurrentThread(); |
+ } |
+ }; |
+ |
+ ~GlobalActivityTracker(); |
+ |
+ static void CreateWithAllocator( |
+ std::unique_ptr<PersistentMemoryAllocator> allocator, |
+ size_t stack_memory); |
+ |
+ static void CreateWithLocalMemory(size_t size, |
+ uint64_t id, |
+ StringPiece name, |
+ size_t stack_memory); |
+ |
+ static void CreateWithFile(const FilePath& file_path, |
+ size_t size, |
+ uint64_t id, |
+ StringPiece name, |
+ size_t stack_memory); |
+ |
+ // Gets the global activity-tracker or null if none exists. |
+ static GlobalActivityTracker* Get() { return g_tracker_; } |
+ |
+ // Gets the thread's activity-tracker, assuming it already exists. This |
+ // is inline for performance reasons. Ownership remains with the global |
+ // tracker. |
+ ThreadActivityTracker* GetTrackerForCurrentThread() { |
+ void* tracker = this_thread_tracker_.Get(); |
+ DCHECK(tracker); |
+ return reinterpret_cast<ThreadActivityTracker*>(tracker); |
+ } |
+ |
+ // Gets the thread's activity-tracker or creates one if none exists. This |
+ // is inline for performance reasons. Ownership remains with the global |
+ // tracker. |
+ ThreadActivityTracker* GetOrCreateTrackerForCurrentThread() { |
+ void* tracker = this_thread_tracker_.Get(); |
+ if (tracker) |
+ return reinterpret_cast<ThreadActivityTracker*>(tracker); |
+ return CreateTrackerForCurrentThread(); |
+ } |
+ |
+ // Creates an activity-tracker for the current thread. |
+ ThreadActivityTracker* CreateTrackerForCurrentThread(); |
+ |
+ // Releases the activity-tracker for the current thread (for testing only). |
+ void ReleaseTrackerForCurrentThreadForTesting(); |
+ |
+ private: |
+ class ManagedActivityTracker : public ThreadActivityTracker { |
+ public: |
+ ManagedActivityTracker(PersistentMemoryAllocator::Reference mem_reference, |
+ void* base, |
+ size_t size); |
+ ~ManagedActivityTracker() override; |
+ |
+ private: |
+ const PersistentMemoryAllocator::Reference mem_reference_; |
+ void* const mem_base_; |
+ }; |
+ |
+ GlobalActivityTracker(std::unique_ptr<PersistentMemoryAllocator> allocator, |
+ size_t stack_memory); |
+ |
+ // Returns the memory used by an activity-tracker managed by this class. |
+ void ReturnTrackerMemory(ManagedActivityTracker* tracker, |
+ PersistentMemoryAllocator::Reference mem_reference, |
+ void* mem_base); |
+ |
+ static void OnTLSDestroy(void* value); |
+ |
+ std::unique_ptr<PersistentMemoryAllocator> allocator_; |
+ const size_t stack_memory_; |
+ |
+ base::ThreadLocalStorage::Slot this_thread_tracker_; |
+ |
+ Lock lock_; |
+ std::set<ManagedActivityTracker*> thread_trackers_; |
+ std::vector<PersistentMemoryAllocator::Reference> available_memories_; |
+ |
+ static GlobalActivityTracker* g_tracker_; |
+}; |
+ |
+class BASE_EXPORT ScopedTaskActivity |
+ : public GlobalActivityTracker::ScopedThreadActivity { |
+ public: |
+ ScopedTaskActivity(const PendingTask& task); |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_METRICS_ACTIVITY_TRACKER_H_ |