/root/doris/be/src/util/debug_points.cpp
| 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 |  |  | 
| 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 | 183k | DebugPoints* DebugPoints::instance() { | 
| 28 | 183k |     static DebugPoints instance; | 
| 29 | 183k |     return &instance; | 
| 30 | 183k | } | 
| 31 |  |  | 
| 32 | 13 | bool DebugPoints::is_enable(const std::string& name) { | 
| 33 | 13 |     return get_debug_point(name) != nullptr; | 
| 34 | 13 | } | 
| 35 |  |  | 
| 36 | 183k | std::shared_ptr<DebugPoint> DebugPoints::get_debug_point(const std::string& name) { | 
| 37 | 183k |     if (!config::enable_debug_points) { | 
| 38 | 0 |         return nullptr; | 
| 39 | 0 |     } | 
| 40 | 183k |     auto map_ptr = _debug_points.load(); | 
| 41 | 183k |     auto it = map_ptr->find(name); | 
| 42 | 183k |     if (it == map_ptr->end()) { | 
| 43 | 183k |         return nullptr; | 
| 44 | 183k |     } | 
| 45 |  |  | 
| 46 | 72 |     auto debug_point = it->second; | 
| 47 | 72 |     if ((debug_point->expire_ms > 0 && MonotonicMillis() >= debug_point->expire_ms) || | 
| 48 | 72 |         (debug_point->execute_limit > 0 && | 
| 49 | 71 |          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 | 70 |     return debug_point; | 
| 56 | 72 | } | 
| 57 |  |  | 
| 58 | 59 | void DebugPoints::add(const std::string& name, std::shared_ptr<DebugPoint> debug_point) { | 
| 59 | 59 |     update([&](DebugPointMap& new_points) { new_points[name] = debug_point; }); | 
| 60 |  |  | 
| 61 | 59 |     std::ostringstream oss; | 
| 62 | 59 |     oss << "{"; | 
| 63 | 59 |     for (auto [key, value] : debug_point->params) { | 
| 64 | 17 |         oss << key << " : " << value << ", "; | 
| 65 | 17 |     } | 
| 66 | 59 |     oss << "}"; | 
| 67 |  |  | 
| 68 | 59 |     LOG(INFO) << "add debug point: name=" << name << ", params=" << oss.str(); | 
| 69 | 59 | } | 
| 70 |  |  | 
| 71 | 44 | void DebugPoints::remove(const std::string& name) { | 
| 72 | 44 |     bool exists = false; | 
| 73 | 44 |     update([&](DebugPointMap& new_points) { exists = new_points.erase(name) > 0; }); | 
| 74 |  |  | 
| 75 | 44 |     LOG(INFO) << "remove debug point: name=" << name << ", exists=" << exists; | 
| 76 | 44 | } | 
| 77 |  |  | 
| 78 | 103 | void DebugPoints::update(std::function<void(DebugPointMap&)>&& handler) { | 
| 79 | 103 |     auto old_points = _debug_points.load(); | 
| 80 | 103 |     while (true) { | 
| 81 | 103 |         auto new_points = std::make_shared<DebugPointMap>(*old_points); | 
| 82 | 103 |         handler(*new_points); | 
| 83 | 103 |         if (_debug_points.compare_exchange_strong( | 
| 84 | 103 |                     old_points, std::static_pointer_cast<const DebugPointMap>(new_points))) { | 
| 85 | 103 |             break; | 
| 86 | 103 |         } | 
| 87 | 103 |     } | 
| 88 | 103 | } | 
| 89 |  |  | 
| 90 | 4 | void DebugPoints::clear() { | 
| 91 | 4 |     _debug_points.store(std::make_shared<const DebugPointMap>()); | 
| 92 |  |     LOG(INFO) << "clear debug points"; | 
| 93 | 4 | } | 
| 94 |  |  | 
| 95 |  | } // namespace doris |