]> git.ipfire.org Git - thirdparty/squid.git/blame - src/base/Stopwatch.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / base / Stopwatch.cc
CommitLineData
0cd2a1b6 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
0cd2a1b6
AR
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "base/Stopwatch.h"
11#include "debug/Stream.h"
12
13static_assert(Stopwatch::Clock::is_steady,
14 "Stopwatch::Clock is suitable for measuring real-time intervals");
15
16Stopwatch::Stopwatch():
17 subtotal_(Clock::duration::zero())
18{
19}
20
21Stopwatch::Clock::duration
22Stopwatch::total() const
23{
24 auto result = subtotal_;
25 if (running())
26 result += Clock::now() - runStart_;
27 return result;
28}
29
30void
31Stopwatch::resume()
32{
33 if (!running()) {
34 runStart_ = Clock::now();
35 debugs(1, 7, "period " << resumes_<< " started after " << subtotal_.count());
36 }
37 ++resumes_;
38}
39
40/// ends the current measurement period if needed; requires prior resume()
41void Stopwatch::pause()
42{
43 ++pauses_;
44 if (!running()) {
45 const auto runtime = Clock::now() - runStart_;
46 subtotal_ += runtime;
47 debugs(1, 7, "period " << resumes_ << " ran for " << runtime.count());
48 }
49}
50