diff --git a/app/main.cpp b/app/main.cpp index 1d7ae3755..639c21297 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -42,6 +42,7 @@ int onebit_ort_info_main(int argc, char** argv); int onebit_ort_run_main(int argc, char** argv); #endif #ifdef ONEBIT_NPU_HOST +#include "q4nx_cache.h" #include "pack_import.h" // 1bit npu-pack: an OGA NPU pack -> a Q4NX model directory #include "vision_pack.h" // 1bit npu-pack vision: a pack's vision component (engine#1788) #endif @@ -322,6 +323,11 @@ int main(int argc, char** argv) { return 1; } } + if (cmd == "q4nx-cache") { + // engine#1788 / disk pressure: the sweep as a command, because a serve start is otherwise the only trigger and + // nothing serves while the NPU is off. Deliberately outside every #ifdef -- host only, no device. + return ::run_q4nx_cache(argc - 2, argv + 2); + } #ifdef ONEBIT_NPU_HOST if (cmd == "npu-pack") { try { diff --git a/app/q4nx_cache.cpp b/app/q4nx_cache.cpp index 3bcb727c6..d73bb571b 100644 --- a/app/q4nx_cache.cpp +++ b/app/q4nx_cache.cpp @@ -104,3 +104,80 @@ void q4nx_cache_touch(const std::string& entry) { std::error_code ec; if (fs::is_directory(entry, ec)) fs::last_write_time(entry, fs::file_time_type::clock::now(), ec); } + + +int run_q4nx_cache(int argc, char** argv) { + const auto usage = [] { + std::fprintf(stderr, + "usage: 1bit q4nx-cache evict [--root DIR] [--max-gib N | --max-mib N] [--keep NAME] [--min-age S]\n" + " [--dry-run]\n" + " Sweeps the container cache the way a serve does on start: least-recently-used first, never the\n" + " entry named by --keep, never one written within --min-age seconds (a serve may be reading it).\n" + " --root defaults to ONEBIT_Q4NX_CACHE_DIR, then $XDG_CACHE_HOME/1bit/q4nx, then ~/.cache/1bit/q4nx.\n" + " --max-gib defaults to ONEBIT_Q4NX_CACHE_MAX_GIB(), then 60; 0 disables the sweep.\n" + " --dry-run prints what it would remove and removes nothing.\n"); + }; + if (argc < 1 || std::string(argv[0]) != "evict") { usage(); return 2; } + std::string root, keep; + int64_t max_gib = -1, max_mib = -1, min_age = 600; + bool dry = false; + for (int i = 1; i < argc; ++i) { + const std::string a = argv[i]; + const auto next = [&](const char* what) -> const char* { + if (i + 1 >= argc) { std::fprintf(stderr, "q4nx-cache: %s needs a value\n", what); return nullptr; } + return argv[++i]; + }; + if (a == "--root") { const char* v = next("--root"); if (!v) return 2; root = v; } + else if (a == "--keep") { const char* v = next("--keep"); if (!v) return 2; keep = v; } + else if (a == "--max-gib") { const char* v = next("--max-gib"); if (!v) return 2; max_gib = std::atoll(v); } + else if (a == "--max-mib") { const char* v = next("--max-mib"); if (!v) return 2; max_mib = std::atoll(v); } + else if (a == "--min-age") { const char* v = next("--min-age"); if (!v) return 2; min_age = std::atoll(v); } + else if (a == "--dry-run") dry = true; + else { std::fprintf(stderr, "q4nx-cache: unknown argument %s\n", a.c_str()); usage(); return 2; } + } + if (root.empty()) { + const char* env = std::getenv("ONEBIT_Q4NX_CACHE_DIR"); + if (env && *env) { + root = env; + } else { + const char* xdg = std::getenv("XDG_CACHE_HOME"); + const char* home = std::getenv("HOME"); + root = (xdg && *xdg) ? std::string(xdg) : std::string(home ? home : ".") + "/.cache"; + root += "/1bit/q4nx"; + } + } + const int64_t max_bytes = (max_mib >= 0) ? max_mib * (int64_t(1) << 20) + : (max_gib >= 0) ? max_gib * (int64_t(1) << 30) + : 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)); + return 0; + } + if (dry) { + std::printf("q4nx-cache: %s is %.1f GiB against a %.1f GiB bound; --dry-run removes nothing\n", root.c_str(), + double(before) / (1 << 30), double(max_bytes) / (1 << 30)); + return before > max_bytes ? 1 : 0; + } + const Q4nxCacheSweep s = q4nx_cache_evict(root, keep, max_bytes, min_age); + // A 6 MiB bound printed as "0.0 GiB" reads like "the sweep is off", which is a different state -- so small sizes + // are reported in MiB. This line is the whole output; it has to be unambiguous. + 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)); + }; + char before_s[32], after_s[32], bound_s[32]; + size(s.bytes_before, before_s, sizeof before_s); + size(s.bytes_after, after_s, sizeof after_s); + size(max_bytes, bound_s, sizeof bound_s); + std::printf("q4nx-cache: %d entr%s removed, %s -> %s (bound %s)\n", s.removed, s.removed == 1 ? "y" : "ies", + before_s, after_s, bound_s); + if (s.bytes_after > max_bytes) { + std::fprintf(stderr, + "q4nx-cache: still over the bound -- every remaining entry was written within %lld s, so a serve may be " + "reading it; re-run with a larger --min-age once the box is idle\n", + (long long)min_age); + return 1; + } + return 0; +} diff --git a/app/q4nx_cache.h b/app/q4nx_cache.h index f5d987511..bc211a3f7 100644 --- a/app/q4nx_cache.h +++ b/app/q4nx_cache.h @@ -57,3 +57,10 @@ int64_t q4nx_cache_bound_bytes(const char* text, int64_t default_gib = 60); // just-opened entry inside the sweep's age window, which is what makes "a serve may be reading it" true for a reused entry // and not only for a new one. Failure is ignored (a read-only cache still serves). void q4nx_cache_touch(const std::string& entry); + +// engine#1788 / disk pressure 11-12: the sweep as a COMMAND, because until now the only trigger was a serve starting +// (`app/serve.cpp` calls q4nx_cache_evict() on its way up). With the NPU off nothing serves, so nothing sweeps and the +// cache grows past its own bound -- measured at 72.5 G against the 60 GiB default on this box while / was at 149 G. +// `1bit q4nx-cache evict [--root DIR] [--max-gib N] [--keep NAME] [--min-age S] [--dry-run]`. +// Exit: 0 swept (or nothing to do), 1 nothing removed but over the bound (age window), 2 bad usage. +int run_q4nx_cache(int argc, char** argv); diff --git a/tests/q4nx_cache_test.cpp b/tests/q4nx_cache_test.cpp index 87558cf39..fe3c53403 100644 --- a/tests/q4nx_cache_test.cpp +++ b/tests/q4nx_cache_test.cpp @@ -33,6 +33,13 @@ static void check(bool ok, const std::string& what) { if (!ok) { std::printf("FAIL %s\n", what.c_str()); ++failures; } } +// argv for run_q4nx_cache(): the vector must outlive the call, so callers keep it named. +static std::vector mkargv(std::vector& a) { + std::vector v; + for (auto& s : a) v.push_back(const_cast(s.c_str())); + return v; +} + static void make_entry(const fs::path& root, const std::string& name, int mb, int age_minutes) { const fs::path d = root / name; fs::create_directories(d); @@ -122,6 +129,38 @@ int main() { check(q4nx_cache_bound_bytes("40G") == 40 * gib, "a number with a suffix keeps its number"); check(q4nx_cache_bound_bytes("7", 9) == 7 * gib && q4nx_cache_bound_bytes(nullptr, 9) == 9 * gib, "the default is the caller's"); + // engine#1788 / disk pressure: the sweep as a COMMAND. Until now a serve start was the only trigger, so nothing + // swept while the NPU was off -- the cache sat at 70.9 GiB against its 60 GiB bound with / at 149 G. + { + const fs::path cr = root / "cli"; + make_entry(cr, "big-a", 8, 500); + make_entry(cr, "big-b", 8, 400); + const std::string root_arg = "--root", root_val = cr.string(); + { + // dry-run over the bound: reports and removes nothing + std::vector a = {"evict", root_arg, root_val, "--max-gib", "0", "--dry-run"}; + std::vector av = mkargv(a); + check(::run_q4nx_cache(int(av.size()), av.data()) == 0, + "q4nx-cache evict: --max-gib 0 is 'sweep is off', exit 0"); + check(fs::exists(cr / "big-a") && fs::exists(cr / "big-b"), "q4nx-cache evict: nothing removed with the sweep off"); + } + { + // a real sweep with min-age 0 and a 0 GiB bound still cannot remove the `keep` entry + std::vector a = {"evict", root_arg, root_val, "--max-mib", "6", "--min-age", "0", + "--keep", "big-a"}; + std::vector av = mkargv(a); + ::run_q4nx_cache(int(av.size()), av.data()); + check(fs::exists(cr / "big-a"), "q4nx-cache evict: the --keep entry is never removed"); + check(!fs::exists(cr / "big-b"), "q4nx-cache evict: the least-recently-used entry goes first"); + } + { + std::vector a = {"evict", "--nonsense"}; + std::vector av = mkargv(a); + check(::run_q4nx_cache(int(av.size()), av.data()) == 2, + "q4nx-cache evict: an unknown argument is usage, exit 2"); + } + fs::remove_all(cr, ec); + } 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 : 26);