-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1889 from Expensify/main
Update expensify_prod branch
- Loading branch information
Showing
11 changed files
with
261 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "ResourceMonitorThread.h" | ||
#include "libstuff/libstuff.h" | ||
#include <format> | ||
#include <cmath> | ||
|
||
thread_local uint64_t ResourceMonitorThread::threadStartTime; | ||
thread_local double ResourceMonitorThread::cpuStartTime; | ||
|
||
void ResourceMonitorThread::beforeProcessStart() { | ||
threadStartTime = STimeNow(); | ||
cpuStartTime = SGetCPUUserTime(); | ||
} | ||
|
||
void ResourceMonitorThread::afterProcessFinished() { | ||
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 (threadUserTime == 0) { | ||
return; | ||
} | ||
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "libstuff/libstuff.h" | ||
#include <thread> | ||
|
||
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 | ||
{ | ||
public: | ||
// When calling this constructor, if you're passing a class member function as the `f` parameter and that | ||
// function receives parameters, you will need to wrap your function call in a lambda, doing something like: | ||
// ResourceMonitorThread([=, this]{ this->memberFunction(param1, param2);}); | ||
template<typename F, typename... Args> | ||
ResourceMonitorThread(F&& f, Args&&... args): | ||
thread(ResourceMonitorThread::wrapper<F&&, Args&&...>, forward<F&&>(f), forward<Args&&>(args)...){}; | ||
private: | ||
thread_local static uint64_t threadStartTime; | ||
thread_local static double cpuStartTime; | ||
|
||
static void beforeProcessStart(); | ||
static void afterProcessFinished(); | ||
|
||
template<typename F, typename... Args> | ||
static void wrapper(F&& f, Args&&... args) { | ||
beforeProcessStart(); | ||
invoke(forward<F>(f), forward<Args>(args)...); | ||
afterProcessFinished(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.