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

Do Not Cache PID/TID in Logging #994

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions libkineto/include/ThreadUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

namespace libkineto {

int32_t systemThreadId();
int32_t systemThreadId(bool cache=true);
int32_t threadId();
bool setThreadName(const std::string& name);
std::string getThreadName();

int32_t processId();
int32_t processId(bool cache=true);
std::string processName(int32_t pid);

// Return a list of pids and process names for the current process
Expand Down
2 changes: 1 addition & 1 deletion libkineto/src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Logger::Logger(int severity, int line, const char* filePath, int errnum)
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
const char* file = strrchr(filePath, '/');
buf_ << fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(tt)) << " "
<< processId() << ":" << systemThreadId() << " "
<< processId(false) << ":" << systemThreadId(false) << " "
<< (file ? file + 1 : filePath) << ":" << line << "] ";
}

Expand Down
26 changes: 18 additions & 8 deletions libkineto/src/ThreadUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,38 @@ thread_local int32_t _tid = 0;
thread_local int32_t _sysTid = 0;
}

int32_t processId() {
int32_t processId(bool cache) {
int32_t pid = 0;
if (!_pid) {
#ifndef _WIN32
_pid = (int32_t)getpid();
pid = (int32_t)getpid();
#else
_pid = (int32_t)GetCurrentProcessId();
pid = (int32_t)GetCurrentProcessId();
#endif
if (cache) {
_pid = pid;
}
return pid;
}
return _pid;
}

int32_t systemThreadId() {
int32_t systemThreadId(bool cache) {
int32_t sysTid = 0;
if (!_sysTid) {
#ifdef __APPLE__
_sysTid = (int32_t)syscall(SYS_thread_selfid);
sysTid = (int32_t)syscall(SYS_thread_selfid);
#elif defined _WIN32
_sysTid = (int32_t)GetCurrentThreadId();
sysTid = (int32_t)GetCurrentThreadId();
#elif defined __FreeBSD__
syscall(SYS_thr_self, &_sysTid);
syscall(SYS_thr_self, &sysTid);
#else
_sysTid = (int32_t)syscall(SYS_gettid);
sysTid = (int32_t)syscall(SYS_gettid);
#endif
if (cache) {
_sysTid = sysTid;
}
return sysTid;
}
return _sysTid;
}
Expand Down
Loading