1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-17 07:52: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:
Ethan Evans 2025-08-12 23:34:49 -07:00
parent ffe5c1ecb4
commit 7b8ceb5d2d
6 changed files with 69 additions and 15 deletions

View 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

View file

@ -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',