mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 07:22:43 +01:00
libutil, libexpr: #10542 abstract over getrusage for getting cpuTime stat and implement windows version
Update src/libutil/windows/current-process.cc Prefer `nullptr` over `NULL` Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo> Update src/libutil/unix/current-process.cc Prefer C++ type casts Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo> Update src/libutil/windows/current-process.cc Prefer C++ type casts Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo> Update src/libutil/unix/current-process.cc Don't allocate exception Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
This commit is contained in:
parent
ffe5c1ecb4
commit
7b8ceb5d2d
6 changed files with 69 additions and 15 deletions
|
|
@ -2,6 +2,7 @@
|
|||
///@file
|
||||
|
||||
#include <optional>
|
||||
#include <chrono>
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <sys/resource.h>
|
||||
|
|
@ -11,6 +12,11 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* Get the current process's user space CPU time.
|
||||
*/
|
||||
std::chrono::microseconds getCpuUserTime();
|
||||
|
||||
/**
|
||||
* If cgroups are active, attempt to calculate the number of CPUs available.
|
||||
* If cgroups are unavailable or if cpu.max is set to "max", return 0.
|
||||
|
|
|
|||
23
src/libutil/unix/current-process.cc
Normal file
23
src/libutil/unix/current-process.cc
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "nix/util/current-process.hh"
|
||||
#include "nix/util/error.hh"
|
||||
#include <cmath>
|
||||
|
||||
#include <sys/resource.h>
|
||||
|
||||
namespace nix {
|
||||
|
||||
std::chrono::microseconds getCpuUserTime()
|
||||
{
|
||||
struct rusage buf;
|
||||
|
||||
if (getrusage(RUSAGE_SELF, &buf) != 0) {
|
||||
throw SysError("failed to get CPU time");
|
||||
}
|
||||
|
||||
std::chrono::seconds seconds(buf.ru_utime.tv_sec);
|
||||
std::chrono::microseconds microseconds(buf.ru_utime.tv_usec);
|
||||
|
||||
return seconds + microseconds;
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
@ -49,6 +49,7 @@ config_unix_priv_h = configure_file(
|
|||
sources += config_unix_priv_h
|
||||
|
||||
sources += files(
|
||||
'current-process.cc',
|
||||
'environment-variables.cc',
|
||||
'file-descriptor.cc',
|
||||
'file-path.cc',
|
||||
|
|
|
|||
35
src/libutil/windows/current-process.cc
Normal file
35
src/libutil/windows/current-process.cc
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "nix/util/current-process.hh"
|
||||
#include "nix/util/windows-error.hh"
|
||||
#include <cmath>
|
||||
|
||||
#ifdef _WIN32
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <windows.h>
|
||||
|
||||
namespace nix {
|
||||
|
||||
std::chrono::microseconds getCpuUserTime()
|
||||
{
|
||||
FILETIME creationTime;
|
||||
FILETIME exitTime;
|
||||
FILETIME kernelTime;
|
||||
FILETIME userTime;
|
||||
|
||||
if (!GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime)) {
|
||||
auto lastError = GetLastError();
|
||||
throw windows::WinError(lastError, "failed to get CPU time");
|
||||
}
|
||||
|
||||
ULARGE_INTEGER uLargeInt;
|
||||
uLargeInt.LowPart = userTime.dwLowDateTime;
|
||||
uLargeInt.HighPart = userTime.dwHighDateTime;
|
||||
|
||||
// FILETIME stores units of 100 nanoseconds.
|
||||
// Dividing by 10 gives microseconds.
|
||||
std::chrono::microseconds microseconds(uLargeInt.QuadPart / 10);
|
||||
|
||||
return microseconds;
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
#endif // ifdef _WIN32
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
sources += files(
|
||||
'current-process.cc',
|
||||
'environment-variables.cc',
|
||||
'file-descriptor.cc',
|
||||
'file-path.cc',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue