From 23b8b7e096d3a2a784387a09f68115706b1e9552 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 16 Aug 2017 17:00:24 +0200 Subject: nix optimise-store: Add This replaces "nix-store --optimise". Main difference is that it has a progress indicator. --- src/libstore/optimise-store.cc | 14 ++++++++++++-- src/libutil/logging.hh | 1 + src/nix/optimise-store.cc | 41 +++++++++++++++++++++++++++++++++++++++++ src/nix/progress-bar.cc | 10 ++++++---- 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/nix/optimise-store.cc (limited to 'src') diff --git a/src/libstore/optimise-store.cc b/src/libstore/optimise-store.cc index b736307b31b4..5093305a13e3 100644 --- a/src/libstore/optimise-store.cc +++ b/src/libstore/optimise-store.cc @@ -249,14 +249,24 @@ void LocalStore::optimisePath_(OptimiseStats & stats, const Path & path, InodeHa void LocalStore::optimiseStore(OptimiseStats & stats) { + Activity act(*logger, actOptimiseStore); + PathSet paths = queryAllValidPaths(); InodeHash inodeHash = loadInodeHash(); + act.progress(0, paths.size()); + + uint64_t done = 0; + for (auto & i : paths) { addTempRoot(i); if (!isValidPath(i)) continue; /* path was GC'ed, probably */ - //Activity act(*logger, lvlChatty, format("hashing files in '%1%'") % i); - optimisePath_(stats, realStoreDir + "/" + baseNameOf(i), inodeHash); + { + Activity act(*logger, actUnknown, fmt("optimising path '%s'", i)); + optimisePath_(stats, realStoreDir + "/" + baseNameOf(i), inodeHash); + } + done++; + act.progress(done, paths.size()); } } diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index edc3553f6942..bb89643d0a0b 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -21,6 +21,7 @@ typedef enum { actCopyPaths = 103, actBuilds = 104, actBuild = 105, + actOptimiseStore = 106, } ActivityType; typedef uint64_t ActivityId; diff --git a/src/nix/optimise-store.cc b/src/nix/optimise-store.cc new file mode 100644 index 000000000000..725fb75a19ec --- /dev/null +++ b/src/nix/optimise-store.cc @@ -0,0 +1,41 @@ +#include "command.hh" +#include "shared.hh" +#include "store-api.hh" + +#include + +using namespace nix; + +struct CmdOptimiseStore : StoreCommand +{ + CmdOptimiseStore() + { + } + + std::string name() override + { + return "optimise-store"; + } + + std::string description() override + { + return "replace identical files in the store by hard links"; + } + + Examples examples() override + { + return { + Example{ + "To optimise the Nix store:", + "nix optimise-store" + }, + }; + } + + void run(ref store) override + { + store->optimiseStore(); + } +}; + +static RegisterCommand r1(make_ref()); diff --git a/src/nix/progress-bar.cc b/src/nix/progress-bar.cc index 6288737088a2..15ef95370a8b 100644 --- a/src/nix/progress-bar.cc +++ b/src/nix/progress-bar.cc @@ -210,7 +210,7 @@ public: std::string res; - auto renderActivity = [&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt, double unit) { + auto renderActivity = [&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) { auto & act = state.activitiesByType[type]; uint64_t done = act.done, expected = act.done, running = 0, failed = act.failed; for (auto & j : act.its) { @@ -242,16 +242,16 @@ public: return s; }; - auto showActivity = [&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt, double unit) { + auto showActivity = [&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) { auto s = renderActivity(type, itemFmt, numberFmt, unit); if (s.empty()) return; if (!res.empty()) res += ", "; res += s; }; - showActivity(actBuilds, "%s built", "%d", 1); + showActivity(actBuilds, "%s built"); - auto s1 = renderActivity(actCopyPaths, "%s copied", "%d", 1); + auto s1 = renderActivity(actCopyPaths, "%s copied"); auto s2 = renderActivity(actCopyPath, "%s MiB", "%.1f", MiB); if (!s1.empty() || !s2.empty()) { @@ -262,6 +262,8 @@ public: showActivity(actDownload, "%s MiB DL", "%.1f", MiB); + showActivity(actOptimiseStore, "%s paths optimised"); + return res; } }; -- cgit 1.4.1