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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* 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 TraceLogging_h
#define TraceLogging_h
#include "mozilla/GuardObjects.h"
#include "mozilla/LinkedList.h"
#include "jsalloc.h"
#include "js/HashTable.h"
#include "js/TypeDecls.h"
#include "js/Vector.h"
#include "threading/Thread.h"
#include "vm/MutexIDs.h"
#include "vm/TraceLoggingGraph.h"
#include "vm/TraceLoggingTypes.h"
struct JSRuntime;
namespace JS {
class ReadOnlyCompileOptions;
} // namespace JS
namespace js {
class PerThreadData;
namespace jit {
class CompileRuntime;
} // namespace jit
/*
* Tracelogging overview.
*
* Tracelogging makes it possible to trace the occurrence of a single event
* and/or the start and stop of an event. This is implemented with as low
* overhead as possible to not interfere with running.
*
* Logging something is done in 3 stages.
* 1) Get the tracelogger of the current thread.
* - TraceLoggerForMainThread(JSRuntime*)
* - TraceLoggerForCurrentThread(); // Should NOT be used for the mainthread.
*
* 2) Optionally create a TraceLoggerEvent for the text that needs to get logged. This
* step takes some time, so try to do this beforehand, outside the hot
* path and don't do unnecessary repetitions, since it will cripple
* performance.
* - TraceLoggerEvent event(logger, "foo");
*
* There are also some predefined events. They are located in
* TraceLoggerTextId. They don't require to create an TraceLoggerEvent and
* can also be used as an argument to these functions.
*
* 3) Log the occurrence of a single event:
* - TraceLogTimestamp(logger, TraceLoggerTextId);
* Note: it is temporarily not supported to provide an TraceLoggerEvent as
* argument to log the occurrence of a single event.
*
* or log the start and stop of an event:
* - TraceLogStartEvent(logger, TraceLoggerTextId);
* - TraceLogStartEvent(logger, TraceLoggerEvent);
* - TraceLogStopEvent(logger, TraceLoggerTextId);
* - TraceLogStopEvent(logger, TraceLoggerEvent);
*
* or the start/stop of an event with a RAII class:
* - AutoTraceLog atl(logger, TraceLoggerTextId);
* - AutoTraceLog atl(logger, TraceLoggerEvent);
*/
class AutoTraceLog;
class TraceLoggerEventPayload;
class TraceLoggerThread;
/**
* An event that can be used to report start/stop events to TraceLogger. It
* prepares the given info by requesting a TraceLoggerEventPayload containing
* the string to report and an unique id. It also increases the useCount of
* this payload, so it cannot get removed.
*/
class TraceLoggerEvent {
private:
TraceLoggerEventPayload* payload_;
public:
TraceLoggerEvent() { payload_ = nullptr; };
#ifdef JS_TRACE_LOGGING
TraceLoggerEvent(TraceLoggerThread* logger, TraceLoggerTextId textId);
TraceLoggerEvent(TraceLoggerThread* logger, TraceLoggerTextId type, JSScript* script);
TraceLoggerEvent(TraceLoggerThread* logger, TraceLoggerTextId type,
const JS::ReadOnlyCompileOptions& compileOptions);
TraceLoggerEvent(TraceLoggerThread* logger, const char* text);
TraceLoggerEvent(const TraceLoggerEvent& event);
TraceLoggerEvent& operator=(const TraceLoggerEvent& other);
~TraceLoggerEvent();
#else
TraceLoggerEvent (TraceLoggerThread* logger, TraceLoggerTextId textId) {}
TraceLoggerEvent (TraceLoggerThread* logger, TraceLoggerTextId type, JSScript* script) {}
TraceLoggerEvent (TraceLoggerThread* logger, TraceLoggerTextId type,
const JS::ReadOnlyCompileOptions& compileOptions) {}
TraceLoggerEvent (TraceLoggerThread* logger, const char* text) {}
TraceLoggerEvent(const TraceLoggerEvent& event) {}
TraceLoggerEvent& operator=(const TraceLoggerEvent& other) {};
~TraceLoggerEvent() {}
#endif
TraceLoggerEventPayload* payload() const {
MOZ_ASSERT(hasPayload());
return payload_;
}
bool hasPayload() const {
return !!payload_;
}
};
/**
* An internal class holding the string information to report, together with an
* unique id and a useCount. Whenever this useCount reaches 0, this event
* cannot get started/stopped anymore. Consumers may still request the
* string information.
*/
class TraceLoggerEventPayload {
uint32_t textId_;
UniqueChars string_;
uint32_t uses_;
public:
TraceLoggerEventPayload(uint32_t textId, char* string)
: textId_(textId),
string_(string),
uses_(0)
{ }
~TraceLoggerEventPayload() {
MOZ_ASSERT(uses_ == 0);
}
uint32_t textId() {
return textId_;
}
const char* string() {
return string_.get();
}
uint32_t uses() {
return uses_;
}
void use() {
uses_++;
}
void release() {
uses_--;
}
};
class TraceLoggerThread
{
#ifdef JS_TRACE_LOGGING
private:
typedef HashMap<const void*,
TraceLoggerEventPayload*,
PointerHasher<const void*, 3>,
SystemAllocPolicy> PointerHashMap;
typedef HashMap<uint32_t,
TraceLoggerEventPayload*,
DefaultHasher<uint32_t>,
SystemAllocPolicy> TextIdHashMap;
uint32_t enabled_;
bool failed;
UniquePtr<TraceLoggerGraph> graph;
PointerHashMap pointerMap;
TextIdHashMap textIdPayloads;
uint32_t nextTextId;
ContinuousSpace<EventEntry> events;
// Every time the events get flushed, this count is increased by one.
// Together with events.lastEntryId(), this gives an unique id for every
// event.
uint32_t iteration_;
#ifdef DEBUG
typedef Vector<uint32_t, 1, js::SystemAllocPolicy > GraphStack;
GraphStack graphStack;
#endif
public:
AutoTraceLog* top;
TraceLoggerThread()
: enabled_(0),
failed(false),
graph(),
nextTextId(TraceLogger_Last),
iteration_(0),
top(nullptr)
{ }
bool init();
~TraceLoggerThread();
bool init(uint32_t loggerId);
void initGraph();
bool enable();
bool enable(JSContext* cx);
bool disable(bool force = false, const char* = "");
bool enabled() { return enabled_ > 0; }
private:
bool fail(JSContext* cx, const char* error);
public:
// Given the previous iteration and size, return an array of events
// (there could be lost events). At the same time update the iteration and
// size and gives back how many events there are.
EventEntry* getEventsStartingAt(uint32_t* lastIteration, uint32_t* lastSize, size_t* num) {
EventEntry* start;
if (iteration_ == *lastIteration) {
MOZ_ASSERT(*lastSize <= events.size());
*num = events.size() - *lastSize;
start = events.data() + *lastSize;
} else {
*num = events.size();
start = events.data();
}
getIterationAndSize(lastIteration, lastSize);
return start;
}
void getIterationAndSize(uint32_t* iteration, uint32_t* size) const {
*iteration = iteration_;
*size = events.size();
}
// Extract the details filename, lineNumber and columnNumber out of a event
// containing script information.
void extractScriptDetails(uint32_t textId, const char** filename, size_t* filename_len,
const char** lineno, size_t* lineno_len, const char** colno,
size_t* colno_len);
bool lostEvents(uint32_t lastIteration, uint32_t lastSize) {
// If still logging in the same iteration, there are no lost events.
if (lastIteration == iteration_) {
MOZ_ASSERT(lastSize <= events.size());
return false;
}
// If we are in the next consecutive iteration we are only sure we
// didn't lose any events when the lastSize equals the maximum size
// 'events' can get.
if (lastIteration == iteration_ - 1 && lastSize == events.maxSize())
return false;
return true;
}
const char* eventText(uint32_t id);
bool textIdIsScriptEvent(uint32_t id);
// The createTextId functions map a unique input to a logger ID.
// This can be used to give start and stop events. Calls to these functions should be
// limited if possible, because of the overhead.
// Note: it is not allowed to use them in logTimestamp.
TraceLoggerEventPayload* getOrCreateEventPayload(TraceLoggerTextId textId);
TraceLoggerEventPayload* getOrCreateEventPayload(const char* text);
TraceLoggerEventPayload* getOrCreateEventPayload(TraceLoggerTextId type, JSScript* script);
TraceLoggerEventPayload* getOrCreateEventPayload(TraceLoggerTextId type,
const JS::ReadOnlyCompileOptions& script);
private:
TraceLoggerEventPayload* getOrCreateEventPayload(TraceLoggerTextId type, const char* filename,
size_t lineno, size_t colno, const void* p);
public:
// Log an event (no start/stop, only the timestamp is recorded).
void logTimestamp(TraceLoggerTextId id);
// Record timestamps for start and stop of an event.
void startEvent(TraceLoggerTextId id);
void startEvent(const TraceLoggerEvent& event);
void stopEvent(TraceLoggerTextId id);
void stopEvent(const TraceLoggerEvent& event);
// These functions are actually private and shouldn't be used in normal
// code. They are made public so they can be used in assembly.
void logTimestamp(uint32_t id);
void startEvent(uint32_t id);
void stopEvent(uint32_t id);
private:
void stopEvent();
void log(uint32_t id);
public:
static unsigned offsetOfEnabled() {
return offsetof(TraceLoggerThread, enabled_);
}
#endif
};
#ifdef JS_TRACE_LOGGING
class TraceLoggerMainThread
: public TraceLoggerThread,
public mozilla::LinkedListElement<TraceLoggerMainThread>
{
};
#endif
class TraceLoggerThreadState
{
#ifdef JS_TRACE_LOGGING
typedef HashMap<Thread::Id,
TraceLoggerThread*,
Thread::Hasher,
SystemAllocPolicy> ThreadLoggerHashMap;
#ifdef DEBUG
bool initialized;
#endif
bool enabledTextIds[TraceLogger_Last];
bool mainThreadEnabled;
bool offThreadEnabled;
bool graphSpewingEnabled;
bool spewErrors;
ThreadLoggerHashMap threadLoggers;
mozilla::LinkedList<TraceLoggerMainThread> traceLoggerMainThreadList;
public:
uint64_t startupTime;
Mutex lock;
TraceLoggerThreadState()
:
#ifdef DEBUG
initialized(false),
#endif
mainThreadEnabled(false),
offThreadEnabled(false),
graphSpewingEnabled(false),
spewErrors(false),
lock(js::mutexid::TraceLoggerThreadState)
{ }
bool init();
~TraceLoggerThreadState();
TraceLoggerThread* forMainThread(JSRuntime* runtime);
TraceLoggerThread* forMainThread(jit::CompileRuntime* runtime);
TraceLoggerThread* forThread(const Thread::Id& thread);
void destroyMainThread(JSRuntime* runtime);
bool isTextIdEnabled(uint32_t textId) {
if (textId < TraceLogger_Last)
return enabledTextIds[textId];
return true;
}
void enableTextId(JSContext* cx, uint32_t textId);
void disableTextId(JSContext* cx, uint32_t textId);
void maybeSpewError(const char* text) {
if (spewErrors)
fprintf(stderr, "%s\n", text);
}
private:
TraceLoggerThread* forMainThread(PerThreadData* mainThread);
#endif
};
#ifdef JS_TRACE_LOGGING
void DestroyTraceLoggerThreadState();
void DestroyTraceLoggerMainThread(JSRuntime* runtime);
TraceLoggerThread* TraceLoggerForMainThread(JSRuntime* runtime);
TraceLoggerThread* TraceLoggerForMainThread(jit::CompileRuntime* runtime);
TraceLoggerThread* TraceLoggerForCurrentThread();
#else
inline TraceLoggerThread* TraceLoggerForMainThread(JSRuntime* runtime) {
return nullptr;
};
inline TraceLoggerThread* TraceLoggerForMainThread(jit::CompileRuntime* runtime) {
return nullptr;
};
inline TraceLoggerThread* TraceLoggerForCurrentThread() {
return nullptr;
};
#endif
inline bool TraceLoggerEnable(TraceLoggerThread* logger) {
#ifdef JS_TRACE_LOGGING
if (logger)
return logger->enable();
#endif
return false;
}
inline bool TraceLoggerEnable(TraceLoggerThread* logger, JSContext* cx) {
#ifdef JS_TRACE_LOGGING
if (logger)
return logger->enable(cx);
#endif
return false;
}
inline bool TraceLoggerDisable(TraceLoggerThread* logger) {
#ifdef JS_TRACE_LOGGING
if (logger)
return logger->disable();
#endif
return false;
}
#ifdef JS_TRACE_LOGGING
bool TraceLogTextIdEnabled(uint32_t textId);
void TraceLogEnableTextId(JSContext* cx, uint32_t textId);
void TraceLogDisableTextId(JSContext* cx, uint32_t textId);
#else
inline bool TraceLogTextIdEnabled(uint32_t textId) {
return false;
}
inline void TraceLogEnableTextId(JSContext* cx, uint32_t textId) {}
inline void TraceLogDisableTextId(JSContext* cx, uint32_t textId) {}
#endif
inline void TraceLogTimestamp(TraceLoggerThread* logger, TraceLoggerTextId textId) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->logTimestamp(textId);
#endif
}
inline void TraceLogStartEvent(TraceLoggerThread* logger, TraceLoggerTextId textId) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->startEvent(textId);
#endif
}
inline void TraceLogStartEvent(TraceLoggerThread* logger, const TraceLoggerEvent& event) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->startEvent(event);
#endif
}
inline void TraceLogStopEvent(TraceLoggerThread* logger, TraceLoggerTextId textId) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->stopEvent(textId);
#endif
}
inline void TraceLogStopEvent(TraceLoggerThread* logger, const TraceLoggerEvent& event) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->stopEvent(event);
#endif
}
// Helper functions for assembly. May not be used otherwise.
inline void TraceLogTimestampPrivate(TraceLoggerThread* logger, uint32_t id) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->logTimestamp(id);
#endif
}
inline void TraceLogStartEventPrivate(TraceLoggerThread* logger, uint32_t id) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->startEvent(id);
#endif
}
inline void TraceLogStopEventPrivate(TraceLoggerThread* logger, uint32_t id) {
#ifdef JS_TRACE_LOGGING
if (logger)
logger->stopEvent(id);
#endif
}
// Automatic logging at the start and end of function call.
class MOZ_RAII AutoTraceLog
{
#ifdef JS_TRACE_LOGGING
TraceLoggerThread* logger;
union {
const TraceLoggerEvent* event;
TraceLoggerTextId id;
} payload;
bool isEvent;
bool executed;
AutoTraceLog* prev;
public:
AutoTraceLog(TraceLoggerThread* logger,
const TraceLoggerEvent& event MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: logger(logger),
isEvent(true),
executed(false)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
payload.event = &event;
if (logger) {
logger->startEvent(event);
prev = logger->top;
logger->top = this;
}
}
AutoTraceLog(TraceLoggerThread* logger, TraceLoggerTextId id MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: logger(logger),
isEvent(false),
executed(false)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
payload.id = id;
if (logger) {
logger->startEvent(id);
prev = logger->top;
logger->top = this;
}
}
~AutoTraceLog()
{
if (logger) {
while (this != logger->top)
logger->top->stop();
stop();
}
}
private:
void stop() {
if (!executed) {
executed = true;
if (isEvent)
logger->stopEvent(*payload.event);
else
logger->stopEvent(payload.id);
}
if (logger->top == this)
logger->top = prev;
}
#else
public:
AutoTraceLog(TraceLoggerThread* logger, uint32_t textId MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
AutoTraceLog(TraceLoggerThread* logger,
const TraceLoggerEvent& event MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
#endif
private:
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
} // namespace js
#endif /* TraceLogging_h */
|