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_METRICS_ACTIVITY_ANALYZER_H_ | |
6 #define BASE_METRICS_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) {} | |
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 threads of a process as stored in a | |
manzagop (departed)
2016/07/04 21:20:56
nit: "all threads of a process" vs below "all know
bcwhite
2016/07/11 15:24:18
Done.
| |
97 // persistent memory allocator. It supports retreival of them through iteration | |
manzagop (departed)
2016/07/04 21:20:56
typo: retreival
bcwhite
2016/07/11 15:24:18
Done.
| |
98 // and directly using a ThreadKey, which allows for cross-references to be | |
99 // resolved. | |
manzagop (departed)
2016/07/04 21:20:56
Perhaps mention the snapshots may not be consisten
bcwhite
2016/07/11 15:24:18
Done.
| |
100 class BASE_EXPORT GlobalActivityAnalyzer { | |
101 public: | |
102 using ThreadKey = ThreadActivityAnalyzer::ThreadKey; | |
103 | |
104 // Creates a global analyzer from a persistent memory allocator. | |
105 explicit GlobalActivityAnalyzer( | |
106 std::unique_ptr<PersistentMemoryAllocator> allocator); | |
107 | |
108 ~GlobalActivityAnalyzer(); | |
109 | |
110 #if !defined(OS_NACL) | |
111 // Creates a global analyzer using the contents of a file given in | |
112 // |file_path|. | |
113 std::unique_ptr<GlobalActivityAnalyzer> CreateWithFile( | |
114 const FilePath& file_path); | |
115 #endif // !defined(OS_NACL) | |
116 | |
117 // Iterates over all known analyzers or returns null if there are no more. | |
118 // Ownership stays with the global analyzer object and all existing analyzer | |
119 // pointers are invalidated when GetFirstAnalyzer() is called. | |
120 ThreadActivityAnalyzer* GetFirstAnalyzer(); | |
121 ThreadActivityAnalyzer* GetNextAnalyzer(); | |
122 | |
123 // Gets the analyzer for a specific thread or null if there is none. | |
124 // Ownership stays with the global analyzer object. | |
125 ThreadActivityAnalyzer* GetAnalyzerForThread(const ThreadKey& key); | |
126 | |
127 private: | |
128 using AnalyzerMap = | |
129 std::map<ThreadKey, std::unique_ptr<ThreadActivityAnalyzer>>; | |
130 | |
131 // Finds, creates, and indexes analyzers for all known procsses and threads. | |
manzagop (departed)
2016/07/04 21:20:56
typo: procsses
bcwhite
2016/07/11 15:24:18
Done.
| |
132 void PrepareAllAnalyzers(); | |
133 | |
134 // The persistent memory allocator holding all tracking data. | |
135 std::unique_ptr<PersistentMemoryAllocator> allocator_; | |
136 | |
137 // The iterator for finding tracking information in the allocator. | |
138 PersistentMemoryAllocator::Iterator allocator_iterator_; | |
139 | |
140 // A set of all tracker memory references found within the allocator. | |
141 std::set<PersistentMemoryAllocator::Reference> tracker_references_; | |
142 | |
143 // A map, keyed by ThreadKey, of all valid activity analyzers. | |
144 AnalyzerMap analyzers_; | |
145 | |
146 // The iterator within the analyzers_ map for returning analyzers through | |
147 // first/next iteration. | |
148 AnalyzerMap::iterator analyzers_iterator_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(GlobalActivityAnalyzer); | |
151 }; | |
152 | |
153 } // namespace debug | |
154 } // namespace base | |
155 | |
156 #endif // BASE_METRICS_ACTIVITY_ANALYZER_H_ | |
OLD | NEW |