Skip to content

Commit c5515bb

Browse files
authored
Merge pull request #14364 from MarcelCoding/human-sizes
diff-closures: print sizes with dynamic unit
2 parents 91ed370 + 584a8e8 commit c5515bb

File tree

10 files changed

+24
-30
lines changed

10 files changed

+24
-30
lines changed

src/libfetchers/git-utils.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "nix/util/users.hh"
1010
#include "nix/util/fs-sink.hh"
1111
#include "nix/util/sync.hh"
12+
#include "nix/util/util.hh"
1213

1314
#include <git2/attr.h>
1415
#include <git2/blob.h>
@@ -530,12 +531,12 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
530531
auto act = (Activity *) payload;
531532
act->result(
532533
resFetchStatus,
533-
fmt("%d/%d objects received, %d/%d deltas indexed, %.1f MiB",
534+
fmt("%d/%d objects received, %d/%d deltas indexed, %s",
534535
stats->received_objects,
535536
stats->total_objects,
536537
stats->indexed_deltas,
537538
stats->total_deltas,
538-
stats->received_bytes / (1024.0 * 1024.0)));
539+
renderSize(stats->received_bytes)));
539540
return getInterrupted() ? -1 : 0;
540541
}
541542

src/libmain/include/nix/main/shared.hh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ extern volatile ::sig_atomic_t blockInt;
8989

9090
/* GC helpers. */
9191

92-
std::string showBytes(uint64_t bytes);
93-
9492
struct GCResults;
9593

9694
struct PrintFreed

src/libmain/shared.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "nix/main/loggers.hh"
77
#include "nix/main/progress-bar.hh"
88
#include "nix/util/signals.hh"
9+
#include "nix/util/util.hh"
910

1011
#include <algorithm>
1112
#include <exception>
@@ -64,18 +65,19 @@ void printMissing(ref<Store> store, const MissingPaths & missing, Verbosity lvl)
6465
}
6566

6667
if (!missing.willSubstitute.empty()) {
67-
const float downloadSizeMiB = missing.downloadSize / (1024.f * 1024.f);
68-
const float narSizeMiB = missing.narSize / (1024.f * 1024.f);
6968
if (missing.willSubstitute.size() == 1) {
7069
printMsg(
71-
lvl, "this path will be fetched (%.2f MiB download, %.2f MiB unpacked):", downloadSizeMiB, narSizeMiB);
70+
lvl,
71+
"this path will be fetched (%s download, %s unpacked):",
72+
renderSize(missing.downloadSize),
73+
renderSize(missing.narSize));
7274
} else {
7375
printMsg(
7476
lvl,
75-
"these %d paths will be fetched (%.2f MiB download, %.2f MiB unpacked):",
77+
"these %d paths will be fetched (%s download, %s unpacked):",
7678
missing.willSubstitute.size(),
77-
downloadSizeMiB,
78-
narSizeMiB);
79+
renderSize(missing.downloadSize),
80+
renderSize(missing.narSize));
7981
}
8082
std::vector<const StorePath *> willSubstituteSorted = {};
8183
std::for_each(missing.willSubstitute.begin(), missing.willSubstitute.end(), [&](const StorePath & p) {
@@ -406,7 +408,7 @@ RunPager::~RunPager()
406408
PrintFreed::~PrintFreed()
407409
{
408410
if (show)
409-
std::cout << fmt("%d store paths deleted, %s freed\n", results.paths.size(), showBytes(results.bytesFreed));
411+
std::cout << fmt("%d store paths deleted, %s freed\n", results.paths.size(), renderSize(results.bytesFreed));
410412
}
411413

412414
} // namespace nix

src/libstore/gc.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "nix/util/finally.hh"
66
#include "nix/util/unix-domain-socket.hh"
77
#include "nix/util/signals.hh"
8+
#include "nix/util/util.hh"
89
#include "nix/store/posix-fs-canonicalise.hh"
910

1011
#include "store-config-private.hh"
@@ -906,9 +907,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
906907
#endif
907908
;
908909

909-
printInfo(
910-
"note: currently hard linking saves %.2f MiB",
911-
((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
910+
printInfo("note: currently hard linking saves %s", renderSize(unsharedSize - actualSize - overhead));
912911
}
913912

914913
/* While we're at it, vacuum the database. */

src/libstore/optimise-store.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void LocalStore::optimiseStore()
312312

313313
optimiseStore(stats);
314314

315-
printInfo("%s freed by hard-linking %d files", showBytes(stats.bytesFreed), stats.filesLinked);
315+
printInfo("%s freed by hard-linking %d files", renderSize(stats.bytesFreed), stats.filesLinked);
316316
}
317317

318318
void LocalStore::optimisePath(const Path & path, RepairFlag repair)

src/libutil-tests/util.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ TEST(renderSize, misc)
158158
ASSERT_EQ(renderSize(972, true), " 0.9 KiB");
159159
ASSERT_EQ(renderSize(973, true), " 1.0 KiB"); // FIXME: should round down
160160
ASSERT_EQ(renderSize(1024, true), " 1.0 KiB");
161+
ASSERT_EQ(renderSize(-1024, true), " -1.0 KiB");
161162
ASSERT_EQ(renderSize(1024 * 1024, true), "1024.0 KiB");
162163
ASSERT_EQ(renderSize(1100 * 1024, true), " 1.1 MiB");
163164
ASSERT_EQ(renderSize(2ULL * 1024 * 1024 * 1024, true), " 2.0 GiB");

src/libutil/include/nix/util/util.hh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ N string2IntWithUnitPrefix(std::string_view s)
104104
* GiB`. If `align` is set, the number will be right-justified by
105105
* padding with spaces on the left.
106106
*/
107-
std::string renderSize(uint64_t value, bool align = false);
107+
std::string renderSize(int64_t value, bool align = false);
108108

109109
/**
110110
* Parse a string into a float.
@@ -333,8 +333,6 @@ struct overloaded : Ts...
333333
template<class... Ts>
334334
overloaded(Ts...) -> overloaded<Ts...>;
335335

336-
std::string showBytes(uint64_t bytes);
337-
338336
/**
339337
* Provide an addition operator between strings and string_views
340338
* inexplicably omitted from the standard library.

src/libutil/util.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,16 @@ std::optional<N> string2Float(const std::string_view s)
132132
template std::optional<double> string2Float<double>(const std::string_view s);
133133
template std::optional<float> string2Float<float>(const std::string_view s);
134134

135-
std::string renderSize(uint64_t value, bool align)
135+
std::string renderSize(int64_t value, bool align)
136136
{
137137
static const std::array<char, 9> prefixes{{'K', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'}};
138138
size_t power = 0;
139-
double res = value;
140-
while (res > 1024 && power < prefixes.size()) {
139+
double abs_value = std::abs(value);
140+
while (abs_value > 1024 && power < prefixes.size()) {
141141
++power;
142-
res /= 1024;
142+
abs_value /= 1024;
143143
}
144+
double res = (double) value / std::pow(1024.0, power);
144145
return fmt(align ? "%6.1f %ciB" : "%.1f %ciB", power == 0 ? res / 1024 : res, prefixes.at(power));
145146
}
146147

@@ -256,9 +257,4 @@ std::pair<std::string_view, std::string_view> getLine(std::string_view s)
256257
}
257258
}
258259

259-
std::string showBytes(uint64_t bytes)
260-
{
261-
return fmt("%.2f MiB", bytes / (1024.0 * 1024.0));
262-
}
263-
264260
} // namespace nix

src/nix/diff-closures.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ void printClosureDiff(
107107
if (!removed.empty() || !added.empty())
108108
items.push_back(fmt("%s → %s", showVersions(removed), showVersions(added)));
109109
if (showDelta)
110-
items.push_back(
111-
fmt("%s%+.1f KiB" ANSI_NORMAL, sizeDelta > 0 ? ANSI_RED : ANSI_GREEN, sizeDelta / 1024.0));
110+
items.push_back(fmt("%s%s" ANSI_NORMAL, sizeDelta > 0 ? ANSI_RED : ANSI_GREEN, renderSize(sizeDelta)));
112111
logger->cout("%s%s: %s", indent, name, concatStringsSep(", ", items));
113112
}
114113
}

src/nix/path-info.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
141141
void printSize(std::ostream & str, uint64_t value)
142142
{
143143
if (humanReadable)
144-
str << fmt("\t%s", renderSize(value, true));
144+
str << fmt("\t%s", renderSize((int64_t) value, true));
145145
else
146146
str << fmt("\t%11d", value);
147147
}

0 commit comments

Comments
 (0)