using CanCommon; namespace CanSlave; /// /// Orchestrates a slave benchmark run: opens the channel (with error frames /// enabled), receives + validates for the requested duration, prints a 1 Hz /// live report, and writes a summary + log. Frame-loss is computed later by the /// report script from the master's frames_sent and the slave's valid_frames. /// public sealed class CanBenchmark { private readonly BenchmarkOptions _opt; public CanBenchmark(BenchmarkOptions opt) => _opt = opt; public int Run() { if (_opt.Configure) InterfaceConfigurator.SetBitrate(_opt.Interface, _opt.Bitrate); string logPath = RunLogger.BuildPath(_opt.ResultsDir, "slave", _opt.Interface, _opt.Bitrate); using var log = new RunLogger(logPath); var stats = new Statistics(); using var channel = new SocketCanChannel(_opt.Interface, rcvTimeoutMs: 200, wantErrorFrames: true); channel.Open(); PrintHeader(log); using var cts = new CancellationTokenSource(); Console.CancelKeyPress += (_, e) => { e.Cancel = true; cts.Cancel(); }; var receiver = new CanReceiver(channel, stats, _opt.CanId, _opt.Dlc, _opt.PayloadFill, _opt.Realtime, _opt.RtPriority); stats.Start(); var worker = new Thread(() => receiver.Run(cts.Token)) { IsBackground = true, Priority = ThreadPriority.Highest }; worker.Start(); while (receiver.RealtimeStatus == "pending") Thread.Sleep(5); log.Line(receiver.RealtimeStatus is null ? $"Realtime : SCHED_FIFO prio {_opt.RtPriority} engaged" : $"Realtime : not engaged ({receiver.RealtimeStatus})"); log.Line(""); RunReportingLoop(log, stats, cts); cts.Cancel(); worker.Join(2000); stats.Stop(); PrintSummary(log, stats.Take()); log.Line($"\nLog written to: {log.Path}"); return 0; } private void RunReportingLoop(RunLogger log, Statistics stats, CancellationTokenSource cts) { var deadline = DateTime.UtcNow.AddSeconds(_opt.DurationSeconds); long lastFrames = 0; double lastSec = 0; int second = 0; while (DateTime.UtcNow < deadline && !cts.IsCancellationRequested) { Thread.Sleep(1000); var s = stats.Take(); long dFrames = s.Frames - lastFrames; double dSec = s.Seconds - lastSec; double instFps = dSec > 0 ? dFrames / dSec : 0; double instMbps = dSec > 0 ? dFrames * _opt.Dlc * 8.0 / dSec / 1_000_000.0 : 0; lastFrames = s.Frames; lastSec = s.Seconds; log.Line($"{DateTime.Now:HH:mm:ss.fff} [{++second,3}s] rx frames={s.Frames,-12} valid={s.ValidFrames,-12} " + $"inst {instFps,8:0} fps {instMbps,6:0.000} Mbps " + $"invalid={s.InvalidFrames} err={s.Errors}"); } } private void PrintHeader(RunLogger log) { log.Line("========== CAN SLAVE =========="); log.Line($"Interface : {_opt.Interface}"); log.Line($"Bitrate : {_opt.BitrateLabel}"); log.Line($"CAN ID : 0x{_opt.CanId:X3}"); log.Line($"DLC : {_opt.Dlc}"); log.Line($"Duration : {_opt.DurationSeconds} s"); log.Line($"Started : {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}"); } private void PrintSummary(RunLogger log, Statistics.Snapshot s) { log.Line(""); log.Line("---------- SUMMARY (SLAVE) ----------"); log.Line($"CAN bitrate : {_opt.BitrateLabel}"); log.Line($"Duration (actual) : {s.Seconds:0.000} s"); log.Line($"Frames received : {s.Frames}"); log.Line($"Valid frames : {s.ValidFrames}"); log.Line($"Invalid frames : {s.InvalidFrames}"); log.Line($"CAN/bus errors : {s.Errors}"); log.Line($"Payload bytes : {s.PayloadBytes}"); log.Line($"Average frame/s : {s.FramesPerSecond:0.00}"); log.Line($"Payload throughput : {s.PayloadBytesPerSecond:0} B/s ({s.PayloadMbps:0.0000} Mbps)"); log.Summary(new Dictionary { ["role"] = "slave", ["interface"] = _opt.Interface, ["bitrate"] = _opt.Bitrate.ToString(), ["duration_s"] = s.Seconds.ToString("0.000"), ["frames_received"] = s.Frames.ToString(), ["valid_frames"] = s.ValidFrames.ToString(), ["invalid_frames"] = s.InvalidFrames.ToString(), ["can_errors"] = s.Errors.ToString(), ["payload_bytes"] = s.PayloadBytes.ToString(), ["rx_frames_per_s"] = s.FramesPerSecond.ToString("0.00"), ["valid_frames_per_s"] = (s.Seconds > 0 ? s.ValidFrames / s.Seconds : 0).ToString("0.00"), ["payload_mbps"] = s.PayloadMbps.ToString("0.0000"), }); } }