nix optimise-store: Add

This replaces "nix-store --optimise". Main difference is that it has a
progress indicator.
This commit is contained in:
Eelco Dolstra 2017-08-16 17:00:24 +02:00
parent 40bffe0a43
commit 23b8b7e096
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
4 changed files with 60 additions and 6 deletions

View file

@ -249,15 +249,25 @@ void LocalStore::optimisePath_(OptimiseStats & stats, const Path & path, InodeHa
void LocalStore::optimiseStore(OptimiseStats & stats) void LocalStore::optimiseStore(OptimiseStats & stats)
{ {
Activity act(*logger, actOptimiseStore);
PathSet paths = queryAllValidPaths(); PathSet paths = queryAllValidPaths();
InodeHash inodeHash = loadInodeHash(); InodeHash inodeHash = loadInodeHash();
act.progress(0, paths.size());
uint64_t done = 0;
for (auto & i : paths) { for (auto & i : paths) {
addTempRoot(i); addTempRoot(i);
if (!isValidPath(i)) continue; /* path was GC'ed, probably */ if (!isValidPath(i)) continue; /* path was GC'ed, probably */
//Activity act(*logger, lvlChatty, format("hashing files in '%1%'") % i); {
Activity act(*logger, actUnknown, fmt("optimising path '%s'", i));
optimisePath_(stats, realStoreDir + "/" + baseNameOf(i), inodeHash); optimisePath_(stats, realStoreDir + "/" + baseNameOf(i), inodeHash);
} }
done++;
act.progress(done, paths.size());
}
} }
static string showBytes(unsigned long long bytes) static string showBytes(unsigned long long bytes)

View file

@ -21,6 +21,7 @@ typedef enum {
actCopyPaths = 103, actCopyPaths = 103,
actBuilds = 104, actBuilds = 104,
actBuild = 105, actBuild = 105,
actOptimiseStore = 106,
} ActivityType; } ActivityType;
typedef uint64_t ActivityId; typedef uint64_t ActivityId;

41
src/nix/optimise-store.cc Normal file
View file

@ -0,0 +1,41 @@
#include "command.hh"
#include "shared.hh"
#include "store-api.hh"
#include <atomic>
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> store) override
{
store->optimiseStore();
}
};
static RegisterCommand r1(make_ref<CmdOptimiseStore>());

View file

@ -210,7 +210,7 @@ public:
std::string res; 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]; auto & act = state.activitiesByType[type];
uint64_t done = act.done, expected = act.done, running = 0, failed = act.failed; uint64_t done = act.done, expected = act.done, running = 0, failed = act.failed;
for (auto & j : act.its) { for (auto & j : act.its) {
@ -242,16 +242,16 @@ public:
return s; 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); auto s = renderActivity(type, itemFmt, numberFmt, unit);
if (s.empty()) return; if (s.empty()) return;
if (!res.empty()) res += ", "; if (!res.empty()) res += ", ";
res += s; 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); auto s2 = renderActivity(actCopyPath, "%s MiB", "%.1f", MiB);
if (!s1.empty() || !s2.empty()) { if (!s1.empty() || !s2.empty()) {
@ -262,6 +262,8 @@ public:
showActivity(actDownload, "%s MiB DL", "%.1f", MiB); showActivity(actDownload, "%s MiB DL", "%.1f", MiB);
showActivity(actOptimiseStore, "%s paths optimised");
return res; return res;
} }
}; };