forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_load_benchmark.cpp
More file actions
456 lines (401 loc) · 16.5 KB
/
Copy pathfile_load_benchmark.cpp
File metadata and controls
456 lines (401 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <algorithm>
#include <cerrno>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <string_view>
#include <thread>
#include <tuple>
#include <vector>
#include "openvino/util/file_util.hpp"
#include "openvino/util/memory.hpp"
#include "openvino/util/mmap_object.hpp"
#ifdef __linux__
# include <fcntl.h>
# include <sys/mman.h>
# include <unistd.h>
#elif defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
#endif
#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/file_utils.hpp"
// These benchmarks measure wall-clock timing and are meaningless (and extremely slow for
// multi-GB files) in a Debug (-O0) build.
#ifndef NDEBUG
# error \
"file_load_benchmark.cpp must be built in Release mode: rebuild with -DCMAKE_BUILD_TYPE=Release, or delete this #error to build in Debug anyway."
#endif
namespace ov::test {
namespace {
const size_t page_size = static_cast<size_t>(util::get_system_page_size());
#ifdef __linux__
// mlock forces every page resident before returning; munlock releases the pin without evicting.
// Bounded by RLIMIT_MEMLOCK -- no limit on a privileged process.
void ensure_memory_resident(const std::shared_ptr<ov::MappedMemory>& mapped) {
ASSERT_EQ(::mlock(mapped->data(), mapped->size()), 0)
<< "mlock failed (errno=" << errno << "); check RLIMIT_MEMLOCK";
::munlock(mapped->data(), mapped->size());
}
#else
// VirtualLock forces pages resident; VirtualUnlock releases the pin without evicting.
// The lock is bounded by the working-set quota, so grow it to cover the region first.
void ensure_memory_resident(const std::shared_ptr<ov::MappedMemory>& mapped) {
const size_t need = mapped->size() + 64 * util::one_mib; // headroom for code/stack
SIZE_T min_ws = 0, max_ws = 0;
if (GetProcessWorkingSetSize(GetCurrentProcess(), &min_ws, &max_ws) && max_ws < need) {
SetProcessWorkingSetSize(GetCurrentProcess(), need, need);
}
ASSERT_NE(VirtualLock(mapped->data(), mapped->size()), 0)
<< "VirtualLock failed (GetLastError=" << GetLastError() << ")";
VirtualUnlock(mapped->data(), mapped->size());
}
#endif
struct TestFile {
size_t size_mib;
std::filesystem::path path;
size_t size_bytes() const {
return size_mib * util::one_mib;
}
};
std::filesystem::path generate_test_file(const TestFile& tf) {
auto path = std::filesystem::path("test_file" + std::to_string(tf.size_mib) + "mib.bin");
if (util::file_exists(path) && std::filesystem::file_size(path) == tf.size_bytes()) {
return path;
}
std::vector<uint8_t> chunk(util::one_mib);
for (size_t i = 0; i < chunk.size(); ++i)
chunk[i] = static_cast<uint8_t>(i % 251);
std::ofstream f(path, std::ios::binary);
for (size_t written = 0; written < tf.size_bytes(); written += chunk.size()) {
const auto to_write = std::min(chunk.size(), tf.size_bytes() - written);
f.write(reinterpret_cast<const char*>(chunk.data()), static_cast<std::streamsize>(to_write));
}
return path;
}
long long measure_ms(const std::function<void()>& fn) {
auto start = std::chrono::high_resolution_clock::now();
fn();
auto elapsed = std::chrono::high_resolution_clock::now() - start;
return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
}
void evict_cache(const std::filesystem::path& path, size_t file_size) {
static bool warned = false;
auto warn_once = [](std::string_view msg) {
if (!warned) {
std::cout << "[WARNING] " << msg << " Results may be unreliable." << std::endl;
warned = true;
}
};
#ifdef __linux__
// Prefer /proc/sys/vm/drop_caches (requires root / CAP_SYS_ADMIN, available when the container
// is started with --privileged). Writing "3" flushes the host page
// cache, dentries, and inodes — the only fully reliable way to guarantee a cold-cache run.
// Fallback: posix_fadvise(DONTNEED) is best-effort; the kernel may ignore it.
::sync(); // commit all dirty pages before dropping
if (std::ofstream drop_caches("/proc/sys/vm/drop_caches"); drop_caches) {
drop_caches << "3";
sleep(1); // give the kernel a moment to settle
return;
}
warn_once("No access to /proc/sys/vm/drop_caches, falling back to posix_fadvise(DONTNEED).");
// Fallback: best-effort fadvise.
int fd = ::open(path.c_str(), O_RDONLY);
if (fd >= 0) {
posix_fadvise(fd, 0, static_cast<off_t>(file_size), POSIX_FADV_DONTNEED);
::close(fd);
}
#elif defined(_WIN32)
// Windows moves evicted file pages to the standby list ("Cached" in Task Manager). Purging it is
// the equivalent of `drop_caches 1`: NtSetSystemInformation(SystemMemoryListInformation,
// MemoryPurgeStandbyList) clears it. Requires SeProfileSingleProcessPrivilege (run elevated).
// Both mmap and read paths start cold once the standby list is empty.
(void)path;
(void)file_size;
enum SYSTEM_MEMORY_LIST_COMMAND { MemoryPurgeStandbyList = 4 };
constexpr int SystemMemoryListInformation = 80;
using NtSetSystemInformation_t = LONG(WINAPI*)(int, PVOID, ULONG);
// Acquire the privilege required to purge the standby list.
HANDLE token = nullptr;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) {
TOKEN_PRIVILEGES tp{};
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue(nullptr, SE_PROF_SINGLE_PROCESS_NAME, &tp.Privileges[0].Luid);
AdjustTokenPrivileges(token, FALSE, &tp, sizeof(tp), nullptr, nullptr);
CloseHandle(token);
}
auto ntdll = GetModuleHandleW(L"ntdll.dll");
auto nt_set =
ntdll ? reinterpret_cast<NtSetSystemInformation_t>(GetProcAddress(ntdll, "NtSetSystemInformation")) : nullptr;
if (!nt_set) {
warn_once("NtSetSystemInformation unavailable; cannot purge standby list.");
return;
}
int command = MemoryPurgeStandbyList;
if (nt_set(SystemMemoryListInformation, &command, sizeof(command)) != 0) {
warn_once("Standby-list purge failed (run elevated for cold-cache benchmarking).");
}
#else
(void)path;
(void)file_size;
warn_once("No cache eviction strategy available on this platform.");
#endif
}
long long bench(const std::function<void()>& fn,
const std::filesystem::path& path,
size_t file_size,
int warmup_runs = 1,
int measured_runs = 5) {
for (int i = 0; i < warmup_runs; ++i) {
evict_cache(path, file_size);
fn();
}
long long total = 0;
for (int i = 0; i < measured_runs; ++i) {
evict_cache(path, file_size);
total += measure_ms(fn);
}
return total / measured_runs;
}
double throughput_mibs(size_t size_mib, long long ms) {
if (ms <= 0)
return 0.0;
return static_cast<double>(size_mib) * 1000.0 / static_cast<double>(ms);
}
namespace strategy {
// Note: the mmap destructor (munmap + close) runs inside the timed window;
void sync_vm_prefetch_mem_lock(const std::filesystem::path& path, size_t /*file_size*/) {
auto mapped = load_mmap_object(path);
util::vm_prefetch(mapped->data(), mapped->size(), std::thread::hardware_concurrency());
ensure_memory_resident(mapped); // should be near no-op and just lock/unlock resident pages
}
void loop_touch_mem_lock(const std::filesystem::path& path, size_t /*file_size*/) {
auto mapped = load_mmap_object(path);
volatile uint8_t sink = 0;
for (auto first = mapped->data(), last = first + mapped->size(); first < last; first += page_size) {
sink += *first;
}
ensure_memory_resident(mapped); // should be near no-op and just lock/unlock resident pages
}
// --- "compute" scenario -----------------------------------------------------------------
// Instead of a raw memcpy or mlock(), run a std::transform pass over the mapped bytes (e.g.
// mimicking a dequantization/dtype-conversion pass over model weights).
void compute_over_mapped(const std::shared_ptr<ov::MappedMemory>& mapped) {
constexpr size_t chunk_size = 128 * util::one_mib; // 128 MiB chunks
const size_t file_size = mapped->size();
std::vector<uint64_t> out(std::min(chunk_size, file_size) / sizeof(uint64_t));
uint64_t acc = 0;
for (size_t offset = 0; offset < file_size; offset += chunk_size) {
const size_t n = std::min(chunk_size, file_size - offset);
const size_t n_words = n / sizeof(uint64_t);
const auto* first = reinterpret_cast<const uint64_t*>(mapped->data() + offset);
std::transform(first, first + n_words, out.begin(), [](uint64_t v) {
return v * 3u + 7u;
});
if (n_words > 0)
acc += out[0] + out[n_words / 2] + out[n_words - 1]; // prevents optimization
}
volatile uint64_t sink = acc;
(void)sink;
}
void mmap_then_compute(const std::filesystem::path& path, size_t /*file_size*/) {
auto mapped = load_mmap_object(path);
compute_over_mapped(mapped);
}
void mmap_prefetch_then_compute(const std::filesystem::path& path, size_t /*file_size*/) {
auto mapped = load_mmap_object(path);
mapped->hint_prefetch();
compute_over_mapped(mapped);
}
void mmap_prefetch_then_memcpy_partial(const std::filesystem::path& path,
size_t /*file_size*/,
size_t offset,
size_t size) {
auto mapped = load_mmap_object(path);
mapped->hint_prefetch(offset, size);
const auto total_copy_size = std::min(size, mapped->size() - offset);
constexpr size_t chunk_size = 128 * util::one_mib;
std::vector<char> buffer(std::min(chunk_size, total_copy_size));
volatile char sink = 0;
for (size_t i = 0; i < total_copy_size; i += chunk_size) {
const size_t copy_size = std::min(chunk_size, total_copy_size - i);
std::memcpy(buffer.data(), mapped->data() + offset + i, copy_size);
// Sample multiple positions to prevent memcpy optimization
sink += buffer[0] + buffer[copy_size / 2] + buffer[copy_size - 1];
}
}
} // namespace strategy
} // namespace
// See developer_benchmarks.md for build/run instructions.
class FileLoadBenchmark : public ::testing::Test {};
TEST_F(FileLoadBenchmark, read_into_mmap_and_compute) {
const std::vector<size_t> sizes_mib = {10, 100, 500, 1000};
constexpr int warmup = 0;
constexpr int runs = 3;
// Generate all test files
std::vector<TestFile> files;
for (size_t mib : sizes_mib) {
TestFile tf{mib, {}};
tf.path = generate_test_file(tf);
evict_cache(tf.path, tf.size_bytes());
files.push_back(tf);
}
// Collect results: [file_idx] -> {no hint, sync prefetch}
struct Row {
size_t mib;
long long t_no_hint;
long long t_sync_prefetch;
};
std::vector<Row> results;
for (const auto& tf : files) {
Row r{};
r.mib = tf.size_mib;
r.t_no_hint = bench(
[&]() {
strategy::mmap_then_compute(tf.path, tf.size_bytes());
},
tf.path,
tf.size_bytes(),
warmup,
runs);
r.t_sync_prefetch = bench(
[&]() {
strategy::mmap_prefetch_then_compute(tf.path, tf.size_bytes());
},
tf.path,
tf.size_bytes(),
warmup,
runs);
results.push_back(r);
}
printf("\n--- Latency (ms, mean of %d runs, cold cache) ---\n", runs);
printf("%-10s | %17s | %13s\n", "Size (MiB)", "sync prefetch", "mmap+compute");
printf("%-10s-|-%17s-|-%13s\n", "----------", "-----------------", "-------------");
for (const auto& r : results) {
printf("%-10zu | %14lld ms | %10lld ms\n", r.mib, r.t_sync_prefetch, r.t_no_hint);
}
printf("\n--- Throughput (MiB/s) ---\n");
printf("%-10s | %17s | %13s\n", "Size (MiB)", "sync prefetch", "mmap+compute");
printf("%-10s-|-%17s-|-%13s\n", "----------", "-----------------", "-------------");
for (const auto& r : results) {
printf("%-10zu | %12.0f MiB/s | %8.0f MiB/s\n",
r.mib,
throughput_mibs(r.mib, r.t_sync_prefetch),
throughput_mibs(r.mib, r.t_no_hint));
}
}
TEST_F(FileLoadBenchmark, test_speed_load_data_into_mmap_region) {
const std::vector<size_t> sizes_mib = {10, 100, 500, 1000};
constexpr int warmup = 0;
constexpr int runs = 3;
// Generate all test files
std::vector<TestFile> files;
for (size_t mib : sizes_mib) {
TestFile tf{mib, {}};
tf.path = generate_test_file(tf);
evict_cache(tf.path, tf.size_bytes());
files.push_back(tf);
}
// Collect results: [file_idx] -> {mmap_vm_prefetch_mem_lock, loop_touch_mem_lock}
struct Row {
size_t mib;
long long t_prefetch_mlock;
long long t_mlock;
};
std::vector<Row> results;
for (const auto& tf : files) {
Row r{};
r.mib = tf.size_mib;
r.t_mlock = bench(
[&]() {
strategy::loop_touch_mem_lock(tf.path, tf.size_bytes());
},
tf.path,
tf.size_bytes(),
warmup,
runs);
r.t_prefetch_mlock = bench(
[&]() {
strategy::sync_vm_prefetch_mem_lock(tf.path, tf.size_bytes());
},
tf.path,
tf.size_bytes(),
warmup,
runs);
results.push_back(r);
}
printf("\n--- Latency (ms, mean of %d runs, cold cache) ---\n", runs);
printf("%-10s | %18s | %13s\n", "Size (MiB)", "parallel loop sync", "loop touch");
printf("%-10s-|-%18s-|-%13s\n", "----------", "------------------", "-------------");
for (const auto& r : results) {
printf("%-10zu | %15lld ms | %10lld ms\n", r.mib, r.t_prefetch_mlock, r.t_mlock);
}
printf("\n--- Throughput (MiB/s) ---\n");
printf("%-10s | %18s | %13s\n", "Size (MiB)", "parallel loop sync", "loop touch");
printf("%-10s-|-%18s-|-%13s\n", "----------", "------------------", "-------------");
for (const auto& r : results) {
printf("%-10zu | %13.0f MiB/s | %8.0f MiB/s\n",
r.mib,
throughput_mibs(r.mib, r.t_prefetch_mlock),
throughput_mibs(r.mib, r.t_mlock));
}
}
TEST_F(FileLoadBenchmark, hint_prefetch_with_offset_table) {
constexpr size_t file_size_mib = 1200;
constexpr int warmup = 0;
constexpr int runs = 3;
TestFile tf{file_size_mib, {}};
tf.path = generate_test_file(tf);
evict_cache(tf.path, tf.size_bytes()); // Flush dirty pages from file generation
const std::vector<size_t> offsets_mib = {0, 1, 17, 500, 700, 800};
const std::vector<size_t> region_sizes_mib = {10, 100, 500};
// Pre-compute all results[size_idx][offset_idx]; -1 means "exceeds"
std::vector<std::vector<long long>> results(region_sizes_mib.size(),
std::vector<long long>(offsets_mib.size(), -1));
for (size_t si = 0; si < region_sizes_mib.size(); ++si) {
for (size_t oi = 0; oi < offsets_mib.size(); ++oi) {
const size_t off_bytes = offsets_mib[oi] * util::one_mib;
const size_t sz_bytes = region_sizes_mib[si] * util::one_mib;
if (off_bytes + sz_bytes > tf.size_bytes())
continue;
results[si][oi] = bench(
[&]() {
strategy::mmap_prefetch_then_memcpy_partial(tf.path, tf.size_bytes(), off_bytes, sz_bytes);
},
tf.path,
tf.size_bytes(),
warmup,
runs);
}
}
// Print: sizes as rows, offsets as columns
printf("\n--- partial prefault: hint_prefetch with offset ---\n");
printf(" %-14s", "size \\ offset");
for (size_t off_mib : offsets_mib)
printf(" | %7zu MiB", off_mib);
printf("\n %s\n", std::string(14 + offsets_mib.size() * 14, '-').c_str());
for (size_t si = 0; si < region_sizes_mib.size(); ++si) {
printf(" %-14zu", region_sizes_mib[si]);
for (size_t oi = 0; oi < offsets_mib.size(); ++oi) {
if (results[si][oi] < 0)
printf(" | (exceeds)");
else
printf(" | %7lld ms", results[si][oi]);
}
printf("\n");
}
}
} // namespace ov::test