Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: base/debug/activity_analyzer.cc

Issue 1980743002: Track thread activities in order to diagnose hangs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@readwrite-mmf
Patch Set: rebased Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/debug/activity_analyzer.h ('k') | base/debug/activity_analyzer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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(ThreadActivityTracker(base, size)) {}
24
25 ThreadActivityAnalyzer::ThreadActivityAnalyzer(
26 PersistentMemoryAllocator* allocator,
27 PersistentMemoryAllocator::Reference reference)
28 : ThreadActivityAnalyzer(allocator->GetAsObject<char>(
29 reference,
30 GlobalActivityTracker::kTypeIdActivityTracker),
31 allocator->GetAllocSize(reference)) {}
32
33 ThreadActivityAnalyzer::~ThreadActivityAnalyzer() {}
34
35 GlobalActivityAnalyzer::GlobalActivityAnalyzer(
36 std::unique_ptr<PersistentMemoryAllocator> allocator)
37 : allocator_(std::move(allocator)), allocator_iterator_(allocator_.get()) {}
38
39 GlobalActivityAnalyzer::~GlobalActivityAnalyzer() {}
40
41 #if !defined(OS_NACL)
42 // static
43 std::unique_ptr<GlobalActivityAnalyzer> GlobalActivityAnalyzer::CreateWithFile(
44 const FilePath& file_path) {
45 // Map the file read-write so it can guarantee consistency between
46 // the analyzer and any trackers that my still be active.
47 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile());
48 mmfile->Initialize(file_path, MemoryMappedFile::READ_WRITE);
49 if (!mmfile->IsValid())
50 return nullptr;
51
52 if (!FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, true))
53 return nullptr;
54
55 return WrapUnique(new GlobalActivityAnalyzer(
56 WrapUnique(new FilePersistentMemoryAllocator(
57 std::move(mmfile), 0, 0, base::StringPiece(),
58 true))));
59 }
60 #endif // !defined(OS_NACL)
61
62 ThreadActivityAnalyzer* GlobalActivityAnalyzer::GetFirstAnalyzer() {
63 PrepareAllAnalyzers();
64 analyzers_iterator_ = analyzers_.begin();
65 if (analyzers_iterator_ == analyzers_.end())
66 return nullptr;
67 return analyzers_iterator_->second.get();
68 }
69
70 ThreadActivityAnalyzer* GlobalActivityAnalyzer::GetNextAnalyzer() {
71 DCHECK(analyzers_iterator_ != analyzers_.end());
72 ++analyzers_iterator_;
73 if (analyzers_iterator_ == analyzers_.end())
74 return nullptr;
75 return analyzers_iterator_->second.get();
76 }
77
78 ThreadActivityAnalyzer* GlobalActivityAnalyzer::GetAnalyzerForThread(
79 const ThreadKey& key) {
80 auto found = analyzers_.find(key);
81 if (found == analyzers_.end())
82 return nullptr;
83 return found->second.get();
84 }
85
86 void GlobalActivityAnalyzer::PrepareAllAnalyzers() {
87 // Fetch all the records. This will retrieve only ones created since the
88 // last run since the PMA iterator will continue from where it left off.
89 uint32_t type;
90 PersistentMemoryAllocator::Reference ref;
91 while ((ref = allocator_iterator_.GetNext(&type)) != 0) {
92 switch (type) {
93 case GlobalActivityTracker::kTypeIdActivityTracker:
94 case GlobalActivityTracker::kTypeIdActivityTrackerFree:
95 // Free or not, add it to the list of references for later analysis.
96 tracker_references_.insert(ref);
97 break;
98 }
99 }
100
101 // Go through all the known references and create analyzers for them with
102 // snapshots of the current state.
103 analyzers_.clear();
104 for (PersistentMemoryAllocator::Reference tracker_ref : tracker_references_) {
105 // Get the actual data segment for the tracker. This can fail if the
106 // record has been marked "free" since the type will not match.
107 void* base = allocator_->GetAsObject<char>(
108 tracker_ref, GlobalActivityTracker::kTypeIdActivityTracker);
109 if (!base)
110 continue;
111
112 // Create the analyzer on the data. This will capture a snapshot of the
113 // tracker state. This can fail if the tracker is somehow corrupted or is
114 // in the process of shutting down.
115 std::unique_ptr<ThreadActivityAnalyzer> analyzer(new ThreadActivityAnalyzer(
116 base, allocator_->GetAllocSize(tracker_ref)));
117 if (!analyzer->IsValid())
118 continue;
119
120 // Add this analyzer to the map of known ones, indexed by a unique thread
121 // identifier.
122 DCHECK(!ContainsKey(analyzers_, analyzer->GetThreadKey()));
123 analyzer->allocator_reference_ = ref;
124 analyzers_[analyzer->GetThreadKey()] = std::move(analyzer);
125 }
126 }
127
128 } // namespace debug
129 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/activity_analyzer.h ('k') | base/debug/activity_analyzer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698