-
Notifications
You must be signed in to change notification settings - Fork 54
/
CombinedStacks.cpp
215 lines (184 loc) · 6.72 KB
/
CombinedStacks.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*- 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 "CombinedStacks.h"
#include "jsapi.h"
#include "mozilla/HangAnnotations.h"
namespace mozilla {
namespace Telemetry {
// The maximum number of chrome hangs stacks that we're keeping.
const size_t kMaxChromeStacksKept = 50;
CombinedStacks::CombinedStacks() : CombinedStacks(kMaxChromeStacksKept) {}
CombinedStacks::CombinedStacks(size_t aMaxStacksCount)
: mNextIndex(0), mMaxStacksCount(aMaxStacksCount) {}
size_t CombinedStacks::GetModuleCount() const { return mModules.size(); }
const Telemetry::ProcessedStack::Module& CombinedStacks::GetModule(
unsigned aIndex) const {
return mModules[aIndex];
}
size_t CombinedStacks::AddStack(const Telemetry::ProcessedStack& aStack) {
size_t index = mNextIndex;
// Advance the indices of the circular queue holding the stacks.
mNextIndex = (mNextIndex + 1) % mMaxStacksCount;
// Grow the vector up to the maximum size, if needed.
if (mStacks.size() < mMaxStacksCount) {
mStacks.resize(mStacks.size() + 1);
}
// Get a reference to the location holding the new stack.
CombinedStacks::Stack& adjustedStack = mStacks[index];
// If we're using an old stack to hold aStack, clear it.
adjustedStack.clear();
size_t stackSize = aStack.GetStackSize();
for (size_t i = 0; i < stackSize; ++i) {
const Telemetry::ProcessedStack::Frame& frame = aStack.GetFrame(i);
uint16_t modIndex;
if (frame.mModIndex == std::numeric_limits<uint16_t>::max()) {
modIndex = frame.mModIndex;
} else {
const Telemetry::ProcessedStack::Module& module =
aStack.GetModule(frame.mModIndex);
std::vector<Telemetry::ProcessedStack::Module>::iterator modIterator =
std::find(mModules.begin(), mModules.end(), module);
if (modIterator == mModules.end()) {
mModules.push_back(module);
modIndex = mModules.size() - 1;
} else {
modIndex = modIterator - mModules.begin();
}
}
Telemetry::ProcessedStack::Frame adjustedFrame = {frame.mOffset, modIndex};
adjustedStack.push_back(adjustedFrame);
}
return index;
}
const CombinedStacks::Stack& CombinedStacks::GetStack(unsigned aIndex) const {
return mStacks[aIndex];
}
size_t CombinedStacks::GetStackCount() const { return mStacks.size(); }
size_t CombinedStacks::SizeOfExcludingThis() const {
// This is a crude approximation. We would like to do something like
// aMallocSizeOf(&mModules[0]), but on linux aMallocSizeOf will call
// malloc_usable_size which is only safe on the pointers returned by malloc.
// While it works on current libstdc++, it is better to be safe and not assume
// that &vec[0] points to one. We could use a custom allocator, but
// it doesn't seem worth it.
size_t n = 0;
n += mModules.capacity() * sizeof(Telemetry::ProcessedStack::Module);
n += mStacks.capacity() * sizeof(Stack);
for (const auto& s : mStacks) {
n += s.capacity() * sizeof(Telemetry::ProcessedStack::Frame);
}
return n;
}
void CombinedStacks::RemoveStack(unsigned aIndex) {
MOZ_ASSERT(aIndex < mStacks.size());
mStacks.erase(mStacks.begin() + aIndex);
if (aIndex < mNextIndex) {
if (mNextIndex == 0) {
mNextIndex = mStacks.size();
} else {
mNextIndex--;
}
}
if (mNextIndex > mStacks.size()) {
mNextIndex = mStacks.size();
}
}
#if defined(MOZ_GECKO_PROFILER)
void CombinedStacks::Clear() {
mNextIndex = 0;
mStacks.clear();
mModules.clear();
}
#endif
JSObject* CreateJSStackObject(JSContext* cx, const CombinedStacks& stacks) {
JS::Rooted<JSObject*> ret(cx, JS_NewPlainObject(cx));
if (!ret) {
return nullptr;
}
JS::Rooted<JSObject*> moduleArray(cx, JS_NewArrayObject(cx, 0));
if (!moduleArray) {
return nullptr;
}
bool ok =
JS_DefineProperty(cx, ret, "memoryMap", moduleArray, JSPROP_ENUMERATE);
if (!ok) {
return nullptr;
}
const size_t moduleCount = stacks.GetModuleCount();
for (size_t moduleIndex = 0; moduleIndex < moduleCount; ++moduleIndex) {
// Current module
const Telemetry::ProcessedStack::Module& module =
stacks.GetModule(moduleIndex);
JS::Rooted<JSObject*> moduleInfoArray(cx, JS_NewArrayObject(cx, 0));
if (!moduleInfoArray) {
return nullptr;
}
if (!JS_DefineElement(cx, moduleArray, moduleIndex, moduleInfoArray,
JSPROP_ENUMERATE)) {
return nullptr;
}
unsigned index = 0;
// Module name
JS::Rooted<JSString*> str(cx, JS_NewUCStringCopyZ(cx, module.mName.get()));
if (!str || !JS_DefineElement(cx, moduleInfoArray, index++, str,
JSPROP_ENUMERATE)) {
return nullptr;
}
// Module breakpad identifier
JS::Rooted<JSString*> id(cx,
JS_NewStringCopyZ(cx, module.mBreakpadId.get()));
if (!id ||
!JS_DefineElement(cx, moduleInfoArray, index, id, JSPROP_ENUMERATE)) {
return nullptr;
}
}
JS::Rooted<JSObject*> reportArray(cx, JS_NewArrayObject(cx, 0));
if (!reportArray) {
return nullptr;
}
ok = JS_DefineProperty(cx, ret, "stacks", reportArray, JSPROP_ENUMERATE);
if (!ok) {
return nullptr;
}
const size_t length = stacks.GetStackCount();
for (size_t i = 0; i < length; ++i) {
// Represent call stack PCs as (module index, offset) pairs.
JS::Rooted<JSObject*> pcArray(cx, JS_NewArrayObject(cx, 0));
if (!pcArray) {
return nullptr;
}
if (!JS_DefineElement(cx, reportArray, i, pcArray, JSPROP_ENUMERATE)) {
return nullptr;
}
const CombinedStacks::Stack& stack = stacks.GetStack(i);
const uint32_t pcCount = stack.size();
for (size_t pcIndex = 0; pcIndex < pcCount; ++pcIndex) {
const Telemetry::ProcessedStack::Frame& frame = stack[pcIndex];
JS::Rooted<JSObject*> framePair(cx, JS_NewArrayObject(cx, 0));
if (!framePair) {
return nullptr;
}
int modIndex = (std::numeric_limits<uint16_t>::max() == frame.mModIndex)
? -1
: frame.mModIndex;
if (!JS_DefineElement(cx, framePair, 0, modIndex, JSPROP_ENUMERATE)) {
return nullptr;
}
if (!JS_DefineElement(cx, framePair, 1,
static_cast<double>(frame.mOffset),
JSPROP_ENUMERATE)) {
return nullptr;
}
if (!JS_DefineElement(cx, pcArray, pcIndex, framePair,
JSPROP_ENUMERATE)) {
return nullptr;
}
}
}
return ret;
}
} // namespace Telemetry
} // namespace mozilla