-
Notifications
You must be signed in to change notification settings - Fork 54
/
ProcessedStack.cpp
161 lines (129 loc) · 4.9 KB
/
ProcessedStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ProcessedStack.h"
namespace {
struct StackFrame {
uintptr_t mPC; // The program counter at this position in the call stack.
uint16_t mIndex; // The number of this frame in the call stack.
uint16_t mModIndex; // The index of module that has this program counter.
};
#ifdef MOZ_GECKO_PROFILER
static bool CompareByPC(const StackFrame &a, const StackFrame &b) {
return a.mPC < b.mPC;
}
static bool CompareByIndex(const StackFrame &a, const StackFrame &b) {
return a.mIndex < b.mIndex;
}
#endif
} // namespace
namespace mozilla {
namespace Telemetry {
const size_t kMaxChromeStackDepth = 50;
ProcessedStack::ProcessedStack() = default;
size_t ProcessedStack::GetStackSize() const { return mStack.size(); }
size_t ProcessedStack::GetNumModules() const { return mModules.size(); }
bool ProcessedStack::Module::operator==(const Module &aOther) const {
return mName == aOther.mName && mBreakpadId == aOther.mBreakpadId;
}
const ProcessedStack::Frame &ProcessedStack::GetFrame(unsigned aIndex) const {
MOZ_ASSERT(aIndex < mStack.size());
return mStack[aIndex];
}
void ProcessedStack::AddFrame(const Frame &aFrame) { mStack.push_back(aFrame); }
const ProcessedStack::Module &ProcessedStack::GetModule(unsigned aIndex) const {
MOZ_ASSERT(aIndex < mModules.size());
return mModules[aIndex];
}
void ProcessedStack::AddModule(const Module &aModule) {
mModules.push_back(aModule);
}
void ProcessedStack::Clear() {
mModules.clear();
mStack.clear();
}
ProcessedStack GetStackAndModules(const std::vector<uintptr_t> &aPCs) {
return BatchProcessedStackGenerator().GetStackAndModules(aPCs);
}
BatchProcessedStackGenerator::BatchProcessedStackGenerator()
#ifdef MOZ_GECKO_PROFILER
: mSortedRawModules(SharedLibraryInfo::GetInfoForSelf())
#endif
{
#ifdef MOZ_GECKO_PROFILER
mSortedRawModules.SortByAddress();
#endif
}
ProcessedStack BatchProcessedStackGenerator::GetStackAndModules(
const std::vector<uintptr_t> &aPCs) {
std::vector<StackFrame> rawStack;
auto stackEnd = aPCs.begin() + std::min(aPCs.size(), kMaxChromeStackDepth);
for (auto i = aPCs.begin(); i != stackEnd; ++i) {
uintptr_t aPC = *i;
StackFrame Frame = {aPC, static_cast<uint16_t>(rawStack.size()),
std::numeric_limits<uint16_t>::max()};
rawStack.push_back(Frame);
}
#ifdef MOZ_GECKO_PROFILER
SharedLibraryInfo rawModules(mSortedRawModules);
// Remove all modules not referenced by a PC on the stack
std::sort(rawStack.begin(), rawStack.end(), CompareByPC);
size_t moduleIndex = 0;
size_t stackIndex = 0;
size_t stackSize = rawStack.size();
while (moduleIndex < rawModules.GetSize()) {
const SharedLibrary &module = rawModules.GetEntry(moduleIndex);
uintptr_t moduleStart = module.GetStart();
uintptr_t moduleEnd = module.GetEnd() - 1;
// the interval is [moduleStart, moduleEnd)
bool moduleReferenced = false;
for (; stackIndex < stackSize; ++stackIndex) {
uintptr_t pc = rawStack[stackIndex].mPC;
if (pc >= moduleEnd) break;
if (pc >= moduleStart) {
// If the current PC is within the current module, mark
// module as used
moduleReferenced = true;
rawStack[stackIndex].mPC -= moduleStart;
rawStack[stackIndex].mModIndex = moduleIndex;
} else {
// PC does not belong to any module. It is probably from
// the JIT. Use a fixed mPC so that we don't get different
// stacks on different runs.
rawStack[stackIndex].mPC = std::numeric_limits<uintptr_t>::max();
}
}
if (moduleReferenced) {
++moduleIndex;
} else {
// Remove module if no PCs within its address range
rawModules.RemoveEntries(moduleIndex, moduleIndex + 1);
}
}
for (; stackIndex < stackSize; ++stackIndex) {
// These PCs are past the last module.
rawStack[stackIndex].mPC = std::numeric_limits<uintptr_t>::max();
}
std::sort(rawStack.begin(), rawStack.end(), CompareByIndex);
#endif
// Copy the information to the return value.
ProcessedStack Ret;
for (auto &rawFrame : rawStack) {
mozilla::Telemetry::ProcessedStack::Frame frame = {rawFrame.mPC,
rawFrame.mModIndex};
Ret.AddFrame(frame);
}
#ifdef MOZ_GECKO_PROFILER
for (unsigned i = 0, n = rawModules.GetSize(); i != n; ++i) {
const SharedLibrary &info = rawModules.GetEntry(i);
mozilla::Telemetry::ProcessedStack::Module module = {info.GetDebugName(),
info.GetBreakpadId()};
Ret.AddModule(module);
}
#endif
return Ret;
}
} // namespace Telemetry
} // namespace mozilla