Coverage Report

Created: 2026-08-18 15:30

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
278M
    CustomStopWatch(bool auto_start = false) {
44
278M
        _total_time = 0;
45
278M
        _running = false;
46
278M
        if (auto_start) {
47
1.43k
            start();
48
1.43k
        }
49
278M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
43
261M
    CustomStopWatch(bool auto_start = false) {
44
261M
        _total_time = 0;
45
261M
        _running = false;
46
261M
        if (auto_start) {
47
1.12k
            start();
48
1.12k
        }
49
261M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
43
17.7M
    CustomStopWatch(bool auto_start = false) {
44
17.7M
        _total_time = 0;
45
17.7M
        _running = false;
46
17.7M
        if (auto_start) {
47
311
            start();
48
311
        }
49
17.7M
    }
50
51
    timespec start_time() const { return _start; }
52
53
288M
    void start() {
54
288M
        if (!_running) {
55
277M
            _record_start_thread_id();
56
277M
            clock_gettime(Clock, &_start);
57
277M
            _running = true;
58
277M
        }
59
288M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
53
271M
    void start() {
54
271M
        if (!_running) {
55
259M
            _record_start_thread_id();
56
259M
            clock_gettime(Clock, &_start);
57
259M
            _running = true;
58
259M
        }
59
271M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
53
17.7M
    void start() {
54
17.7M
        if (!_running) {
55
17.7M
            _record_start_thread_id();
56
17.7M
            clock_gettime(Clock, &_start);
57
17.7M
            _running = true;
58
17.7M
        }
59
17.7M
    }
60
61
115M
    void stop() {
62
115M
        if (_running) {
63
114M
            _total_time += elapsed_time();
64
114M
            _running = false;
65
114M
        }
66
115M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
61
103M
    void stop() {
62
103M
        if (_running) {
63
103M
            _total_time += elapsed_time();
64
103M
            _running = false;
65
103M
        }
66
103M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
61
11.2M
    void stop() {
62
11.2M
        if (_running) {
63
11.2M
            _total_time += elapsed_time();
64
11.2M
            _running = false;
65
11.2M
        }
66
11.2M
    }
67
68
    // Restarts the timer. Returns the elapsed time until this point.
69
14.5M
    int64_t reset() {
70
14.5M
        int64_t ret = elapsed_time();
71
72
14.5M
        if (_running) {
73
11.4M
            _record_start_thread_id();
74
11.4M
            clock_gettime(Clock, &_start);
75
11.4M
        }
76
77
14.5M
        return ret;
78
14.5M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
69
934k
    int64_t reset() {
70
934k
        int64_t ret = elapsed_time();
71
72
934k
        if (_running) {
73
52.4k
            _record_start_thread_id();
74
52.4k
            clock_gettime(Clock, &_start);
75
52.4k
        }
76
77
934k
        return ret;
78
934k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
69
13.5M
    int64_t reset() {
70
13.5M
        int64_t ret = elapsed_time();
71
72
13.5M
        if (_running) {
73
11.3M
            _record_start_thread_id();
74
11.3M
            clock_gettime(Clock, &_start);
75
11.3M
        }
76
77
13.5M
        return ret;
78
13.5M
    }
79
80
    // Returns time in nanosecond.
81
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
82
393M
    int64_t elapsed_time() const {
83
393M
        if (!_running) {
84
120M
            return _total_time;
85
120M
        }
86
87
272M
        _check_thread_id();
88
272M
        timespec end;
89
272M
        clock_gettime(Clock, &end);
90
272M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
272M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
272M
        if (end_nanos < start_nanos) {
93
9.01k
            LOG_EVERY_T(WARNING, 1)
94
971
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
9.01k
            return 0;
96
9.01k
        }
97
272M
        return end_nanos - start_nanos;
98
272M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
82
364M
    int64_t elapsed_time() const {
83
364M
        if (!_running) {
84
108M
            return _total_time;
85
108M
        }
86
87
255M
        _check_thread_id();
88
255M
        timespec end;
89
255M
        clock_gettime(Clock, &end);
90
255M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
255M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
255M
        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
255M
        return end_nanos - start_nanos;
98
255M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
82
29.2M
    int64_t elapsed_time() const {
83
29.2M
        if (!_running) {
84
12.0M
            return _total_time;
85
12.0M
        }
86
87
17.1M
        _check_thread_id();
88
17.1M
        timespec end;
89
17.1M
        clock_gettime(Clock, &end);
90
17.1M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
17.1M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
17.1M
        if (end_nanos < start_nanos) {
93
9.01k
            LOG_EVERY_T(WARNING, 1)
94
971
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
9.01k
            return 0;
96
9.01k
        }
97
17.1M
        return end_nanos - start_nanos;
98
17.1M
    }
99
100
    // Return time in microseconds
101
224k
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
102
103
    // Return time in milliseconds
104
280k
    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
32.1k
    int64_t elapsed_time_seconds(timespec end) const {
109
32.1k
        if (!_running) {
110
0
            return _total_time / 1000L / 1000L / 1000L;
111
0
        }
112
32.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
4
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
117
11
            return 0;
118
11
        }
119
32.1k
        return end.tv_sec - _start.tv_sec;
120
32.1k
    }
121
122
private:
123
34.8M
    static std::string _get_thread_id() {
124
34.8M
        std::stringstream ss;
125
34.8M
        ss << std::this_thread::get_id();
126
34.8M
        return ss.str();
127
34.8M
    }
128
288M
    void _record_start_thread_id() {
129
288M
#ifndef NDEBUG
130
288M
        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
17.7M
            _start_thread_id = _get_thread_id();
134
17.7M
        }
135
288M
#endif
136
288M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
128
270M
    void _record_start_thread_id() {
129
270M
#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
270M
#endif
136
270M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
128
17.7M
    void _record_start_thread_id() {
129
17.7M
#ifndef NDEBUG
130
17.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
17.7M
            _start_thread_id = _get_thread_id();
134
17.7M
        }
135
17.7M
#endif
136
17.7M
    }
137
138
280M
    void _check_thread_id() const {
139
280M
#ifndef NDEBUG
140
280M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
17.2M
            auto current_thread_id = _get_thread_id();
142
17.2M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
18.0k
                             << " but stopped in thread " << current_thread_id;
145
18.0k
            }
146
17.2M
        }
147
280M
#endif
148
280M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
138
262M
    void _check_thread_id() const {
139
262M
#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
262M
#endif
148
262M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
138
17.2M
    void _check_thread_id() const {
139
17.2M
#ifndef NDEBUG
140
17.2M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
17.2M
            auto current_thread_id = _get_thread_id();
142
17.2M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
18.0k
                             << " but stopped in thread " << current_thread_id;
145
18.0k
            }
146
17.2M
        }
147
17.2M
#endif
148
17.2M
    }
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