save
This commit is contained in:
83
CanCommon/BenchmarkOptions.cs
Normal file
83
CanCommon/BenchmarkOptions.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
namespace CanCommon;
|
||||
|
||||
/// <summary>Common command-line options shared by master and slave.</summary>
|
||||
public sealed class BenchmarkOptions
|
||||
{
|
||||
public string Interface { get; set; } = "";
|
||||
public int Bitrate { get; set; } = 500_000;
|
||||
public int DurationSeconds { get; set; } = 60;
|
||||
public string ResultsDir { get; set; } = "results";
|
||||
|
||||
// Fixed benchmark frame (per the spec: ID 0x123, DLC 8, payload all 0xAA).
|
||||
public uint CanId { get; set; } = 0x123;
|
||||
public int Dlc { get; set; } = 8;
|
||||
public byte PayloadFill { get; set; } = 0xAA;
|
||||
|
||||
/// <summary>If true, (re)configure the interface bitrate via `ip link` before running.</summary>
|
||||
public bool Configure { get; set; }
|
||||
|
||||
/// <summary>Frames pushed per sendmmsg syscall (master TX). 1 = per-frame write(). Bigger keeps the FIFO fuller.</summary>
|
||||
public int TxBatch { get; set; } = 64;
|
||||
|
||||
/// <summary>Try to run the tx/rx thread on SCHED_FIFO (needs CAP_SYS_NICE). Best-effort.</summary>
|
||||
public bool Realtime { get; set; } = true;
|
||||
|
||||
/// <summary>SCHED_FIFO priority to request when <see cref="Realtime"/> is on.</summary>
|
||||
public int RtPriority { get; set; } = 80;
|
||||
|
||||
public string BitrateLabel => Bitrate >= 1_000_000
|
||||
? $"{Bitrate / 1_000_000.0:0.###} Mbps"
|
||||
: $"{Bitrate / 1000} kbps";
|
||||
|
||||
public string PayloadHex => string.Join(' ', Enumerable.Repeat(PayloadFill.ToString("X2"), Dlc));
|
||||
|
||||
/// <summary>
|
||||
/// Parse args like: --interface can_com3 --bitrate 500000 --duration 60 [--configure] [--results DIR].
|
||||
/// Unknown flags throw. Returns null and prints help on --help.
|
||||
/// </summary>
|
||||
public static BenchmarkOptions? Parse(string[] args, string defaultInterface, string role)
|
||||
{
|
||||
var o = new BenchmarkOptions { Interface = defaultInterface };
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
string a = args[i];
|
||||
string Next(string name) =>
|
||||
i + 1 < args.Length ? args[++i] : throw new ArgumentException($"{name} requires a value");
|
||||
|
||||
switch (a)
|
||||
{
|
||||
case "--interface" or "-i": o.Interface = Next(a); break;
|
||||
case "--bitrate" or "-b": o.Bitrate = int.Parse(Next(a)); break;
|
||||
case "--duration" or "-d": o.DurationSeconds = int.Parse(Next(a)); break;
|
||||
case "--results": o.ResultsDir = Next(a); break;
|
||||
case "--configure": o.Configure = true; break;
|
||||
case "--batch": o.TxBatch = Math.Max(1, int.Parse(Next(a))); break;
|
||||
case "--rt-prio": o.RtPriority = int.Parse(Next(a)); break;
|
||||
case "--no-rt": o.Realtime = false; break;
|
||||
case "--help" or "-h": PrintHelp(role); return null;
|
||||
default: throw new ArgumentException($"Unknown argument: {a}");
|
||||
}
|
||||
}
|
||||
if (o.Bitrate is not (500_000 or 1_000_000))
|
||||
Console.Error.WriteLine($"[warn] bitrate {o.Bitrate} is outside the spec's {{500000, 1000000}}.");
|
||||
return o;
|
||||
}
|
||||
|
||||
private static void PrintHelp(string role) => Console.WriteLine(
|
||||
$"""
|
||||
Can{role} — CAN max-bandwidth benchmark ({role.ToLower()})
|
||||
|
||||
--interface, -i NAME SocketCAN interface (e.g. can_com3)
|
||||
--bitrate, -b N 500000 or 1000000 (default 500000)
|
||||
--duration, -d SECS benchmark length (default 60)
|
||||
--results DIR log output dir (default results)
|
||||
--configure set interface bitrate via `ip link` first (needs sudo)
|
||||
--batch N frames per sendmmsg syscall (master, default 64; 1 = write() each)
|
||||
--rt-prio N SCHED_FIFO priority to request (default 80)
|
||||
--no-rt do not attempt real-time scheduling
|
||||
--help, -h this help
|
||||
|
||||
Example:
|
||||
Can{role} --interface {(role == "Master" ? "can_com3" : "can_com4")} --bitrate 500000 --duration 60
|
||||
""");
|
||||
}
|
||||
Reference in New Issue
Block a user