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 #include "base/debug/activity_analyzer.h" | |
6 | |
7 #include "base/files/file.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/memory_mapped_file.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/stl_util.h" | |
13 #include "base/strings/string_util.h" | |
14 | |
15 namespace base { | |
16 namespace debug { | |
17 | |
18 ThreadActivityAnalyzer::ThreadActivityAnalyzer( | |
19 const ThreadActivityTracker* tracker) | |
20 : activity_snapshot_valid_(tracker->Snapshot(&activity_snapshot_)) {} | |
21 | |
22 ThreadActivityAnalyzer::ThreadActivityAnalyzer(void* base, size_t size) | |
23 : ThreadActivityAnalyzer( | |
24 WrapUnique(new ThreadActivityTracker(base, size)).get()) {} | |
25 | |
26 ThreadActivityAnalyzer::ThreadActivityAnalyzer( | |
27 PersistentMemoryAllocator* allocator, | |
28 PersistentMemoryAllocator::Reference reference) | |
29 : ThreadActivityAnalyzer(allocator->GetAsObject<char>( | |
30 reference, | |
31 GlobalActivityTracker::kTypeIdActivityTracker), | |
32 allocator->GetAllocSize(reference)) {} | |
33 | |
34 ThreadActivityAnalyzer::~ThreadActivityAnalyzer() {} | |
35 | |
36 GlobalActivityAnalyzer::GlobalActivityAnalyzer( | |
37 std::unique_ptr<PersistentMemoryAllocator> allocator) | |
38 : allocator_(std::move(allocator)), allocator_iterator_(allocator_.get()) {} | |
39 | |
40 GlobalActivityAnalyzer::~GlobalActivityAnalyzer() {} | |
41 | |
42 #if !defined(OS_NACL) | |
43 std::unique_ptr<GlobalActivityAnalyzer> GlobalActivityAnalyzer::CreateWithFile( | |
44 const FilePath& file_path) { | |
45 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); | |
46 mmfile->Initialize(file_path); | |
47 if (!mmfile->IsValid()) | |
48 return nullptr; | |
49 | |
50 if (!FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, true)) | |
51 return nullptr; | |
52 | |
53 return WrapUnique(new GlobalActivityAnalyzer( | |
54 WrapUnique(new FilePersistentMemoryAllocator( | |
55 std::move(mmfile), 0, 0, base::StringPiece(), | |
56 true)))); | |
57 } | |
58 #endif // !defined(OS_NACL) | |
59 | |
60 ThreadActivityAnalyzer* GlobalActivityAnalyzer::GetFirstAnalyzer() { | |
61 // Fetch all the records. This will retrieve only ones created since the | |
62 // last run since the PMA iterator will continue from where it left off. | |
63 uint32_t type; | |
Sigurður Ásgeirsson
2016/06/14 15:28:12
Maybe tuck most of the body of this function into
bcwhite
2016/06/14 15:54:56
Ummm... I specifically said that the activity_ana
bcwhite
2016/06/14 19:48:45
Done.
| |
64 PersistentMemoryAllocator::Reference ref; | |
65 while ((ref = allocator_iterator_.GetNext(&type)) != 0) { | |
66 switch (type) { | |
67 case GlobalActivityTracker::kTypeIdActivityTracker: | |
68 case GlobalActivityTracker::kTypeIdActivityTrackerFree: | |
69 // Free or not, add it to the list of references for later analysis. | |
70 tracker_references_.insert(ref); | |
71 break; | |
72 } | |
73 } | |
74 | |
75 // Go through all the known references and create analyzers for them with | |
76 // snapshots of the current state. | |
77 analyzers_.clear(); | |
78 for (PersistentMemoryAllocator::Reference tracker_ref : tracker_references_) { | |
79 // Get the actual data segment for the tracker. This can fail if the | |
80 // record has been marked "free" since the type will not match. | |
81 void* base = allocator_->GetAsObject<char>( | |
82 tracker_ref, GlobalActivityTracker::kTypeIdActivityTracker); | |
83 if (!base) | |
84 continue; | |
85 | |
86 // Create the analyzer on the data. This will capture a snapshot of the | |
87 // tracker state. This can fail if the tracker is somehow corrupted or is | |
88 // in the process of shutting down. | |
89 std::unique_ptr<ThreadActivityAnalyzer> analyzer(new ThreadActivityAnalyzer( | |
90 base, allocator_->GetAllocSize(tracker_ref))); | |
91 if (!analyzer->IsValid()) | |
92 continue; | |
93 | |
94 // Add this analyzer to the map of known ones, indexed by a unique thread | |
95 // identifier. | |
96 analyzer->allocator_reference_ = ref; | |
97 analyzers_[analyzer->GetThreadKey()] = std::move(analyzer); | |
Sigurður Ásgeirsson
2016/06/14 15:28:12
not having read the analyzer code, it's not clear
bcwhite
2016/06/14 19:48:45
Only active (currently executing) trackers are inc
| |
98 } | |
99 | |
100 // Set up the iterator for going through all the analyzers. | |
101 analyzers_iterator_ = analyzers_.begin(); | |
102 if (analyzers_iterator_ == analyzers_.end()) | |
103 return nullptr; | |
104 return analyzers_iterator_->second.get(); | |
105 } | |
106 | |
107 ThreadActivityAnalyzer* GlobalActivityAnalyzer::GetNextAnalyzer() { | |
108 DCHECK(analyzers_iterator_ != analyzers_.end()); | |
109 ++analyzers_iterator_; | |
110 if (analyzers_iterator_ == analyzers_.end()) | |
111 return nullptr; | |
112 return analyzers_iterator_->second.get(); | |
113 } | |
114 | |
115 ThreadActivityAnalyzer* GlobalActivityAnalyzer::GetAnalyzerForThread( | |
116 const ThreadKey& key) { | |
117 auto found = analyzers_.find(key); | |
118 if (found == analyzers_.end()) | |
119 return nullptr; | |
120 return found->second.get(); | |
121 } | |
122 | |
123 } // namespace debug | |
124 } // namespace base | |
OLD | NEW |