From 530b27df1e71852580d8b0d474543aeffe65618f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 15 Dec 2005 21:11:39 +0000 Subject: [PATCH] * `nix-store --gc' prints out the number of bytes freed on stdout (even when it is interrupted by a signal). --- src/libstore/gc.cc | 8 ++++++-- src/libstore/gc.hh | 3 ++- src/libstore/store.cc | 5 +++-- src/libstore/store.hh | 2 +- src/libutil/util.cc | 16 +++++++++++++--- src/libutil/util.hh | 5 ++++- src/nix-store/main.cc | 17 ++++++++++++++++- 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index bdaf2946c..cb808b6d1 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -303,9 +303,11 @@ static Paths topoSort(const PathSet & paths) } -void collectGarbage(GCAction action, PathSet & result) +void collectGarbage(GCAction action, PathSet & result, + unsigned long long & bytesFreed) { result.clear(); + bytesFreed = 0; bool gcKeepOutputs = queryBoolSetting("gc-keep-outputs", false); @@ -452,7 +454,9 @@ void collectGarbage(GCAction action, PathSet & result) printMsg(lvlInfo, format("deleting `%1%'") % *i); /* Okay, it's safe to delete. */ - deleteFromStore(*i); + unsigned long long freed; + deleteFromStore(*i, freed); + bytesFreed += freed; if (fdLock != -1) /* Write token to stale (deleted) lock file. */ diff --git a/src/libstore/gc.hh b/src/libstore/gc.hh index b6a367c4b..eb1858729 100644 --- a/src/libstore/gc.hh +++ b/src/libstore/gc.hh @@ -19,7 +19,8 @@ typedef enum { closure of) the roots. If `action' is `gcReturnDead', return the set of paths not reachable from the roots. If `action' is `gcDeleteDead', actually delete the latter set. */ -void collectGarbage(GCAction action, PathSet & result); +void collectGarbage(GCAction action, PathSet & result, + unsigned long long & bytesFreed); /* Register a temporary GC root. This root will automatically disappear when this process exits. WARNING: this function should diff --git a/src/libstore/store.cc b/src/libstore/store.cc index 281ccc4bf..dc3625a1d 100644 --- a/src/libstore/store.cc +++ b/src/libstore/store.cc @@ -746,8 +746,9 @@ Path addTextToStore(const string & suffix, const string & s, } -void deleteFromStore(const Path & _path) +void deleteFromStore(const Path & _path, unsigned long long & bytesFreed) { + bytesFreed = 0; Path path(canonPath(_path)); assertStorePath(path); @@ -763,7 +764,7 @@ void deleteFromStore(const Path & _path) } txn.commit(); - deletePath(path); + deletePath(path, bytesFreed); } diff --git a/src/libstore/store.hh b/src/libstore/store.hh index 0f35ff0c6..2d8018d5f 100644 --- a/src/libstore/store.hh +++ b/src/libstore/store.hh @@ -155,7 +155,7 @@ Path addTextToStore(const string & suffix, const string & s, const PathSet & references); /* Delete a value from the nixStore directory. */ -void deleteFromStore(const Path & path); +void deleteFromStore(const Path & path, unsigned long long & bytesFreed); void verifyStore(bool checkContents); diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 2e684e9c1..5a728617d 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -194,7 +194,7 @@ void writeFile(const Path & path, const string & s) } -static void _deletePath(const Path & path) +static void _deletePath(const Path & path, unsigned long long & bytesFreed) { checkInterrupt(); @@ -204,6 +204,8 @@ static void _deletePath(const Path & path) if (lstat(path.c_str(), &st)) throw SysError(format("getting attributes of path `%1%'") % path); + bytesFreed += st.st_size; + if (S_ISDIR(st.st_mode)) { Strings names = readDirectory(path); @@ -214,7 +216,7 @@ static void _deletePath(const Path & path) } for (Strings::iterator i = names.begin(); i != names.end(); ++i) - _deletePath(path + "/" + *i); + _deletePath(path + "/" + *i, bytesFreed); } if (remove(path.c_str()) == -1) @@ -223,10 +225,18 @@ static void _deletePath(const Path & path) void deletePath(const Path & path) +{ + unsigned long long dummy; + deletePath(path, dummy); +} + + +void deletePath(const Path & path, unsigned long long & bytesFreed) { startNest(nest, lvlDebug, format("recursively deleting path `%1%'") % path); - _deletePath(path); + bytesFreed = 0; + _deletePath(path, bytesFreed); } diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 9e7eb11bd..9601e65b3 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -98,9 +98,12 @@ string readFile(const Path & path); void writeFile(const Path & path, const string & s); /* Delete a path; i.e., in the case of a directory, it is deleted - recursively. Don't use this at home, kids. */ + recursively. Don't use this at home, kids. The second variant + returns the number of bytes freed. */ void deletePath(const Path & path); +void deletePath(const Path & path, unsigned long long & bytesFreed); + /* Make a path read-only recursively. */ void makePathReadOnly(const Path & path); diff --git a/src/nix-store/main.cc b/src/nix-store/main.cc index d1a96aa3a..8bb1b1254 100644 --- a/src/nix-store/main.cc +++ b/src/nix-store/main.cc @@ -489,6 +489,20 @@ static void opCheckValidity(Strings opFlags, Strings opArgs) } +struct PrintFreed +{ + bool show; + unsigned long long bytesFreed; + PrintFreed(bool _show) : bytesFreed(0), show(_show) { } + ~PrintFreed() + { + if (show) + cout << format("%d bytes freed (%.2f MiB)\n") + % bytesFreed % (bytesFreed / (1024.0 * 1024.0)); + } +}; + + static void opGC(Strings opFlags, Strings opArgs) { GCAction action = gcDeleteDead; @@ -503,7 +517,8 @@ static void opGC(Strings opFlags, Strings opArgs) else throw UsageError(format("bad sub-operation `%1%' in GC") % *i); PathSet result; - collectGarbage(action, result); + PrintFreed freed(action == gcDeleteDead); + collectGarbage(action, result, freed.bytesFreed); if (action != gcDeleteDead) { for (PathSet::iterator i = result.begin(); i != result.end(); ++i)