Skip to content

Commit

Permalink
Track CPU usage during compression operations
Browse files Browse the repository at this point in the history
Summary: In order to compare compression performance, wall time for processing aren't enough, we also need to know how much CPU time was spent.

Reviewed By: kiminoue7

Differential Revision: D52235516

fbshipit-source-id: 42d80dbce28915b3578ec3bfc6ef7529574fda37
  • Loading branch information
Georges Berenger authored and facebook-github-bot committed Dec 18, 2023
1 parent 969a7e3 commit 0beb40f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
53 changes: 53 additions & 0 deletions vrs/os/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

#include <vrs/os/Platform.h>

#if IS_MAC_PLATFORM() || IS_LINUX_PLATFORM()
#include <sys/resource.h>
#elif IS_WINDOWS_PLATFORM()
#include <Windows.h>
#endif

namespace vrs {
namespace os {

Expand All @@ -40,5 +46,52 @@ int64_t getTimestampMs() {
return duration_cast<std::chrono::milliseconds>(steady_clock::now().time_since_epoch()).count();
}

bool getProcessCpuTimes(double& outUserCpuTime, double& outSystemCpuTime) {
#if IS_MAC_PLATFORM() || IS_LINUX_PLATFORM()
struct rusage r_usage;
getrusage(RUSAGE_SELF, &r_usage);
const double cMicroseconds = 1e-6;
outUserCpuTime = r_usage.ru_utime.tv_sec + r_usage.ru_utime.tv_usec * cMicroseconds;
outSystemCpuTime = r_usage.ru_stime.tv_sec + r_usage.ru_stime.tv_usec * cMicroseconds;
return true;

#elif IS_WINDOWS_PLATFORM()
FILETIME start;
FILETIME exit;
ULARGE_INTEGER kernelTime;
ULARGE_INTEGER userTime;

if (GetProcessTimes(
GetCurrentProcess(),
&start,
&exit,
reinterpret_cast<PFILETIME>(&kernelTime),
reinterpret_cast<PFILETIME>(&userTime)) == 0) {
outUserCpuTime = 0;
outSystemCpuTime = 0;
return false;
}

const double cSecFactor = 1e-7; // source is a count of 0.1us
outUserCpuTime = userTime.QuadPart * cSecFactor;
outSystemCpuTime = kernelTime.QuadPart * cSecFactor;
return true;

#else
outUserCpuTime = 0;
outSystemCpuTime = 0;
return false;
#endif
}

double getTotalProcessCpuTime() {
double user = 0;
double kernel = 0;
if (!getProcessCpuTimes(user, kernel)) {
return 0;
}
return user + kernel;
}

} // namespace os
} // namespace vrs
8 changes: 8 additions & 0 deletions vrs/os/Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@ int64_t getTimestampMs();
/// Therefore, using double is misleading & dangerous.
int64_t getCurrentTimeSecSinceEpoch();

/// Get process usage time, splitting User CPU time and System CPU time
/// Returns false if not implemented on that platform.
bool getProcessCpuTimes(double& outUserCpuTime, double& outSystemCpuTime);

/// Get User and System CPU time as one
/// Returns 0 if not supported.
double getTotalProcessCpuTime();

} // namespace os
} // namespace vrs
4 changes: 4 additions & 0 deletions vrs/os/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ int64_t fileTell(std::FILE* file);
int fileSeek(std::FILE* file, int64_t offset, int origin);
int fileSetSize(std::FILE* file, int64_t size);

inline bool fileWriteString(FILE* f, const std::string& str) {
return os::fileWrite(str.c_str(), 1, str.size(), f) == str.size();
}

/// Misc helpers
int remove(const std::string& path); // file or folder
int rename(const std::string& originalName, const std::string& newName); // file or folder
Expand Down

0 comments on commit 0beb40f

Please sign in to comment.