Chromium Code Reviews| Index: base/debug/activity_analyzer.h |
| diff --git a/base/debug/activity_analyzer.h b/base/debug/activity_analyzer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e7f98208784928f8e07cfb6b7bf5b839cfd24640 |
| --- /dev/null |
| +++ b/base/debug/activity_analyzer.h |
| @@ -0,0 +1,129 @@ |
| +// 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_ANALYZER_H_ |
| +#define BASE_METRICS_ACTIVITY_ANALYZER_H_ |
| + |
| +#include <map> |
| +#include <memory> |
| +#include <set> |
| + |
| +#include "base/base_export.h" |
| +#include "base/debug/activity_tracker.h" |
| + |
| +namespace base { |
| + |
| +namespace debug { |
| + |
| +// Be sure not to create multiple Analyzers for given tracker data as parallel |
| +// operation could lead to inconsistencies from concurrent synchronization with |
| +// an active tracker. |
| +class BASE_EXPORT ThreadActivityAnalyzer { |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
describe purpose and perhaps intended usage of cla
bcwhite
2016/06/14 19:48:45
Done.
|
| + public: |
| + using Activity = ThreadActivityTracker::Activity; |
| + using ActivitySnapshot = ThreadActivityTracker::ActivitySnapshot; |
| + |
| + class ThreadKey { |
| + public: |
| + ThreadKey(int64_t pid, int64_t tid) : pid_(pid), tid_(tid) {} |
| + |
| + bool operator<(const ThreadKey& rhs) const { |
| + if (pid_ != rhs.pid_) |
| + return pid_ < rhs.pid_; |
| + return tid_ < rhs.tid_; |
| + } |
| + |
| + bool operator==(const ThreadKey& rhs) const { |
| + return (pid_ == rhs.pid_ && tid_ == rhs.tid_); |
| + } |
| + |
| + private: |
| + int64_t pid_; |
| + int64_t tid_; |
| + }; |
| + |
| + // Creates an analyzer for an existing activity-tracker. The passed tracker |
|
Sigurður Ásgeirsson
2016/06/14 15:28:12
nit: |tracker| throughout this comment?
bcwhite
2016/06/14 19:48:45
Done.
|
| + // must live at least as long as the created object. The tracker may continue |
| + // to be active even with an attached analyzer. |
| + explicit ThreadActivityAnalyzer(const ThreadActivityTracker* tracker); |
| + |
| + // Creates an anaylzer for a block of memory currently or previously in-use |
|
Sigurður Ásgeirsson
2016/06/14 15:28:12
nit: analyzer
bcwhite
2016/06/14 19:48:45
Done.
|
| + // by an activity-tracker. The memory must live at least as long as the |
| + // created object. It's permissable for a tracker to remain active on the |
| + // memory from this thread, other threads, or even other processes. |
| + ThreadActivityAnalyzer(void* base, size_t size); |
| + |
| + // Creates an anaylzer for a block of memory held within a persistent-memory |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
nit: analyzer
bcwhite
2016/06/14 19:48:45
Done.
|
| + // |allocator| at the given |reference|. The memory must live at least as |
| + // long as the created object. It's permissable for a tracker to remain active |
| + // on the memory from this thread, other threads, or even other processes. |
| + // The reference must be to an object of type kTypeIdActivityTracker. |
| + ThreadActivityAnalyzer(PersistentMemoryAllocator* allocator, |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
as a general comment on this interface, you have p
bcwhite
2016/06/14 19:48:45
Like all other constructors, passing bad data will
|
| + PersistentMemoryAllocator::Reference reference); |
| + |
| + ~ThreadActivityAnalyzer(); |
| + |
| + // Returns if the contained data is valid. Results from all other methods |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
ubernit: and if contained data is invalid, never r
bcwhite
2016/06/14 19:48:45
Done. :-)
|
| + // are undefined if this returns false. |
| + bool IsValid() { return activity_snapshot_valid_; } |
| + |
| + // Gets the name of the thread. |
| + std::string& GetThreadName() { |
|
Sigurður Ásgeirsson
2016/06/14 15:28:12
no non-const references.
bcwhite
2016/06/14 19:48:45
Done.
|
| + return activity_snapshot_.thread_name; |
| + } |
| + |
| + // Gets the TheadKey for this thread. |
| + ThreadKey GetThreadKey() { |
| + return ThreadKey(activity_snapshot_.process_id, |
| + activity_snapshot_.thread_id); |
| + } |
| + |
| + private: |
| + friend class GlobalActivityAnalyzer; |
| + |
| + ActivitySnapshot activity_snapshot_; |
| + bool activity_snapshot_valid_; |
| + |
| + // A reference into a persistent memory allocator, used by the global |
| + // analyzer to know where this tracker came from. |
| + PersistentMemoryAllocator::Reference allocator_reference_ = 0; |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
nice - I didn't know we could do this.
bcwhite
2016/06/14 19:48:45
Inline initialization? New in C++11. Very handy!
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(ThreadActivityAnalyzer); |
| +}; |
| + |
| + |
| +class BASE_EXPORT GlobalActivityAnalyzer { |
|
Sigurður Ásgeirsson
2016/06/14 15:28:12
Describe class purpose and intended usage?
bcwhite
2016/06/14 19:48:45
Done.
|
| + public: |
| + using ThreadKey = ThreadActivityAnalyzer::ThreadKey; |
| + |
| + explicit GlobalActivityAnalyzer( |
|
Sigurður Ásgeirsson
2016/06/14 15:28:12
document member functions
bcwhite
2016/06/14 19:48:45
Done.
|
| + std::unique_ptr<PersistentMemoryAllocator> allocator); |
| + ~GlobalActivityAnalyzer(); |
| + |
| +#if !defined(OS_NACL) |
| + std::unique_ptr<GlobalActivityAnalyzer> CreateWithFile( |
| + const FilePath& file_path); |
| +#endif // !defined(OS_NACL) |
| + |
| + ThreadActivityAnalyzer* GetFirstAnalyzer(); |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
document member functions - presumably this class
bcwhite
2016/06/14 19:48:45
Done.
|
| + ThreadActivityAnalyzer* GetNextAnalyzer(); |
| + ThreadActivityAnalyzer* GetAnalyzerForThread(const ThreadKey& key); |
| + |
| + private: |
| + using AnalyzerMap = |
| + std::map<ThreadKey, std::unique_ptr<ThreadActivityAnalyzer>>; |
| + |
| + std::unique_ptr<PersistentMemoryAllocator> allocator_; |
| + PersistentMemoryAllocator::Iterator allocator_iterator_; |
| + std::set<PersistentMemoryAllocator::Reference> tracker_references_; |
| + AnalyzerMap analyzers_; |
| + AnalyzerMap::iterator analyzers_iterator_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(GlobalActivityAnalyzer); |
| +}; |
| + |
| +} // namespace debug |
| +} // namespace base |
| + |
| +#endif // BASE_METRICS_ACTIVITY_ANALYZER_H_ |