Coverage Report

Created: 2024-11-21 20:39

/root/doris/be/src/util/debug_points.cpp
Line
Count
Source (jump to first uncovered line)
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
18
#include "util/debug_points.h"
19
20
#include "common/logging.h"
21
#include "util/time.h"
22
23
namespace doris {
24
25
1
DebugPoints::DebugPoints() : _debug_points(std::make_shared<const DebugPointMap>()) {}
26
27
137k
DebugPoints* DebugPoints::instance() {
28
137k
    static DebugPoints instance;
29
137k
    return &instance;
30
137k
}
31
32
13
bool DebugPoints::is_enable(const std::string& name) {
33
13
    return get_debug_point(name) != nullptr;
34
13
}
35
36
137k
std::shared_ptr<DebugPoint> DebugPoints::get_debug_point(const std::string& name) {
37
137k
    if (!config::enable_debug_points) {
38
0
        return nullptr;
39
0
    }
40
137k
    auto map_ptr = std::atomic_load_explicit(&_debug_points, std::memory_order_relaxed);
41
137k
    auto it = map_ptr->find(name);
42
137k
    if (it == map_ptr->end()) {
43
137k
        return nullptr;
44
137k
    }
45
46
29
    auto debug_point = it->second;
47
29
    if ((debug_point->expire_ms > 0 && MonotonicMillis() >= debug_point->expire_ms) ||
48
29
        (debug_point->execute_limit > 0 &&
49
28
         debug_point->execute_num.fetch_add(1, std::memory_order_relaxed) >=
50
4
                 debug_point->execute_limit)) {
51
2
        remove(name);
52
2
        return nullptr;
53
2
    }
54
55
27
    return debug_point;
56
29
}
57
58
12
void DebugPoints::add(const std::string& name, std::shared_ptr<DebugPoint> debug_point) {
59
12
    update([&](DebugPointMap& new_points) { new_points[name] = debug_point; });
60
61
12
    std::ostringstream oss;
62
12
    oss << "{";
63
17
    for (auto [key, value] : debug_point->params) {
64
17
        oss << key << " : " << value << ", ";
65
17
    }
66
12
    oss << "}";
67
68
12
    LOG(INFO) << "add debug point: name=" << name << ", params=" << oss.str();
69
12
}
70
71
3
void DebugPoints::remove(const std::string& name) {
72
3
    bool exists = false;
73
3
    update([&](DebugPointMap& new_points) { exists = new_points.erase(name) > 0; });
74
75
3
    LOG(INFO) << "remove debug point: name=" << name << ", exists=" << exists;
76
3
}
77
78
15
void DebugPoints::update(std::function<void(DebugPointMap&)>&& handler) {
79
15
    auto old_points = std::atomic_load_explicit(&_debug_points, std::memory_order_relaxed);
80
15
    while (true) {
81
15
        auto new_points = std::make_shared<DebugPointMap>(*old_points);
82
15
        handler(*new_points);
83
15
        if (std::atomic_compare_exchange_strong_explicit(
84
15
                    &_debug_points, &old_points,
85
15
                    std::static_pointer_cast<const DebugPointMap>(new_points),
86
15
                    std::memory_order_relaxed, std::memory_order_relaxed)) {
87
15
            break;
88
15
        }
89
15
    }
90
15
}
91
92
4
void DebugPoints::clear() {
93
4
    std::atomic_store_explicit(&_debug_points, std::make_shared<const DebugPointMap>(),
94
4
                               std::memory_order_relaxed);
95
4
    LOG(INFO) << "clear debug points";
96
4
}
97
98
} // namespace doris