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 Sep 23, 2024
1 parent 4802079 commit 29016b7
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <limits.h>
#include <stdint.h>
#include <sys/resource.h>

#include <atomic>
#include <memory>
Expand Down Expand Up @@ -139,6 +140,31 @@ typedef FILE* FileHandle;

namespace logging {

namespace {
std::string print_rusage(int who,
const char* prefix = "",
const char* postfix = "") {
struct rusage usage;
if (getrusage(who, &usage) != -1) {
long total = ((usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) * 1000000 +
(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec));
long total_sec = total / 1000000;
long total_usec = total % 1000000;
return starboard::FormatString(
"%s(CPU:u%ld.%03ld+s%ld.%03ld=%ld.%03ld)%s",
prefix, usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000,
usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000, total_sec,
total_usec / 1000, postfix);
}
return std::string(strerror(errno));
}

std::string print_stats() {
return print_rusage(RUSAGE_THREAD, "(", "/") +
print_rusage(RUSAGE_SELF, "", ")");
}
} // namespace

namespace {

int g_min_log_level = 0;
Expand Down Expand Up @@ -1106,7 +1132,8 @@ void LogMessage::Init(const char* file, int line) {
#if defined(STARBOARD)
// Logging the thread name is added for Starboard logs.
stream_ << base::PlatformThread::GetName() << '/'
<< base::PlatformThread::CurrentId() << ":";
<< base::PlatformThread::CurrentId() << "/" << print_stats()
<< ":";
#else
stream_ << base::PlatformThread::CurrentId() << ':';
#endif
Expand Down

0 comments on commit 29016b7

Please sign in to comment.