mirror of
https://github.com/NixOS/nix.git
synced 2025-11-13 05:56:03 +01:00
For example, instead of doing
#include "nix/store-config.hh"
#include "nix/derived-path.hh"
Now do
#include "nix/store/config.hh"
#include "nix/store/derived-path.hh"
This was originally planned in the issue, and also recent requested by
Eelco.
Most of the change is purely mechanical. There is just one small
additional issue. See how, in the example above, we took this
opportunity to also turn `<comp>-config.hh` into `<comp>/config.hh`.
Well, there was already a `nix/util/config.{cc,hh}`. Even though there
is not a public configuration header for libutil (which also would be
called `nix/util/config.{cc,hh}`) that's still confusing, To avoid any
such confusion, we renamed that to `nix/util/configuration.{cc,hh}`.
Finally, note that the libflake headers already did this, so we didn't
need to do anything to them. We wouldn't want to mistakenly get
`nix/flake/flake/flake.hh`!
Progress on #7876
125 lines
2.9 KiB
C++
125 lines
2.9 KiB
C++
#include <algorithm>
|
|
#include <cstring>
|
|
|
|
#include "nix/util/current-process.hh"
|
|
#include "nix/util/util.hh"
|
|
#include "nix/util/finally.hh"
|
|
#include "nix/util/file-system.hh"
|
|
#include "nix/util/processes.hh"
|
|
#include "nix/util/signals.hh"
|
|
#include <math.h>
|
|
|
|
#ifdef __APPLE__
|
|
# include <mach-o/dyld.h>
|
|
#endif
|
|
|
|
#if __linux__
|
|
# include <mutex>
|
|
# include "nix/util/cgroup.hh"
|
|
# include "nix/util/namespaces.hh"
|
|
#endif
|
|
|
|
namespace nix {
|
|
|
|
unsigned int getMaxCPU()
|
|
{
|
|
#if __linux__
|
|
try {
|
|
auto cgroupFS = getCgroupFS();
|
|
if (!cgroupFS) return 0;
|
|
|
|
auto cpuFile = *cgroupFS + "/" + getCurrentCgroup() + "/cpu.max";
|
|
|
|
auto cpuMax = readFile(cpuFile);
|
|
auto cpuMaxParts = tokenizeString<std::vector<std::string>>(cpuMax, " \n");
|
|
|
|
if (cpuMaxParts.size() != 2) {
|
|
return 0;
|
|
}
|
|
|
|
auto quota = cpuMaxParts[0];
|
|
auto period = cpuMaxParts[1];
|
|
if (quota != "max")
|
|
return std::ceil(std::stoi(quota) / std::stof(period));
|
|
} catch (Error &) { ignoreExceptionInDestructor(lvlDebug); }
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
#ifndef _WIN32
|
|
size_t savedStackSize = 0;
|
|
|
|
void setStackSize(size_t stackSize)
|
|
{
|
|
struct rlimit limit;
|
|
if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) {
|
|
savedStackSize = limit.rlim_cur;
|
|
limit.rlim_cur = std::min(static_cast<rlim_t>(stackSize), limit.rlim_max);
|
|
if (setrlimit(RLIMIT_STACK, &limit) != 0) {
|
|
logger->log(
|
|
lvlError,
|
|
HintFmt(
|
|
"Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): %4%",
|
|
savedStackSize,
|
|
stackSize,
|
|
limit.rlim_max,
|
|
std::strerror(errno)
|
|
).str()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
void restoreProcessContext(bool restoreMounts)
|
|
{
|
|
#ifndef _WIN32
|
|
unix::restoreSignals();
|
|
#endif
|
|
if (restoreMounts) {
|
|
#if __linux__
|
|
restoreMountNamespace();
|
|
#endif
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
if (savedStackSize) {
|
|
struct rlimit limit;
|
|
if (getrlimit(RLIMIT_STACK, &limit) == 0) {
|
|
limit.rlim_cur = savedStackSize;
|
|
setrlimit(RLIMIT_STACK, &limit);
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
std::optional<Path> getSelfExe()
|
|
{
|
|
static auto cached = []() -> std::optional<Path>
|
|
{
|
|
#if __linux__ || __GNU__
|
|
return readLink("/proc/self/exe");
|
|
#elif __APPLE__
|
|
char buf[1024];
|
|
uint32_t size = sizeof(buf);
|
|
if (_NSGetExecutablePath(buf, &size) == 0)
|
|
return buf;
|
|
else
|
|
return std::nullopt;
|
|
#else
|
|
return std::nullopt;
|
|
#endif
|
|
}();
|
|
return cached;
|
|
}
|
|
|
|
}
|