Coverage Report

Created: 2026-09-17 08:42

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
28.6M
    CustomStopWatch(bool auto_start = false) {
44
28.6M
        _total_time = 0;
45
28.6M
        _running = false;
46
28.6M
        if (auto_start) {
47
563
            start();
48
563
        }
49
28.6M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
43
27.8M
    CustomStopWatch(bool auto_start = false) {
44
27.8M
        _total_time = 0;
45
27.8M
        _running = false;
46
27.8M
        if (auto_start) {
47
529
            start();
48
529
        }
49
27.8M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
43
830k
    CustomStopWatch(bool auto_start = false) {
44
830k
        _total_time = 0;
45
830k
        _running = false;
46
830k
        if (auto_start) {
47
34
            start();
48
34
        }
49
830k
    }
50
51
    timespec start_time() const { return _start; }
52
53
28.6M
    void start() {
54
28.6M
        if (!_running) {
55
28.0M
            _record_start_thread_id();
56
28.0M
            clock_gettime(Clock, &_start);
57
28.0M
            _running = true;
58
28.0M
        }
59
28.6M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
53
27.7M
    void start() {
54
27.7M
        if (!_running) {
55
27.2M
            _record_start_thread_id();
56
27.2M
            clock_gettime(Clock, &_start);
57
27.2M
            _running = true;
58
27.2M
        }
59
27.7M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
53
835k
    void start() {
54
835k
        if (!_running) {
55
830k
            _record_start_thread_id();
56
830k
            clock_gettime(Clock, &_start);
57
830k
            _running = true;
58
830k
        }
59
835k
    }
60
61
8.02M
    void stop() {
62
8.02M
        if (_running) {
63
7.92M
            _total_time += elapsed_time();
64
7.92M
            _running = false;
65
7.92M
        }
66
8.02M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
61
7.52M
    void stop() {
62
7.52M
        if (_running) {
63
7.42M
            _total_time += elapsed_time();
64
7.42M
            _running = false;
65
7.42M
        }
66
7.52M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
61
501k
    void stop() {
62
501k
        if (_running) {
63
501k
            _total_time += elapsed_time();
64
501k
            _running = false;
65
501k
        }
66
501k
    }
67
68
    // Restarts the timer. Returns the elapsed time until this point.
69
1.06M
    int64_t reset() {
70
1.06M
        int64_t ret = elapsed_time();
71
72
1.06M
        if (_running) {
73
852k
            _record_start_thread_id();
74
852k
            clock_gettime(Clock, &_start);
75
852k
        }
76
77
1.06M
        return ret;
78
1.06M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
69
86.0k
    int64_t reset() {
70
86.0k
        int64_t ret = elapsed_time();
71
72
86.0k
        if (_running) {
73
5.27k
            _record_start_thread_id();
74
5.27k
            clock_gettime(Clock, &_start);
75
5.27k
        }
76
77
86.0k
        return ret;
78
86.0k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
69
974k
    int64_t reset() {
70
974k
        int64_t ret = elapsed_time();
71
72
974k
        if (_running) {
73
847k
            _record_start_thread_id();
74
847k
            clock_gettime(Clock, &_start);
75
847k
        }
76
77
974k
        return ret;
78
974k
    }
79
80
    // Returns time in nanosecond.
81
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
82
34.5M
    int64_t elapsed_time() const {
83
34.5M
        if (!_running) {
84
8.10M
            return _total_time;
85
8.10M
        }
86
87
26.4M
        _check_thread_id();
88
26.4M
        timespec end;
89
26.4M
        clock_gettime(Clock, &end);
90
26.4M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
26.4M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
26.4M
        if (end_nanos < start_nanos) {
93
1.16k
            LOG_EVERY_T(WARNING, 1)
94
208
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
1.16k
            return 0;
96
1.16k
        }
97
26.4M
        return end_nanos - start_nanos;
98
26.4M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
82
33.1M
    int64_t elapsed_time() const {
83
33.1M
        if (!_running) {
84
7.52M
            return _total_time;
85
7.52M
        }
86
87
25.6M
        _check_thread_id();
88
25.6M
        timespec end;
89
25.6M
        clock_gettime(Clock, &end);
90
25.6M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
25.6M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
25.6M
        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
25.6M
        return end_nanos - start_nanos;
98
25.6M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
82
1.41M
    int64_t elapsed_time() const {
83
1.41M
        if (!_running) {
84
582k
            return _total_time;
85
582k
        }
86
87
830k
        _check_thread_id();
88
830k
        timespec end;
89
830k
        clock_gettime(Clock, &end);
90
830k
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
830k
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
830k
        if (end_nanos < start_nanos) {
93
1.16k
            LOG_EVERY_T(WARNING, 1)
94
208
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
95
1.16k
            return 0;
96
1.16k
        }
97
829k
        return end_nanos - start_nanos;
98
830k
    }
99
100
    // Return time in microseconds
101
11.9k
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
102
103
    // Return time in milliseconds
104
38.9k
    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
7.58k
    int64_t elapsed_time_seconds(timespec end) const {
109
7.58k
        if (!_running) {
110
0
            return _total_time / 1000L / 1000L / 1000L;
111
0
        }
112
7.58k
        if (end.tv_sec < _start.tv_sec) {
113
1
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
114
1
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
115
1
            LOG_EVERY_T(WARNING, 1)
116
1
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
117
1
            return 0;
118
1
        }
119
7.57k
        return end.tv_sec - _start.tv_sec;
120
7.58k
    }
121
122
private:
123
1.66M
    static std::string _get_thread_id() {
124
1.66M
        std::stringstream ss;
125
1.66M
        ss << std::this_thread::get_id();
126
1.66M
        return ss.str();
127
1.66M
    }
128
28.8M
    void _record_start_thread_id() {
129
28.8M
#ifndef NDEBUG
130
28.8M
        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
835k
            _start_thread_id = _get_thread_id();
134
835k
        }
135
28.8M
#endif
136
28.8M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
128
28.0M
    void _record_start_thread_id() {
129
28.0M
#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
28.0M
#endif
136
28.0M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
128
835k
    void _record_start_thread_id() {
129
835k
#ifndef NDEBUG
130
835k
        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
835k
            _start_thread_id = _get_thread_id();
134
835k
        }
135
835k
#endif
136
835k
    }
137
138
26.5M
    void _check_thread_id() const {
139
26.5M
#ifndef NDEBUG
140
26.5M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
832k
            auto current_thread_id = _get_thread_id();
142
832k
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
2.37k
                             << " but stopped in thread " << current_thread_id;
145
2.37k
            }
146
832k
        }
147
26.5M
#endif
148
26.5M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
138
25.7M
    void _check_thread_id() const {
139
25.7M
#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
25.7M
#endif
148
25.7M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
138
832k
    void _check_thread_id() const {
139
832k
#ifndef NDEBUG
140
832k
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
832k
            auto current_thread_id = _get_thread_id();
142
832k
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
2.37k
                             << " but stopped in thread " << current_thread_id;
145
2.37k
            }
146
832k
        }
147
832k
#endif
148
832k
    }
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