Coverage Report

Created: 2025-04-11 12:31

/root/doris/be/src/util/trace.h
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
#pragma once
18
19
#include <butil/macros.h>
20
21
#include "util/scoped_cleanup.h"
22
23
// If this scope times out, make a simple trace.
24
// It will log the cost time only.
25
// Timeout is chrono duration struct, eg: 5ms, 100 * 1s.
26
#define SCOPED_SIMPLE_TRACE_IF_TIMEOUT(timeout) \
27
54.3k
    SCOPED_SIMPLE_TRACE_TO_STREAM_IF_TIMEOUT(timeout, LOG(WARNING))
28
29
// If this scope times out, then put simple trace to the stream.
30
// Timeout is chrono duration struct, eg: 5ms, 100 * 1s.
31
// For example:
32
//
33
//    std::string tag = "[foo]";
34
//    SCOPED_SIMPLE_TRACE_TO_STREAM_IF_TIMEOUT(5s, LOG(INFO) << tag);
35
//
36
#define SCOPED_SIMPLE_TRACE_TO_STREAM_IF_TIMEOUT(timeout, stream)                       \
37
54.3k
    using namespace std::chrono_literals;                                               \
38
54.3k
    auto VARNAME_LINENUM(scoped_simple_trace) = doris::MonotonicMicros();               \
39
54.3k
    SCOPED_CLEANUP({                                                                    \
40
54.3k
        auto VARNAME_LINENUM(timeout_us) =                                              \
41
54.3k
                std::chrono::duration_cast<std::chrono::microseconds>(timeout).count(); \
42
54.3k
        auto VARNAME_LINENUM(cost_us) =                                                 \
43
54.3k
                doris::MonotonicMicros() - VARNAME_LINENUM(scoped_simple_trace);        \
44
54.3k
        if (VARNAME_LINENUM(cost_us) >= VARNAME_LINENUM(timeout_us)) {                  \
45
54.3k
            stream << "Simple trace cost(us): " << VARNAME_LINENUM(cost_us);            \
46
54.3k
        }                                                                               \
47
54.3k
    })