From 4a1a59b070d35c5f80d6cbf732166309985d060e Mon Sep 17 00:00:00 2001 From: r1viollet Date: Tue, 1 Oct 2024 12:04:34 +0200 Subject: [PATCH 1/2] Remove a looping allocation when updating threads --- ddprof-lib/src/main/cpp/profiler.cpp | 29 +++++++++++++------------- ddprof-lib/src/main/cpp/threadInfo.cpp | 18 ++++++++-------- ddprof-lib/src/main/cpp/threadInfo.h | 3 +-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/ddprof-lib/src/main/cpp/profiler.cpp b/ddprof-lib/src/main/cpp/profiler.cpp index 06b5d16fb4..f657b7fb6d 100644 --- a/ddprof-lib/src/main/cpp/profiler.cpp +++ b/ddprof-lib/src/main/cpp/profiler.cpp @@ -36,6 +36,7 @@ #include "vmStructs.h" #include "wallClock.h" #include +#include #include #include #include @@ -996,21 +997,21 @@ void Profiler::updateJavaThreadNames() { } void Profiler::updateNativeThreadNames() { - ThreadList *thread_list = OS::listThreads(); - for (int tid; (tid = thread_list->next()) != -1;) { - _thread_info.updateThreadName( - tid, [](int tid) -> std::unique_ptr { - const size_t buffer_size = 64; - char *name_buf = new char[buffer_size]; - if (OS::threadName(tid, name_buf, buffer_size)) { - return std::unique_ptr(name_buf); - } - delete[] name_buf; - return nullptr; - }); - } + ThreadList *thread_list = OS::listThreads(); + constexpr size_t buffer_size = 64; + char name_buf[buffer_size]; // Stack-allocated buffer + + for (int tid; (tid = thread_list->next()) != -1;) { + _thread_info.updateThreadName( + tid, [&](int tid) -> std::string { + if (OS::threadName(tid, name_buf, buffer_size)) { + return std::string(name_buf, buffer_size); + } + return std::string(); + }); + } - delete thread_list; + delete thread_list; } Engine *Profiler::selectCpuEngine(Arguments &args) { diff --git a/ddprof-lib/src/main/cpp/threadInfo.cpp b/ddprof-lib/src/main/cpp/threadInfo.cpp index 0ca6a906b4..18b9c87282 100644 --- a/ddprof-lib/src/main/cpp/threadInfo.cpp +++ b/ddprof-lib/src/main/cpp/threadInfo.cpp @@ -67,17 +67,17 @@ int ThreadInfo::size() { } void ThreadInfo::updateThreadName( - int tid, std::function(int)> resolver) { - MutexLocker ml(_ti_lock); + int tid, std::function resolver) { + MutexLocker ml(_ti_lock); - std::map::iterator it = _thread_names.lower_bound(tid); - if (it == _thread_names.end() || it->first != tid) { - std::unique_ptr namePtr = resolver(tid); - if (namePtr.get() != nullptr) { - _thread_names.insert(it, std::map::value_type( - tid, static_cast(namePtr.get()))); + std::map::iterator it = _thread_names.lower_bound(tid); + if (it == _thread_names.end() || it->first != tid) { + std::string name = resolver(tid); + if (!name.empty()) { + _thread_names.insert(it, std::map::value_type( + tid, std::move(name))); // Move the string + } } - } } void ThreadInfo::reportCounters() { diff --git a/ddprof-lib/src/main/cpp/threadInfo.h b/ddprof-lib/src/main/cpp/threadInfo.h index 5834a3cd04..9fab8d2f5f 100644 --- a/ddprof-lib/src/main/cpp/threadInfo.h +++ b/ddprof-lib/src/main/cpp/threadInfo.h @@ -22,8 +22,7 @@ class ThreadInfo { void set(int tid, const char *name, u64 java_thread_id); std::pair, u64> get(int tid); - void updateThreadName(int threadId, - std::function(int)> resolver); + void updateThreadName(int tid, std::function resolver); int size(); From da975d756cd43c28b2a77e51fdd362b243f152a7 Mon Sep 17 00:00:00 2001 From: r1viollet Date: Wed, 9 Oct 2024 12:29:13 +0200 Subject: [PATCH 2/2] Thread name update - Use a find API instead of the lower bound. --- ddprof-lib/src/main/cpp/profiler.cpp | 1 - ddprof-lib/src/main/cpp/threadInfo.cpp | 9 ++++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/ddprof-lib/src/main/cpp/profiler.cpp b/ddprof-lib/src/main/cpp/profiler.cpp index f657b7fb6d..7399635cbf 100644 --- a/ddprof-lib/src/main/cpp/profiler.cpp +++ b/ddprof-lib/src/main/cpp/profiler.cpp @@ -36,7 +36,6 @@ #include "vmStructs.h" #include "wallClock.h" #include -#include #include #include #include diff --git a/ddprof-lib/src/main/cpp/threadInfo.cpp b/ddprof-lib/src/main/cpp/threadInfo.cpp index 18b9c87282..10299b628c 100644 --- a/ddprof-lib/src/main/cpp/threadInfo.cpp +++ b/ddprof-lib/src/main/cpp/threadInfo.cpp @@ -69,13 +69,12 @@ int ThreadInfo::size() { void ThreadInfo::updateThreadName( int tid, std::function resolver) { MutexLocker ml(_ti_lock); - - std::map::iterator it = _thread_names.lower_bound(tid); - if (it == _thread_names.end() || it->first != tid) { + auto it = _thread_names.find(tid); + if (it == _thread_names.end()) { + // Thread ID not found, insert new entry std::string name = resolver(tid); if (!name.empty()) { - _thread_names.insert(it, std::map::value_type( - tid, std::move(name))); // Move the string + _thread_names.emplace(tid, std::move(name)); } } }