1 # === Fix the SM38 tracelogger ===
2 # This patch is a squashed version of several patches that were adapted
3 # to fix failing hunks.
5 # Applied in the following order, they are:
6 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1223767
7 # Assertion failure: i < size_, at js/src/vm/TraceLoggingTypes.h:210
8 # Also fix stop-information to make reduce.py work correctly.
9 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1227914
10 # Limit the memory tracelogger can take.
11 # This causes tracelogger to flush data to the disk regularly and prevents out of
12 # memory issues if a lot of data gets logged.
13 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1155618
14 # Fix tracelogger destructor that touches possibly uninitialised hash table.
15 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1223636
16 # Don't treat extraTextId as containing only extra ids.
17 # This fixes an assertion failure: id == nextTextId at js/src/vm/TraceLoggingGraph.cpp
18 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1227028
19 # Fix when to keep the payload of a TraceLogger event.
20 # This fixes an assertion failure: textId < uint32_t(1 << 31) at js/src/vm/TraceLoggingGraph.h
21 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1266649
22 # Handle failing to add to pointermap gracefully.
23 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1280648
24 # Don't cache based on pointers to movable GC things.
25 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1224123
26 # Fix the use of LastEntryId in tracelogger.h.
27 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1231170
28 # Use size in debugger instead of the current id to track last logged item.
29 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1221844
30 # Move TraceLogger_Invalidation to LOG_ITEM.
31 # Add some debug checks to logTimestamp.
32 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1255766
33 # Also mark resizing of memory.
34 # * https://bugzilla.mozilla.org/show_bug.cgi?id=1259403
35 # Only increase capacity by multiples of 2.
36 # Always make sure there are 3 free slots for events.
39 diff --git a/js/src/jit-test/tests/tracelogger/bug1231170.js b/js/src/jit-test/tests/tracelogger/bug1231170.js
41 index 0000000..023e93e
43 +++ b/js/src/jit-test/tests/tracelogger/bug1231170.js
45 +var du = new Debugger();
46 +if (typeof du.drainTraceLogger === "function")
47 + du.drainTraceLogger();
48 diff --git a/js/src/jit-test/tests/tracelogger/bug1266649.js b/js/src/jit-test/tests/tracelogger/bug1266649.js
50 index 0000000..81ae7ad
52 +++ b/js/src/jit-test/tests/tracelogger/bug1266649.js
55 +var du = new Debugger();
56 +if (typeof du.setupTraceLogger === "function" &&
57 + typeof oomTest === 'function')
59 + du.setupTraceLogger({
62 + oomTest(() => function(){});
64 diff --git a/js/src/jit/Ion.cpp b/js/src/jit/Ion.cpp
65 index 93e2fda..09049d6 100644
66 --- a/js/src/jit/Ion.cpp
67 +++ b/js/src/jit/Ion.cpp
68 @@ -1055,6 +1055,8 @@ IonScript::Destroy(FreeOp* fop, IonScript* script)
70 script->destroyCaches();
71 script->unlinkFromRuntime(fop);
72 + // Frees the potential event we have set.
73 + script->traceLoggerScriptEvent_ = TraceLoggerEvent();
77 diff --git a/js/src/vm/Debugger.cpp b/js/src/vm/Debugger.cpp
78 index 26262fd..af7f313 100644
79 --- a/js/src/vm/Debugger.cpp
80 +++ b/js/src/vm/Debugger.cpp
81 @@ -369,10 +369,10 @@ Debugger::Debugger(JSContext* cx, NativeObject* dbg)
85 - traceLoggerLastDrainedId(0),
86 + traceLoggerLastDrainedSize(0),
87 traceLoggerLastDrainedIteration(0),
89 - traceLoggerScriptedCallsLastDrainedId(0),
90 + traceLoggerScriptedCallsLastDrainedSize(0),
91 traceLoggerScriptedCallsLastDrainedIteration(0)
93 assertSameCompartment(cx, dbg);
94 @@ -3907,9 +3907,9 @@ Debugger::drainTraceLogger(JSContext* cx, unsigned argc, Value* vp)
96 TraceLoggerThread* logger = TraceLoggerForMainThread(cx->runtime());
97 bool lostEvents = logger->lostEvents(dbg->traceLoggerLastDrainedIteration,
98 - dbg->traceLoggerLastDrainedId);
99 + dbg->traceLoggerLastDrainedSize);
100 EventEntry* events = logger->getEventsStartingAt(&dbg->traceLoggerLastDrainedIteration,
101 - &dbg->traceLoggerLastDrainedId,
102 + &dbg->traceLoggerLastDrainedSize,
105 RootedObject array(cx, NewDenseEmptyArray(cx));
106 @@ -4002,10 +4002,10 @@ Debugger::drainTraceLoggerScriptCalls(JSContext* cx, unsigned argc, Value* vp)
108 TraceLoggerThread* logger = TraceLoggerForMainThread(cx->runtime());
109 bool lostEvents = logger->lostEvents(dbg->traceLoggerScriptedCallsLastDrainedIteration,
110 - dbg->traceLoggerScriptedCallsLastDrainedId);
111 + dbg->traceLoggerScriptedCallsLastDrainedSize);
112 EventEntry* events = logger->getEventsStartingAt(
113 &dbg->traceLoggerScriptedCallsLastDrainedIteration,
114 - &dbg->traceLoggerScriptedCallsLastDrainedId,
115 + &dbg->traceLoggerScriptedCallsLastDrainedSize,
118 RootedObject array(cx, NewDenseEmptyArray(cx));
119 diff --git a/js/src/vm/Debugger.h b/js/src/vm/Debugger.h
120 index 8cac36a..c92d685 100644
121 --- a/js/src/vm/Debugger.h
122 +++ b/js/src/vm/Debugger.h
123 @@ -314,10 +314,10 @@ class Debugger : private mozilla::LinkedListElement<Debugger>
127 - uint32_t traceLoggerLastDrainedId;
128 + uint32_t traceLoggerLastDrainedSize;
129 uint32_t traceLoggerLastDrainedIteration;
131 - uint32_t traceLoggerScriptedCallsLastDrainedId;
132 + uint32_t traceLoggerScriptedCallsLastDrainedSize;
133 uint32_t traceLoggerScriptedCallsLastDrainedIteration;
136 diff --git a/js/src/vm/TraceLogging.cpp b/js/src/vm/TraceLogging.cpp
137 index 6715b36..9766a6f 100644
138 --- a/js/src/vm/TraceLogging.cpp
139 +++ b/js/src/vm/TraceLogging.cpp
140 @@ -131,7 +131,7 @@ TraceLoggerThread::init()
142 if (!pointerMap.init())
144 - if (!extraTextId.init())
145 + if (!textIdPayloads.init())
149 @@ -185,10 +185,10 @@ TraceLoggerThread::~TraceLoggerThread()
153 - for (TextIdHashMap::Range r = extraTextId.all(); !r.empty(); r.popFront())
154 - js_delete(r.front().value());
155 - extraTextId.finish();
156 - pointerMap.finish();
157 + if (textIdPayloads.initialized()) {
158 + for (TextIdHashMap::Range r = textIdPayloads.all(); !r.empty(); r.popFront())
159 + js_delete(r.front().value());
164 @@ -287,7 +287,7 @@ TraceLoggerThread::eventText(uint32_t id)
165 if (id < TraceLogger_Last)
166 return TLTextIdString(static_cast<TraceLoggerTextId>(id));
168 - TextIdHashMap::Ptr p = extraTextId.lookup(id);
169 + TextIdHashMap::Ptr p = textIdPayloads.lookup(id);
172 return p->value()->string();
173 @@ -341,13 +341,15 @@ TraceLoggerThread::extractScriptDetails(uint32_t textId, const char** filename,
174 TraceLoggerEventPayload*
175 TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId textId)
177 - TextIdHashMap::AddPtr p = extraTextId.lookupForAdd(textId);
179 + TextIdHashMap::AddPtr p = textIdPayloads.lookupForAdd(textId);
181 + MOZ_ASSERT(p->value()->textId() == textId); // Sanity check.
185 TraceLoggerEventPayload* payload = js_new<TraceLoggerEventPayload>(textId, (char*)nullptr);
187 - if (!extraTextId.add(p, textId, payload))
188 + if (!textIdPayloads.add(p, textId, payload))
192 @@ -357,8 +359,10 @@ TraceLoggerEventPayload*
193 TraceLoggerThread::getOrCreateEventPayload(const char* text)
195 PointerHashMap::AddPtr p = pointerMap.lookupForAdd((const void*)text);
198 + MOZ_ASSERT(p->value()->textId() < nextTextId); // Sanity check.
202 size_t len = strlen(text);
203 char* str = js_pod_malloc<char>(len + 1);
204 @@ -369,7 +373,7 @@ TraceLoggerThread::getOrCreateEventPayload(const char* text)
205 MOZ_ASSERT(ret == len);
206 MOZ_ASSERT(strlen(str) == len);
208 - uint32_t textId = extraTextId.count() + TraceLogger_Last;
209 + uint32_t textId = nextTextId;
211 TraceLoggerEventPayload* payload = js_new<TraceLoggerEventPayload>(textId, str);
213 @@ -377,17 +381,19 @@ TraceLoggerThread::getOrCreateEventPayload(const char* text)
217 - if (!extraTextId.putNew(textId, payload)) {
218 + if (!textIdPayloads.putNew(textId, payload)) {
223 - if (!pointerMap.add(p, text, payload))
227 graph->addTextId(textId, str);
231 + if (!pointerMap.add(p, text, payload))
237 @@ -407,9 +413,14 @@ TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type, const char* f
238 if (!traceLoggerState->isTextIdEnabled(type))
239 return getOrCreateEventPayload(type);
241 - PointerHashMap::AddPtr p = pointerMap.lookupForAdd(ptr);
244 + PointerHashMap::AddPtr p;
246 + p = pointerMap.lookupForAdd(ptr);
248 + MOZ_ASSERT(p->value()->textId() < nextTextId); // Sanity check.
253 // Compute the length of the string to create.
254 size_t lenFilename = strlen(filename);
255 @@ -428,24 +439,28 @@ TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type, const char* f
256 MOZ_ASSERT(ret == len);
257 MOZ_ASSERT(strlen(str) == len);
259 - uint32_t textId = extraTextId.count() + TraceLogger_Last;
260 + uint32_t textId = nextTextId;
261 TraceLoggerEventPayload* payload = js_new<TraceLoggerEventPayload>(textId, str);
267 - if (!extraTextId.putNew(textId, payload)) {
268 + if (!textIdPayloads.putNew(textId, payload)) {
273 - if (!pointerMap.add(p, ptr, payload))
277 graph->addTextId(textId, str);
282 + if (!pointerMap.add(p, ptr, payload))
289 @@ -453,14 +468,14 @@ TraceLoggerEventPayload*
290 TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type, JSScript* script)
292 return getOrCreateEventPayload(type, script->filename(), script->lineno(), script->column(),
297 TraceLoggerEventPayload*
298 TraceLoggerThread::getOrCreateEventPayload(TraceLoggerTextId type,
299 const JS::ReadOnlyCompileOptions& script)
301 - return getOrCreateEventPayload(type, script.filename(), script.lineno, script.column, &script);
302 + return getOrCreateEventPayload(type, script.filename(), script.lineno, script.column, nullptr);
306 @@ -485,7 +500,7 @@ TraceLoggerThread::startEvent(uint32_t id)
307 if (!traceLoggerState->isTextIdEnabled(id))
315 @@ -510,7 +525,7 @@ TraceLoggerThread::stopEvent(uint32_t id)
316 if (!traceLoggerState->isTextIdEnabled(id))
319 - logTimestamp(TraceLogger_Stop);
320 + log(TraceLogger_Stop);
324 @@ -522,23 +537,57 @@ TraceLoggerThread::logTimestamp(TraceLoggerTextId id)
326 TraceLoggerThread::logTimestamp(uint32_t id)
328 + MOZ_ASSERT(id > TraceLogger_LastTreeItem && id < TraceLogger_Last);
333 +TraceLoggerThread::log(uint32_t id)
338 MOZ_ASSERT(traceLoggerState);
339 - if (!events.ensureSpaceBeforeAdd()) {
341 + // We request for 3 items to add, since if we don't have enough room
342 + // we record the time it took to make more place. To log this information
343 + // we need 2 extra free entries.
344 + if (!events.hasSpaceForAdd(3)) {
345 uint64_t start = rdtsc() - traceLoggerState->startupTime;
348 - graph->log(events);
349 + if (!events.ensureSpaceBeforeAdd(3)) {
351 + graph->log(events);
356 + // Remove the item in the pointerMap for which the payloads
357 + // have no uses anymore
358 + for (PointerHashMap::Enum e(pointerMap); !e.empty(); e.popFront()) {
359 + if (e.front().value()->uses() != 0)
362 + TextIdHashMap::Ptr p = textIdPayloads.lookup(e.front().value()->textId());
364 + textIdPayloads.remove(p);
371 + // Free all payloads that have no uses anymore.
372 + for (TextIdHashMap::Enum e(textIdPayloads); !e.empty(); e.popFront()) {
373 + if (e.front().value()->uses() == 0) {
374 + js_delete(e.front().value());
380 // Log the time it took to flush the events as being from the
383 - MOZ_ASSERT(events.capacity() > 2);
384 + MOZ_ASSERT(events.hasSpaceForAdd(2));
385 EventEntry& entryStart = events.pushUninitialized();
386 entryStart.time = start;
387 entryStart.textId = TraceLogger_Internal;
388 @@ -548,13 +597,6 @@ TraceLoggerThread::logTimestamp(uint32_t id)
389 entryStop.textId = TraceLogger_Stop;
392 - // Free all TextEvents that have no uses anymore.
393 - for (TextIdHashMap::Enum e(extraTextId); !e.empty(); e.popFront()) {
394 - if (e.front().value()->uses() == 0) {
395 - js_delete(e.front().value());
401 uint64_t time = rdtsc() - traceLoggerState->startupTime;
402 @@ -956,3 +998,16 @@ TraceLoggerEvent::~TraceLoggerEvent()
408 +TraceLoggerEvent::operator=(const TraceLoggerEvent& other)
411 + payload()->release();
412 + if (other.hasPayload())
413 + other.payload()->use();
415 + payload_ = other.payload_;
419 diff --git a/js/src/vm/TraceLogging.h b/js/src/vm/TraceLogging.h
420 index a124dcb..91a1eb0 100644
421 --- a/js/src/vm/TraceLogging.h
422 +++ b/js/src/vm/TraceLogging.h
423 @@ -110,6 +110,9 @@ class TraceLoggerEvent {
424 bool hasPayload() const {
428 + TraceLoggerEvent& operator=(const TraceLoggerEvent& other);
429 + TraceLoggerEvent(const TraceLoggerEvent& event) = delete;
433 @@ -130,6 +133,10 @@ class TraceLoggerEventPayload {
437 + ~TraceLoggerEventPayload() {
438 + MOZ_ASSERT(uses_ == 0);
444 @@ -166,7 +173,8 @@ class TraceLoggerThread
445 mozilla::UniquePtr<TraceLoggerGraph> graph;
447 PointerHashMap pointerMap;
448 - TextIdHashMap extraTextId;
449 + TextIdHashMap textIdPayloads;
450 + uint32_t nextTextId;
452 ContinuousSpace<EventEntry> events;
454 @@ -181,6 +189,7 @@ class TraceLoggerThread
458 + nextTextId(TraceLogger_Last),
462 @@ -195,22 +204,22 @@ class TraceLoggerThread
463 bool enable(JSContext* cx);
466 - // Given the previous iteration and lastEntryId, return an array of events
467 + // Given the previous iteration and size, return an array of events
468 // (there could be lost events). At the same time update the iteration and
469 - // lastEntry and gives back how many events there are.
470 - EventEntry* getEventsStartingAt(uint32_t* lastIteration, uint32_t* lastEntryId, size_t* num) {
471 + // size and gives back how many events there are.
472 + EventEntry* getEventsStartingAt(uint32_t* lastIteration, uint32_t* lastSize, size_t* num) {
474 if (iteration_ == *lastIteration) {
475 - MOZ_ASSERT(events.lastEntryId() >= *lastEntryId);
476 - *num = events.lastEntryId() - *lastEntryId;
477 - start = events.data() + *lastEntryId + 1;
478 + MOZ_ASSERT(*lastSize <= events.size());
479 + *num = events.size() - *lastSize;
480 + start = events.data() + *lastSize;
482 - *num = events.lastEntryId() + 1;
483 + *num = events.size();
484 start = events.data();
487 *lastIteration = iteration_;
488 - *lastEntryId = events.lastEntryId();
489 + *lastSize = events.size();
493 @@ -220,16 +229,16 @@ class TraceLoggerThread
494 const char** lineno, size_t* lineno_len, const char** colno,
497 - bool lostEvents(uint32_t lastIteration, uint32_t lastEntryId) {
498 + bool lostEvents(uint32_t lastIteration, uint32_t lastSize) {
499 // If still logging in the same iteration, there are no lost events.
500 if (lastIteration == iteration_) {
501 - MOZ_ASSERT(lastEntryId <= events.lastEntryId());
502 + MOZ_ASSERT(lastSize <= events.size());
506 - // When proceeded to the next iteration and lastEntryId points to
507 - // the maximum capacity there are no logs that are lost.
508 - if (lastIteration + 1 == iteration_ && lastEntryId == events.capacity())
509 + // If we are in a consecutive iteration we are only sure we didn't lose any events,
510 + // when the lastSize equals the maximum size 'events' can get.
511 + if (lastIteration == iteration_ - 1 && lastSize == events.maxSize())
515 @@ -268,6 +277,7 @@ class TraceLoggerThread
516 void stopEvent(uint32_t id);
519 + void log(uint32_t id);
522 static unsigned offsetOfEnabled() {
523 diff --git a/js/src/vm/TraceLoggingGraph.cpp b/js/src/vm/TraceLoggingGraph.cpp
524 index d1b7f2e..a4eb273 100644
525 --- a/js/src/vm/TraceLoggingGraph.cpp
526 +++ b/js/src/vm/TraceLoggingGraph.cpp
527 @@ -276,7 +276,7 @@ TraceLoggerGraph::flush()
528 if (bytesWritten < tree.size())
531 - treeOffset += tree.lastEntryId();
532 + treeOffset += tree.size();
536 @@ -359,7 +359,7 @@ TraceLoggerGraph::startEventInternal(uint32_t id, uint64_t timestamp)
538 if (parent.lastChildId() == 0) {
539 MOZ_ASSERT(!entry.hasChildren());
540 - MOZ_ASSERT(parent.treeId() == tree.lastEntryId() + treeOffset);
541 + MOZ_ASSERT(parent.treeId() == treeOffset + tree.size() - 1);
543 if (!updateHasChildren(parent.treeId()))
545 diff --git a/js/src/vm/TraceLoggingTypes.h b/js/src/vm/TraceLoggingTypes.h
546 index f1c9d0c..10b76d6 100644
547 --- a/js/src/vm/TraceLoggingTypes.h
548 +++ b/js/src/vm/TraceLoggingTypes.h
555 _(IonCompilationPaused) \
559 #define TRACELOGGER_LOG_ITEMS(_) \
565 @@ -130,6 +130,9 @@ class ContinuousSpace {
569 + // The maximum amount of ram memory a continuous space structure can take (in bytes).
570 + static const uint32_t LIMIT = 200 * 1024 * 1024;
575 @@ -151,6 +154,10 @@ class ContinuousSpace {
579 + static uint32_t maxSize() {
580 + return LIMIT / sizeof(T);
586 @@ -187,11 +194,14 @@ class ContinuousSpace {
587 if (hasSpaceForAdd(count))
590 + // Limit the size of a continuous buffer.
591 + if (size_ + count > maxSize())
594 uint32_t nCapacity = capacity_ * 2;
595 - if (size_ + count > nCapacity)
596 - nCapacity = size_ + count;
597 - T* entries = (T*) js_realloc(data_, nCapacity * sizeof(T));
598 + nCapacity = (nCapacity < maxSize()) ? nCapacity : maxSize();
600 + T* entries = (T*) js_realloc(data_, nCapacity * sizeof(T));