Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldoglas committed Oct 8, 2024
1 parent 2337fd3 commit 631b9f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
22 changes: 11 additions & 11 deletions libstuff/ResourceMonitorThread.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#include "ResourceMonitorThread.h"
#include "libstuff/libstuff.h"
#include "format"
#include <format>
#include <cmath>

thread_local uint64_t ResourceMonitorThread::startTime;
thread_local double ResourceMonitorThread::cpuStartTime;
uint64_t ResourceMonitorThread::threadStartTime;
double ResourceMonitorThread::cpuStartTime;

void ResourceMonitorThread::before(){
startTime = STimeNow();
void ResourceMonitorThread::before() {
threadStartTime = STimeNow();
cpuStartTime = SGetCPUUserTime();
}

void ResourceMonitorThread::after(){
const uint64_t threadEndTime = STimeNow() - startTime;
const double CPUUserTime = SGetCPUUserTime() - cpuStartTime;
void ResourceMonitorThread::after() {
const uint64_t threadUserTime = STimeNow() - threadStartTime;
const double cpuUserTime = SGetCPUUserTime() - cpuStartTime;

// This shouldn't happen since the time to start/finish a thread should take more than a microsecond, but to be
// sure we're not dividing by 0 and causing crashes, let's add an if here and return if threadEndTime is 0.
if (threadEndTime == 0) {
if (threadUserTime == 0) {
return;
}
const double cpuUserPercentage = round((CPUUserTime / static_cast<double>(threadEndTime)) * 100 * 1000) / 1000;
const double cpuUserPercentage = round((cpuUserTime / static_cast<double>(threadUserTime)) * 100 * 1000) / 1000;
const pid_t tid = syscall(SYS_gettid);
SINFO(format("Thread finished. pID: '{}', CPUTime: '{}µs', CPUPercentage: '{}%'", tid, CPUUserTime, cpuUserPercentage, startTime, cpuStartTime));
SINFO(format("Thread finished. pID: '{}', CPUTime: '{}µs', CPUPercentage: '{}%'", tid, cpuUserTime, cpuUserPercentage));
}
8 changes: 4 additions & 4 deletions libstuff/ResourceMonitorThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace std;
// This class is a wrapper around the default thread. We use it to collect the thread CPU usage. That allows us
// to investigate if we have any threads using more resources than it should, which can cause CPU usage peaks in
// the cluster.
class ResourceMonitorThread: public thread
class ResourceMonitorThread : public thread
{
public:
// When calling this constructor, if you're passing a class member function as the `f` parameter and that
Expand All @@ -16,16 +16,16 @@ class ResourceMonitorThread: public thread
ResourceMonitorThread(F&& f, Args&&... args):
thread(ResourceMonitorThread::wrapper<F&&, Args&&...>, forward<F&&>(f), forward<Args&&>(args)...){};
private:
static thread_local uint64_t startTime;
static thread_local double cpuStartTime;
static uint64_t threadStartTime;
static double cpuStartTime;

static void before();
static void after();

template<typename F, typename... Args>
static void wrapper(F&& f, Args&&... args) {
before();
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
invoke(forward<F>(f), forward<Args>(args)...);
after();
}
};
1 change: 1 addition & 0 deletions libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,7 @@ SString& SString::operator=(const bool from) {
double SGetCPUUserTime() {
struct rusage usage;
getrusage(RUSAGE_THREAD, &usage);

// Returns the current threads CPU user time in microseconds
return static_cast<double>(usage.ru_utime.tv_sec) * 1e6 + static_cast<double>(usage.ru_utime.tv_usec);
}
3 changes: 2 additions & 1 deletion libstuff/libstuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ string SGUnzip(const string& content);
// Command-line helpers
STable SParseCommandLine(int argc, char* argv[]);

// Returns the current CPU usage inside the current process
// Returns the CPU usage inside the current thread
double SGetCPUUserTime();

#endif // LIBSTUFF_H

0 comments on commit 631b9f5

Please sign in to comment.