Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_DEBUG_ACTIVITY_ANALYZER_H_ | |
| 6 #define BASE_DEBUG_ACTIVITY_ANALYZER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 #include <set> | |
| 11 | |
| 12 #include "base/base_export.h" | |
| 13 #include "base/debug/activity_tracker.h" | |
| 14 | |
| 15 namespace base { | |
| 16 namespace debug { | |
| 17 | |
| 18 // This class provides analysis of data captured from a ThreadActivityTracker. | |
| 19 // When created, it takes a snapshot of the data held by the tracker and | |
| 20 // makes that information available to other code. | |
| 21 class BASE_EXPORT ThreadActivityAnalyzer { | |
| 22 public: | |
| 23 using Activity = ThreadActivityTracker::Activity; | |
| 24 using ActivitySnapshot = ThreadActivityTracker::ActivitySnapshot; | |
| 25 | |
| 26 // This class provides keys that uniquely identify a thread, even across | |
| 27 // multiple processes. | |
| 28 class ThreadKey { | |
| 29 public: | |
| 30 ThreadKey(int64_t pid, int64_t tid) : pid_(pid), tid_(tid) {} | |
|
Mark Mentovai
2016/08/01 15:59:37
You’ve gone to some work to disambiguate threads a
bcwhite
2016/08/01 17:36:11
Acknowledged.
| |
| 31 | |
| 32 bool operator<(const ThreadKey& rhs) const { | |
| 33 if (pid_ != rhs.pid_) | |
| 34 return pid_ < rhs.pid_; | |
| 35 return tid_ < rhs.tid_; | |
| 36 } | |
| 37 | |
| 38 bool operator==(const ThreadKey& rhs) const { | |
| 39 return (pid_ == rhs.pid_ && tid_ == rhs.tid_); | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 int64_t pid_; | |
| 44 int64_t tid_; | |
| 45 }; | |
| 46 | |
| 47 // Creates an analyzer for an existing activity |tracker|. A snapshot is taken | |
| 48 // immediately and the tracker is not referenced again. | |
| 49 explicit ThreadActivityAnalyzer(const ThreadActivityTracker& tracker); | |
| 50 | |
| 51 // Creates an analyzer for a block of memory currently or previously in-use | |
| 52 // by an activity-tracker. A snapshot is taken immediately and the memory | |
| 53 // is not referenced again. | |
| 54 ThreadActivityAnalyzer(void* base, size_t size); | |
| 55 | |
| 56 // Creates an analyzer for a block of memory held within a persistent-memory | |
| 57 // |allocator| at the given |reference|. A snapshot is taken immediately and | |
| 58 // the memory is not referenced again. | |
| 59 ThreadActivityAnalyzer(PersistentMemoryAllocator* allocator, | |
| 60 PersistentMemoryAllocator::Reference reference); | |
| 61 | |
| 62 ~ThreadActivityAnalyzer(); | |
| 63 | |
| 64 // Returns true iff the contained data is valid. Results from all other | |
| 65 // methods are undefined if this returns false. | |
| 66 bool IsValid() { return activity_snapshot_valid_; } | |
| 67 | |
| 68 // Gets the name of the thread. | |
| 69 const std::string& GetThreadName() { | |
| 70 return activity_snapshot_.thread_name; | |
| 71 } | |
| 72 | |
| 73 // Gets the TheadKey for this thread. | |
| 74 ThreadKey GetThreadKey() { | |
| 75 return ThreadKey(activity_snapshot_.process_id, | |
| 76 activity_snapshot_.thread_id); | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 friend class GlobalActivityAnalyzer; | |
| 81 | |
| 82 // The snapshot of the activity tracker taken at the moment of construction. | |
| 83 ActivitySnapshot activity_snapshot_; | |
| 84 | |
| 85 // Flag indicating if the snapshot data is valid. | |
| 86 bool activity_snapshot_valid_; | |
| 87 | |
| 88 // A reference into a persistent memory allocator, used by the global | |
| 89 // analyzer to know where this tracker came from. | |
| 90 PersistentMemoryAllocator::Reference allocator_reference_ = 0; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(ThreadActivityAnalyzer); | |
| 93 }; | |
| 94 | |
| 95 | |
| 96 // This class manages analyzers for all known processes and threads as stored | |
| 97 // in a persistent memory allocator. It supports retrieval of them through | |
| 98 // iteration and directly using a ThreadKey, which allows for cross-references | |
| 99 // to be resolved. | |
| 100 // Note that though atomic snapshots are used and everything has its snapshot | |
| 101 // taken at the same time, the multi-snapshot itself is not atomic and thus may | |
| 102 // show small inconsistencies between threads if attempted on a live system. | |
| 103 class BASE_EXPORT GlobalActivityAnalyzer { | |
| 104 public: | |
| 105 using ThreadKey = ThreadActivityAnalyzer::ThreadKey; | |
| 106 | |
| 107 // Creates a global analyzer from a persistent memory allocator. | |
| 108 explicit GlobalActivityAnalyzer( | |
| 109 std::unique_ptr<PersistentMemoryAllocator> allocator); | |
| 110 | |
| 111 ~GlobalActivityAnalyzer(); | |
| 112 | |
| 113 #if !defined(OS_NACL) | |
| 114 // Creates a global analyzer using the contents of a file given in | |
| 115 // |file_path|. | |
| 116 std::unique_ptr<GlobalActivityAnalyzer> CreateWithFile( | |
| 117 const FilePath& file_path); | |
| 118 #endif // !defined(OS_NACL) | |
| 119 | |
| 120 // Iterates over all known analyzers or returns null if there are no more. | |
| 121 // Ownership stays with the global analyzer object and all existing analyzer | |
| 122 // pointers are invalidated when GetFirstAnalyzer() is called. | |
| 123 ThreadActivityAnalyzer* GetFirstAnalyzer(); | |
| 124 ThreadActivityAnalyzer* GetNextAnalyzer(); | |
| 125 | |
| 126 // Gets the analyzer for a specific thread or null if there is none. | |
| 127 // Ownership stays with the global analyzer object. | |
| 128 ThreadActivityAnalyzer* GetAnalyzerForThread(const ThreadKey& key); | |
| 129 | |
| 130 private: | |
| 131 using AnalyzerMap = | |
| 132 std::map<ThreadKey, std::unique_ptr<ThreadActivityAnalyzer>>; | |
| 133 | |
| 134 // Finds, creates, and indexes analyzers for all known processes and threads. | |
| 135 void PrepareAllAnalyzers(); | |
| 136 | |
| 137 // The persistent memory allocator holding all tracking data. | |
| 138 std::unique_ptr<PersistentMemoryAllocator> allocator_; | |
| 139 | |
| 140 // The iterator for finding tracking information in the allocator. | |
| 141 PersistentMemoryAllocator::Iterator allocator_iterator_; | |
| 142 | |
| 143 // A set of all tracker memory references found within the allocator. | |
| 144 std::set<PersistentMemoryAllocator::Reference> tracker_references_; | |
| 145 | |
| 146 // A map, keyed by ThreadKey, of all valid activity analyzers. | |
| 147 AnalyzerMap analyzers_; | |
| 148 | |
| 149 // The iterator within the analyzers_ map for returning analyzers through | |
| 150 // first/next iteration. | |
| 151 AnalyzerMap::iterator analyzers_iterator_; | |
| 152 | |
| 153 DISALLOW_COPY_AND_ASSIGN(GlobalActivityAnalyzer); | |
| 154 }; | |
| 155 | |
| 156 } // namespace debug | |
| 157 } // namespace base | |
| 158 | |
| 159 #endif // BASE_DEBUG_ACTIVITY_ANALYZER_H_ | |
| OLD | NEW |