1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-13 14:02:42 +01:00

Use the standalone gc lib in the default gc

This commit is contained in:
Théophane Hufschmitt 2022-04-11 10:20:36 +02:00
parent fbec849281
commit 2c47b08e17
4 changed files with 43 additions and 19 deletions

View file

@ -3,6 +3,7 @@
#include "local-store.hh" #include "local-store.hh"
#include "local-fs-store.hh" #include "local-fs-store.hh"
#include "finally.hh" #include "finally.hh"
#include "find-roots.hh"
#include <functional> #include <functional>
#include <queue> #include <queue>
@ -284,14 +285,28 @@ void LocalStore::findRootsNoTempNoExternalDaemon(Roots & roots, bool censor)
{ {
debug("Cant connect to the tracing daemon socket, fallback to the internal trace"); debug("Cant connect to the tracing daemon socket, fallback to the internal trace");
/* Process direct roots in {gcroots,profiles}. */ using namespace nix::roots_tracer;
findRoots(stateDir + "/" + gcRootsDir, DT_UNKNOWN, roots);
findRoots(stateDir + "/profiles", DT_UNKNOWN, roots);
/* Add additional roots returned by different platforms-specific const TracerConfig opts {
heuristics. This is typically used to add running programs to .storeDir = fs::path(storeDir),
the set of roots (to prevent them from being garbage collected). */ .stateDir = fs::path(stateDir.get())
findRuntimeRoots(roots, censor); };
const std::set<fs::path> standardRoots = {
opts.stateDir / fs::path(gcRootsDir),
opts.stateDir / fs::path("gcroots"),
};
auto traceResult = traceStaticRoots(opts, standardRoots);
auto runtimeRoots = getRuntimeRoots(opts);
traceResult.storeRoots.insert(runtimeRoots.begin(), runtimeRoots.end());
for (auto & [rawRootInStore, externalRoots] : traceResult.storeRoots) {
if (!isInStore(rawRootInStore)) continue;
auto rootInStore = toStorePath(rawRootInStore).first;
if (!isValidPath(rootInStore)) continue;
std::unordered_set<std::string> primRoots;
for (const auto & externalRoot : externalRoots)
primRoots.insert(externalRoot.string());
roots.emplace(rootInStore, primRoots);
}
} }

View file

@ -6,7 +6,7 @@ libstore_DIR := $(d)
libstore_SOURCES := $(wildcard $(d)/*.cc $(d)/builtins/*.cc $(d)/build/*.cc) libstore_SOURCES := $(wildcard $(d)/*.cc $(d)/builtins/*.cc $(d)/build/*.cc)
libstore_LIBS = libutil libstore_LIBS = libutil find-roots
libstore_LDFLAGS += $(SQLITE3_LIBS) $(LIBCURL_LIBS) $(SODIUM_LIBS) -pthread libstore_LDFLAGS += $(SQLITE3_LIBS) $(LIBCURL_LIBS) $(SODIUM_LIBS) -pthread
ifdef HOST_LINUX ifdef HOST_LINUX
@ -32,7 +32,7 @@ ifeq ($(HAVE_SECCOMP), 1)
endif endif
libstore_CXXFLAGS += \ libstore_CXXFLAGS += \
-I src/libutil -I src/libstore -I src/libstore/build \ -I src/libutil -I src/libstore -I src/libstore/build -I src/nix-find-roots/lib \
-DNIX_PREFIX=\"$(prefix)\" \ -DNIX_PREFIX=\"$(prefix)\" \
-DNIX_STORE_DIR=\"$(storedir)\" \ -DNIX_STORE_DIR=\"$(storedir)\" \
-DNIX_DATA_DIR=\"$(datadir)\" \ -DNIX_DATA_DIR=\"$(datadir)\" \

View file

@ -26,9 +26,9 @@ inline void logNone(std::string_view)
{ } { }
struct TracerConfig { struct TracerConfig {
fs::path storeDir = "/nix/store"; const fs::path storeDir = "/nix/store";
fs::path stateDir = "/nix/var/nix"; const fs::path stateDir = "/nix/var/nix";
fs::path socketPath = "/nix/var/nix/gc-socket/socket"; const fs::path socketPath = "/nix/var/nix/gc-socket/socket";
std::function<void(std::string_view msg)> log = logNone; std::function<void(std::string_view msg)> log = logNone;
std::function<void(std::string_view msg)> debug = logNone; std::function<void(std::string_view msg)> debug = logNone;

View file

@ -15,8 +15,12 @@ void logStderr(std::string_view msg)
TracerConfig parseCmdLine(int argc, char** argv) TracerConfig parseCmdLine(int argc, char** argv)
{ {
TracerConfig res; std::function<void(std::string_view msg)> log = logStderr;
res.log = logStderr; std::function<void(std::string_view msg)> debug = logNone;
fs::path storeDir = "/nix/store";
fs::path stateDir = "/nix/var/nix";
fs::path socketPath = "/nix/var/nix/gc-trace-socket/socket";
auto usage = [&]() { auto usage = [&]() {
std::cerr << "Usage: " << string(argv[0]) << " [--verbose|-v] [-s storeDir] [-d stateDir] [-l socketPath]" << std::endl; std::cerr << "Usage: " << string(argv[0]) << " [--verbose|-v] [-s storeDir] [-d stateDir] [-l socketPath]" << std::endl;
exit(1); exit(1);
@ -40,23 +44,28 @@ TracerConfig parseCmdLine(int argc, char** argv)
usage(); usage();
break; break;
case 'v': case 'v':
res.debug = logStderr; debug = logStderr;
break; break;
case 's': case 's':
res.storeDir = fs::path(optarg); storeDir = fs::path(optarg);
break; break;
case 'd': case 'd':
res.stateDir = fs::path(optarg); stateDir = fs::path(optarg);
break; break;
case 'l': case 'l':
res.socketPath = fs::path(optarg); socketPath = fs::path(optarg);
break; break;
default: default:
std::cerr << "Got invalid char: " << (char)opt_char << std::endl; std::cerr << "Got invalid char: " << (char)opt_char << std::endl;
abort(); abort();
} }
}; };
return res; return TracerConfig {
.storeDir = storeDir,
.stateDir = stateDir,
.socketPath = socketPath,
.debug = debug,
};
} }