Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions app/q4nx_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ int run_q4nx_cache(int argc, char** argv) {
: q4nx_cache_bound_bytes(std::getenv("ONEBIT_Q4NX_CACHE_MAX_GIB"));
const int64_t before = q4nx_dir_bytes(root);
if (max_bytes <= 0) {
std::printf("q4nx-cache: %s is %.1f GiB, the sweep is off (bound 0)\n", root.c_str(), double(before) / (1 << 30));
std::printf("q4nx-cache: %s is %s, the sweep is off (bound 0)\n", root.c_str(), q4nx_cache_size_text(before).c_str());
return 0;
}
const auto size = [](int64_t b, char* out, size_t n) {
if (b >= (int64_t(1) << 30)) std::snprintf(out, n, "%.1f GiB", double(b) / (1 << 30));
else std::snprintf(out, n, "%.1f MiB", double(b) / (1 << 20));
};
const auto size = [](int64_t b, char* out, size_t n) { std::snprintf(out, n, "%s", q4nx_cache_size_text(b).c_str()); };
if (dry) {
const std::vector<Q4nxCacheCandidate> would = q4nx_cache_candidates(root, keep, max_bytes, min_age);
int64_t would_free = 0;
Expand Down
19 changes: 16 additions & 3 deletions npu/q4nx_cache_sweep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@

namespace fs = std::filesystem;

std::string q4nx_cache_size_text(int64_t bytes) {
char out[32];
if (bytes >= (int64_t(1) << 30)) {
std::snprintf(out, sizeof out, "%.1f GiB", double(bytes) / (1 << 30));
return out;
}
std::snprintf(out, sizeof out, "%.1f MiB", double(bytes) / (1 << 20));
// Within half a MiB of a GiB the MiB reading rounds to "1024.0", which is a value in the unit it has just left -- the same
// kind of misleading exchange this formatter exists to stop. Print the GiB it is about to become.
if (std::string(out) == "1024.0 MiB") std::snprintf(out, sizeof out, "%.1f GiB", double(bytes) / (1 << 30));
return out;
}

int64_t q4nx_dir_bytes(const std::string& path) {
std::error_code ec;
if (!fs::is_directory(path, ec)) return 0;
Expand Down Expand Up @@ -90,10 +103,10 @@ Q4nxCacheSweep q4nx_cache_evict(const std::string& root, const std::string& keep
if (r.removed > 0) {
r.reported = true;
std::fprintf(stderr,
"note: q4nx cache: removed %d entr%s, %.1f GiB -> %.1f GiB (bound %.1f GiB, %s); entries younger than "
"note: q4nx cache: removed %d entr%s, %s -> %s (bound %s, %s); entries younger than "
"%lld s and the one just written are kept (engine#1956)\n",
r.removed, r.removed == 1 ? "y" : "ies", double(r.bytes_before) / (1 << 30),
double(r.bytes_after) / (1 << 30), double(max_bytes) / (1 << 30), root.c_str(),
r.removed, r.removed == 1 ? "y" : "ies", q4nx_cache_size_text(r.bytes_before).c_str(),
q4nx_cache_size_text(r.bytes_after).c_str(), q4nx_cache_size_text(max_bytes).c_str(), root.c_str(),
(long long)min_age_seconds);
}
return r;
Expand Down
5 changes: 5 additions & 0 deletions npu/q4nx_cache_sweep.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ struct Q4nxCacheCandidate {
std::vector<Q4nxCacheCandidate> q4nx_cache_candidates(const std::string& root, const std::string& keep, int64_t max_bytes,
int64_t min_age_seconds = 600);

// A size as the cache's messages print it: GiB from 1 GiB up, MiB below. A small cache or bound printed as "0.0 GiB" reads
// like "the sweep is off", which is a different state. The one formatter for the sweep's note (shared by every binary that
// links this, and the only output a conversion-only box gets) and for `1bit q4nx-cache`, so the two cannot disagree.
std::string q4nx_cache_size_text(int64_t bytes);

// Directory bytes under `path` (0 when it does not exist).
int64_t q4nx_dir_bytes(const std::string& path);

Expand Down
12 changes: 10 additions & 2 deletions tests/q4nx_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,16 @@ int main() {
"q4nx-cache candidates: the age window protects everything inside it");
fs::remove_all(cr, ec);
}
// engine#2263 review: the sweep's note and the CLI print sizes through one formatter, MiB below 1 GiB, so a small
// cache never reads "0.0 GiB" (which looks like the sweep being off).
check(q4nx_cache_size_text(int64_t(6) << 20) == "6.0 MiB", "size text: 6 MiB is MiB, not 0.0 GiB");
check(q4nx_cache_size_text(0) == "0.0 MiB", "size text: an empty cache is 0.0 MiB");
check(q4nx_cache_size_text((int64_t(1) << 30) - 1) == "1.0 GiB", "size text: just under 1 GiB is 1.0 GiB, not \"1024.0 MiB\"");
check(q4nx_cache_size_text(int64_t(1023) << 20) == "1023.0 MiB", "size text: a real 1023 MiB still reads MiB");
check(q4nx_cache_size_text(int64_t(60) << 30) == "60.0 GiB", "size text: 60 GiB is GiB");
fs::remove_all(root, ec);
std::printf("q4nx cache: %d arm(s) over the sweep (bound, LRU order, keep, age window, disabled, touch on a hit, bound text)\n",
failures ? 0 : 32);
std::printf("q4nx cache: %d arm(s) over the sweep (bound, LRU order, keep, age window, disabled, touch on a hit, bound text, "
"size text)\n",
failures ? 0 : 36);
return failures ? 1 : 0;
}
Loading