Coverage Report

Created: 2026-10-30 09:01

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
414M
    CustomStopWatch(bool auto_start = false) {
44
414M
        _total_time = 0;
45
414M
        _running = false;
46
414M
        if (auto_start) {
47
1.35k
            start();
48
1.35k
        }
49
414M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
43
389M
    CustomStopWatch(bool auto_start = false) {
44
389M
        _total_time = 0;
45
389M
        _running = false;
46
389M
        if (auto_start) {
47
1.14k
            start();
48
1.14k
        }
49
389M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
43
24.4M
    CustomStopWatch(bool auto_start = false) {
44
24.4M
        _total_time = 0;
45
24.4M
        _running = false;
46
24.4M
        if (auto_start) {
47
202
            start();
48
202
        }
49
24.4M
    }
50
51
    timespec start_time() const { return _start; }
52
53
424M
    void start() {
54
424M
        if (!_running) {
55
409M
            _record_start_thread_id();
56
409M
            clock_gettime(Clock, &_start);
57
409M
            _running = true;
58
409M
        }
59
424M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
53
400M
    void start() {
54
400M
        if (!_running) {
55
385M
            _record_start_thread_id();
56
385M
            clock_gettime(Clock, &_start);
57
385M
            _running = true;
58
385M
        }
59
400M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
53
24.4M
    void start() {
54
24.4M
        if (!_running) {
55
24.3M
            _record_start_thread_id();
56
24.3M
            clock_gettime(Clock, &_start);
57
24.3M
            _running = true;
58
24.3M
        }
59
24.4M
    }
60
61
217M
    void stop() {
62
218M
        if (_running) {
63
218M
            _total_time += elapsed_time();
64
218M
            _running = false;
65
218M
        }
66
217M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
61
202M
    void stop() {
62
202M
        if (_running) {
63
202M
            _total_time += elapsed_time();
64
202M
            _running = false;
65
202M
        }
66
202M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
61
15.4M
    void stop() {
62
15.4M
        if (_running) {
63
15.4M
            _total_time += elapsed_time();
64
15.4M
            _running = false;
65
15.4M
        }
66
15.4M
    }
67
68
    // Restarts the timer. Returns the elapsed time until this point.
69
18.8M
    int64_t reset() {
70
18.8M
        int64_t ret = elapsed_time();
71
72
18.8M
        if (_running) {
73
14.9M
            _record_start_thread_id();
74
14.9M
            clock_gettime(Clock, &_start);
75
14.9M
        }
76
77
18.8M
        return ret;
78
18.8M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
69
987k
    int64_t reset() {
70
987k
        int64_t ret = elapsed_time();
71
72
987k
        if (_running) {
73
63.0k
            _record_start_thread_id();
74
63.0k
            clock_gettime(Clock, &_start);
75
63.0k
        }
76
77
987k
        return ret;
78
987k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
69
17.8M
    int64_t reset() {
70
17.8M
        int64_t ret = elapsed_time();
71
72
17.8M
        if (_running) {
73
14.9M
            _record_start_thread_id();
74
14.9M
            clock_gettime(Clock, &_start);
75
14.9M
        }
76
77
17.8M
        return ret;
78
17.8M
    }
79
80
    // Returns time in nanosecond.
81
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
82
635M
    int64_t elapsed_time() const {
83
635M
        if (!_running) {
84
248M
            return _total_time;
85
248M
        }
86
87
386M
        _check_thread_id();
88
386M
        timespec end;
89
386M
        clock_gettime(Clock, &end);
90
386M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
386M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
386M
        if (end_nanos < start_nanos) {
93
15.3k
            LOG_EVERY_T(WARNING, 1)
94
1.21k
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
15.3k
            return 0;
96
15.3k
        }
97
386M
        return end_nanos - start_nanos;
98
386M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
82
595M
    int64_t elapsed_time() const {
83
595M
        if (!_running) {
84
232M
            return _total_time;
85
232M
        }
86
87
363M
        _check_thread_id();
88
363M
        timespec end;
89
363M
        clock_gettime(Clock, &end);
90
363M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
363M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
363M
        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
363M
        return end_nanos - start_nanos;
98
363M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
82
39.4M
    int64_t elapsed_time() const {
83
39.4M
        if (!_running) {
84
16.3M
            return _total_time;
85
16.3M
        }
86
87
23.1M
        _check_thread_id();
88
23.1M
        timespec end;
89
23.1M
        clock_gettime(Clock, &end);
90
23.1M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
23.1M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
23.1M
        if (end_nanos < start_nanos) {
93
15.3k
            LOG_EVERY_T(WARNING, 1)
94
1.21k
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
15.3k
            return 0;
96
15.3k
        }
97
23.0M
        return end_nanos - start_nanos;
98
23.1M
    }
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
365k
    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
39.1k
    int64_t elapsed_time_seconds(timespec end) const {
109
39.1k
        if (!_running) {
110
0
            return _total_time / 1000L / 1000L / 1000L;
111
0
        }
112
39.1k
        if (end.tv_sec < _start.tv_sec) {
113
62
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
114
62
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
115
62
            LOG_EVERY_T(WARNING, 1)
116
11
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
117
62
            return 0;
118
62
        }
119
39.0k
        return end.tv_sec - _start.tv_sec;
120
39.1k
    }
121
122
private:
123
47.4M
    static std::string _get_thread_id() {
124
47.4M
        std::stringstream ss;
125
47.4M
        ss << std::this_thread::get_id();
126
47.4M
        return ss.str();
127
47.4M
    }
128
423M
    void _record_start_thread_id() {
129
423M
#ifndef NDEBUG
130
423M
        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
24.4M
            _start_thread_id = _get_thread_id();
134
24.4M
        }
135
423M
#endif
136
423M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
128
399M
    void _record_start_thread_id() {
129
399M
#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
399M
#endif
136
399M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
128
24.4M
    void _record_start_thread_id() {
129
24.4M
#ifndef NDEBUG
130
24.4M
        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
24.4M
            _start_thread_id = _get_thread_id();
134
24.4M
        }
135
24.4M
#endif
136
24.4M
    }
137
138
411M
    void _check_thread_id() const {
139
411M
#ifndef NDEBUG
140
411M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
23.3M
            auto current_thread_id = _get_thread_id();
142
23.3M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
30.7k
                             << " but stopped in thread " << current_thread_id;
145
30.7k
            }
146
23.3M
        }
147
411M
#endif
148
411M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
138
388M
    void _check_thread_id() const {
139
388M
#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
388M
#endif
148
388M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
138
23.3M
    void _check_thread_id() const {
139
23.3M
#ifndef NDEBUG
140
23.3M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
23.3M
            auto current_thread_id = _get_thread_id();
142
23.3M
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
30.7k
                             << " but stopped in thread " << current_thread_id;
145
30.7k
            }
146
23.3M
        }
147
23.3M
#endif
148
23.3M
    }
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