-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
421 lines (385 loc) · 16.5 KB
/
Copy pathmain.cpp
File metadata and controls
421 lines (385 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
// Copyright 2026 Timo Heimonen <timo.heimonen@proton.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
/**
* @file main.cpp
* @brief Main entry point for the memory benchmark application
*
* This file contains the main program logic that selects and dispatches memory
* benchmark modes. Result-producing modes handle configuration parsing,
* mode-specific buffer preparation, benchmark execution, and console/JSON
* output here. The LLM-memory path owns its dedicated production runner and
* schema-v1 transport behind the standalone command boundary.
*
* The program supports six benchmark modes:
* - Standard benchmarks: Memory bandwidth and latency tests for different cache levels
* - Pattern benchmarks: Access pattern-specific tests (forward, reverse, strided, random)
* - TLB analysis: Page-native paired locality measurements and boundary analysis
* - Core-to-core analysis: Best-effort inter-core round-trip latency measurements
* - GPU bandwidth: Standalone Metal GPU memory read/write/copy measurements
* - LLM memory profile: Standalone CPU/Metal synthetic decode/prefill memory workloads with contiguous or paged KV
*
* Standard, pattern, TLB, and core-to-core modes also support validated parameter sweeps.
* GPU bandwidth and the LLM memory profile are intentionally standalone and do
* not participate in sweeps.
*
* @author Timo Heimonen
*/
#include <cstdlib> // Exit codes
#include <exception>
#include <iomanip> // Output formatting
#include <iostream>
#include <optional>
#include <string>
#include "utils/benchmark.h"
#include "core/config/config.h"
#include "core/config/mode_selector.h"
#include "core/memory/buffer_allocator.h"
#include "benchmark/benchmark_runner.h"
#include "benchmark/core_to_core_latency.h"
#include "benchmark/sweep_runner.h"
#include "benchmark/tlb_analysis.h"
#include "output/console/messages/messages_api.h"
#include "core/config/constants.h"
#include "output/json/json_output/json_output_api.h"
#include "output/json/json_output/json_output_session.h"
#include "pattern_benchmark/pattern_benchmark.h"
#include "core/signal/signal_handler.h"
#include "core/system/benchmark_qos.h"
#include "gpu_bandwidth/gpu_bandwidth.h"
#include "llm_memory/llm_memory.h"
namespace {
void set_benchmark_qos(BenchmarkConfig& config) {
const MainThreadQosResult qos_result = prepare_main_thread_benchmark_qos();
config.main_thread_qos_requested = qos_result.requested;
config.main_thread_qos_applied = qos_result.applied;
config.main_thread_qos_code = qos_result.code;
}
template <typename Fn>
int run_with_benchmark_preparation(BenchmarkConfig& config, Fn&& fn) {
set_benchmark_qos(config);
BenchmarkSignalMaskGuard signal_guard;
return fn();
}
void report_json_output_boundary_failure(const std::string& raw_output,
const std::string& details) noexcept {
try {
std::cerr << Messages::error_prefix();
if (raw_output == "-") {
std::cerr << Messages::error_json_stdout_write_failed(details);
} else {
std::cerr << Messages::error_file_write_failed(raw_output, details);
}
std::cerr << std::endl;
} catch (...) {
// A secondary diagnostic failure must not escape the command boundary.
}
}
template <typename Builder>
int build_and_write_final_json(JsonOutputSession& session,
const std::string& raw_output,
Builder&& builder) noexcept {
try {
return session.write_final(builder());
} catch (const std::exception& error) {
report_json_output_boundary_failure(
raw_output,
Messages::error_json_payload_construction_failed(error.what()));
} catch (...) {
report_json_output_boundary_failure(
raw_output, Messages::error_json_payload_construction_failed(""));
}
return EXIT_FAILURE;
}
} // namespace
/**
* @brief Main entry point for the memory benchmark application
*
* This function selects a mode and parses and validates its command-line
* arguments. For result-producing modes, it then configures system settings,
* prepares any required buffers, executes the requested benchmark, and emits
* human and optional JSON results. The LLM-memory path performs its own
* preflight, full-size resource preparation, execution, console rendering,
* and JSON transport.
*
* The program supports multiple execution modes:
* - Bandwidth-only measurements (--only-bandwidth)
* - Latency-only measurements (--only-latency)
* - Pattern-specific benchmarks (--patterns)
* - Standalone TLB analysis (--analyze-tlb)
* - Standalone core-to-core analysis (--analyze-core2core)
* - Standalone GPU memory bandwidth (--gpu-bandwidth)
* - Standalone LLM decode/prefill memory profile (--llm-memory)
* - Validated multi-configuration runs (--sweep)
* - Multiple loop iterations for statistical analysis (--count)
*
* @param argc Number of command-line arguments
* @param argv Array of command-line argument strings
*
* @return `EXIT_SUCCESS` for help, complete execution, and the established
* graceful-interruption paths. Success alone does not prove that a
* JSON result is complete; callers must apply the mode-specific
* predicate documented in `documents/API.md`.
* @return `EXIT_FAILURE` on configuration, allocation, benchmark, or output
* failure. An initialized mode may still emit inspectable terminal
* evidence before returning failure.
*
* @note Standard, pattern, TLB, sweep, GPU, and LLM-memory entry paths make a
* best-effort QOS_CLASS_USER_INTERACTIVE request for the command thread.
* Core-to-core mode instead requests QoS independently for its workers.
* @note All allocated buffers are automatically freed when going out of scope
* @note Standard mode uses per-phase allocation; pattern mode owns one shared
* source/destination pair for the command lifetime.
*
* @see parse_arguments() for command-line argument details
* @see run_all_benchmarks() for standard benchmark execution
* @see run_all_pattern_benchmarks() for pattern benchmark execution
*/
int main(int argc, char *argv[]) {
// Install signal handlers early (before any benchmark logic)
install_signal_handlers();
const PrimaryModeSelection mode_selection =
select_primary_benchmark_mode(argc, argv);
if (mode_selection.mode == PrimaryBenchmarkMode::Conflict) {
std::cerr << Messages::error_prefix()
<< Messages::error_mutually_exclusive_modes(
mode_selection.selected_options[0],
mode_selection.selected_options[1])
<< std::endl;
return EXIT_FAILURE;
}
if (mode_selection.mode == PrimaryBenchmarkMode::LlmMemory) {
return run_llm_memory_mode(argc, argv);
}
if (mode_selection.mode == PrimaryBenchmarkMode::GpuBandwidth) {
return run_gpu_bandwidth_mode(argc, argv);
}
if (mode_selection.mode == PrimaryBenchmarkMode::AnalyzeCoreToCore) {
return run_core_to_core_latency_mode(argc, argv);
}
// Start total execution timer
auto timer_opt = HighResTimer::create();
if (!timer_opt) {
std::cerr << Messages::error_prefix()
<< Messages::error_timer_creation_failed()
<< std::endl;
return EXIT_FAILURE;
}
auto& total_execution_timer = *timer_opt;
total_execution_timer.start();
// --- Parse and Validate Configuration ---
BenchmarkConfig config;
int parse_result = parse_arguments(argc, argv, config);
if (parse_result != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
// If -h/--help was handled, usage already printed — exit now
if (config.help_printed) {
return EXIT_SUCCESS;
}
// If no arguments provided, show help
if (argc == 1) {
print_help(argv[0]);
return EXIT_SUCCESS;
}
// If no mode flag is set (neither --benchmark nor --patterns nor --analyze-tlb), show help
if (!config.analyze_tlb && !config.run_benchmark && !config.run_patterns) {
print_help(argv[0]);
return EXIT_SUCCESS;
}
// Every general CPU command shares one target owner. Install stdout routing
// before sweep validation and before any benchmark worker can start.
std::optional<JsonOutputSession> output_session;
try {
output_session.emplace(make_json_output_target(
config.output_file,
JsonFilePathPolicy::ResolveAgainstCurrentDirectory));
} catch (const std::exception& error) {
report_json_output_boundary_failure(
config.output_file,
Messages::error_json_output_initialization_failed(error.what()));
return EXIT_FAILURE;
} catch (...) {
report_json_output_boundary_failure(
config.output_file,
Messages::error_json_output_initialization_failed(""));
return EXIT_FAILURE;
}
if (config.run_sweep) {
if (validate_config(config) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
try {
const SweepExecutionResult execution =
run_sweep_mode(config, *output_session);
if (output_session->kind() == JsonOutputKind::Stdout &&
!execution.output_json.empty() &&
output_session->write_final(execution.output_json) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
return execution.exit_code;
} catch (const std::exception& error) {
std::cerr << Messages::error_prefix()
<< Messages::error_command_execution_exception(
"Sweep", error.what())
<< std::endl;
} catch (...) {
std::cerr << Messages::error_prefix()
<< Messages::error_command_execution_exception("Sweep", "")
<< std::endl;
}
return EXIT_FAILURE;
}
if (config.analyze_tlb) {
if (validate_config(config) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
print_runtime_banner();
return run_with_benchmark_preparation(config, [&]() {
try {
if (output_session->kind() == JsonOutputKind::Disabled) {
return run_tlb_analysis(config);
}
nlohmann::ordered_json result_json;
const int run_status = run_tlb_analysis_collect(config, result_json);
if (!result_json.empty() &&
output_session->write_final(result_json) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
return run_status;
} catch (const std::exception& error) {
std::cerr << Messages::error_prefix()
<< Messages::error_command_execution_exception(
"TLB analysis", error.what())
<< std::endl;
} catch (...) {
std::cerr << Messages::error_prefix()
<< Messages::error_command_execution_exception(
"TLB analysis", "")
<< std::endl;
}
return EXIT_FAILURE;
});
}
if (validate_config(config) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
calculate_buffer_sizes(config);
calculate_access_counts(config);
size_t peak_allocation_bytes = 0;
if (calculate_total_allocation_bytes(config, peak_allocation_bytes) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
// --- Print Config ---
print_runtime_banner();
print_configuration(config.buffer_size, config.buffer_size_mb, peak_allocation_bytes,
config.iterations, config.loop_count,
config.use_non_cacheable, config.latency_stride_bytes,
latency_chain_mode_to_string(resolve_latency_chain_mode(
config.latency_chain_mode, config.latency_tlb_locality_bytes)),
config.latency_tlb_locality_bytes,
config.cpu_name, config.perf_cores, config.eff_cores, config.num_threads,
config.only_bandwidth, config.only_latency, config.run_patterns,
config.user_specified_iterations);
print_cache_info(config.l1_cache_size, config.l2_cache_size, config.use_custom_cache_size,
config.custom_cache_size_bytes);
// --- Run Benchmarks ---
const int benchmark_result = run_with_benchmark_preparation(config, [&]() {
if (config.run_patterns) {
// The pattern coordinator owns its shared src/dst mappings.
PatternStatistics pattern_stats;
const int pattern_run_status =
run_all_pattern_benchmarks(config, pattern_stats);
// Print detailed single-loop results or robust median headlines for repeated loops.
if (config.loop_count == 1 && !pattern_stats.loop_results.empty()) {
print_pattern_results(extract_pattern_results_at(pattern_stats, 0));
} else if (!pattern_stats.loop_results.empty()) {
print_pattern_results(extract_pattern_median_results(pattern_stats));
// Print summary statistics
print_pattern_statistics(config.loop_count, pattern_stats);
}
// --- Save JSON Output if requested ---
if (output_session->kind() != JsonOutputKind::Disabled) {
double total_elapsed_time_sec = total_execution_timer.stop();
if (build_and_write_final_json(
*output_session, config.output_file,
[&]() {
return build_pattern_results_json(
config, pattern_stats, total_elapsed_time_sec);
}) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
}
if (pattern_run_status != EXIT_SUCCESS) {
return pattern_run_status;
}
} else {
// Run standard benchmarks
std::cout << Messages::msg_running_benchmarks() << std::endl;
BenchmarkStatistics stats;
const int standard_run_status =
run_all_benchmarks(config, stats, nullptr, &*output_session);
// --- Print Stats ---
// Print summary statistics if more than one loop was run
if (standard_run_status == EXIT_SUCCESS) {
print_statistics(config.loop_count, stats.all_read_bw_gb_s, stats.all_write_bw_gb_s, stats.all_copy_bw_gb_s,
stats.all_l1_latency_ns, stats.all_l2_latency_ns,
stats.all_l1_read_bw_gb_s, stats.all_l1_write_bw_gb_s, stats.all_l1_copy_bw_gb_s,
stats.all_l2_read_bw_gb_s, stats.all_l2_write_bw_gb_s, stats.all_l2_copy_bw_gb_s,
stats.all_average_latency_ns,
stats.all_tlb_hit_latency_ns,
stats.all_tlb_miss_latency_ns,
stats.all_page_walk_penalty_ns,
config.use_custom_cache_size,
stats.all_custom_latency_ns, stats.all_custom_read_bw_gb_s,
stats.all_custom_write_bw_gb_s, stats.all_custom_copy_bw_gb_s,
stats.all_main_mem_latency_samples,
stats.all_l1_latency_samples,
stats.all_l2_latency_samples,
stats.all_custom_latency_samples,
config.only_bandwidth,
config.only_latency);
}
// --- Save JSON Output if requested ---
// A failed file checkpoint is terminal and must not be retried here.
// Stdout checkpoints are successful no-ops, so a failed run can still
// emit its representable terminal evidence exactly once.
const bool write_standard_final = should_write_standard_final_json(
output_session->kind(), standard_run_status);
if (write_standard_final) {
double total_elapsed_time_sec = total_execution_timer.stop();
if (build_and_write_final_json(
*output_session, config.output_file,
[&]() {
return build_results_json(
config, stats, total_elapsed_time_sec);
}) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
}
if (standard_run_status != EXIT_SUCCESS) {
return standard_run_status;
}
}
return EXIT_SUCCESS;
});
if (benchmark_result != EXIT_SUCCESS) {
return benchmark_result;
}
// --- Print Total Time ---
double total_elapsed_time_sec = total_execution_timer.stop(); // Stop overall timer
std::cout << Messages::msg_done_total_time(total_elapsed_time_sec) << std::endl; // Print duration
return EXIT_SUCCESS; // Indicate success
}