| 21 | |
| 22 | #if defined(_WIN32) |
| 23 | LockResult lock_resident(void* p, uint64_t bytes) { |
| 24 | LockResult r; |
| 25 | if (p == nullptr || bytes == 0) { r.note = "nothing to lock"; return r; } |
| 26 | HANDLE self = GetCurrentProcess(); |
| 27 | SIZE_T min_ws = 0, max_ws = 0; |
| 28 | DWORD flags = 0; |
| 29 | if (!GetProcessWorkingSetSizeEx(self, &min_ws, &max_ws, &flags)) { |
| 30 | r.note = "GetProcessWorkingSetSizeEx failed (error " + std::to_string(GetLastError()) + ")"; |
| 31 | return r; |
| 32 | } |
| 33 | // Locked pages count against the minimum working set, so it must grow by the region plus headroom for the |
| 34 | // rest of the process. Soft limits: the maximum is not enforced, only the minimum is raised. |
| 35 | const SIZE_T margin = (SIZE_T) 512 << 20; |
| 36 | const SIZE_T new_min = min_ws + (SIZE_T) bytes + margin; |
| 37 | const SIZE_T new_max = max_ws > new_min + margin ? max_ws : new_min + margin; |
| 38 | if (!SetProcessWorkingSetSizeEx(self, new_min, new_max, |
| 39 | QUOTA_LIMITS_HARDWS_MIN_DISABLE | QUOTA_LIMITS_HARDWS_MAX_DISABLE)) { |
| 40 | r.note = "SetProcessWorkingSetSizeEx(" + std::to_string((unsigned long long) (new_min >> 20)) + |
| 41 | " MiB) failed (error " + std::to_string(GetLastError()) + ")"; |
| 42 | return r; |
| 43 | } |
| 44 | const uint64_t chunk = 1ull << 30; |
| 45 | uint8_t* base = (uint8_t*) p; |
| 46 | for (uint64_t off = 0; off < bytes; off += chunk) { |
| 47 | const uint64_t n = bytes - off < chunk ? bytes - off : chunk; |
| 48 | if (!VirtualLock(base + off, (SIZE_T) n)) { |
| 49 | r.note = "VirtualLock stopped at " + std::to_string((unsigned long long) (off >> 20)) + " of " + |
| 50 | std::to_string((unsigned long long) (bytes >> 20)) + " MiB (error " + |
| 51 | std::to_string(GetLastError()) + ")"; |
| 52 | r.ok = off > 0; |
| 53 | return r; |
| 54 | } |
| 55 | r.locked_bytes = off + n; |
| 56 | } |
| 57 | r.ok = true; |
| 58 | r.note = "locked " + std::to_string((unsigned long long) (bytes >> 20)) + " MiB via working-set minimum + VirtualLock"; |
| 59 | return r; |
| 60 | } |
| 61 | |
| 62 | LockResult lock_resident_ranges(const std::vector<std::pair<void*, uint64_t>>& regions) { |
| 63 | LockResult r; |