Loop Timing Witness
Native controller and runtime interfaces; hardware qualification pending
Loading...
Searching...
No Matches
run_metadata.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Commercial license available
3// © Concepts 1996–2026 Miroslav Šotek. All rights reserved.
4// © Code 2020–2026 Miroslav Šotek. All rights reserved.
5// ORCID: 0009-0009-3560-0851
6// Contact: www.anulum.li | protoscience@anulum.li
7// Loop Timing Witness — native configuration and completion metadata
8
12
13#ifndef WITNESS_RUN_METADATA_H
14#define WITNESS_RUN_METADATA_H
15#include "run_control.h"
16#include "refusal.h"
17#include "linux/run_policy.h"
18#include "linux/file_digest.h"
19#include <openssl/crypto.h>
20
21namespace witness {
23inline void write_run_metadata(const char *path, const RunConfiguration &config,
24 const RunResult &result, bool thermal, const char *kind,
25 const RunOptions &options, const HostPolicy &policy,
26 const std::vector<ArtifactDigest> &artifacts) {
28 std::FILE *file = std::fopen(path, "wx");
29 if (!file)
30 throw witness::RuntimeRefused("cannot create exclusive native metadata");
31 const std::array<const char *, 4> faults = {"drop", "delay", "freeze", "overload_request"};
32 const auto &c = config.coefficients;
33 const bool failed =
34 std::fprintf(file,
35 "{\n \"schema\": \"loop-timing-witness.native-run.v1\",\n"
36 " \"source_kind\": \"%s\",\n \"controller\": \"%s\",\n"
37 " \"cycles\": %u, \"period_ticks\": %u, \"thermal\": %s,\n"
38 " \"coefficients\": {\"kp\": %d, \"ki_period\": %d, \"derivative_decay\": %d,"
39 " \"derivative_gain\": %d, \"position_gain\": %d, \"velocity_gain\": %d,"
40 " \"reference_gain\": %d, \"output_min\": %d, \"output_max\": %d,"
41 " \"integral_min\": %d, \"integral_max\": %d},\n"
42 " \"reference\": {\"mode\": %u, \"amplitude\": %d, \"offset\": %d,"
43 " \"ramp\": %d, \"phase\": %u},\n"
44 " \"fault\": {\"kind\": \"%s\", \"cycle\": %u, \"duration_periods\": %u},\n"
45 " \"overload\": {\"iterations\": %u, \"modeled_nanoseconds\": %u},\n"
46 " \"result\": {\"samples\": %llu, \"records\": %llu, \"misses\": %u,"
47 " \"overflow\": %u, \"safe\": %s},\n",
48 kind, config.lqr ? "lqr" : "pid", config.cycles, config.period_ticks,
49 thermal ? "true" : "false", c.kp, c.ki_period, c.derivative_decay,
50 c.derivative_gain, c.position_gain, c.velocity_gain, c.reference_gain,
51 c.output_min, c.output_max, c.integral_min, c.integral_max,
52 config.reference_mode, config.amplitude, config.offset, config.ramp,
53 config.phase, config.fault_enabled ? faults[config.fault_kind] : "none",
54 config.fault_cycle, config.fault_periods, config.overload_iterations,
55 config.modeled_overload_ns, static_cast<unsigned long long>(result.samples),
56 static_cast<unsigned long long>(result.records), result.misses,
57 result.overflow, result.safe ? "true" : "false") < 0;
58 bool artifact_failed = std::fputs(" \"artifacts\": {", file) < 0;
59 for (std::size_t index = 0; index < artifacts.size(); ++index) {
60 const auto &artifact = artifacts[index];
61 if (std::fprintf(file, "%s\"%s\": {\"sha256\": \"%s\", \"bytes\": %llu}", index ? ", " : "",
62 artifact.role, artifact.digest.sha256.c_str(),
63 static_cast<unsigned long long>(artifact.digest.bytes)) < 0)
64 artifact_failed = true;
65 }
66 artifact_failed = std::fputs("},\n", file) < 0 || artifact_failed;
67 const bool crypto_failed =
68 std::fprintf(
69 file,
70 " \"crypto_library\": {\"algorithm\": \"sha256\", \"header_version_number\": %llu, "
71 "\"runtime_version_number\": %llu},\n",
72 static_cast<unsigned long long>(OPENSSL_VERSION_NUMBER),
73 static_cast<unsigned long long>(OpenSSL_version_num())) < 0;
74 const bool policy_failed = write_host_policy(file, options, policy);
75 const bool end_failed = std::fprintf(file, "}\n") < 0;
76 const int status = std::fclose(file);
77 if (failed || artifact_failed || crypto_failed || policy_failed || end_failed || status)
78 throw witness::RuntimeRefused("cannot write native metadata");
79}
80} // namespace witness
81#endif
void validate_configuration(const RunConfiguration &configuration)
void write_run_metadata(const char *path, const RunConfiguration &config, const RunResult &result, bool thermal, const char *kind, const RunOptions &options, const HostPolicy &policy, const std::vector< ArtifactDigest > &artifacts)
bool write_host_policy(std::FILE *file, const RunOptions &options, const HostPolicy &actual)
Definition run_policy.h:122
witness_coefficients coefficients
std::uint32_t overflow
Definition run_control.h:29
std::uint64_t samples
Definition run_control.h:28
std::uint64_t records
Definition run_control.h:28
std::uint32_t misses
Definition run_control.h:29