mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-12 10:13:51 +01:00
enh(Foundation): add Poco::Process::timesMicroseconds()
This commit is contained in:
parent
21f93e3e6a
commit
cecccf7b74
@ -94,6 +94,10 @@ public:
|
||||
/// 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
|
||||
@ -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
|
||||
|
||||
|
||||
|
@ -56,6 +56,7 @@ public:
|
||||
|
||||
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,
|
||||
|
@ -57,6 +57,7 @@ public:
|
||||
|
||||
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,
|
||||
|
@ -61,6 +61,7 @@ public:
|
||||
|
||||
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,
|
||||
|
@ -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__)
|
||||
|
@ -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()");
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user