* `nix-store --gc' prints out the number of bytes freed on stdout

(even when it is interrupted by a signal).
This commit is contained in:
Eelco Dolstra 2005-12-15 21:11:39 +00:00
parent 5144f750c4
commit 530b27df1e
7 changed files with 45 additions and 11 deletions

View file

@ -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(); result.clear();
bytesFreed = 0;
bool gcKeepOutputs = bool gcKeepOutputs =
queryBoolSetting("gc-keep-outputs", false); queryBoolSetting("gc-keep-outputs", false);
@ -452,7 +454,9 @@ void collectGarbage(GCAction action, PathSet & result)
printMsg(lvlInfo, format("deleting `%1%'") % *i); printMsg(lvlInfo, format("deleting `%1%'") % *i);
/* Okay, it's safe to delete. */ /* Okay, it's safe to delete. */
deleteFromStore(*i); unsigned long long freed;
deleteFromStore(*i, freed);
bytesFreed += freed;
if (fdLock != -1) if (fdLock != -1)
/* Write token to stale (deleted) lock file. */ /* Write token to stale (deleted) lock file. */

View file

@ -19,7 +19,8 @@ typedef enum {
closure of) the roots. If `action' is `gcReturnDead', return the closure of) the roots. If `action' is `gcReturnDead', return the
set of paths not reachable from the roots. If `action' is set of paths not reachable from the roots. If `action' is
`gcDeleteDead', actually delete the latter set. */ `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 /* Register a temporary GC root. This root will automatically
disappear when this process exits. WARNING: this function should disappear when this process exits. WARNING: this function should

View file

@ -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)); Path path(canonPath(_path));
assertStorePath(path); assertStorePath(path);
@ -763,7 +764,7 @@ void deleteFromStore(const Path & _path)
} }
txn.commit(); txn.commit();
deletePath(path); deletePath(path, bytesFreed);
} }

View file

@ -155,7 +155,7 @@ Path addTextToStore(const string & suffix, const string & s,
const PathSet & references); const PathSet & references);
/* Delete a value from the nixStore directory. */ /* 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); void verifyStore(bool checkContents);

View file

@ -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(); checkInterrupt();
@ -204,6 +204,8 @@ static void _deletePath(const Path & path)
if (lstat(path.c_str(), &st)) if (lstat(path.c_str(), &st))
throw SysError(format("getting attributes of path `%1%'") % path); throw SysError(format("getting attributes of path `%1%'") % path);
bytesFreed += st.st_size;
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
Strings names = readDirectory(path); Strings names = readDirectory(path);
@ -214,7 +216,7 @@ static void _deletePath(const Path & path)
} }
for (Strings::iterator i = names.begin(); i != names.end(); ++i) for (Strings::iterator i = names.begin(); i != names.end(); ++i)
_deletePath(path + "/" + *i); _deletePath(path + "/" + *i, bytesFreed);
} }
if (remove(path.c_str()) == -1) if (remove(path.c_str()) == -1)
@ -223,10 +225,18 @@ static void _deletePath(const Path & path)
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, startNest(nest, lvlDebug,
format("recursively deleting path `%1%'") % path); format("recursively deleting path `%1%'") % path);
_deletePath(path); bytesFreed = 0;
_deletePath(path, bytesFreed);
} }

View file

@ -98,9 +98,12 @@ string readFile(const Path & path);
void writeFile(const Path & path, const string & s); void writeFile(const Path & path, const string & s);
/* Delete a path; i.e., in the case of a directory, it is deleted /* 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);
void deletePath(const Path & path, unsigned long long & bytesFreed);
/* Make a path read-only recursively. */ /* Make a path read-only recursively. */
void makePathReadOnly(const Path & path); void makePathReadOnly(const Path & path);

View file

@ -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) static void opGC(Strings opFlags, Strings opArgs)
{ {
GCAction action = gcDeleteDead; 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); else throw UsageError(format("bad sub-operation `%1%' in GC") % *i);
PathSet result; PathSet result;
collectGarbage(action, result); PrintFreed freed(action == gcDeleteDead);
collectGarbage(action, result, freed.bytesFreed);
if (action != gcDeleteDead) { if (action != gcDeleteDead) {
for (PathSet::iterator i = result.begin(); i != result.end(); ++i) for (PathSet::iterator i = result.begin(); i != result.end(); ++i)