Coverage Report

Created: 2024-11-21 15:53

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