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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/* -*- Mode: C++; tab-width: 2; 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/. */
#ifndef ProfileBufferEntry_h
#define ProfileBufferEntry_h
#include "BaseProfilingCategory.h"
#include "gtest/MozGtestFriend.h"
#include "mozilla/BaseProfileJSONWriter.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/HashTable.h"
#include "mozilla/Maybe.h"
#include "mozilla/ProfileBufferEntryKinds.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Variant.h"
#include "mozilla/Vector.h"
#include <string>
namespace mozilla {
namespace baseprofiler {
class ProfileBufferEntry {
public:
using KindUnderlyingType = ::mozilla::ProfileBufferEntryKindUnderlyingType;
using Kind = ::mozilla::ProfileBufferEntryKind;
ProfileBufferEntry();
static constexpr size_t kNumChars = ::mozilla::ProfileBufferEntryNumChars;
private:
// aString must be a static string.
ProfileBufferEntry(Kind aKind, const char* aString);
ProfileBufferEntry(Kind aKind, char aChars[kNumChars]);
ProfileBufferEntry(Kind aKind, void* aPtr);
ProfileBufferEntry(Kind aKind, double aDouble);
ProfileBufferEntry(Kind aKind, int64_t aInt64);
ProfileBufferEntry(Kind aKind, uint64_t aUint64);
ProfileBufferEntry(Kind aKind, int aInt);
ProfileBufferEntry(Kind aKind, BaseProfilerThreadId aThreadId);
public:
#define CTOR(KIND, TYPE, SIZE) \
static ProfileBufferEntry KIND(TYPE aVal) { \
return ProfileBufferEntry(Kind::KIND, aVal); \
}
FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(CTOR)
#undef CTOR
Kind GetKind() const { return mKind; }
#define IS_KIND(KIND, TYPE, SIZE) \
bool Is##KIND() const { return mKind == Kind::KIND; }
FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(IS_KIND)
#undef IS_KIND
private:
FRIEND_TEST(ThreadProfile, InsertOneEntry);
FRIEND_TEST(ThreadProfile, InsertOneEntryWithTinyBuffer);
FRIEND_TEST(ThreadProfile, InsertEntriesNoWrap);
FRIEND_TEST(ThreadProfile, InsertEntriesWrap);
FRIEND_TEST(ThreadProfile, MemoryMeasure);
friend class ProfileBuffer;
Kind mKind;
uint8_t mStorage[kNumChars];
const char* GetString() const;
void* GetPtr() const;
double GetDouble() const;
int GetInt() const;
int64_t GetInt64() const;
uint64_t GetUint64() const;
BaseProfilerThreadId GetThreadId() const;
void CopyCharsInto(char (&aOutArray)[kNumChars]) const;
};
// Packed layout: 1 byte for the tag + 8 bytes for the value.
static_assert(sizeof(ProfileBufferEntry) == 9, "bad ProfileBufferEntry size");
class UniqueStacks {
public:
struct FrameKey {
explicit FrameKey(const char* aLocation)
: mData(NormalFrameData{std::string(aLocation), false, 0, Nothing(),
Nothing()}) {}
FrameKey(std::string&& aLocation, bool aRelevantForJS,
uint64_t aInnerWindowID, const Maybe<unsigned>& aLine,
const Maybe<unsigned>& aColumn,
const Maybe<ProfilingCategoryPair>& aCategoryPair)
: mData(NormalFrameData{aLocation, aRelevantForJS, aInnerWindowID,
aLine, aColumn, aCategoryPair}) {}
FrameKey(const FrameKey& aToCopy) = default;
uint32_t Hash() const;
bool operator==(const FrameKey& aOther) const {
return mData == aOther.mData;
}
struct NormalFrameData {
bool operator==(const NormalFrameData& aOther) const;
std::string mLocation;
bool mRelevantForJS;
uint64_t mInnerWindowID;
Maybe<unsigned> mLine;
Maybe<unsigned> mColumn;
Maybe<ProfilingCategoryPair> mCategoryPair;
};
Variant<NormalFrameData> mData;
};
struct FrameKeyHasher {
using Lookup = FrameKey;
static HashNumber hash(const FrameKey& aLookup) {
HashNumber hash = 0;
if (aLookup.mData.is<FrameKey::NormalFrameData>()) {
const FrameKey::NormalFrameData& data =
aLookup.mData.as<FrameKey::NormalFrameData>();
if (!data.mLocation.empty()) {
hash = AddToHash(hash, HashString(data.mLocation.c_str()));
}
hash = AddToHash(hash, data.mRelevantForJS);
hash = mozilla::AddToHash(hash, data.mInnerWindowID);
if (data.mLine.isSome()) {
hash = AddToHash(hash, *data.mLine);
}
if (data.mColumn.isSome()) {
hash = AddToHash(hash, *data.mColumn);
}
if (data.mCategoryPair.isSome()) {
hash = AddToHash(hash, static_cast<uint32_t>(*data.mCategoryPair));
}
}
return hash;
}
static bool match(const FrameKey& aKey, const FrameKey& aLookup) {
return aKey == aLookup;
}
static void rekey(FrameKey& aKey, const FrameKey& aNewKey) {
aKey = aNewKey;
}
};
struct StackKey {
Maybe<uint32_t> mPrefixStackIndex;
uint32_t mFrameIndex;
explicit StackKey(uint32_t aFrame)
: mFrameIndex(aFrame), mHash(HashGeneric(aFrame)) {}
StackKey(const StackKey& aPrefix, uint32_t aPrefixStackIndex,
uint32_t aFrame)
: mPrefixStackIndex(Some(aPrefixStackIndex)),
mFrameIndex(aFrame),
mHash(AddToHash(aPrefix.mHash, aFrame)) {}
HashNumber Hash() const { return mHash; }
bool operator==(const StackKey& aOther) const {
return mPrefixStackIndex == aOther.mPrefixStackIndex &&
mFrameIndex == aOther.mFrameIndex;
}
private:
HashNumber mHash;
};
struct StackKeyHasher {
using Lookup = StackKey;
static HashNumber hash(const StackKey& aLookup) { return aLookup.Hash(); }
static bool match(const StackKey& aKey, const StackKey& aLookup) {
return aKey == aLookup;
}
static void rekey(StackKey& aKey, const StackKey& aNewKey) {
aKey = aNewKey;
}
};
UniqueStacks();
// Return a StackKey for aFrame as the stack's root frame (no prefix).
[[nodiscard]] StackKey BeginStack(const FrameKey& aFrame);
// Return a new StackKey that is obtained by appending aFrame to aStack.
[[nodiscard]] StackKey AppendFrame(const StackKey& aStack,
const FrameKey& aFrame);
[[nodiscard]] uint32_t GetOrAddFrameIndex(const FrameKey& aFrame);
[[nodiscard]] uint32_t GetOrAddStackIndex(const StackKey& aStack);
void SpliceFrameTableElements(SpliceableJSONWriter& aWriter);
void SpliceStackTableElements(SpliceableJSONWriter& aWriter);
UniqueJSONStrings& UniqueStrings() {
MOZ_RELEASE_ASSERT(mUniqueStrings.get());
return *mUniqueStrings;
}
private:
void StreamNonJITFrame(const FrameKey& aFrame);
void StreamStack(const StackKey& aStack);
UniquePtr<UniqueJSONStrings> mUniqueStrings;
SpliceableChunkedJSONWriter mFrameTableWriter;
HashMap<FrameKey, uint32_t, FrameKeyHasher> mFrameToIndexMap;
SpliceableChunkedJSONWriter mStackTableWriter;
HashMap<StackKey, uint32_t, StackKeyHasher> mStackToIndexMap;
};
//
// Thread profile JSON Format
// --------------------------
//
// The profile contains much duplicate information. The output JSON of the
// profile attempts to deduplicate strings, frames, and stack prefixes, to cut
// down on size and to increase JSON streaming speed. Deduplicated values are
// streamed as indices into their respective tables.
//
// Further, arrays of objects with the same set of properties (e.g., samples,
// frames) are output as arrays according to a schema instead of an object
// with property names. A property that is not present is represented in the
// array as null or undefined.
//
// The format of the thread profile JSON is shown by the following example
// with 1 sample and 1 marker:
//
// {
// "name": "Foo",
// "tid": 42,
// "samples":
// {
// "schema":
// {
// "stack": 0, /* index into stackTable */
// "time": 1, /* number */
// "eventDelay": 2, /* number */
// },
// "data":
// [
// [ 1, 0.0, 0.0 ] /* { stack: 1, time: 0.0, eventDelay: 0.0 } */
// ]
// },
//
// "markers":
// {
// "schema":
// {
// "name": 0, /* index into stringTable */
// "time": 1, /* number */
// "data": 2 /* arbitrary JSON */
// },
// "data":
// [
// [ 3, 0.1 ] /* { name: 'example marker', time: 0.1 } */
// ]
// },
//
// "stackTable":
// {
// "schema":
// {
// "prefix": 0, /* index into stackTable */
// "frame": 1 /* index into frameTable */
// },
// "data":
// [
// [ null, 0 ], /* (root) */
// [ 0, 1 ] /* (root) > foo.js */
// ]
// },
//
// "frameTable":
// {
// "schema":
// {
// "location": 0, /* index into stringTable */
// "relevantForJS": 1, /* bool */
// "innerWindowID": 2, /* inner window ID of global JS `window` object */
// "implementation": 3, /* index into stringTable */
// "optimizations": 4, /* arbitrary JSON */
// "line": 5, /* number */
// "column": 6, /* number */
// "category": 7, /* index into profile.meta.categories */
// "subcategory": 8 /* index into
// profile.meta.categories[category].subcategories */
// },
// "data":
// [
// [ 0 ], /* { location: '(root)' } */
// [ 1, 2 ] /* { location: 'foo.js',
// implementation: 'baseline' } */
// ]
// },
//
// "stringTable":
// [
// "(root)",
// "foo.js",
// "baseline",
// "example marker"
// ]
// }
//
// Process:
// {
// "name": "Bar",
// "pid": 24,
// "threads":
// [
// <0-N threads from above>
// ],
// "counters": /* includes the memory counter */
// [
// {
// "name": "qwerty",
// "category": "uiop",
// "description": "this is qwerty uiop",
// "sample_groups:
// [
// {
// "id": 42, /* number (thread id, or object identifier (tab), etc) */
// "samples:
// {
// "schema":
// {
// "time": 1, /* number */
// "number": 2, /* number (of times the counter was touched) */
// "count": 3 /* number (total for the counter) */
// },
// "data":
// [
// [ 0.1, 1824,
// 454622 ] /* { time: 0.1, number: 1824, count: 454622 } */
// ]
// },
// },
// /* more sample-group objects with different id's */
// ]
// },
// /* more counters */
// ],
// }
//
} // namespace baseprofiler
} // namespace mozilla
#endif /* ndef ProfileBufferEntry_h */
|