Skip to content

Commit

Permalink
enh(Foundation): add Poco::Process::timesMicroseconds()
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Nov 21, 2024
1 parent 21f93e3 commit cecccf7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 5 deletions.
10 changes: 10 additions & 0 deletions Foundation/include/Poco/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class Foundation_API Process: public ProcessImpl
/// Returns the number of seconds spent by the
/// current process in user and kernel mode.

static void timesMicroseconds(Poco::Int64& userTime, Poco::Int64& kernelTime);
/// Returns the number of microseconds spent by the
/// current process in user and kernel mode.

static ProcessHandle launch(const std::string& command, const Args& args, int options = 0);
/// Creates a new process for the given command and returns
/// a ProcessHandle of the new process. The given arguments are
Expand Down Expand Up @@ -268,6 +272,12 @@ inline void Process::times(long& userTime, long& kernelTime)
}


inline void Process::timesMicroseconds(Poco::Int64& userTime, Poco::Int64& kernelTime)
{
ProcessImpl::timesMicrosecondsImpl(userTime, kernelTime);
}


} // namespace Poco


Expand Down
1 change: 1 addition & 0 deletions Foundation/include/Poco/Process_UNIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Foundation_API ProcessImpl

static PIDImpl idImpl();
static void timesImpl(long& userTime, long& kernelTime);
static void timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime);
static ProcessHandleImpl* launchImpl(
const std::string& command,
const ArgsImpl& args,
Expand Down
1 change: 1 addition & 0 deletions Foundation/include/Poco/Process_VX.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Foundation_API ProcessImpl

static PIDImpl idImpl();
static void timesImpl(long& userTime, long& kernelTime);
static void timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime);
static ProcessHandleImpl* launchImpl(
const std::string& command,
const ArgsImpl& args,
Expand Down
1 change: 1 addition & 0 deletions Foundation/include/Poco/Process_WIN32U.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Foundation_API ProcessImpl

static PIDImpl idImpl();
static void timesImpl(long& userTime, long& kernelTime);
static void timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime);
static ProcessHandleImpl* launchImpl(
const std::string& command,
const ArgsImpl& args,
Expand Down
9 changes: 9 additions & 0 deletions Foundation/src/Process_UNIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ void ProcessImpl::timesImpl(long& userTime, long& kernelTime)
}


void ProcessImpl::timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime)
{
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
userTime = static_cast<Poco::Int64>(usage.ru_utime.tv_sec)*1000000 + usage.ru_utime.tv_usec;
kernelTime = static_cast<Poco::Int64>(usage.ru_stime.tv_sec)*1000000 + usage.ru_stime.tv_usec;
}


ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env, int options)
{
#if defined(__QNX__)
Expand Down
7 changes: 7 additions & 0 deletions Foundation/src/Process_VX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ void ProcessImpl::timesImpl(long& userTime, long& kernelTime)
}


void ProcessImpl::timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime)
{
userTime = 0;
kernelTime = 0;
}


ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory,Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env)
{
throw Poco::NotImplementedException("Process::launch()");
Expand Down
24 changes: 24 additions & 0 deletions Foundation/src/Process_WIN32U.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ void ProcessImpl::timesImpl(long& userTime, long& kernelTime)
}


void ProcessImpl::timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime)
{
FILETIME ftCreation;
FILETIME ftExit;
FILETIME ftKernel;
FILETIME ftUser;

if (GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, &ftUser) != 0)
{
ULARGE_INTEGER time;
time.LowPart = ftKernel.dwLowDateTime;
time.HighPart = ftKernel.dwHighDateTime;
kernelTime = Poco::Int64(time.QuadPart/10);
time.LowPart = ftUser.dwLowDateTime;
time.HighPart = ftUser.dwHighDateTime;
userTime = Poco::Int64(time.QuadPart/10);
}
else
{
userTime = kernelTime = -1;
}
}


bool ProcessImpl::mustEscapeArg(const std::string& arg)
{
bool result = false;
Expand Down
10 changes: 5 additions & 5 deletions Prometheus/src/ProcessCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ void ProcessCollector::exportTo(Exporter& exporter) const

void ProcessCollector::buildMetrics()
{
_metrics.push_back(std::make_unique<CallbackIntGauge>(
_metrics.push_back(std::make_unique<CallbackGauge>(
name() + "_cpu_seconds_total"s,
"Total user and system CPU time spent in seconds"s,
nullptr,
[]()
{
long user;
long system;
Poco::Process::times(user, system);
return static_cast<Poco::Int64>(user) + static_cast<Poco::Int64>(system);
Poco::Int64 user;
Poco::Int64 system;
Poco::Process::timesMicroseconds(user, system);
return static_cast<double>(user/1000 + system/1000)/1000.0;
}));

#ifdef POCO_OS_FAMILY_UNIX
Expand Down

0 comments on commit cecccf7

Please sign in to comment.