Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 14 additions & 14 deletions ddprof-lib/src/main/cpp/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,21 +996,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);
Comment thread
r1viollet marked this conversation as resolved.
Resolved
}
return std::string();
});
}

delete thread_list;
delete thread_list;
}

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

void ThreadInfo::updateThreadName(
int tid, std::function<std::unique_ptr<char[]>(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())));
int tid, std::function<std::string(int)> resolver) {
MutexLocker ml(_ti_lock);
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.emplace(tid, std::move(name));
}
}
}
Comment thread
r1viollet marked this conversation as resolved.
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