Inference engine, OpenAI/Anthropic API on localhost, optional image input, one-file setup (START-HERE.bat / setup.sh). Model files are downloaded from Hugging Face by the setup.
77 lines
2.9 KiB
C++
77 lines
2.9 KiB
C++
// src/platform/memory.cpp - see include/strata/platform/memory.hpp.
|
|
#include "strata/platform/memory.hpp"
|
|
|
|
#if defined(_WIN32)
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#define NOMINMAX
|
|
#include <windows.h>
|
|
#else
|
|
#include <sys/mman.h>
|
|
#endif
|
|
|
|
namespace strata::platform {
|
|
|
|
#if defined(_WIN32)
|
|
LockResult lock_resident(void* p, uint64_t bytes) {
|
|
LockResult r;
|
|
if (p == nullptr || bytes == 0) { r.note = "nothing to lock"; return r; }
|
|
HANDLE self = GetCurrentProcess();
|
|
SIZE_T min_ws = 0, max_ws = 0;
|
|
DWORD flags = 0;
|
|
if (!GetProcessWorkingSetSizeEx(self, &min_ws, &max_ws, &flags)) {
|
|
r.note = "GetProcessWorkingSetSizeEx failed (error " + std::to_string(GetLastError()) + ")";
|
|
return r;
|
|
}
|
|
// Locked pages count against the minimum working set, so it must grow by the region plus headroom for the
|
|
// rest of the process. Soft limits: the maximum is not enforced, only the minimum is raised.
|
|
const SIZE_T margin = (SIZE_T) 512 << 20;
|
|
const SIZE_T new_min = min_ws + (SIZE_T) bytes + margin;
|
|
const SIZE_T new_max = max_ws > new_min + margin ? max_ws : new_min + margin;
|
|
if (!SetProcessWorkingSetSizeEx(self, new_min, new_max,
|
|
QUOTA_LIMITS_HARDWS_MIN_DISABLE | QUOTA_LIMITS_HARDWS_MAX_DISABLE)) {
|
|
r.note = "SetProcessWorkingSetSizeEx(" + std::to_string((unsigned long long) (new_min >> 20)) +
|
|
" MiB) failed (error " + std::to_string(GetLastError()) + ")";
|
|
return r;
|
|
}
|
|
const uint64_t chunk = 1ull << 30;
|
|
uint8_t* base = (uint8_t*) p;
|
|
for (uint64_t off = 0; off < bytes; off += chunk) {
|
|
const uint64_t n = bytes - off < chunk ? bytes - off : chunk;
|
|
if (!VirtualLock(base + off, (SIZE_T) n)) {
|
|
r.note = "VirtualLock stopped at " + std::to_string((unsigned long long) (off >> 20)) + " of " +
|
|
std::to_string((unsigned long long) (bytes >> 20)) + " MiB (error " +
|
|
std::to_string(GetLastError()) + ")";
|
|
r.ok = off > 0;
|
|
return r;
|
|
}
|
|
r.locked_bytes = off + n;
|
|
}
|
|
r.ok = true;
|
|
r.note = "locked " + std::to_string((unsigned long long) (bytes >> 20)) + " MiB via working-set minimum + VirtualLock";
|
|
return r;
|
|
}
|
|
|
|
void unlock_resident(void* p, uint64_t bytes) {
|
|
if (p == nullptr || bytes == 0) return;
|
|
const uint64_t chunk = 1ull << 30;
|
|
for (uint64_t off = 0; off < bytes; off += chunk)
|
|
VirtualUnlock((uint8_t*) p + off, (SIZE_T) (bytes - off < chunk ? bytes - off : chunk));
|
|
}
|
|
#else
|
|
LockResult lock_resident(void* p, uint64_t bytes) {
|
|
LockResult r;
|
|
if (p == nullptr || bytes == 0) { r.note = "nothing to lock"; return r; }
|
|
if (mlock(p, bytes) != 0) { r.note = "mlock failed (raise ulimit -l)"; return r; }
|
|
r.ok = true;
|
|
r.locked_bytes = bytes;
|
|
r.note = "mlock";
|
|
return r;
|
|
}
|
|
|
|
void unlock_resident(void* p, uint64_t bytes) {
|
|
if (p != nullptr && bytes != 0) munlock(p, bytes);
|
|
}
|
|
#endif
|
|
|
|
} // namespace strata::platform
|