guix_mirror_bot pushed a commit to branch master
in repository guix.

commit cc588d8eb6a5e05bc8c9d41855685a1d8ce70187
Author: Efraim Flashner <[email protected]>
AuthorDate: Sun Jun 29 10:21:12 2025 +0300

    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
---
 guix/scripts/gc.scm            | 10 +++++-----
 guix/ui.scm                    | 30 ++++++++++++++++++++++++++++++
 nix/libstore/gc.cc             |  7 +++----
 nix/libstore/misc.cc           | 21 +++++++++++++++++++++
 nix/libstore/misc.hh           |  1 +
 nix/libstore/optimise-store.cc |  6 +-----
 6 files changed, 61 insertions(+), 14 deletions(-)

diff --git a/guix/scripts/gc.scm b/guix/scripts/gc.scm
index 58af827617..7663efe7f8 100644
--- a/guix/scripts/gc.scm
+++ b/guix/scripts/gc.scm
@@ -261,10 +261,10 @@ is deprecated; use '-D'~%"))
     ;; Attempt to have at least SPACE bytes available in STORE.
     (let ((free (free-disk-space (%store-prefix))))
       (if (> free space)
-          (info (G_ "already ~,2h MiBs available on ~a, nothing to do~%")
-                (/ free 1024. 1024.) (%store-prefix))
+          (info (G_ "already ~a available on ~a, nothing to do~%")
+                (number->size free) (%store-prefix))
           (let ((to-free (- space free)))
-            (info (G_ "freeing ~,2h MiBs~%") (/ to-free 1024. 1024.))
+            (info (G_ "freeing ~a~%") (number->size to-free))
             (collect-garbage store to-free)))))
 
   (define (delete-generations store pattern)
@@ -328,10 +328,10 @@ is deprecated; use '-D'~%"))
              (ensure-free-space store free-space))
             (min-freed
              (let-values (((paths freed) (collect-garbage store min-freed)))
-              (info (G_ "freed ~,2h MiBs~%") (/ freed 1024. 1024.))))
+              (info (G_ "freed ~a~%") (number->size freed))))
             (else
              (let-values (((paths freed) (collect-garbage store)))
-              (info (G_ "freed ~,2h MiBs~%") (/ freed 1024. 1024.)))))))
+              (info (G_ "freed ~a~%") (number->size freed)))))))
         ((list-roots)
          (assert-no-extra-arguments)
          (list-roots))
diff --git a/guix/ui.scm b/guix/ui.scm
index cd9eb1013d..d6d5eb9dcd 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -19,6 +19,7 @@
 ;;; Copyright © 2018 Steve Sprang <[email protected]>
 ;;; Copyright © 2022 Taiju HIGASHI <[email protected]>
 ;;; Copyright © 2022 Liliana Marie Prikler <[email protected]>
+;;; Copyright © 2025 Efraim Flashner <[email protected]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -99,6 +100,7 @@
             make-regexp*
             string->number*
             size->number
+            number->size
             show-derivation-outputs
             build-notifier
             show-what-to-build
@@ -695,6 +697,34 @@ interpreted."
           (x
            (leave (G_ "unknown unit: ~a~%") unit)))))))
 
+(define (number->size num)
+  "Convert NUM, an integer number of bytes, to a human readable string using
+common storage prefixes."
+  (define (pretty-print-number number exponent)
+    (number->string (inexact->exact (round (/ number (expt 2 exponent))))))
+
+  (unless (number? num)
+    (leave (G_ "invalid number: ~a~%") (object->string num)))
+
+  (cond
+    ((> num (expt 2 80))
+     (string-append (pretty-print-number num 80) " YiB"))
+    ((> num (expt 2 70))
+     (string-append (pretty-print-number num 70) " ZiB"))
+    ((> num (expt 2 60))
+     (string-append (pretty-print-number num 60) " EiB"))
+    ((> num (expt 2 50))
+     (string-append (pretty-print-number num 50) " PiB"))
+    ((> num (expt 2 40))
+     (string-append (pretty-print-number num 40) " TiB"))
+    ((> num (expt 2 30))
+     (string-append (pretty-print-number num 30) " GiB"))
+    ((> num (expt 2 20))
+     (string-append (pretty-print-number num 20) " MiB"))
+    ((> num (expt 2 10))
+     (string-append (pretty-print-number num 10) " KiB"))
+    (#t (string-append (number->string num) " bytes"))))
+
 (define (display-collision-resolution-hint collision)
   "Display hints on how to resolve COLLISION, a &profile-collistion-error."
   (define (top-most-entry entry)
diff --git a/nix/libstore/gc.cc b/nix/libstore/gc.cc
index 1766a68412..08638b5115 100644
--- a/nix/libstore/gc.cc
+++ b/nix/libstore/gc.cc
@@ -433,8 +433,7 @@ void LocalStore::deletePathRecursive(GCState & state, const 
Path & path)
        printMsg(lvlInfo, format("[%1%%%] deleting '%2%'") % percentage % path);
     } else {
        auto freed = state.results.bytesFreed + state.bytesInvalidated;
-       freed /=  1024ULL * 1024ULL;
-       printMsg(lvlInfo, format("[%1% MiB] deleting '%2%'") % freed % path);
+       printMsg(lvlInfo, format("[%1%] deleting '%2%'") % showBytes(freed) % 
path);
     }
 
     state.results.paths.insert(path);
@@ -629,9 +628,9 @@ void LocalStore::removeUnusedLinks(const GCState & state)
     if (stat(linksDir.c_str(), &st) == -1)
         throw SysError(format("statting `%1%'") % linksDir);
     long long overhead = st.st_size;
+    long long freedbytes = (unsharedSize - actualSize - overhead);
 
-    printMsg(lvlInfo, format("note: currently hard linking saves %.2f MiB")
-        % ((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
+    printMsg(lvlInfo, format("note: currently hard linking saves %1%") % 
showBytes(freedbytes));
 }
 
 
diff --git a/nix/libstore/misc.cc b/nix/libstore/misc.cc
index bc5dec88bf..e9904f3c4f 100644
--- a/nix/libstore/misc.cc
+++ b/nix/libstore/misc.cc
@@ -1,4 +1,5 @@
 #include "misc.hh"
+#include <math.h>
 #include "store-api.hh"
 #include "local-store.hh"
 #include "globals.hh"
@@ -94,5 +95,25 @@ Paths topoSortPaths(StoreAPI & store, const PathSet & paths)
     return sorted;
 }
 
+/* Max of LLONG_MAX is 8 EiB */
+string showBytes(long long bytes)
+{
+    if (llabs(bytes > exp2l(60))) {
+        return (format("%7.2f EiB") % (bytes / exp2l(60))).str();
+    } else if (llabs(bytes > exp2l(50))) {
+        return (format("%7.2f PiB") % (bytes / exp2l(50))).str();
+    } else if (llabs(bytes > exp2l(40))) {
+        return (format("%7.2f TiB") % (bytes / exp2l(40))).str();
+    } else if (llabs(bytes > exp2l(30))) {
+        return (format("%7.2f GiB") % (bytes / exp2l(30))).str();
+    } else if (llabs(bytes > exp2l(20))) {
+        return (format("%7.2f MiB") % (bytes / exp2l(20))).str();
+    } else if (llabs(bytes > exp2l(10))) {
+        return (format("%7.2f KiB") % (bytes / exp2l(10))).str();
+    } else {
+        return (format("%4f  bytes") % bytes).str();
+    }
+}
+
 
 }
diff --git a/nix/libstore/misc.hh b/nix/libstore/misc.hh
index f70cda9fd0..1d36ccf9dc 100644
--- a/nix/libstore/misc.hh
+++ b/nix/libstore/misc.hh
@@ -25,5 +25,6 @@ bool willBuildLocally(const Derivation & drv);
 
 bool substitutesAllowed(const Derivation & drv);
 
+string showBytes(long long bytes);
 
 }
diff --git a/nix/libstore/optimise-store.cc b/nix/libstore/optimise-store.cc
index 8d5bf28da9..e17d9160d6 100644
--- a/nix/libstore/optimise-store.cc
+++ b/nix/libstore/optimise-store.cc
@@ -1,5 +1,6 @@
 #include "config.h"
 
+#include "misc.hh"
 #include "util.hh"
 #include "local-store.hh"
 #include "globals.hh"
@@ -252,11 +253,6 @@ void LocalStore::optimiseStore(OptimiseStats & stats)
     }
 }
 
-static string showBytes(unsigned long long bytes)
-{
-    return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str();
-}
-
 void LocalStore::optimiseStore()
 {
     OptimiseStats stats;

Reply via email to