-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAppPerf.cpp
More file actions
549 lines (482 loc) · 19.3 KB
/
AppPerf.cpp
File metadata and controls
549 lines (482 loc) · 19.3 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#include "App.hpp"
#include "nbl/examples/git/info.h"
#include "nlohmann/json.hpp"
#include "nbl/core/hash/blake.h"
#include <algorithm>
#include <cctype>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <unordered_map>
namespace
{
using perf_json_t = nlohmann::ordered_json;
constexpr std::string_view PerfSchemaVersion = "meshloaders-perf-run-v1";
constexpr std::string_view PerfProtocolVersion = "meshloaders-roundtrip-v1";
std::string normalizeProfileComponent(std::string_view value)
{
std::string normalized;
normalized.reserve(value.size());
bool lastWasSeparator = false;
for (const auto ch : value)
{
if (std::isalnum(static_cast<unsigned char>(ch)))
{
normalized.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(ch))));
lastWasSeparator = false;
}
else if (!lastWasSeparator)
{
normalized.push_back('-');
lastWasSeparator = true;
}
}
while (!normalized.empty() && normalized.back() == '-')
normalized.pop_back();
if (normalized.empty())
normalized = "unknown";
return normalized;
}
std::string hashToHex(const nbl::core::blake3_hash_t& hash)
{
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for (const auto byte : hash.data)
oss << std::setw(2) << static_cast<uint32_t>(byte);
return oss.str();
}
std::string currentTimestampTag()
{
const auto now = std::chrono::system_clock::now();
const auto time = std::chrono::system_clock::to_time_t(now);
std::tm tm = {};
#ifdef _WIN32
gmtime_s(&tm, &time);
#else
gmtime_r(&time, &tm);
#endif
std::ostringstream oss;
oss << std::put_time(&tm, "%Y%m%d_%H%M%S");
return oss.str();
}
std::string currentTimestampIsoUtc()
{
const auto now = std::chrono::system_clock::now();
const auto time = std::chrono::system_clock::to_time_t(now);
std::tm tm = {};
#ifdef _WIN32
gmtime_s(&tm, &time);
#else
gmtime_r(&time, &tm);
#endif
std::ostringstream oss;
oss << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
return oss.str();
}
std::string runModeName(const uint32_t modeValue)
{
switch (modeValue)
{
case 0u:
return "interactive";
case 1u:
return "batch";
case 2u:
return "ci";
default:
return "unknown";
}
}
std::string runtimeTuningModeName(const asset::SFileIOPolicy::SRuntimeTuning::Mode mode)
{
switch (mode)
{
case asset::SFileIOPolicy::SRuntimeTuning::Mode::Sequential:
return "sequential";
case asset::SFileIOPolicy::SRuntimeTuning::Mode::Heuristic:
return "heuristic";
case asset::SFileIOPolicy::SRuntimeTuning::Mode::Hybrid:
return "hybrid";
default:
return "unknown";
}
}
std::string platformName()
{
#if defined(_WIN32)
return "windows";
#elif defined(__linux__)
return "linux";
#elif defined(__ANDROID__)
return "android";
#else
return "unknown";
#endif
}
std::string buildConfigName()
{
#if defined(NBL_MESHLOADERS_BUILD_CONFIG)
return NBL_MESHLOADERS_BUILD_CONFIG;
#elif defined(_DEBUG)
return "Debug";
#else
return "Release";
#endif
}
perf_json_t dirtyStateJson(const std::optional<bool>& dirty)
{
if (!dirty.has_value())
return nullptr;
return perf_json_t(dirty.value());
}
std::filesystem::path normalizePathForPerf(const std::filesystem::path& path)
{
if (path.empty())
return {};
std::error_code ec;
auto normalized = path.lexically_normal();
if (std::filesystem::exists(normalized, ec) && !ec)
{
auto canonical = std::filesystem::weakly_canonical(normalized, ec);
if (!ec)
return canonical.lexically_normal();
ec.clear();
}
else
ec.clear();
if (!normalized.is_absolute())
{
auto absolute = std::filesystem::absolute(normalized, ec);
if (!ec)
return absolute.lexically_normal();
ec.clear();
}
return normalized;
}
std::optional<std::string> tryRelativePerfPath(const std::filesystem::path& targetPath, const std::filesystem::path& basePath)
{
if (targetPath.empty() || basePath.empty())
return std::nullopt;
std::error_code ec;
const auto normalizedTarget = normalizePathForPerf(targetPath);
const auto normalizedBase = normalizePathForPerf(basePath);
const auto relative = std::filesystem::relative(normalizedTarget, normalizedBase, ec);
if (ec || relative.empty() || relative.is_absolute())
return std::nullopt;
return relative.lexically_normal().generic_string();
}
std::string makePortablePerfPath(const std::filesystem::path& path, const std::optional<std::filesystem::path>& preferredBase = std::nullopt)
{
if (path.empty())
return {};
auto normalized = path.lexically_normal();
if (!normalized.is_absolute())
return normalized.generic_string();
if (preferredBase.has_value())
if (auto relative = tryRelativePerfPath(path, *preferredBase); relative.has_value())
return *relative;
std::error_code ec;
const auto cwd = std::filesystem::current_path(ec);
if (!ec)
if (auto relative = tryRelativePerfPath(path, cwd); relative.has_value())
return *relative;
normalized = normalizePathForPerf(path);
if (!normalized.filename().empty())
return normalized.filename().generic_string();
return normalizeProfileComponent(normalized.generic_string());
}
perf_json_t toJson(const MeshLoadersApp::LoadStageMetrics& metrics)
{
return perf_json_t{
{"valid", metrics.valid},
{"input_size", metrics.inputSize},
{"get_asset_ms", metrics.getAssetMs},
{"extract_ms", metrics.extractMs},
{"total_ms", metrics.totalMs},
{"non_loader_ms", metrics.nonLoaderMs}
};
}
perf_json_t toJson(const MeshLoadersApp::WriteStageMetrics& metrics)
{
return perf_json_t{
{"valid", metrics.valid},
{"output_size", metrics.outputSize},
{"open_ms", metrics.openMs},
{"write_ms", metrics.writeMs},
{"stat_ms", metrics.statMs},
{"total_ms", metrics.totalMs},
{"non_writer_ms", metrics.nonWriterMs},
{"used_memory_transport", metrics.usedMemoryTransport},
{"used_disk_fallback", metrics.usedDiskFallback},
{"persisted_disk_artifact", metrics.persistedDiskArtifact}
};
}
bool metricRegression(const double current, const double reference, const double relativeThreshold, const double absoluteThreshold)
{
if (reference <= 0.0)
return current > absoluteThreshold;
const double allowed = std::max(reference * (1.0 + relativeThreshold), reference + absoluteThreshold);
return current > allowed;
}
void compareMetric(core::vector<std::string>& failures, const std::string& caseName, const std::string_view metricName, const double current, const double reference, const double relativeThreshold, const double absoluteThreshold)
{
if (!metricRegression(current, reference, relativeThreshold, absoluteThreshold))
return;
std::ostringstream oss;
oss << caseName << ": " << metricName << " regressed from " << reference << " ms to " << current << " ms";
failures.push_back(oss.str());
}
perf_json_t buildCaseJson(const MeshLoadersApp::CasePerformanceMetrics& metrics, const std::optional<std::filesystem::path>& testListDir)
{
return perf_json_t{
{"name", metrics.caseName},
{"input_path", makePortablePerfPath(metrics.inputPath, testListDir)},
{"original_load", toJson(metrics.originalLoad)},
{"write", toJson(metrics.write)},
{"written_load", toJson(metrics.writtenLoad)}
};
}
bool writePerfJson(system::ISystem* const system, const system::path& path, const perf_json_t& json)
{
if (!system)
return false;
const auto parentDir = path.parent_path();
if (!parentDir.empty())
std::filesystem::create_directories(parentDir);
system->deleteFile(path);
system::ISystem::future_t<core::smart_refctd_ptr<system::IFile>> writeFileFuture;
system->createFile(writeFileFuture, path, system::IFile::ECF_WRITE);
core::smart_refctd_ptr<system::IFile> writeFile;
writeFileFuture.acquire().move_into(writeFile);
if (!writeFile)
return false;
const auto serialized = json.dump(2);
size_t written = 0ull;
while (written < serialized.size())
{
system::IFile::success_t success;
writeFile->write(success, serialized.data() + written, written, serialized.size() - written);
const auto processed = success.getBytesProcessed();
if (!success || processed == 0ull)
return false;
written += processed;
}
return true;
}
}
bool MeshLoadersApp::performanceEnabled() const
{
return m_perf.enabled;
}
void MeshLoadersApp::beginPerformanceRun()
{
if (!performanceEnabled())
return;
m_perf.finalized = false;
m_perf.completedCases.clear();
m_perf.completedCases.reserve(m_runtime.cases.size());
m_perf.comparisonFailures.clear();
m_perf.runStart = std::chrono::steady_clock::now();
const auto systemInfo = m_system->getSystemInfo();
const auto normalizedCpuName = normalizeProfileComponent(systemInfo.cpuName);
if (m_perf.options.profileOverride.has_value())
m_perf.profileId = normalizeProfileComponent(*m_perf.options.profileOverride);
else
{
std::ostringstream profile;
profile << platformName()
<< "__" << normalizedCpuName
<< "__thr-" << std::thread::hardware_concurrency()
<< "__" << normalizeProfileComponent(buildConfigName());
m_perf.profileId = normalizeProfileComponent(profile.str());
}
nbl::core::blake3_hasher workloadHasher;
workloadHasher.update(PerfProtocolVersion.data(), PerfProtocolVersion.size());
workloadHasher << static_cast<uint32_t>(m_runtime.mode);
workloadHasher << static_cast<uint32_t>(m_runtimeTuningMode);
workloadHasher << m_runtime.rowViewEnabled;
if (!m_output.testListPath.empty() && std::filesystem::exists(m_output.testListPath))
{
std::ifstream stream(m_output.testListPath, std::ios::binary);
std::string content((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
workloadHasher.update(content.data(), content.size());
}
else
{
workloadHasher << m_runtime.cases.size();
for (const auto& testCase : m_runtime.cases)
{
workloadHasher << testCase.name;
workloadHasher << makePortablePerfPath(testCase.path);
}
}
m_perf.workloadId = hashToHex(static_cast<nbl::core::blake3_hash_t>(workloadHasher));
}
void MeshLoadersApp::beginPerformanceCase(const TestCase& testCase)
{
if (!performanceEnabled())
return;
m_perf.currentCaseIndex = m_perf.completedCases.size();
m_perf.completedCases.push_back(CasePerformanceMetrics{
.caseName = testCase.name,
.inputPath = testCase.path
});
}
void MeshLoadersApp::recordOriginalLoadMetrics(const LoadStageMetrics& metrics)
{
if (!performanceEnabled() || m_perf.currentCaseIndex >= m_perf.completedCases.size())
return;
m_perf.completedCases[m_perf.currentCaseIndex].originalLoad = metrics;
}
void MeshLoadersApp::recordWrittenLoadMetrics(const LoadStageMetrics& metrics)
{
if (!performanceEnabled() || m_perf.currentCaseIndex >= m_perf.completedCases.size())
return;
m_perf.completedCases[m_perf.currentCaseIndex].writtenLoad = metrics;
}
void MeshLoadersApp::recordWriteMetrics(const WriteStageMetrics& metrics)
{
if (!performanceEnabled() || m_perf.currentCaseIndex >= m_perf.completedCases.size())
return;
m_perf.completedCases[m_perf.currentCaseIndex].write = metrics;
}
void MeshLoadersApp::recordWriteMetrics(const WrittenAssetResult& result)
{
WriteStageMetrics metrics = {};
metrics.openMs = result.openMs;
metrics.writeMs = result.writeMs;
metrics.statMs = result.statMs;
metrics.totalMs = result.totalWriteMs;
metrics.nonWriterMs = result.nonWriterMs;
metrics.outputSize = result.outputSize;
metrics.usedMemoryTransport = result.usedMemoryTransport;
metrics.usedDiskFallback = result.usedDiskFallback;
metrics.persistedDiskArtifact = result.persistedDiskArtifact;
metrics.valid = true;
recordWriteMetrics(metrics);
}
void MeshLoadersApp::endPerformanceCase()
{
if (!performanceEnabled() || m_perf.currentCaseIndex >= m_perf.completedCases.size())
return;
m_perf.currentCaseIndex = ~size_t(0u);
}
void MeshLoadersApp::finalizePerformanceRun()
{
if (!performanceEnabled() || m_perf.finalized)
return;
m_perf.finalized = true;
perf_json_t root = {};
root["schema_version"] = PerfSchemaVersion;
root["protocol_version"] = PerfProtocolVersion;
root["profile_id"] = m_perf.profileId;
root["workload_id"] = m_perf.workloadId;
root["run_mode"] = runModeName(static_cast<uint32_t>(m_runtime.mode));
root["runtime_tuning"] = runtimeTuningModeName(m_runtimeTuningMode);
root["provenance"] = {
{"created_at_utc", currentTimestampIsoUtc()},
{"nabla_commit", std::string(nbl::gtml::nabla_git_info.commitHash())},
{"nabla_dirty", dirtyStateJson(nbl::gtml::nabla_git_info.hasUncommittedChanges())},
{"examples_commit", std::string(nbl::examples::gtml::examples_git_info.commitHash())},
{"examples_dirty", dirtyStateJson(nbl::examples::gtml::examples_git_info.hasUncommittedChanges())}
};
const auto systemInfo = m_system->getSystemInfo();
root["environment"] = {
{"platform", platformName()},
{"os_full_name", systemInfo.OSFullName},
{"cpu_name", systemInfo.cpuName},
{"physical_core_count", systemInfo.physicalCoreCount},
{"gpu_name", m_physicalDevice ? m_physicalDevice->getProperties().deviceName : "unknown"},
{"total_memory_bytes", systemInfo.totalMemory},
{"available_memory_bytes", systemInfo.availableMemory},
{"hardware_concurrency", std::thread::hardware_concurrency()},
{"build_config", buildConfigName()}
};
const auto testListDir = m_output.testListPath.empty() ? std::optional<std::filesystem::path>{} : std::optional<std::filesystem::path>{m_output.testListPath.parent_path()};
const auto testListName = m_output.testListPath.empty() ? std::string{} : m_output.testListPath.filename().generic_string();
root["inputs"] = {
{"test_list_name", testListName},
{"row_view_enabled", m_runtime.rowViewEnabled},
{"case_count", m_runtime.cases.size()}
};
root["cases"] = perf_json_t::array();
for (const auto& metrics : m_perf.completedCases)
root["cases"].push_back(buildCaseJson(metrics, testListDir));
root["totals"] = {
{"run_wall_ms", toMs(std::chrono::steady_clock::now() - m_perf.runStart)}
};
if (m_perf.options.referenceDir)
{
m_perf.referencePath = *m_perf.options.referenceDir / m_perf.workloadId / (m_perf.profileId + ".json");
root["reference"]["lookup_key"] = m_perf.workloadId + "/" + m_perf.profileId + ".json";
if (!m_perf.options.updateReference && std::filesystem::exists(m_perf.referencePath))
{
m_perf.referenceMatched = true;
std::ifstream stream(m_perf.referencePath);
perf_json_t reference;
stream >> reference;
if (!reference.contains("cases") || !reference["cases"].is_array())
m_perf.comparisonFailures.push_back("Reference file does not contain a valid cases array.");
else
{
std::unordered_map<std::string, perf_json_t> referenceCases;
for (const auto& caseJson : reference["cases"])
referenceCases.emplace(caseJson.value("name", ""), caseJson);
if (referenceCases.size() != m_perf.completedCases.size())
m_perf.comparisonFailures.push_back("Reference case count does not match the current run.");
for (const auto& metrics : m_perf.completedCases)
{
const auto refIt = referenceCases.find(metrics.caseName);
if (refIt == referenceCases.end())
{
m_perf.comparisonFailures.push_back(metrics.caseName + ": missing reference case.");
continue;
}
const auto& refCase = refIt->second;
compareMetric(m_perf.comparisonFailures, metrics.caseName, "original_load.total_ms", metrics.originalLoad.totalMs, refCase["original_load"].value("total_ms", 0.0), 0.20, 5.0);
compareMetric(m_perf.comparisonFailures, metrics.caseName, "write.total_ms", metrics.write.totalMs, refCase["write"].value("total_ms", 0.0), 0.20, 5.0);
compareMetric(m_perf.comparisonFailures, metrics.caseName, "written_load.total_ms", metrics.writtenLoad.totalMs, refCase["written_load"].value("total_ms", 0.0), 0.20, 5.0);
const bool refUsedMemoryTransport = refCase["write"].value("used_memory_transport", false);
if (metrics.write.valid && metrics.write.usedMemoryTransport != refUsedMemoryTransport)
m_perf.comparisonFailures.push_back(metrics.caseName + ": memory transport usage does not match the reference.");
}
}
}
}
root["reference"]["matched"] = m_perf.referenceMatched;
root["reference"]["strict"] = m_perf.options.strict;
root["reference"]["updated"] = m_perf.options.updateReference;
root["reference"]["comparison_failures"] = m_perf.comparisonFailures;
if (m_perf.options.dumpDir)
{
const auto dumpDir = *m_perf.options.dumpDir / m_perf.workloadId;
std::filesystem::create_directories(dumpDir);
m_perf.dumpPath = dumpDir / (currentTimestampTag() + "__" + m_perf.profileId + ".json");
if (!writePerfJson(m_system.get(), m_perf.dumpPath, root))
failExit("Failed to write performance dump file: %s", m_perf.dumpPath.string().c_str());
}
if (m_perf.options.updateReference)
{
if (!writePerfJson(m_system.get(), m_perf.referencePath, root))
failExit("Failed to write performance reference file: %s", m_perf.referencePath.string().c_str());
}
if (m_logger)
{
if (m_perf.options.updateReference)
m_logger->log("Performance reference updated for workload=%s profile=%s.", ILogger::ELL_INFO, m_perf.workloadId.c_str(), m_perf.profileId.c_str());
else if (!m_perf.referenceMatched)
m_logger->log("Performance reference not found for workload=%s profile=%s.", ILogger::ELL_INFO, m_perf.workloadId.c_str(), m_perf.profileId.c_str());
else if (m_perf.comparisonFailures.empty())
m_logger->log("Performance reference comparison passed for workload=%s profile=%s.", ILogger::ELL_INFO, m_perf.workloadId.c_str(), m_perf.profileId.c_str());
else
for (const auto& failure : m_perf.comparisonFailures)
m_logger->log("%s", ILogger::ELL_ERROR, failure.c_str());
}
if (m_perf.options.strict && m_perf.referenceMatched && !m_perf.comparisonFailures.empty())
failExit("Structured performance comparison failed.");
}