ple: --ple-io ram faults the table in on up to 16 threads before mlock (#651)
mlock alone brings a cold file in one page at a time from one thread, ~0.47 GiB/s (about two minutes for a 54 GB Q8_0 table); 16 threads reach ~2 GiB/s on anon761's machine. The pages are then resident and mlock only pins them. When mlock fails, the pages stay faulted in (the message says they may be reclaimed) instead of being touched again one by one. ple_reader_test gets --make-table PATH FORMAT (a big synthetic table, with --rows) and --ram-time PATH (open mapped and locked, print the seconds) to time it cold. Ported by the maintainer from anon761's PR #651. Co-authored-by: ksanislo <ksanislo@users.noreply.github.com>
This commit is contained in:
1 parent
e79b8fc143
commit
626a89bc1e
2 files changed
+53
-5
No files matched your search
+24
-5
@@ -15,6 +15,9 @@
|
||||
#if !defined(_WIN32)
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -308,14 +311,30 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO
|
||||
const uintptr_t a0 = (uintptr_t) impl_->data & ~(uintptr_t) (page - 1);
|
||||
const uintptr_t a1 = (uintptr_t) impl_->data + (uintptr_t) need;
|
||||
madvise((void*) a0, a1 - a0, MADV_WILLNEED);
|
||||
// Fault the table in with several threads first: mlock (and a single toucher) brings it in one page at a time
|
||||
// from one thread, ~0.5 GB/s from a cold file - 106 s for a 54 GB Q8_0 table. The pages are then resident
|
||||
// and mlock only pins them.
|
||||
{
|
||||
constexpr uintptr_t kPiece = 64ull << 20;
|
||||
const uintptr_t pieces = (a1 - a0 + kPiece - 1) / kPiece;
|
||||
const unsigned threads = std::max(1u, std::min(16u, std::thread::hardware_concurrency()));
|
||||
std::atomic<uintptr_t> next{0};
|
||||
auto touch = [&] {
|
||||
volatile uint8_t sink = 0;
|
||||
for (uintptr_t i; (i = next.fetch_add(1)) < pieces;)
|
||||
for (uintptr_t p = a0 + i * kPiece, e = std::min(a1, p + kPiece); p < e; p += page)
|
||||
sink = sink + *(const volatile uint8_t*) p;
|
||||
(void) sink;
|
||||
};
|
||||
std::vector<std::thread> pool;
|
||||
for (unsigned t = 0; t < threads; ++t) pool.emplace_back(touch);
|
||||
for (auto& t : pool) t.join();
|
||||
}
|
||||
if (mlock((const void*) a0, a1 - a0) == 0) {
|
||||
impl_->locked = true;
|
||||
} else {
|
||||
std::fprintf(stderr, "strata: PLE table mlock failed (%s; raise `ulimit -l`): touching its pages instead\n",
|
||||
std::strerror(errno));
|
||||
volatile uint8_t sink = 0;
|
||||
for (uintptr_t p = a0; p < a1; p += page) sink = sink + *(const volatile uint8_t*) p;
|
||||
(void) sink;
|
||||
std::fprintf(stderr, "strata: PLE table mlock failed (%s; raise `ulimit -l`): its pages stay faulted in "
|
||||
"but may be reclaimed\n", std::strerror(errno));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -411,9 +411,12 @@ int main(int argc, char** argv) {
|
||||
bool direct_only = false;
|
||||
bool sync_submit = false;
|
||||
bool self = false;
|
||||
std::string make_path, make_fmt, ram_path; // --make-table PATH FORMAT (uses --rows), --ram-time PATH
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
const std::string a = argv[i];
|
||||
if (a == "--selftest") self = true;
|
||||
else if (a == "--make-table" && i + 2 < argc) { make_path = argv[i + 1]; make_fmt = argv[i + 2]; i += 2; }
|
||||
else if (a == "--ram-time" && i + 1 < argc) ram_path = argv[++i];
|
||||
else if (a == "--dir" && i + 1 < argc) dir = argv[++i];
|
||||
else if (a == "--gguf" && i + 1 < argc) gguf = argv[++i];
|
||||
else if (a == "--rows" && i + 1 < argc) rows = std::atoi(argv[++i]);
|
||||
@@ -433,6 +436,32 @@ int main(int argc, char** argv) {
|
||||
const int r110 = selftest(dir, 110);
|
||||
return r90 != 0 ? r90 : r110;
|
||||
}
|
||||
if (!make_path.empty()) { // a big synthetic table, for timing --ple-io ram's fault-in
|
||||
for (int i = 0; i < k::ple_format_count(); ++i) {
|
||||
const k::PleFormatInfo& f = k::ple_formats()[i];
|
||||
if (make_fmt != f.name) continue;
|
||||
const std::string tmp = write_format_table(std::filesystem::path(make_path).parent_path().string(), f, (uint32_t) rows);
|
||||
if (tmp.empty()) return 1;
|
||||
std::filesystem::rename(tmp, make_path);
|
||||
std::printf("wrote %s: %s, %d rows, %.2f GiB\n", make_path.c_str(), f.name, rows,
|
||||
(double) rows * f.row_bytes / (1024.0 * 1024.0 * 1024.0));
|
||||
return 0;
|
||||
}
|
||||
std::fprintf(stderr, "unknown format %s\n", make_fmt.c_str());
|
||||
return 2;
|
||||
}
|
||||
if (!ram_path.empty()) { // open mapped and locked (--ple-io ram): the seconds it takes
|
||||
k::PleTable t;
|
||||
k::PleIoOptions io;
|
||||
io.mode = k::PleIo::Mmap;
|
||||
io.lock = true;
|
||||
std::string err;
|
||||
const double t0 = now_us();
|
||||
const bool ok = t.open(ram_path, err, io);
|
||||
std::printf("ram open: %s, format %s, locked %d, %.2f s\n", ok ? "ok" : err.c_str(), ok ? t.format() : "-", ok && t.locked(),
|
||||
(now_us() - t0) / 1e6);
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
if (!gguf.empty()) return real(gguf, rows, tokens, inflight, direct_first, direct_only, sync_submit);
|
||||
std::fprintf(stderr, "nothing to do\n");
|
||||
return 2;
|
||||
|
||||
Reference in new issue
Block a user