Coverage Report

Created: 2026-09-23 13:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/stopwatch.hpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/stopwatch.hpp
19
// and modified by Doris
20
21
#pragma once
22
23
#include <glog/logging.h>
24
#include <time.h>
25
26
#include <ctime>
27
#include <sstream>
28
#include <thread>
29
30
#include "util/time.h"
31
32
namespace doris {
33
34
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
35
// It is as fast as Rdtsc.
36
// It is also accurate because it not affected by cpu frequency changes and
37
// it is not affected by user setting the system clock.
38
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
39
// It is good for computing elapsed time.
40
template <clockid_t Clock>
41
class CustomStopWatch {
42
public:
43
357M
    CustomStopWatch(bool auto_start = false) {
44
357M
        _total_time = 0;
45
357M
        _running = false;
46
357M
        if (auto_start) {
47
1.36k
            start();
48
1.36k
        }
49
357M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
43
335M
    CustomStopWatch(bool auto_start = false) {
44
335M
        _total_time = 0;
45
335M
        _running = false;
46
335M
        if (auto_start) {
47
1.13k
            start();
48
1.13k
        }
49
335M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
43
22.7M
    CustomStopWatch(bool auto_start = false) {
44
22.7M
        _total_time = 0;
45
22.7M
        _running = false;
46
22.7M
        if (auto_start) {
47
227
            start();
48
227
        }
49
22.7M
    }
50
51
    timespec start_time() const { return _start; }
52
53
368M
    void start() {
54
368M
        if (!_running) {
55
353M
            _record_start_thread_id();
56
353M
            clock_gettime(Clock, &_start);
57
353M
            _running = true;
58
353M
        }
59
368M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
53
345M
    void start() {
54
345M
        if (!_running) {
55
331M
            _record_start_thread_id();
56
331M
            clock_gettime(Clock, &_start);
57
331M
            _running = true;
58
331M
        }
59
345M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
53
22.7M
    void start() {
54
22.7M
        if (!_running) {
55
22.6M
            _record_start_thread_id();
56
22.6M
            clock_gettime(Clock, &_start);
57
22.6M
            _running = true;
58
22.6M
        }
59
22.7M
    }
60
61
167M
    void stop() {
62
167M
        if (_running) {
63
167M
            _total_time += elapsed_time();
64
167M
            _running = false;
65
167M
        }
66
167M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
61
153M
    void stop() {
62
153M
        if (_running) {
63
153M
            _total_time += elapsed_time();
64
153M
            _running = false;
65
153M
        }
66
153M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
61
14.3M
    void stop() {
62
14.3M
        if (_running) {
63
14.3M
            _total_time += elapsed_time();
64
14.3M
            _running = false;
65
14.3M
        }
66
14.3M
    }
67
68
    // Restarts the timer. Returns the elapsed time until this point.
69
17.6M
    int64_t reset() {
70
17.6M
        int64_t ret = elapsed_time();
71
72
17.6M
        if (_running) {
73
14.0M
            _record_start_thread_id();
74
14.0M
            clock_gettime(Clock, &_start);
75
14.0M
        }
76
77
17.6M
        return ret;
78
17.6M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
69
955k
    int64_t reset() {
70
955k
        int64_t ret = elapsed_time();
71
72
955k
        if (_running) {
73
55.1k
            _record_start_thread_id();
74
55.1k
            clock_gettime(Clock, &_start);
75
55.1k
        }
76
77
955k
        return ret;
78
955k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
69
16.6M
    int64_t reset() {
70
16.6M
        int64_t ret = elapsed_time();
71
72
16.6M
        if (_running) {
73
13.9M
            _record_start_thread_id();
74
13.9M
            clock_gettime(Clock, &_start);
75
13.9M
        }
76
77
16.6M
        return ret;
78
16.6M
    }
79
80
    // Returns time in nanosecond.
81
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
82
530M
    int64_t elapsed_time() const {
83
530M
        if (!_running) {
84
197M
            return _total_time;
85
197M
        }
86
87
332M
        _check_thread_id();
88
332M
        timespec end;
89
332M
        clock_gettime(Clock, &end);
90
332M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
332M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
332M
        if (end_nanos < start_nanos) {
93
9.22k
            LOG_EVERY_T(WARNING, 1)
94
983
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
9.22k
            return 0;
96
9.22k
        }
97
332M
        return end_nanos - start_nanos;
98
332M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
82
493M
    int64_t elapsed_time() const {
83
493M
        if (!_running) {
84
182M
            return _total_time;
85
182M
        }
86
87
311M
        _check_thread_id();
88
311M
        timespec end;
89
311M
        clock_gettime(Clock, &end);
90
311M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
311M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
311M
        if (end_nanos < start_nanos) {
93
0
            LOG_EVERY_T(WARNING, 1)
94
0
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
0
            return 0;
96
0
        }
97
311M
        return end_nanos - start_nanos;
98
311M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
82
36.7M
    int64_t elapsed_time() const {
83
36.7M
        if (!_running) {
84
15.1M
            return _total_time;
85
15.1M
        }
86
87
21.5M
        _check_thread_id();
88
21.5M
        timespec end;
89
21.5M
        clock_gettime(Clock, &end);
90
21.5M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
21.5M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
21.5M
        if (end_nanos < start_nanos) {
93
9.22k
            LOG_EVERY_T(WARNING, 1)
94
983
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
9.22k
            return 0;
96
9.22k
        }
97
21.5M
        return end_nanos - start_nanos;
98
21.5M
    }
99
100
    // Return time in microseconds
101
241k
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
102
103
    // Return time in milliseconds
104
299k
    int64_t elapsed_time_milliseconds() const { return elapsed_time() / 1000 / 1000; }
105
106
    // Returns time in seconds.
107
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
108
30.1k
    int64_t elapsed_time_seconds(timespec end) const {
109
30.1k
        if (!_running) {
110
0
            return _total_time / 1000L / 1000L / 1000L;
111
0
        }
112
30.1k
        if (end.tv_sec < _start.tv_sec) {
113
11
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
114
11
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
115
11
            LOG_EVERY_T(WARNING, 1)
116
3
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
117
11
            return 0;
118
11
        }
119
30.1k
        return end.tv_sec - _start.tv_sec;
120
30.1k
    }
121
122
private:
123
44.1M
    static std::string _get_thread_id() {
124
44.1M
        std::stringstream ss;
125
44.1M
        ss << std::this_thread::get_id();
126
44.1M
        return ss.str();
127
44.1M
    }
128
366M
    void _record_start_thread_id() {
129
366M
#ifndef NDEBUG
130
366M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
131
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
132
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
133
22.7M
            _start_thread_id = _get_thread_id();
134
22.7M
        }
135
366M
#endif
136
366M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
128
343M
    void _record_start_thread_id() {
129
343M
#ifndef NDEBUG
130
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
131
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
132
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
133
            _start_thread_id = _get_thread_id();
134
        }
135
343M
#endif
136
343M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
128
22.7M
    void _record_start_thread_id() {
129
22.7M
#ifndef NDEBUG
130
22.7M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
131
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
132
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
133
22.7M
            _start_thread_id = _get_thread_id();
134
22.7M
        }
135
22.7M
#endif
136
22.7M
    }
137
138
354M
    void _check_thread_id() const {
139
354M
#ifndef NDEBUG
140
354M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
21.7M
            auto current_thread_id = _get_thread_id();
142
21.7M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
18.6k
                             << " but stopped in thread " << current_thread_id;
145
18.6k
            }
146
21.7M
        }
147
354M
#endif
148
354M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
138
333M
    void _check_thread_id() const {
139
333M
#ifndef NDEBUG
140
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
            auto current_thread_id = _get_thread_id();
142
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
                             << " but stopped in thread " << current_thread_id;
145
            }
146
        }
147
333M
#endif
148
333M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
138
21.7M
    void _check_thread_id() const {
139
21.7M
#ifndef NDEBUG
140
21.7M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
21.7M
            auto current_thread_id = _get_thread_id();
142
21.7M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
18.6k
                             << " but stopped in thread " << current_thread_id;
145
18.6k
            }
146
21.7M
        }
147
21.7M
#endif
148
21.7M
    }
149
150
    timespec _start;
151
    int64_t _total_time; // in nanosec
152
    bool _running;
153
#ifndef NDEBUG
154
    std::string _start_thread_id;
155
#endif
156
};
157
158
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
159
// It is as fast as Rdtsc.
160
// It is also accurate because it not affected by cpu frequency changes and
161
// it is not affected by user setting the system clock.
162
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
163
// It is good for computing elapsed time.
164
using MonotonicStopWatch = CustomStopWatch<CLOCK_MONOTONIC>;
165
166
// Stop watch for reporting elapsed nanosec based on CLOCK_THREAD_CPUTIME_ID.
167
using ThreadCpuStopWatch = CustomStopWatch<CLOCK_THREAD_CPUTIME_ID>;
168
169
} // namespace doris