Skip to content

Commit

Permalink
Log thread and process CPU time
Browse files Browse the repository at this point in the history
  • Loading branch information
jellefoks committed Aug 16, 2024
1 parent a4aa9f5 commit 3a8f4a7
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "base/logging.h"

#include <limits.h>
#include <pthread.h>

#include "base/macros.h"
#include "base/trace_event/trace_event.h"
Expand Down Expand Up @@ -125,6 +126,34 @@ typedef pthread_mutex_t* MutexHandle;

namespace logging {

namespace {
using ClockIdMap = std::map<base::PlatformThreadId, clockid_t>;
ClockIdMap* clockids = nullptr;
std::string print_clock(clockid_t cid,
const char* prefix = "",
const char* postfix = "") {
struct timespec ts;
if (clock_gettime(cid, &ts) != -1)
return starboard::FormatString("%s%ld.%06ld%s", prefix, ts.tv_sec,
ts.tv_nsec / 1000, postfix);
return std::string();
}

std::string print_clocks() {
clockid_t clockid = 0;
if (!clockids)
clockids = new ClockIdMap;
auto id = clockids->find(base::PlatformThread::CurrentId());
if (id == clockids->end()) {
pthread_getcpuclockid(pthread_self(), &clockid);
(*clockids)[base::PlatformThread::CurrentId()] = clockid;
} else
clockid = id->second;
return print_clock(clockid, "(", "/") +
print_clock(CLOCK_PROCESS_CPUTIME_ID, "", ")");
}
} // namespace

namespace {

VlogInfo* g_vlog_info = nullptr;
Expand Down Expand Up @@ -1040,7 +1069,9 @@ void LogMessage::Init(const char* file, int line) {
stream_ << CurrentProcessId() << ':';
#endif
if (g_log_thread_id)
stream_ << base::PlatformThread::GetName() << '/' << base::PlatformThread::CurrentId() << ":";
stream_ << base::PlatformThread::GetName() << '/'
<< base::PlatformThread::CurrentId() << "/" << print_clocks()
<< ":";
if (g_log_timestamp) {
#if defined(STARBOARD)
EzTimeValue time_value;
Expand Down

0 comments on commit 3a8f4a7

Please sign in to comment.