1 Squashed version of several commits to fix the tracelogger.
4 https://github.com/GNOME/jhbuild/blob/master/patches/mozjs38-fix-tracelogger.patch.
6 # === Fix the SM38 tracelogger ===
7 # This patch is a squashed version of several patches that were adapted
8 # to fix failing hunks.
10 # Applied in the following order, they are:
11 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1223767
12 # Assertion failure: i < size_, at js/src/vm/TraceLoggingTypes.h:210
13 # Also fix stop-information to make reduce.py work correctly.
14 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1227914
15 # Limit the memory tracelogger can take.
16 # This causes tracelogger to flush data to the disk regularly and prevents out of
17 # memory issues if a lot of data gets logged.
18 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1155618
19 # Fix tracelogger destructor that touches possibly uninitialised hash table.
20 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1223636
21 # Don't treat extraTextId as containing only extra ids.
22 # This fixes an assertion failure: id == nextTextId at js/src/vm/TraceLoggingGraph.cpp
23 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1227028
24 # Fix when to keep the payload of a TraceLogger event.
25 # This fixes an assertion failure: textId < uint32_t(1 << 31) at js/src/vm/TraceLoggingGraph.h
26 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1266649
27 # Handle failing to add to pointermap gracefully.
28 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1280648
29 # Don't cache based on pointers to movable GC things.
30 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1224123
31 # Fix the use of LastEntryId in tracelogger.h.
32 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1231170
33 # Use size in debugger instead of the current id to track last logged item.
34 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1221844
35 # Move TraceLogger_Invalidation to LOG_ITEM.
36 # Add some debug checks to logTimestamp.
37 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1255766
38 # Also mark resizing of memory.
39 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1259403
40 # Only increase capacity by multiples of 2.
41 # Always make sure there are 3 free slots for events.
44 diff --git a/js/src/jit-test/tests/tracelogger/bug1231170.js b/js/src/jit-test/tests/tracelogger/bug1231170.js
46 index 0000000..023e93e
48 +++ b/js/src/jit-test/tests/tracelogger/bug1231170.js
50 +var du = new Debugger();
51 +if (typeof du.drainTraceLogger === "function")
52 + du.drainTraceLogger();
53 diff --git a/js/src/jit-test/tests/tracelogger/bug1266649.js b/js/src/jit-test/tests/tracelogger/bug1266649.js
55 index 0000000..81ae7ad
57 +++ b/js/src/jit-test/tests/tracelogger/bug1266649.js
60 +var du = new Debugger();
61 +if (typeof du.setupTraceLogger === "function" &&
62 + typeof oomTest === 'function')
64 + du.setupTraceLogger({
67 + oomTest(() => function(){});
69 diff --git a/js/src/jit/Ion.cpp b/js/src/jit/Ion.cpp
70 index 93e2fda..09049d6 100644
71 --- a/js/src/jit/Ion.cpp
72 +++ b/js/src/jit/Ion.cpp
73 @@ -1055,6 +1055,8 @@ IonScript::Destroy(FreeOp* fop, IonScript* script)
75 script->destroyCaches();
76 script->unlinkFromRuntime(fop);
77 + // Frees the potential event we have set.
78 + script->traceLoggerScriptEvent_ = TraceLoggerEvent();
82 diff --git a/js/src/vm/Debugger.cpp b/js/src/vm/Debugger.cpp
83 index 26262fd..af7f313 100644
84 --- a/js/src/vm/Debugger.cpp
85 +++ b/js/src/vm/Debugger.cpp
86 @@ -369,10 +369,10 @@ Debugger::Debugger(JSContext* cx, NativeObject* dbg)
90 - traceLoggerLastDrainedId(0),
91 + traceLoggerLastDrainedSize(0),
92 traceLoggerLastDrainedIteration(0),
94 - traceLoggerScriptedCallsLastDrainedId(0),
95 + traceLoggerScriptedCallsLastDrainedSize(0),
96 traceLoggerScriptedCallsLastDrainedIteration(0)
98 assertSameCompartment(cx, dbg);
99 @@ -3907,9 +3907,9 @@ Debugger::drainTraceLogger(JSContext* cx, unsigned argc, Value* vp)
101 TraceLoggerThread* logger = TraceLoggerForMainThread(cx->runtime());
102 bool lostEvents = logger->lostEvents(dbg->traceLoggerLastDrainedIteration,
103 - dbg->traceLoggerLastDrainedId);
104 + dbg->traceLoggerLastDrainedSize);
105 EventEntry* events = logger->getEventsStartingAt(&dbg->traceLoggerLastDrainedIteration,
106 - &dbg->traceLoggerLastDrainedId,
107 + &dbg->traceLoggerLastDrainedSize,
110 RootedObject array(cx, NewDenseEmptyArray(cx));
111 @@ -4002,10 +4002,10 @@ Debugger::drainTraceLoggerScriptCalls(JSContext* cx, unsigned argc, Value* vp)
113 TraceLoggerThread* logger = TraceLoggerForMainThread(cx->runtime());
114 bool lostEvents = logger->lostEvents(dbg->traceLoggerScriptedCallsLastDrainedIteration,
115 - dbg->traceLoggerScriptedCallsLastDrainedId);
116 + dbg->traceLoggerScriptedCallsLastDrainedSize);
117 EventEntry* events = logger->getEventsStartingAt(
118 &dbg->traceLoggerScriptedCallsLastDrainedIteration,
119 - &dbg->traceLoggerScriptedCallsLastDrainedId,
120 + &dbg->traceLoggerScriptedCallsLastDrainedSize,
123 RootedObject array(cx, NewDenseEmptyArray(cx));
124 diff --git a/js/src/vm/Debugger.h b/js/src/vm/Debugger.h
125 index 8cac36a..c92d685 100644
126 --- a/js/src/vm/Debugger.h
127 +++ b/js/src/vm/Debugger.h
128 @@ -314,10 +314,10 @@ class Debugger : private mozilla::LinkedListElement<Debugger>
132 - uint32_t traceLoggerLastDrainedId;
133 + uint32_t traceLoggerLastDrainedSize;
134 uint32_t traceLoggerLastDrainedIteration;
136 - uint32_t traceLoggerScriptedCallsLastDrainedId;
137 + uint32_t traceLoggerScriptedCallsLastDrainedSize;
138 uint32_t traceLoggerScriptedCallsLastDrainedIteration;
141 diff --git a/js/src/vm/TraceLogging.cpp b/js/src/vm/TraceLogging.cpp
142 index 6715b36..9766a6f 100644
143 --- a/js/src/vm/TraceLogging.cpp
144 +++ b/js/src/vm/TraceLogging.cpp
145 @@ -131,7 +131,7 @@ TraceLoggerThread::init()
147 if (!pointerMap.init())
149 - if (!extraTextId.init())
150 + if (!textIdPayloads.init())
154 @@ -185,10 +185,10 @@ TraceLoggerThread::~TraceLoggerThread()
158 - for (TextIdHashMap::Range r = extraTextId.all(); !r.empty(); r.popFront())
159 - js_delete(r.front().value());
160 - extraTextId.finish();
161 - pointerMap.finish();
162 + if (textIdPayloads.initialized()) {
163 + for (TextIdHashMap::Range r = textIdPayloads.all(); !r.empty(); r.popFront())
164 + js_delete(r.front().value());
169 @@ -287,7 +287,7 @@ TraceLoggerThread::eventText(uint32_t id)
170 if (id < TraceLogger_Last)
171 return TLTextIdString(static_cast<TraceLoggerTextId>(id));
173 - TextIdHashMap::Ptr p = extraTextId.lookup(id);
174 + TextIdHashMap::Ptr p = textIdPayloads.lookup(id);
177 return p->value()->string();
178 @@ -341,13 +341,15 @@ TraceLoggerThread::extractScriptDetails(uint32_t textId, const char** filename,
179 TraceLoggerEventPayload*
180 TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId textId)
182 - TextIdHashMap::AddPtr p = extraTextId.lookupForAdd(textId);
184 + TextIdHashMap::AddPtr p = textIdPayloads.lookupForAdd(textId);
186 + MOZ_ASSERT(p->value()->textId() == textId); // Sanity check.
190 TraceLoggerEventPayload* payload = js_new<TraceLoggerEventPayload>(textId, (char*)nullptr);
192 - if (!extraTextId.add(p, textId, payload))
193 + if (!textIdPayloads.add(p, textId, payload))
197 @@ -357,8 +359,10 @@ TraceLoggerEventPayload*
198 TraceLoggerThread::getOrCreateEventPayload(const char* text)
200 PointerHashMap::AddPtr p = pointerMap.lookupForAdd((const void*)text);
203 + MOZ_ASSERT(p->value()->textId() < nextTextId); // Sanity check.
207 size_t len = strlen(text);
208 char* str = js_pod_malloc<char>(len + 1);
209 @@ -369,7 +373,7 @@ TraceLoggerThread::getOrCreateEventPayload(const char* text)
210 MOZ_ASSERT(ret == len);
211 MOZ_ASSERT(strlen(str) == len);
213 - uint32_t textId = extraTextId.count() + TraceLogger_Last;
214 + uint32_t textId = nextTextId;
216 TraceLoggerEventPayload* payload = js_new<TraceLoggerEventPayload>(textId, str);
218 @@ -377,17 +381,19 @@ TraceLoggerThread::getOrCreateEventPayload(const char* text)
222 - if (!extraTextId.putNew(textId, payload)) {
223 + if (!textIdPayloads.putNew(textId, payload)) {
228 - if (!pointerMap.add(p, text, payload))
232 graph->addTextId(textId, str);
236 + if (!pointerMap.add(p, text, payload))
242 @@ -407,9 +413,14 @@ TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type, const char* f
243 if (!traceLoggerState->isTextIdEnabled(type))
244 return getOrCreateEventPayload(type);
246 - PointerHashMap::AddPtr p = pointerMap.lookupForAdd(ptr);
249 + PointerHashMap::AddPtr p;
251 + p = pointerMap.lookupForAdd(ptr);
253 + MOZ_ASSERT(p->value()->textId() < nextTextId); // Sanity check.
258 // Compute the length of the string to create.
259 size_t lenFilename = strlen(filename);
260 @@ -428,24 +439,28 @@ TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type, const char* f
261 MOZ_ASSERT(ret == len);
262 MOZ_ASSERT(strlen(str) == len);
264 - uint32_t textId = extraTextId.count() + TraceLogger_Last;
265 + uint32_t textId = nextTextId;
266 TraceLoggerEventPayload* payload = js_new<TraceLoggerEventPayload>(textId, str);
272 - if (!extraTextId.putNew(textId, payload)) {
273 + if (!textIdPayloads.putNew(textId, payload)) {
278 - if (!pointerMap.add(p, ptr, payload))
282 graph->addTextId(textId, str);
287 + if (!pointerMap.add(p, ptr, payload))
294 @@ -453,14 +468,14 @@ TraceLoggerEventPayload*
295 TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type, JSScript* script)
297 return getOrCreateEventPayload(type, script->filename(), script->lineno(), script->column(),
302 TraceLoggerEventPayload*
303 TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type,
304 const JS::ReadOnlyCompileOptions& script)
306 - return getOrCreateEventPayload(type, script.filename(), script.lineno, script.column, &script);
307 + return getOrCreateEventPayload(type, script.filename(), script.lineno, script.column, nullptr);
311 @@ -485,7 +500,7 @@ TraceLoggerThread::startEvent(uint32_t id)
312 if (!traceLoggerState->isTextIdEnabled(id))
320 @@ -510,7 +525,7 @@ TraceLoggerThread::stopEvent(uint32_t id)
321 if (!traceLoggerState->isTextIdEnabled(id))
324 - logTimestamp(TraceLogger_Stop);
325 + log(TraceLogger_Stop);
329 @@ -522,23 +537,57 @@ TraceLoggerThread::logTimestamp(TraceLoggerTextId id)
331 TraceLoggerThread::logTimestamp(uint32_t id)
333 + MOZ_ASSERT(id > TraceLogger_LastTreeItem && id < TraceLogger_Last);
338 +TraceLoggerThread::log(uint32_t id)
343 MOZ_ASSERT(traceLoggerState);
344 - if (!events.ensureSpaceBeforeAdd()) {
346 + // We request for 3 items to add, since if we don't have enough room
347 + // we record the time it took to make more place. To log this information
348 + // we need 2 extra free entries.
349 + if (!events.hasSpaceForAdd(3)) {
350 uint64_t start = rdtsc() - traceLoggerState->startupTime;
353 - graph->log(events);
354 + if (!events.ensureSpaceBeforeAdd(3)) {
356 + graph->log(events);
361 + // Remove the item in the pointerMap for which the payloads
362 + // have no uses anymore
363 + for (PointerHashMap::Enum e(pointerMap); !e.empty(); e.popFront()) {
364 + if (e.front().value()->uses() != 0)
367 + TextIdHashMap::Ptr p = textIdPayloads.lookup(e.front().value()->textId());
369 + textIdPayloads.remove(p);
376 + // Free all payloads that have no uses anymore.
377 + for (TextIdHashMap::Enum e(textIdPayloads); !e.empty(); e.popFront()) {
378 + if (e.front().value()->uses() == 0) {
379 + js_delete(e.front().value());
385 // Log the time it took to flush the events as being from the
388 - MOZ_ASSERT(events.capacity() > 2);
389 + MOZ_ASSERT(events.hasSpaceForAdd(2));
390 EventEntry& entryStart = events.pushUninitialized();
391 entryStart.time = start;
392 entryStart.textId = TraceLogger_Internal;
393 @@ -548,13 +597,6 @@ TraceLoggerThread::logTimestamp(uint32_t id)
394 entryStop.textId = TraceLogger_Stop;
397 - // Free all TextEvents that have no uses anymore.
398 - for (TextIdHashMap::Enum e(extraTextId); !e.empty(); e.popFront()) {
399 - if (e.front().value()->uses() == 0) {
400 - js_delete(e.front().value());
406 uint64_t time = rdtsc() - traceLoggerState->startupTime;
407 @@ -956,3 +998,16 @@ TraceLoggerEvent::~TraceLoggerEvent()
413 +TraceLoggerEvent::operator=(const TraceLoggerEvent& other)
416 + payload()->release();
417 + if (other.hasPayload())
418 + other.payload()->use();
420 + payload_ = other.payload_;
424 diff --git a/js/src/vm/TraceLogging.h b/js/src/vm/TraceLogging.h
425 index a124dcb..91a1eb0 100644
426 --- a/js/src/vm/TraceLogging.h
427 +++ b/js/src/vm/TraceLogging.h
428 @@ -110,6 +110,9 @@ class TraceLoggerEvent {
429 bool hasPayload() const {
433 + TraceLoggerEvent& operator=(const TraceLoggerEvent& other);
434 + TraceLoggerEvent(const TraceLoggerEvent& event) = delete;
438 @@ -130,6 +133,10 @@ class TraceLoggerEventPayload {
442 + ~TraceLoggerEventPayload() {
443 + MOZ_ASSERT(uses_ == 0);
449 @@ -166,7 +173,8 @@ class TraceLoggerThread
450 mozilla::UniquePtr<TraceLoggerGraph> graph;
452 PointerHashMap pointerMap;
453 - TextIdHashMap extraTextId;
454 + TextIdHashMap textIdPayloads;
455 + uint32_t nextTextId;
457 ContinuousSpace<EventEntry> events;
459 @@ -181,6 +189,7 @@ class TraceLoggerThread
463 + nextTextId(TraceLogger_Last),
467 @@ -195,22 +204,22 @@ class TraceLoggerThread
468 bool enable(JSContext* cx);
471 - // Given the previous iteration and lastEntryId, return an array of events
472 + // Given the previous iteration and size, return an array of events
473 // (there could be lost events). At the same time update the iteration and
474 - // lastEntry and gives back how many events there are.
475 - EventEntry* getEventsStartingAt(uint32_t* lastIteration, uint32_t* lastEntryId, size_t* num) {
476 + // size and gives back how many events there are.
477 + EventEntry* getEventsStartingAt(uint32_t* lastIteration, uint32_t* lastSize, size_t* num) {
479 if (iteration_ == *lastIteration) {
480 - MOZ_ASSERT(events.lastEntryId() >= *lastEntryId);
481 - *num = events.lastEntryId() - *lastEntryId;
482 - start = events.data() + *lastEntryId + 1;
483 + MOZ_ASSERT(*lastSize <= events.size());
484 + *num = events.size() - *lastSize;
485 + start = events.data() + *lastSize;
487 - *num = events.lastEntryId() + 1;
488 + *num = events.size();
489 start = events.data();
492 *lastIteration = iteration_;
493 - *lastEntryId = events.lastEntryId();
494 + *lastSize = events.size();
498 @@ -220,16 +229,16 @@ class TraceLoggerThread
499 const char** lineno, size_t* lineno_len, const char** colno,
502 - bool lostEvents(uint32_t lastIteration, uint32_t lastEntryId) {
503 + bool lostEvents(uint32_t lastIteration, uint32_t lastSize) {
504 // If still logging in the same iteration, there are no lost events.
505 if (lastIteration == iteration_) {
506 - MOZ_ASSERT(lastEntryId <= events.lastEntryId());
507 + MOZ_ASSERT(lastSize <= events.size());
511 - // When proceeded to the next iteration and lastEntryId points to
512 - // the maximum capacity there are no logs that are lost.
513 - if (lastIteration + 1 == iteration_ && lastEntryId == events.capacity())
514 + // If we are in a consecutive iteration we are only sure we didn't lose any events,
515 + // when the lastSize equals the maximum size 'events' can get.
516 + if (lastIteration == iteration_ - 1 && lastSize == events.maxSize())
520 @@ -268,6 +277,7 @@ class TraceLoggerThread
521 void stopEvent(uint32_t id);
524 + void log(uint32_t id);
527 static unsigned offsetOfEnabled() {
528 diff --git a/js/src/vm/TraceLoggingGraph.cpp b/js/src/vm/TraceLoggingGraph.cpp
529 index d1b7f2e..a4eb273 100644
530 --- a/js/src/vm/TraceLoggingGraph.cpp
531 +++ b/js/src/vm/TraceLoggingGraph.cpp
532 @@ -276,7 +276,7 @@ TraceLoggerGraph::flush()
533 if (bytesWritten < tree.size())
536 - treeOffset += tree.lastEntryId();
537 + treeOffset += tree.size();
541 @@ -359,7 +359,7 @@ TraceLoggerGraph::startEventInternal(uint32_t id, uint64_t timestamp)
543 if (parent.lastChildId() == 0) {
544 MOZ_ASSERT(!entry.hasChildren());
545 - MOZ_ASSERT(parent.treeId() == tree.lastEntryId() + treeOffset);
546 + MOZ_ASSERT(parent.treeId() == treeOffset + tree.size() - 1);
548 if (!updateHasChildren(parent.treeId()))
550 diff --git a/js/src/vm/TraceLoggingTypes.h b/js/src/vm/TraceLoggingTypes.h
551 index f1c9d0c..10b76d6 100644
552 --- a/js/src/vm/TraceLoggingTypes.h
553 +++ b/js/src/vm/TraceLoggingTypes.h
560 _(IonCompilationPaused) \
564 #define TRACELOGGER_LOG_ITEMS(_) \
570 @@ -130,6 +130,9 @@ class ContinuousSpace {
574 + // The maximum amount of ram memory a continuous space structure can take (in bytes).
575 + static const uint32_t LIMIT = 200 * 1024 * 1024;
580 @@ -151,6 +154,10 @@ class ContinuousSpace {
584 + static uint32_t maxSize() {
585 + return LIMIT / sizeof(T);
591 @@ -187,11 +194,14 @@ class ContinuousSpace {
592 if (hasSpaceForAdd(count))
595 + // Limit the size of a continuous buffer.
596 + if (size_ + count > maxSize())
599 uint32_t nCapacity = capacity_ * 2;
600 - if (size_ + count > nCapacity)
601 - nCapacity = size_ + count;
602 - T* entries = (T*) js_realloc(data_, nCapacity * sizeof(T));
603 + nCapacity = (nCapacity < maxSize()) ? nCapacity : maxSize();
605 + T* entries = (T*) js_realloc(data_, nCapacity * sizeof(T));