Skip to content

Commit b6c9842

Browse files
committed
SamplingProfiler::StackTrace's move constructor drops stopwatchTimestamp
rdar://190749432 https://bugs.webkit.org/show_bug.cgi?id=328525 Reviewed by Devin Rousso. 267527@main added SamplingProfiler::StackTrace::stopwatchTimestamp, which InspectorScriptProfilerAgent sends to Web Inspector as the timestamp of each sample. StackTrace's hand-written move constructor does not copy it, so every time m_stackTraces grows and WTF::Vector moves the existing elements, those samples lose their timestamp and reach Web Inspector with a value of 0. As a result, the JavaScript call trees in the Timelines tab only count samples inside the selected time range, so they leave those samples out. The move constructor has no custom logic, so default it. It stays declared so that StackTrace stays move-only. Web Inspector's existing ScriptProfiler tests did not catch this because they never enable the Page domain, which is what starts a page's execution stopwatch. With the stopwatch stopped, every timestamp is 0, whether it was copied or not. The new test enables the Page domain and checks that every sample falls between trackingStart and trackingComplete. Test: inspector/script-profiler/sample-timestamps.html * LayoutTests/inspector/script-profiler/sample-timestamps-expected.txt: Added. * LayoutTests/inspector/script-profiler/sample-timestamps.html: Added. * Source/JavaScriptCore/runtime/SamplingProfiler.h: Canonical link: https://commits.webkit.org/324491@main
1 parent 5886192 commit b6c9842

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CONSOLE MESSAGE: busy timer done true
2+
Tests that every stack trace sampled during a ScriptProfiler tracking session has a timestamp within that session.
3+
4+
5+
== Running test suite: ScriptProfiler.Samples.Timestamps
6+
-- Running test case: ScriptProfiler.Samples.Timestamps.WithinTrackingSession
7+
PASS: The execution stopwatch should be running.
8+
PASS: Should have collected at least 50 stack traces.
9+
PASS: Every stack trace timestamp should be within the tracking session.
10+
PASS: Stack trace timestamps should be in increasing order.
11+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script src="../../http/tests/inspector/resources/protocol-test.js"></script>
5+
<script>
6+
function triggerBusyTimer() {
7+
setTimeout(() => {
8+
// Spin for a fixed amount of wall-clock time so the sampling profiler, which samples
9+
// about once per millisecond, collects a few hundred stack traces. The work happens
10+
// inside a timer so that it runs as page script while the profiler is tracking.
11+
let start = Date.now();
12+
let accumulator = 0;
13+
do {
14+
for (let i = 0; i < 10000; ++i)
15+
accumulator += Math.sqrt(i);
16+
} while (Date.now() - start < 300);
17+
console.log("busy timer done " + (accumulator > 0));
18+
TestPage.dispatchEventToFrontend("TestPageBusyWorkDone");
19+
}, 0);
20+
}
21+
22+
function test()
23+
{
24+
let suite = ProtocolTest.createAsyncSuite("ScriptProfiler.Samples.Timestamps");
25+
26+
suite.addTestCase({
27+
name: "ScriptProfiler.Samples.Timestamps.WithinTrackingSession",
28+
description: "Every sampled stack trace should have a timestamp between trackingStart and trackingComplete.",
29+
async test() {
30+
let startTimestamp = NaN;
31+
InspectorProtocol.awaitEvent({event: "ScriptProfiler.trackingStart"}).then((messageObject) => {
32+
startTimestamp = messageObject.params.timestamp;
33+
});
34+
35+
let trackingCompletePromise = InspectorProtocol.awaitEvent({event: "ScriptProfiler.trackingComplete"});
36+
37+
ProtocolTest.awaitEvent("TestPageBusyWorkDone").then(() => {
38+
InspectorProtocol.sendCommand("ScriptProfiler.stopTracking", {});
39+
});
40+
41+
InspectorProtocol.sendCommand("ScriptProfiler.startTracking", {includeSamples: true});
42+
ProtocolTest.evaluateInPage("triggerBusyTimer()");
43+
44+
let messageObject = await trackingCompletePromise;
45+
let endTimestamp = messageObject.params.timestamp;
46+
47+
// A stopwatch that is not running reports 0 for every timestamp, which would make
48+
// the range check below pass no matter what.
49+
ProtocolTest.expectGreaterThan(endTimestamp, startTimestamp, "The execution stopwatch should be running.");
50+
51+
let stackTraces = messageObject.params.samples?.stackTraces || [];
52+
// The stack traces are stored in a Vector that is reallocated as it grows, so make
53+
// sure there are enough of them for that to have happened several times.
54+
ProtocolTest.expectGreaterThanOrEqual(stackTraces.length, 50, "Should have collected at least 50 stack traces.");
55+
56+
let outOfRangeCount = stackTraces.filter((stackTrace) => stackTrace.timestamp < startTimestamp || stackTrace.timestamp > endTimestamp).length;
57+
ProtocolTest.expectEqual(outOfRangeCount, 0, "Every stack trace timestamp should be within the tracking session.");
58+
59+
let isSorted = stackTraces.every((stackTrace, i) => !i || stackTraces[i - 1].timestamp <= stackTrace.timestamp);
60+
ProtocolTest.expectThat(isSorted, "Stack trace timestamps should be in increasing order.");
61+
}
62+
});
63+
64+
// The execution stopwatch only starts when the Page domain is enabled.
65+
InspectorProtocol.sendCommand("Page.enable", {});
66+
67+
// FIXME: <https://webkit.org/b/155851> Web Inspector: We should separate out attaching the debugger from the Debugger.enable event
68+
// Debugger should not need to be enabled for profiling to work.
69+
InspectorProtocol.sendCommand("Debugger.enable", {});
70+
71+
suite.runTestCasesAndFinish();
72+
}
73+
</script>
74+
</head>
75+
<body onload="runTest()">
76+
<p>Tests that every stack trace sampled during a ScriptProfiler tracking session has a timestamp within that session.</p>
77+
</body>
78+
</html>

‎Source/JavaScriptCore/runtime/SamplingProfiler.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ class SamplingProfiler : public ThreadSafeRefCounted<SamplingProfiler> {
175175
Vector<StackFrame> frames;
176176
StackTrace()
177177
{ }
178-
StackTrace(StackTrace&& other)
179-
: timestamp(other.timestamp)
180-
, frames(WTF::move(other.frames))
181-
{ }
178+
StackTrace(StackTrace&&) = default;
182179
};
183180

184181
SamplingProfiler(VM&, Ref<Stopwatch>&&);

0 commit comments

Comments
 (0)