Coverage Report

Created: 2026-05-21 08:31

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 <time.h>
24
25
#include <ctime>
26
#include <sstream>
27
#include <thread>
28
29
#include "util/time.h"
30
31
namespace doris {
32
33
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
34
// It is as fast as Rdtsc.
35
// It is also accurate because it not affected by cpu frequency changes and
36
// it is not affected by user setting the system clock.
37
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
38
// It is good for computing elapsed time.
39
template <clockid_t Clock>
40
class CustomStopWatch {
41
public:
42
369M
    CustomStopWatch(bool auto_start = false) {
43
369M
        _total_time = 0;
44
369M
        _running = false;
45
369M
        if (auto_start) {
46
2.92k
            start();
47
2.92k
        }
48
369M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
42
341M
    CustomStopWatch(bool auto_start = false) {
43
341M
        _total_time = 0;
44
341M
        _running = false;
45
341M
        if (auto_start) {
46
2.92k
            start();
47
2.92k
        }
48
341M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
42
28.4M
    CustomStopWatch(bool auto_start = false) {
43
28.4M
        _total_time = 0;
44
28.4M
        _running = false;
45
28.4M
        if (auto_start) {
46
0
            start();
47
0
        }
48
28.4M
    }
49
50
    timespec start_time() const { return _start; }
51
52
389M
    void start() {
53
389M
        if (!_running) {
54
371M
            _record_start_thread_id();
55
371M
            clock_gettime(Clock, &_start);
56
371M
            _running = true;
57
371M
        }
58
389M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
52
360M
    void start() {
53
360M
        if (!_running) {
54
343M
            _record_start_thread_id();
55
343M
            clock_gettime(Clock, &_start);
56
343M
            _running = true;
57
343M
        }
58
360M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
52
28.5M
    void start() {
53
28.5M
        if (!_running) {
54
28.5M
            _record_start_thread_id();
55
28.5M
            clock_gettime(Clock, &_start);
56
28.5M
            _running = true;
57
28.5M
        }
58
28.5M
    }
59
60
144M
    void stop() {
61
144M
        if (_running) {
62
142M
            _total_time += elapsed_time();
63
142M
            _running = false;
64
142M
        }
65
144M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
60
127M
    void stop() {
61
127M
        if (_running) {
62
125M
            _total_time += elapsed_time();
63
125M
            _running = false;
64
125M
        }
65
127M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
60
17.0M
    void stop() {
61
17.0M
        if (_running) {
62
17.0M
            _total_time += elapsed_time();
63
17.0M
            _running = false;
64
17.0M
        }
65
17.0M
    }
66
67
    // Restarts the timer. Returns the elapsed time until this point.
68
22.1M
    int64_t reset() {
69
22.1M
        int64_t ret = elapsed_time();
70
71
22.1M
        if (_running) {
72
17.3M
            _record_start_thread_id();
73
17.3M
            clock_gettime(Clock, &_start);
74
17.3M
        }
75
76
22.1M
        return ret;
77
22.1M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
68
1.41M
    int64_t reset() {
69
1.41M
        int64_t ret = elapsed_time();
70
71
1.41M
        if (_running) {
72
102k
            _record_start_thread_id();
73
102k
            clock_gettime(Clock, &_start);
74
102k
        }
75
76
1.41M
        return ret;
77
1.41M
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
68
20.7M
    int64_t reset() {
69
20.7M
        int64_t ret = elapsed_time();
70
71
20.7M
        if (_running) {
72
17.2M
            _record_start_thread_id();
73
17.2M
            clock_gettime(Clock, &_start);
74
17.2M
        }
75
76
20.7M
        return ret;
77
20.7M
    }
78
79
    // Returns time in nanosecond.
80
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
81
515M
    int64_t elapsed_time() const {
82
515M
        if (!_running) {
83
143M
            return _total_time;
84
143M
        }
85
86
371M
        _check_thread_id();
87
371M
        timespec end;
88
371M
        clock_gettime(Clock, &end);
89
371M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
371M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
371M
        if (end_nanos < start_nanos) {
92
17.9k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
17.9k
            return 0;
94
17.9k
        }
95
371M
        return end_nanos - start_nanos;
96
371M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
81
470M
    int64_t elapsed_time() const {
82
470M
        if (!_running) {
83
125M
            return _total_time;
84
125M
        }
85
86
345M
        _check_thread_id();
87
345M
        timespec end;
88
345M
        clock_gettime(Clock, &end);
89
345M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
345M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
345M
        if (end_nanos < start_nanos) {
92
0
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
0
            return 0;
94
0
        }
95
345M
        return end_nanos - start_nanos;
96
345M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
81
44.7M
    int64_t elapsed_time() const {
82
44.7M
        if (!_running) {
83
18.3M
            return _total_time;
84
18.3M
        }
85
86
26.3M
        _check_thread_id();
87
26.3M
        timespec end;
88
26.3M
        clock_gettime(Clock, &end);
89
26.3M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
26.3M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
26.3M
        if (end_nanos < start_nanos) {
92
17.9k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
17.9k
            return 0;
94
17.9k
        }
95
26.3M
        return end_nanos - start_nanos;
96
26.3M
    }
97
98
    // Return time in microseconds
99
239k
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
100
101
    // Return time in milliseconds
102
354k
    int64_t elapsed_time_milliseconds() const { return elapsed_time() / 1000 / 1000; }
103
104
    // Returns time in seconds.
105
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
106
63.3k
    int64_t elapsed_time_seconds(timespec end) const {
107
63.3k
        if (!_running) {
108
0
            return _total_time / 1000L / 1000L / 1000L;
109
0
        }
110
63.3k
        if (end.tv_sec < _start.tv_sec) {
111
34
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
112
34
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
113
34
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
114
34
            return 0;
115
34
        }
116
63.3k
        return end.tv_sec - _start.tv_sec;
117
63.3k
    }
118
119
private:
120
54.8M
    static std::string _get_thread_id() {
121
54.8M
        std::stringstream ss;
122
54.8M
        ss << std::this_thread::get_id();
123
54.8M
        return ss.str();
124
54.8M
    }
125
388M
    void _record_start_thread_id() {
126
388M
#ifndef NDEBUG
127
388M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
128
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
129
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
130
28.5M
            _start_thread_id = _get_thread_id();
131
28.5M
        }
132
388M
#endif
133
388M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
125
359M
    void _record_start_thread_id() {
126
359M
#ifndef NDEBUG
127
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
128
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
129
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
130
            _start_thread_id = _get_thread_id();
131
        }
132
359M
#endif
133
359M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
125
28.5M
    void _record_start_thread_id() {
126
28.5M
#ifndef NDEBUG
127
28.5M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
128
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
129
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
130
28.5M
            _start_thread_id = _get_thread_id();
131
28.5M
        }
132
28.5M
#endif
133
28.5M
    }
134
135
377M
    void _check_thread_id() const {
136
377M
#ifndef NDEBUG
137
377M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
26.5M
            auto current_thread_id = _get_thread_id();
139
26.5M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
36.0k
                             << " but stopped in thread " << current_thread_id;
142
36.0k
            }
143
26.5M
        }
144
377M
#endif
145
377M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
135
351M
    void _check_thread_id() const {
136
351M
#ifndef NDEBUG
137
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
            auto current_thread_id = _get_thread_id();
139
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
                             << " but stopped in thread " << current_thread_id;
142
            }
143
        }
144
351M
#endif
145
351M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
135
26.5M
    void _check_thread_id() const {
136
26.5M
#ifndef NDEBUG
137
26.5M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
26.5M
            auto current_thread_id = _get_thread_id();
139
26.5M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
36.0k
                             << " but stopped in thread " << current_thread_id;
142
36.0k
            }
143
26.5M
        }
144
26.5M
#endif
145
26.5M
    }
146
147
    timespec _start;
148
    int64_t _total_time; // in nanosec
149
    bool _running;
150
#ifndef NDEBUG
151
    std::string _start_thread_id;
152
#endif
153
};
154
155
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
156
// It is as fast as Rdtsc.
157
// It is also accurate because it not affected by cpu frequency changes and
158
// it is not affected by user setting the system clock.
159
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
160
// It is good for computing elapsed time.
161
using MonotonicStopWatch = CustomStopWatch<CLOCK_MONOTONIC>;
162
163
// Stop watch for reporting elapsed nanosec based on CLOCK_THREAD_CPUTIME_ID.
164
using ThreadCpuStopWatch = CustomStopWatch<CLOCK_THREAD_CPUTIME_ID>;
165
166
} // namespace doris