Skip to content

Commit 4990218

Browse files
committed
Stopwatch::reset() doesn't clear the stopwatch's past run intervals
rdar://190737340 https://bugs.webkit.org/show_bug.cgi?id=328496 Reviewed by Basuke Suzuki. Stopwatch::reset() kept the list of earlier run intervals, so fromMonotonicTime() mapped times from before the reset onto the new zero point, and shifted times from later runs by the length of the old intervals. A debugger pause adds an interval, and InspectorPageAgent::enable() and WorkerInspectorController reset the stopwatch afterwards, so this can happen. Clear the list in reset(). Drive-by: also rename the misspelt m_pastInternals to m_pastIntervals. Test: Tools/TestWebKitAPI/Tests/WTF/Stopwatch.cpp * Source/WTF/wtf/Stopwatch.h: (WTF::Stopwatch::reset): (WTF::Stopwatch::stop): (WTF::Stopwatch::fromMonotonicTime const): * Tools/TestWebKitAPI/CMakeLists.txt: * Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * Tools/TestWebKitAPI/Tests/WTF/Stopwatch.cpp: Added. (TestWebKitAPI::TEST(WTF_Stopwatch, ResetForgetsTimesFromRunsBeforeTheReset)): (TestWebKitAPI::TEST(WTF_Stopwatch, TimesAfterResetAreRelativeToTheNewStart)): Canonical link: https://commits.webkit.org/324504@main
1 parent 80b83e7 commit 4990218

4 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎Source/WTF/wtf/Stopwatch.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <wtf/MonotonicTime.h>
3030
#include <wtf/Ref.h>
3131
#include <wtf/RefCounted.h>
32+
#include <wtf/Seconds.h>
3233
#include <wtf/Vector.h>
3334

3435
namespace WTF {
@@ -56,13 +57,14 @@ class Stopwatch final : public RefCounted<Stopwatch> {
5657

5758
Seconds m_elapsedTime;
5859
MonotonicTime m_lastStartTime;
59-
Vector<std::pair<MonotonicTime, MonotonicTime>> m_pastInternals;
60+
Vector<std::pair<MonotonicTime, MonotonicTime>> m_pastIntervals;
6061
};
6162

6263
inline void Stopwatch::reset()
6364
{
6465
m_elapsedTime = 0_s;
6566
m_lastStartTime = MonotonicTime::nan();
67+
m_pastIntervals.clear();
6668
}
6769

6870
inline void Stopwatch::start()
@@ -77,7 +79,7 @@ inline void Stopwatch::stop()
7779
ASSERT_WITH_MESSAGE(!m_lastStartTime.isNaN(), "Tried to stop the stopwatch, but it is not running.");
7880

7981
auto stopTime = MonotonicTime::now();
80-
m_pastInternals.append({ m_lastStartTime, stopTime });
82+
m_pastIntervals.append({ m_lastStartTime, stopTime });
8183
m_elapsedTime += stopTime - m_lastStartTime;
8284
m_lastStartTime = MonotonicTime::nan();
8385
}
@@ -110,7 +112,7 @@ inline std::optional<Seconds> Stopwatch::fromMonotonicTime(MonotonicTime timeSta
110112
return Stopwatch::elapsedTimeSince(timeStamp);
111113

112114
Seconds elapsedTime;
113-
for (auto& interval : m_pastInternals) {
115+
for (auto& interval : m_pastIntervals) {
114116
if (timeStamp < interval.first)
115117
return std::nullopt;
116118
if (interval.first <= timeStamp && timeStamp <= interval.second)

‎Tools/TestWebKitAPI/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ set(TestWTF_SOURCES
156156
Tests/WTF/SparseBitVector.cpp
157157
Tests/WTF/StackTraceTest.cpp
158158
Tests/WTF/StdLibExtrasTests.cpp
159+
Tests/WTF/Stopwatch.cpp
159160
Tests/WTF/StringBuilder.cpp
160161
Tests/WTF/StringCommon.cpp
161162
Tests/WTF/StringConcatenate.cpp

‎Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,7 @@
13991399
WTF/SparseBitVector.cpp,
14001400
WTF/StackTraceTest.cpp,
14011401
WTF/StdLibExtrasTests.cpp,
1402+
WTF/Stopwatch.cpp,
14021403
WTF/StringBuilder.cpp,
14031404
WTF/StringCommon.cpp,
14041405
WTF/StringConcatenate.cpp,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (C) 2026 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
* ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23+
* POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#include "config.h"
27+
#include <wtf/Stopwatch.h>
28+
29+
#include <wtf/MonotonicTime.h>
30+
#include <wtf/Seconds.h>
31+
32+
namespace TestWebKitAPI {
33+
34+
TEST(WTF_Stopwatch, ResetForgetsTimesFromRunsBeforeTheReset)
35+
{
36+
auto stopwatch = Stopwatch::create();
37+
stopwatch->start();
38+
WTF::sleep(5_ms);
39+
auto duringFirstRun = MonotonicTime::now();
40+
WTF::sleep(5_ms);
41+
stopwatch->stop();
42+
WTF::sleep(5_ms);
43+
stopwatch->start();
44+
WTF::sleep(5_ms);
45+
auto duringSecondRun = MonotonicTime::now();
46+
WTF::sleep(5_ms);
47+
EXPECT_TRUE(stopwatch->fromMonotonicTime(duringFirstRun));
48+
EXPECT_TRUE(stopwatch->fromMonotonicTime(duringSecondRun));
49+
50+
// Reset while the stopwatch is running.
51+
stopwatch->reset();
52+
EXPECT_FALSE(stopwatch->isActive());
53+
EXPECT_EQ(stopwatch->elapsedTime().value(), 0.0);
54+
EXPECT_FALSE(stopwatch->fromMonotonicTime(duringFirstRun));
55+
EXPECT_FALSE(stopwatch->fromMonotonicTime(duringSecondRun));
56+
57+
// Reset while the stopwatch is stopped.
58+
stopwatch->start();
59+
WTF::sleep(5_ms);
60+
auto duringThirdRun = MonotonicTime::now();
61+
WTF::sleep(5_ms);
62+
stopwatch->stop();
63+
EXPECT_TRUE(stopwatch->fromMonotonicTime(duringThirdRun));
64+
stopwatch->reset();
65+
EXPECT_FALSE(stopwatch->fromMonotonicTime(duringThirdRun));
66+
EXPECT_FALSE(stopwatch->fromMonotonicTime(duringFirstRun));
67+
EXPECT_FALSE(stopwatch->fromMonotonicTime(duringSecondRun));
68+
}
69+
70+
TEST(WTF_Stopwatch, TimesAfterResetAreRelativeToTheNewStart)
71+
{
72+
auto stopwatch = Stopwatch::create();
73+
stopwatch->start();
74+
WTF::sleep(5_ms);
75+
stopwatch->stop();
76+
stopwatch->reset();
77+
WTF::sleep(5_ms);
78+
79+
auto beforeStart = MonotonicTime::now();
80+
stopwatch->start();
81+
auto afterStart = MonotonicTime::now();
82+
WTF::sleep(5_ms);
83+
auto duringRun = MonotonicTime::now();
84+
WTF::sleep(5_ms);
85+
stopwatch->stop();
86+
87+
// The stopwatch started somewhere between beforeStart and afterStart. If the run before the
88+
// reset was still counted, the result would be at least 5 ms above the upper bound.
89+
auto elapsed = stopwatch->fromMonotonicTime(duringRun);
90+
ASSERT_TRUE(elapsed);
91+
EXPECT_TRUE(*elapsed >= duringRun - afterStart);
92+
EXPECT_TRUE(*elapsed <= duringRun - beforeStart);
93+
}
94+
95+
} // namespace TestWebKitAPI

0 commit comments

Comments
 (0)