Coverage Report

Created: 2026-07-08 09:59

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
330M
    CustomStopWatch(bool auto_start = false) {
44
330M
        _total_time = 0;
45
330M
        _running = false;
46
330M
        if (auto_start) {
47
1.13k
            start();
48
1.13k
        }
49
330M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
43
308M
    CustomStopWatch(bool auto_start = false) {
44
308M
        _total_time = 0;
45
308M
        _running = false;
46
308M
        if (auto_start) {
47
1.13k
            start();
48
1.13k
        }
49
308M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
43
21.2M
    CustomStopWatch(bool auto_start = false) {
44
21.2M
        _total_time = 0;
45
21.2M
        _running = false;
46
21.2M
        if (auto_start) {
47
0
            start();
48
0
        }
49
21.2M
    }
50
51
    timespec start_time() const { return _start; }
52
53
343M
    void start() {
54
343M
        if (!_running) {
55
330M
            _record_start_thread_id();
56
330M
            clock_gettime(Clock, &_start);
57
330M
            _running = true;
58
330M
        }
59
343M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
53
322M
    void start() {
54
322M
        if (!_running) {
55
309M
            _record_start_thread_id();
56
309M
            clock_gettime(Clock, &_start);
57
309M
            _running = true;
58
309M
        }
59
322M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
53
21.3M
    void start() {
54
21.3M
        if (!_running) {
55
21.2M
            _record_start_thread_id();
56
21.2M
            clock_gettime(Clock, &_start);
57
21.2M
            _running = true;
58
21.2M
        }
59
21.3M
    }
60
61
113M
    void stop() {
62
113M
        if (_running) {
63
112M
            _total_time += elapsed_time();
64
112M
            _running = false;
65
112M
        }
66
113M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
61
101M
    void stop() {
62
101M
        if (_running) {
63
99.7M
            _total_time += elapsed_time();
64
99.7M
            _running = false;
65
99.7M
        }
66
101M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
61
12.6M
    void stop() {
62
12.6M
        if (_running) {
63
12.6M
            _total_time += elapsed_time();
64
12.6M
            _running = false;
65
12.6M
        }
66
12.6M
    }
67
68
    // Restarts the timer. Returns the elapsed time until this point.
69
16.2M
    int64_t reset() {
70
16.2M
        int64_t ret = elapsed_time();
71
72
16.2M
        if (_running) {
73
12.8M
            _record_start_thread_id();
74
12.8M
            clock_gettime(Clock, &_start);
75
12.8M
        }
76
77
16.2M
        return ret;
78
16.2M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
69
958k
    int64_t reset() {
70
958k
        int64_t ret = elapsed_time();
71
72
958k
        if (_running) {
73
51.8k
            _record_start_thread_id();
74
51.8k
            clock_gettime(Clock, &_start);
75
51.8k
        }
76
77
958k
        return ret;
78
958k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
69
15.3M
    int64_t reset() {
70
15.3M
        int64_t ret = elapsed_time();
71
72
15.3M
        if (_running) {
73
12.8M
            _record_start_thread_id();
74
12.8M
            clock_gettime(Clock, &_start);
75
12.8M
        }
76
77
15.3M
        return ret;
78
15.3M
    }
79
80
    // Returns time in nanosecond.
81
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
82
424M
    int64_t elapsed_time() const {
83
424M
        if (!_running) {
84
114M
            return _total_time;
85
114M
        }
86
87
309M
        _check_thread_id();
88
309M
        timespec end;
89
309M
        clock_gettime(Clock, &end);
90
309M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
309M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
309M
        if (end_nanos < start_nanos) {
93
10.5k
            LOG_EVERY_T(WARNING, 1)
94
1.41k
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
10.5k
            return 0;
96
10.5k
        }
97
309M
        return end_nanos - start_nanos;
98
309M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
82
391M
    int64_t elapsed_time() const {
83
391M
        if (!_running) {
84
101M
            return _total_time;
85
101M
        }
86
87
290M
        _check_thread_id();
88
290M
        timespec end;
89
290M
        clock_gettime(Clock, &end);
90
290M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
290M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
290M
        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
290M
        return end_nanos - start_nanos;
98
290M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
82
32.9M
    int64_t elapsed_time() const {
83
32.9M
        if (!_running) {
84
13.5M
            return _total_time;
85
13.5M
        }
86
87
19.3M
        _check_thread_id();
88
19.3M
        timespec end;
89
19.3M
        clock_gettime(Clock, &end);
90
19.3M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
19.3M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
19.3M
        if (end_nanos < start_nanos) {
93
10.5k
            LOG_EVERY_T(WARNING, 1)
94
1.41k
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
10.5k
            return 0;
96
10.5k
        }
97
19.3M
        return end_nanos - start_nanos;
98
19.3M
    }
99
100
    // Return time in microseconds
101
253k
    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
50.5k
    int64_t elapsed_time_seconds(timespec end) const {
109
50.5k
        if (!_running) {
110
0
            return _total_time / 1000L / 1000L / 1000L;
111
0
        }
112
50.5k
        if (end.tv_sec < _start.tv_sec) {
113
28
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
114
28
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
115
28
            LOG_EVERY_T(WARNING, 1)
116
8
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
117
28
            return 0;
118
28
        }
119
50.4k
        return end.tv_sec - _start.tv_sec;
120
50.5k
    }
121
122
private:
123
40.6M
    static std::string _get_thread_id() {
124
40.6M
        std::stringstream ss;
125
40.6M
        ss << std::this_thread::get_id();
126
40.6M
        return ss.str();
127
40.6M
    }
128
342M
    void _record_start_thread_id() {
129
342M
#ifndef NDEBUG
130
342M
        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
21.3M
            _start_thread_id = _get_thread_id();
134
21.3M
        }
135
342M
#endif
136
342M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
128
320M
    void _record_start_thread_id() {
129
320M
#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
320M
#endif
136
320M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
128
21.3M
    void _record_start_thread_id() {
129
21.3M
#ifndef NDEBUG
130
21.3M
        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
21.3M
            _start_thread_id = _get_thread_id();
134
21.3M
        }
135
21.3M
#endif
136
21.3M
    }
137
138
314M
    void _check_thread_id() const {
139
314M
#ifndef NDEBUG
140
314M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
19.5M
            auto current_thread_id = _get_thread_id();
142
19.5M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
21.1k
                             << " but stopped in thread " << current_thread_id;
145
21.1k
            }
146
19.5M
        }
147
314M
#endif
148
314M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
138
295M
    void _check_thread_id() const {
139
295M
#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
295M
#endif
148
295M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
138
19.5M
    void _check_thread_id() const {
139
19.5M
#ifndef NDEBUG
140
19.5M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
19.5M
            auto current_thread_id = _get_thread_id();
142
19.5M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
21.1k
                             << " but stopped in thread " << current_thread_id;
145
21.1k
            }
146
19.5M
        }
147
19.5M
#endif
148
19.5M
    }
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