summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEfraim Flashner <efraim@flashner.co.il>2025-06-29 10:21:12 +0300
committerEfraim Flashner <efraim@flashner.co.il>2025-07-28 13:57:53 +0300
commitcc588d8eb6a5e05bc8c9d41855685a1d8ce70187 (patch)
tree8f1091354927a109b82ba558dd7637061479b787
parentcf6868187a68feea41b3cde9bd37670df7192fed (diff)
guix gc: Adjust size suffix based on the amount of data.
* guix/ui.scm (number->size): New procedure. * guix/scripts/gc.scm (guix-gc)[actions]: Display the amount of collected-garbage using more specific units. [ensure-free-space]: Display the size using an appropriate size unit. * nix/libstore/gc.cc (deletePathRecursive, removeUnusedLinks): Same. * nix/libstore/optimise-store.cc (showBytes): Move function ... * nix/libstore/misc.cc: ... to here. Expand to adjust the output based on the amount of bytes received. Change-Id: Idceb1a13f8e45f959d327f53d1a8accb29d2678b
-rw-r--r--guix/scripts/gc.scm10
-rw-r--r--guix/ui.scm30
-rw-r--r--nix/libstore/gc.cc7
-rw-r--r--nix/libstore/misc.cc21
-rw-r--r--nix/libstore/misc.hh1
-rw-r--r--nix/libstore/optimise-store.cc6
6 files changed, 61 insertions, 14 deletions
diff --git a/guix/scripts/gc.scm b/guix/scripts/gc.scm
index 58af8276176..7663efe7f82 100644
--- a/guix/scripts/gc.scm
+++ b/guix/scripts/gc.scm
@@ -261,10 +261,10 @@ is deprecated; use '-D'~%"))
261 ;; Attempt to have at least SPACE bytes available in STORE. 261 ;; Attempt to have at least SPACE bytes available in STORE.
262 (let ((free (free-disk-space (%store-prefix)))) 262 (let ((free (free-disk-space (%store-prefix))))
263 (if (> free space) 263 (if (> free space)
264 (info (G_ "already ~,2h MiBs available on ~a, nothing to do~%") 264 (info (G_ "already ~a available on ~a, nothing to do~%")
265 (/ free 1024. 1024.) (%store-prefix)) 265 (number->size free) (%store-prefix))
266 (let ((to-free (- space free))) 266 (let ((to-free (- space free)))
267 (info (G_ "freeing ~,2h MiBs~%") (/ to-free 1024. 1024.)) 267 (info (G_ "freeing ~a~%") (number->size to-free))
268 (collect-garbage store to-free))))) 268 (collect-garbage store to-free)))))
269 269
270 (define (delete-generations store pattern) 270 (define (delete-generations store pattern)
@@ -328,10 +328,10 @@ is deprecated; use '-D'~%"))
328 (ensure-free-space store free-space)) 328 (ensure-free-space store free-space))
329 (min-freed 329 (min-freed
330 (let-values (((paths freed) (collect-garbage store min-freed))) 330 (let-values (((paths freed) (collect-garbage store min-freed)))
331 (info (G_ "freed ~,2h MiBs~%") (/ freed 1024. 1024.)))) 331 (info (G_ "freed ~a~%") (number->size freed))))
332 (else 332 (else
333 (let-values (((paths freed) (collect-garbage store))) 333 (let-values (((paths freed) (collect-garbage store)))
334 (info (G_ "freed ~,2h MiBs~%") (/ freed 1024. 1024.))))))) 334 (info (G_ "freed ~a~%") (number->size freed)))))))
335 ((list-roots) 335 ((list-roots)
336 (assert-no-extra-arguments) 336 (assert-no-extra-arguments)
337 (list-roots)) 337 (list-roots))
diff --git a/guix/ui.scm b/guix/ui.scm
index cd9eb1013d0..d6d5eb9dcd5 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -19,6 +19,7 @@
19;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com> 19;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com>
20;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info> 20;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
21;;; Copyright © 2022 Liliana Marie Prikler <liliana.prikler@gmail.com> 21;;; Copyright © 2022 Liliana Marie Prikler <liliana.prikler@gmail.com>
22;;; Copyright © 2025 Efraim Flashner <efraim@flashner.co.il>
22;;; 23;;;
23;;; This file is part of GNU Guix. 24;;; This file is part of GNU Guix.
24;;; 25;;;
@@ -99,6 +100,7 @@
99 make-regexp* 100 make-regexp*
100 string->number* 101 string->number*
101 size->number 102 size->number
103 number->size
102 show-derivation-outputs 104 show-derivation-outputs
103 build-notifier 105 build-notifier
104 show-what-to-build 106 show-what-to-build
@@ -695,6 +697,34 @@ interpreted."
695 (x 697 (x
696 (leave (G_ "unknown unit: ~a~%") unit))))))) 698 (leave (G_ "unknown unit: ~a~%") unit)))))))
697 699
700(define (number->size num)
701 "Convert NUM, an integer number of bytes, to a human readable string using
702common storage prefixes."
703 (define (pretty-print-number number exponent)
704 (number->string (inexact->exact (round (/ number (expt 2 exponent))))))
705
706 (unless (number? num)
707 (leave (G_ "invalid number: ~a~%") (object->string num)))
708
709 (cond
710 ((> num (expt 2 80))
711 (string-append (pretty-print-number num 80) " YiB"))
712 ((> num (expt 2 70))
713 (string-append (pretty-print-number num 70) " ZiB"))
714 ((> num (expt 2 60))
715 (string-append (pretty-print-number num 60) " EiB"))
716 ((> num (expt 2 50))
717 (string-append (pretty-print-number num 50) " PiB"))
718 ((> num (expt 2 40))
719 (string-append (pretty-print-number num 40) " TiB"))
720 ((> num (expt 2 30))
721 (string-append (pretty-print-number num 30) " GiB"))
722 ((> num (expt 2 20))
723 (string-append (pretty-print-number num 20) " MiB"))
724 ((> num (expt 2 10))
725 (string-append (pretty-print-number num 10) " KiB"))
726 (#t (string-append (number->string num) " bytes"))))
727
698(define (display-collision-resolution-hint collision) 728(define (display-collision-resolution-hint collision)
699 "Display hints on how to resolve COLLISION, a &profile-collistion-error." 729 "Display hints on how to resolve COLLISION, a &profile-collistion-error."
700 (define (top-most-entry entry) 730 (define (top-most-entry entry)
diff --git a/nix/libstore/gc.cc b/nix/libstore/gc.cc
index 1766a684122..08638b51153 100644
--- a/nix/libstore/gc.cc
+++ b/nix/libstore/gc.cc
@@ -433,8 +433,7 @@ void LocalStore::deletePathRecursive(GCState & state, const Path & path)
433 printMsg(lvlInfo, format("[%1%%%] deleting '%2%'") % percentage % path); 433 printMsg(lvlInfo, format("[%1%%%] deleting '%2%'") % percentage % path);
434 } else { 434 } else {
435 auto freed = state.results.bytesFreed + state.bytesInvalidated; 435 auto freed = state.results.bytesFreed + state.bytesInvalidated;
436 freed /= 1024ULL * 1024ULL; 436 printMsg(lvlInfo, format("[%1%] deleting '%2%'") % showBytes(freed) % path);
437 printMsg(lvlInfo, format("[%1% MiB] deleting '%2%'") % freed % path);
438 } 437 }
439 438
440 state.results.paths.insert(path); 439 state.results.paths.insert(path);
@@ -629,9 +628,9 @@ void LocalStore::removeUnusedLinks(const GCState & state)
629 if (stat(linksDir.c_str(), &st) == -1) 628 if (stat(linksDir.c_str(), &st) == -1)
630 throw SysError(format("statting `%1%'") % linksDir); 629 throw SysError(format("statting `%1%'") % linksDir);
631 long long overhead = st.st_size; 630 long long overhead = st.st_size;
631 long long freedbytes = (unsharedSize - actualSize - overhead);
632 632
633 printMsg(lvlInfo, format("note: currently hard linking saves %.2f MiB") 633 printMsg(lvlInfo, format("note: currently hard linking saves %1%") % showBytes(freedbytes));
634 % ((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
635} 634}
636 635
637 636
diff --git a/nix/libstore/misc.cc b/nix/libstore/misc.cc
index bc5dec88bf2..e9904f3c4f4 100644
--- a/nix/libstore/misc.cc
+++ b/nix/libstore/misc.cc
@@ -1,4 +1,5 @@
1#include "misc.hh" 1#include "misc.hh"
2#include <math.h>
2#include "store-api.hh" 3#include "store-api.hh"
3#include "local-store.hh" 4#include "local-store.hh"
4#include "globals.hh" 5#include "globals.hh"
@@ -94,5 +95,25 @@ Paths topoSortPaths(StoreAPI & store, const PathSet & paths)
94 return sorted; 95 return sorted;
95} 96}
96 97
98/* Max of LLONG_MAX is 8 EiB */
99string showBytes(long long bytes)
100{
101 if (llabs(bytes > exp2l(60))) {
102 return (format("%7.2f EiB") % (bytes / exp2l(60))).str();
103 } else if (llabs(bytes > exp2l(50))) {
104 return (format("%7.2f PiB") % (bytes / exp2l(50))).str();
105 } else if (llabs(bytes > exp2l(40))) {
106 return (format("%7.2f TiB") % (bytes / exp2l(40))).str();
107 } else if (llabs(bytes > exp2l(30))) {
108 return (format("%7.2f GiB") % (bytes / exp2l(30))).str();
109 } else if (llabs(bytes > exp2l(20))) {
110 return (format("%7.2f MiB") % (bytes / exp2l(20))).str();
111 } else if (llabs(bytes > exp2l(10))) {
112 return (format("%7.2f KiB") % (bytes / exp2l(10))).str();
113 } else {
114 return (format("%4f bytes") % bytes).str();
115 }
116}
117
97 118
98} 119}
diff --git a/nix/libstore/misc.hh b/nix/libstore/misc.hh
index f70cda9fd0c..1d36ccf9dce 100644
--- a/nix/libstore/misc.hh
+++ b/nix/libstore/misc.hh
@@ -25,5 +25,6 @@ bool willBuildLocally(const Derivation & drv);
25 25
26bool substitutesAllowed(const Derivation & drv); 26bool substitutesAllowed(const Derivation & drv);
27 27
28string showBytes(long long bytes);
28 29
29} 30}
diff --git a/nix/libstore/optimise-store.cc b/nix/libstore/optimise-store.cc
index 8d5bf28da9d..e17d9160d6c 100644
--- a/nix/libstore/optimise-store.cc
+++ b/nix/libstore/optimise-store.cc
@@ -1,5 +1,6 @@
1#include "config.h" 1#include "config.h"
2 2
3#include "misc.hh"
3#include "util.hh" 4#include "util.hh"
4#include "local-store.hh" 5#include "local-store.hh"
5#include "globals.hh" 6#include "globals.hh"
@@ -252,11 +253,6 @@ void LocalStore::optimiseStore(OptimiseStats & stats)
252 } 253 }
253} 254}
254 255
255static string showBytes(unsigned long long bytes)
256{
257 return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str();
258}
259
260void LocalStore::optimiseStore() 256void LocalStore::optimiseStore()
261{ 257{
262 OptimiseStats stats; 258 OptimiseStats stats;