Coverage Report

Created: 2026-05-18 05:00

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
374M
    CustomStopWatch(bool auto_start = false) {
43
374M
        _total_time = 0;
44
374M
        _running = false;
45
374M
        if (auto_start) {
46
2.91k
            start();
47
2.91k
        }
48
374M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
42
348M
    CustomStopWatch(bool auto_start = false) {
43
348M
        _total_time = 0;
44
348M
        _running = false;
45
348M
        if (auto_start) {
46
2.91k
            start();
47
2.91k
        }
48
348M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
42
26.2M
    CustomStopWatch(bool auto_start = false) {
43
26.2M
        _total_time = 0;
44
26.2M
        _running = false;
45
26.2M
        if (auto_start) {
46
0
            start();
47
0
        }
48
26.2M
    }
49
50
    timespec start_time() const { return _start; }
51
52
391M
    void start() {
53
391M
        if (!_running) {
54
374M
            _record_start_thread_id();
55
374M
            clock_gettime(Clock, &_start);
56
374M
            _running = true;
57
374M
        }
58
391M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
52
364M
    void start() {
53
364M
        if (!_running) {
54
348M
            _record_start_thread_id();
55
348M
            clock_gettime(Clock, &_start);
56
348M
            _running = true;
57
348M
        }
58
364M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
52
26.4M
    void start() {
53
26.4M
        if (!_running) {
54
26.3M
            _record_start_thread_id();
55
26.3M
            clock_gettime(Clock, &_start);
56
26.3M
            _running = true;
57
26.3M
        }
58
26.4M
    }
59
60
134M
    void stop() {
61
134M
        if (_running) {
62
132M
            _total_time += elapsed_time();
63
132M
            _running = false;
64
132M
        }
65
134M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
60
118M
    void stop() {
61
118M
        if (_running) {
62
116M
            _total_time += elapsed_time();
63
116M
            _running = false;
64
116M
        }
65
118M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
60
15.8M
    void stop() {
61
15.8M
        if (_running) {
62
15.8M
            _total_time += elapsed_time();
63
15.8M
            _running = false;
64
15.8M
        }
65
15.8M
    }
66
67
    // Restarts the timer. Returns the elapsed time until this point.
68
20.6M
    int64_t reset() {
69
20.6M
        int64_t ret = elapsed_time();
70
71
20.6M
        if (_running) {
72
16.1M
            _record_start_thread_id();
73
16.1M
            clock_gettime(Clock, &_start);
74
16.1M
        }
75
76
20.6M
        return ret;
77
20.6M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
68
1.37M
    int64_t reset() {
69
1.37M
        int64_t ret = elapsed_time();
70
71
1.37M
        if (_running) {
72
98.8k
            _record_start_thread_id();
73
98.8k
            clock_gettime(Clock, &_start);
74
98.8k
        }
75
76
1.37M
        return ret;
77
1.37M
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
68
19.2M
    int64_t reset() {
69
19.2M
        int64_t ret = elapsed_time();
70
71
19.2M
        if (_running) {
72
16.0M
            _record_start_thread_id();
73
16.0M
            clock_gettime(Clock, &_start);
74
16.0M
        }
75
76
19.2M
        return ret;
77
19.2M
    }
78
79
    // Returns time in nanosecond.
80
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
81
509M
    int64_t elapsed_time() const {
82
509M
        if (!_running) {
83
135M
            return _total_time;
84
135M
        }
85
86
373M
        _check_thread_id();
87
373M
        timespec end;
88
373M
        clock_gettime(Clock, &end);
89
373M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
373M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
373M
        if (end_nanos < start_nanos) {
92
19.7k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
19.7k
            return 0;
94
19.7k
        }
95
373M
        return end_nanos - start_nanos;
96
373M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
81
467M
    int64_t elapsed_time() const {
82
467M
        if (!_running) {
83
118M
            return _total_time;
84
118M
        }
85
86
349M
        _check_thread_id();
87
349M
        timespec end;
88
349M
        clock_gettime(Clock, &end);
89
349M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
349M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
349M
        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
349M
        return end_nanos - start_nanos;
96
349M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
81
41.5M
    int64_t elapsed_time() const {
82
41.5M
        if (!_running) {
83
17.1M
            return _total_time;
84
17.1M
        }
85
86
24.4M
        _check_thread_id();
87
24.4M
        timespec end;
88
24.4M
        clock_gettime(Clock, &end);
89
24.4M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
24.4M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
24.4M
        if (end_nanos < start_nanos) {
92
19.7k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
19.7k
            return 0;
94
19.7k
        }
95
24.4M
        return end_nanos - start_nanos;
96
24.4M
    }
97
98
    // Return time in microseconds
99
325k
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
100
101
    // Return time in milliseconds
102
416k
    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
64.4k
    int64_t elapsed_time_seconds(timespec end) const {
107
64.4k
        if (!_running) {
108
0
            return _total_time / 1000L / 1000L / 1000L;
109
0
        }
110
64.4k
        if (end.tv_sec < _start.tv_sec) {
111
15
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
112
15
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
113
15
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
114
15
            return 0;
115
15
        }
116
64.4k
        return end.tv_sec - _start.tv_sec;
117
64.4k
    }
118
119
private:
120
50.7M
    static std::string _get_thread_id() {
121
50.7M
        std::stringstream ss;
122
50.7M
        ss << std::this_thread::get_id();
123
50.7M
        return ss.str();
124
50.7M
    }
125
389M
    void _record_start_thread_id() {
126
389M
#ifndef NDEBUG
127
389M
        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
26.3M
            _start_thread_id = _get_thread_id();
131
26.3M
        }
132
389M
#endif
133
389M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
125
363M
    void _record_start_thread_id() {
126
363M
#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
363M
#endif
133
363M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
125
26.3M
    void _record_start_thread_id() {
126
26.3M
#ifndef NDEBUG
127
26.3M
        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
26.3M
            _start_thread_id = _get_thread_id();
131
26.3M
        }
132
26.3M
#endif
133
26.3M
    }
134
135
378M
    void _check_thread_id() const {
136
378M
#ifndef NDEBUG
137
378M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
24.6M
            auto current_thread_id = _get_thread_id();
139
24.6M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
39.5k
                             << " but stopped in thread " << current_thread_id;
142
39.5k
            }
143
24.6M
        }
144
378M
#endif
145
378M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
135
353M
    void _check_thread_id() const {
136
353M
#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
353M
#endif
145
353M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
135
24.6M
    void _check_thread_id() const {
136
24.6M
#ifndef NDEBUG
137
24.6M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
24.6M
            auto current_thread_id = _get_thread_id();
139
24.6M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
39.5k
                             << " but stopped in thread " << current_thread_id;
142
39.5k
            }
143
24.6M
        }
144
24.6M
#endif
145
24.6M
    }
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