enh(Foundation): add Poco::Process::timesMicroseconds()

This commit is contained in:
Günter Obiltschnig 2024-11-21 08:50:49 +01:00
parent 21f93e3e6a
commit cecccf7b74
8 changed files with 58 additions and 5 deletions

View File

@ -94,6 +94,10 @@ public:
/// Returns the number of seconds spent by the /// Returns the number of seconds spent by the
/// current process in user and kernel mode. /// 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); static ProcessHandle launch(const std::string& command, const Args& args, int options = 0);
/// Creates a new process for the given command and returns /// Creates a new process for the given command and returns
/// a ProcessHandle of the new process. The given arguments are /// a ProcessHandle of the new process. The given arguments are
@ -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 } // namespace Poco

View File

@ -56,6 +56,7 @@ public:
static PIDImpl idImpl(); static PIDImpl idImpl();
static void timesImpl(long& userTime, long& kernelTime); static void timesImpl(long& userTime, long& kernelTime);
static void timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime);
static ProcessHandleImpl* launchImpl( static ProcessHandleImpl* launchImpl(
const std::string& command, const std::string& command,
const ArgsImpl& args, const ArgsImpl& args,

View File

@ -57,6 +57,7 @@ public:
static PIDImpl idImpl(); static PIDImpl idImpl();
static void timesImpl(long& userTime, long& kernelTime); static void timesImpl(long& userTime, long& kernelTime);
static void timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime);
static ProcessHandleImpl* launchImpl( static ProcessHandleImpl* launchImpl(
const std::string& command, const std::string& command,
const ArgsImpl& args, const ArgsImpl& args,

View File

@ -61,6 +61,7 @@ public:
static PIDImpl idImpl(); static PIDImpl idImpl();
static void timesImpl(long& userTime, long& kernelTime); static void timesImpl(long& userTime, long& kernelTime);
static void timesMicrosecondsImpl(Poco::Int64& userTime, Poco::Int64& kernelTime);
static ProcessHandleImpl* launchImpl( static ProcessHandleImpl* launchImpl(
const std::string& command, const std::string& command,
const ArgsImpl& args, const ArgsImpl& args,

View File

@ -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) 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__) #if defined(__QNX__)

View File

@ -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) 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()"); throw Poco::NotImplementedException("Process::launch()");

View File

@ -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 ProcessImpl::mustEscapeArg(const std::string& arg)
{ {
bool result = false; bool result = false;

View File

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