Files
VCCPlusOSS/tests/macos/SustainedChecks.mm
T

114 lines
7.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-or-later
// Opt-in real-time integration runner. Reads a user ROM, never mounts/writes media.
#import <Foundation/Foundation.h>
#import "../../platforms/macos/Bridge/VCCMachineController.h"
#include <mach/mach.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <string>
using Clock = std::chrono::steady_clock;
static void pump(double seconds) {
const auto end = Clock::now() + std::chrono::duration<double>(seconds);
while (Clock::now() < end) @autoreleasepool {
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.005]];
}
}
static VCCMachineSnapshot* command(void (^operation)(VCCMachineCompletion)) {
__block VCCMachineSnapshot* result = nil;
operation(^(VCCMachineSnapshot* snapshot) { result = snapshot; });
const auto deadline = Clock::now() + std::chrono::seconds(10);
while (!result && Clock::now() < deadline) pump(0.005);
if (!result) throw std::runtime_error("Machine command timed out");
if (result.errorMessage) throw std::runtime_error(result.errorMessage.UTF8String);
return result;
}
static uint64_t key(char c) {
if (c >= 'A' && c <= 'Z') return uint64_t(1) << (c - '@');
if (c >= '0' && c <= '9') return uint64_t(1) << (32 + c - '0');
if (c == ',') return uint64_t(1) << 44;
if (c == ' ') return uint64_t(1) << 31;
if (c == '\n') return uint64_t(1) << 48;
throw std::runtime_error("Unsupported probe key");
}
static void type(VCCMachineController* machine, const std::string& text) {
for (char c : text) { [machine setKeyboardMask:key(c)]; pump(0.10); [machine setKeyboardMask:0]; pump(0.10); }
}
static uint64_t residentBytes() {
mach_task_basic_info_data_t info{}; mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&info), &count) != KERN_SUCCESS)
throw std::runtime_error("Unable to read process memory usage");
return info.resident_size;
}
int main(int argc, const char* argv[]) { @autoreleasepool {
if (argc < 4 || argc > 5) {
std::fprintf(stderr, "Usage: SustainedChecks ROM seconds 6809|6309 [silent]\n"); return 2;
}
auto machine = [VCCMachineController new];
try {
std::size_t parsed = 0;
const double duration = std::stod(argv[2], &parsed);
if (parsed != std::string(argv[2]).size()) throw std::runtime_error("Invalid duration");
if (!std::isfinite(duration) || duration < 5 || duration > 86400) throw std::runtime_error("Duration must be 5–86400 seconds");
const std::string cpu = argv[3];
if (cpu != "6809" && cpu != "6309") throw std::runtime_error("Select 6809 or 6309");
const bool audio = argc == 4;
if (argc == 5 && std::string(argv[4]) != "silent") throw std::runtime_error("Unknown pacing mode");
auto rom = [NSData dataWithContentsOfFile:[NSString stringWithUTF8String:argv[1]] options:NSDataReadingMappedIfSafe error:nil];
if (rom.length != 32768) throw std::runtime_error("Supply a readable 32 KiB CoCo 3 ROM");
command(^(VCCMachineCompletion done) { [machine setAudioEnabled:audio volume:0 completion:done]; });
command(^(VCCMachineCompletion done) { [machine loadROM:rom use6309:cpu == "6309" completion:done]; });
command(^(VCCMachineCompletion done) { [machine resumeWithCompletion:done]; });
pump(3);
type(machine, "10 SOUND100,255\n20 PRINT 42\n30 GOTO10\nRUN\n");
pump(5);
auto baseline = command(^(VCCMachineCompletion done) { [machine snapshotWithCompletion:done]; });
const auto start = Clock::now();
const auto initialRSS = residentBytes(); uint64_t maximumRSS = initialRSS;
double maxDrift = 0, maxAudioDrift = 0; unsigned polls = 0; uint64_t previousSignal = baseline.audioSignalSamples;
std::printf("START cpu=%s audio=%d seconds=%.0f frames=%llu consumed=%llu rss=%llu\n",cpu.c_str(),audio,duration,baseline.frames,baseline.audioConsumed,initialRSS); std::fflush(stdout);
VCCMachineSnapshot* latest = baseline;
while (std::chrono::duration<double>(Clock::now() - start).count() < duration) {
@autoreleasepool {
pump(std::min(1.0, duration - std::chrono::duration<double>(Clock::now() - start).count()));
latest = command(^(VCCMachineCompletion done) { [machine snapshotWithCompletion:done]; });
const auto wall = std::chrono::duration<double>(Clock::now() - start).count();
const double emulated = (latest.frames - baseline.frames) * (239856.0 / 14318180.0);
const double consumed = (latest.audioConsumed - baseline.audioConsumed) / 44100.0;
maximumRSS = std::max(maximumRSS, residentBytes());
maxDrift = std::max(maxDrift, std::abs(emulated - wall));
if (audio) maxAudioDrift = std::max(maxAudioDrift, std::abs(consumed - wall));
if (latest.state != VCCMachineStateRunning || latest.generation != baseline.generation || latest.frames <= baseline.frames)
throw std::runtime_error("Machine stopped, reset or failed to advance");
if (++polls % 30 == 0 || wall >= duration) {
if (audio && wall >= 30 && latest.audioSignalSamples == previousSignal)
throw std::runtime_error("BASIC tone loop stopped producing a signal");
previousSignal = latest.audioSignalSamples;
std::printf("SAMPLE wall=%.3f emulated=%.3f audio=%.3f drift_ms=%.3f underruns=%llu overruns=%llu rss=%llu buffered=%llu starts=%llu signal=%llu max_request=%llu max_refill_gap_ms=%.3f\n",
wall, emulated, consumed, (emulated-wall)*1000, latest.audioUnderruns-baseline.audioUnderruns,
latest.audioOverruns-baseline.audioOverruns, residentBytes(), latest.audioBuffered, latest.audioStarts, latest.audioSignalSamples, latest.audioMaximumRequest, latest.audioMaximumRefillGapNanoseconds / 1000000.0); std::fflush(stdout);
}
}
}
auto paused = command(^(VCCMachineCompletion done) { [machine pauseWithCompletion:done]; });
pump(0.25);
auto still = command(^(VCCMachineCompletion done) { [machine snapshotWithCompletion:done]; });
if (still.frames != paused.frames || still.audioConsumed != paused.audioConsumed) throw std::runtime_error("Pause did not stop execution/audio");
command(^(VCCMachineCompletion done) { [machine shutdownWithCompletion:done]; });
std::printf("RESULT seconds=%.3f max_frame_drift_ms=%.3f max_audio_drift_ms=%.3f rss_growth=%lld underruns=%llu overruns=%llu\n",
std::chrono::duration<double>(Clock::now()-start).count(),maxDrift*1000,maxAudioDrift*1000,
static_cast<long long>(maximumRSS-initialRSS),latest.audioUnderruns-baseline.audioUnderruns,latest.audioOverruns-baseline.audioOverruns);
std::fflush(stdout);
if (maxDrift > 0.25 || (audio && maxAudioDrift > 0.25) || maximumRSS > initialRSS + 64*1024*1024 ||
latest.audioUnderruns != baseline.audioUnderruns || latest.audioOverruns != baseline.audioOverruns)
throw std::runtime_error("Sustained timing/buffering/memory acceptance threshold exceeded");
std::puts("PASS: sustained native controller run and pause stability"); return 0;
} catch (const std::exception& error) {
std::fprintf(stderr,"FAIL: %s\n",error.what());
[machine shutdownWithCompletion:^(VCCMachineSnapshot*) {}]; pump(0.1); return 1;
}
} }