Branch data Line data Source code
1 : : /* 2 : : * Copyright (C) 2026 by Thun Lu. All rights reserved. 3 : : * Author: Thun Lu <thun.lu@zohomail.cn> 4 : : * Repo: https://github.com/thun-res/vlink 5 : : * _ __ __ _ __ 6 : : * | | / / / / (_) ____ / /__ 7 : : * | | / / / / / / / __ \ / //_/ 8 : : * | |/ / / /___ / / / / / / / ,< 9 : : * |___/ /_____/ /_/ /_/ /_/ /_/|_| 10 : : * 11 : : * Licensed under the Apache License, Version 2.0 (the "License"); 12 : : * you may not use this file except in compliance with the License. 13 : : * You may obtain a copy of the License at 14 : : * 15 : : * http://www.apache.org/licenses/LICENSE-2.0 16 : : * 17 : : * Unless required by applicable law or agreed to in writing, software 18 : : * distributed under the License is distributed on an "AS IS" BASIS, 19 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 : : * See the License for the specific language governing permissions and 21 : : * limitations under the License. 22 : : */ 23 : : 24 : : #include "./base/cpu_profiler.h" 25 : : 26 : : #include <string> 27 : : 28 : : #include "./base/utils.h" 29 : : 30 : : #define VLINK_PROFILER_DEFAULT_STATE 0 31 : : 32 : : namespace vlink { 33 : : 34 : 41 : [[maybe_unused]] static bool get_profiler_enabled_for_env() { 35 : : #if VLINK_PROFILER_DEFAULT_STATE 36 : : std::string enable_str = Utils::get_env("VLINK_PROFILER_ENABLE", "1"); 37 : : #else 38 [ + - + - ]: 82 : std::string enable_str = Utils::get_env("VLINK_PROFILER_ENABLE", "0"); 39 : : #endif 40 : : 41 : 82 : return enable_str == "1"; 42 : 41 : } 43 : : 44 : : // CpuProfiler 45 : 14 : CpuProfiler::CpuProfiler() noexcept = default; 46 : : 47 : 14 : CpuProfiler::~CpuProfiler() noexcept = default; 48 : : 49 : 915 : bool CpuProfiler::is_global_enabled() noexcept { 50 [ + + + - ]: 915 : static bool profiler_enable = get_profiler_enabled_for_env(); 51 : 915 : return profiler_enable; 52 : : } 53 : : 54 : 19 : void CpuProfiler::begin() noexcept { 55 : 19 : SpinLockGuard lock(spin_); 56 : : 57 : 19 : cpu_active_timer_.restart(); 58 : 19 : cpu_timestamp_timer_.start(); 59 : 19 : } 60 : : 61 : 20 : void CpuProfiler::end() noexcept { 62 : 20 : SpinLockGuard lock(spin_); 63 : : 64 : 20 : auto active = cpu_active_timer_.restart(); 65 : : 66 [ + + ]: 20 : if VUNLIKELY (active < 0) { 67 : 1 : return; 68 : : } 69 : : 70 : 19 : total_active_.fetch_add(active, std::memory_order_acq_rel); 71 [ + + ]: 20 : } 72 : : 73 : 15 : double CpuProfiler::get() const noexcept { 74 : 15 : auto total_timestamp = cpu_timestamp_timer_.get(); 75 : : 76 [ + + ]: 15 : if VUNLIKELY (total_timestamp <= 0) { 77 : 3 : return 0; 78 : : } 79 : : 80 : 12 : return static_cast<double>(total_active_.load(std::memory_order_acquire)) / static_cast<double>(total_timestamp) * 81 : 12 : 100.0; 82 : : } 83 : : 84 : 3 : double CpuProfiler::restart() noexcept { 85 : 3 : SpinLockGuard lock(spin_); 86 : : 87 : 3 : auto value = get(); 88 : : 89 : 3 : cpu_active_timer_.restart(); 90 : 3 : cpu_timestamp_timer_.restart(); 91 : : 92 : 3 : total_active_.store(0, std::memory_order_release); 93 : : 94 : 6 : return value; 95 : 3 : } 96 : : 97 : : } // namespace vlink