Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROF-10688] Remove a looping allocation when updating threads #135

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions ddprof-lib/src/main/cpp/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "vmStructs.h"
#include "wallClock.h"
#include <algorithm>
#include <array>
r1viollet marked this conversation as resolved.
Show resolved Hide resolved
#include <dlfcn.h>
#include <fstream>
#include <memory>
Expand Down Expand Up @@ -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<char[]> {
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<char[]>(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);
r1viollet marked this conversation as resolved.
Show resolved Hide resolved
}
return std::string();
});
}

delete thread_list;
delete thread_list;
}

Engine *Profiler::selectCpuEngine(Arguments &args) {
Expand Down
18 changes: 9 additions & 9 deletions ddprof-lib/src/main/cpp/threadInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ int ThreadInfo::size() {
}

void ThreadInfo::updateThreadName(
int tid, std::function<std::unique_ptr<char[]>(int)> resolver) {
MutexLocker ml(_ti_lock);
int tid, std::function<std::string(int)> resolver) {
MutexLocker ml(_ti_lock);

std::map<int, std::string>::iterator it = _thread_names.lower_bound(tid);
if (it == _thread_names.end() || it->first != tid) {
std::unique_ptr<char[]> namePtr = resolver(tid);
if (namePtr.get() != nullptr) {
_thread_names.insert(it, std::map<int, std::string>::value_type(
tid, static_cast<char *>(namePtr.get())));
std::map<int, std::string>::iterator it = _thread_names.lower_bound(tid);
if (it == _thread_names.end() || it->first != tid) {
r1viollet marked this conversation as resolved.
Show resolved Hide resolved
std::string name = resolver(tid);
if (!name.empty()) {
_thread_names.insert(it, std::map<int, std::string>::value_type(
tid, std::move(name))); // Move the string
}
}
}
r1viollet marked this conversation as resolved.
Show resolved Hide resolved
}

void ThreadInfo::reportCounters() {
Expand Down
3 changes: 1 addition & 2 deletions ddprof-lib/src/main/cpp/threadInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class ThreadInfo {
void set(int tid, const char *name, u64 java_thread_id);
std::pair<std::shared_ptr<std::string>, u64> get(int tid);

void updateThreadName(int threadId,
std::function<std::unique_ptr<char[]>(int)> resolver);
void updateThreadName(int tid, std::function<std::string(int)> resolver);

int size();

Expand Down
Loading