From af261ab82e273f8b2d1a283c89797f014a092348 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:16:36 +0800 Subject: [PATCH] feat use raise auto stop profiler --- 3party/gperftools/src/profiler.cc | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/3party/gperftools/src/profiler.cc b/3party/gperftools/src/profiler.cc index 71a3cef..9f0c65b 100644 --- a/3party/gperftools/src/profiler.cc +++ b/3party/gperftools/src/profiler.cc @@ -142,9 +142,9 @@ private: static void CpuProfilerSwitch(int signal_number) { - static unsigned profile_count; static char base_profile_name[PATH_MAX]; - static bool started = false; + static std::atomic profile_count; + static std::atomic started = false; if (base_profile_name[0] == '\0') { if (!GetUniquePathFromEnv("CPUPROFILE", base_profile_name)) { @@ -154,7 +154,7 @@ CpuProfilerSwitch(int signal_number) } char full_profile_name[PATH_MAX + 16]; - snprintf(full_profile_name, sizeof(full_profile_name), "%s.%u", base_profile_name, profile_count); + snprintf(full_profile_name, sizeof(full_profile_name), "%s.%u", base_profile_name, profile_count.load()); if (!started) { if (!ProfilerStart(full_profile_name)) { RAW_LOG(FATAL, "Can't turn on cpu profiling for '%s': %s\n", full_profile_name, strerror(errno)); @@ -167,12 +167,10 @@ CpuProfilerSwitch(int signal_number) if (interval > 0) { RAW_LOG(INFO, "Auto Stop cpu Profiling After %ds", interval); int cur_profile_count = profile_count; - std::thread([interval, cur_profile_count] { + std::thread([interval, cur_profile_count, signal_number] { std::this_thread::sleep_for(std::chrono::seconds(interval)); - if (profile_count == cur_profile_count + 1) { - RAW_LOG(INFO, "Trigger Auto stop CPU Profiling, duration=%ds", interval); - ProfilerStop(); - } + // send signal to self + if (started && profile_count.load() == cur_profile_count + 1) { raise(signal_number); } }).detach(); } }