This commit is contained in:
2026-08-20 14:56:23 +07:00
parent 7f00e38980
commit be3a0e9549
186 changed files with 350212 additions and 0 deletions

165
scripts/run_test.sh.orig Normal file
View File

@@ -0,0 +1,165 @@
#!/usr/bin/env bash
# Run one full master+slave benchmark at a given bitrate and print the result row.
#
# Usage:
# ./run_test.sh <bitrate> [duration] [master_if] [slave_if] [--configure] [--monitor]
# ./run_test.sh <bitrate> [duration] [master_if] --monitor-only (slave_if not needed)
# e.g. ./run_test.sh 500000 60 can_com3 can_com4 --configure --monitor
# ./run_test.sh 500000 30 can_com3 --monitor-only
#
# The slave starts first (and runs a little longer) so it is listening for the
# entire master send window. Loss is computed from master frames_sent and slave
# valid_frames — never assumed equal.
#
# Every file from one invocation (master log, slave log, and all --monitor
# captures) is gathered into a single per-run directory whose name ends in the
# run's timestamp:
# results/<master_if>-<slave_if>_<bitrate>_<yyyyMMdd-HHmmss>/
#
# --monitor runs can-utils in parallel as an independent cross-check of the C#
# numbers, writing extra files into that same run directory:
# * canbusload -> real bus-load % (incl. bit-stuffing) vs the C# bus_load_pct
# * candump -e -> error frames with timestamps (when/why the bus faulted)
# * ip -s link -> driver counters before/after (bus-off, restarts, tx drops)
#
# --monitor-only PASSIVELY observes the bus for <duration>: it runs ONLY the
# can-utils monitors (canbusload + full candump traffic + ip stats) and NEVER
# transmits a frame. Safe to point at a live bus (e.g. one an active service is
# already driving). It does not reconfigure the interface even with --configure.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
RESULTS="$ROOT/results"
mkdir -p "$RESULTS"
# Split flags from positional args so a flag can appear anywhere (and so
# slave_if may be omitted, e.g. in --monitor-only which only touches master_if).
CONFIGURE=""
MONITOR=""
MONITOR_ONLY=""
POS=()
for a in "$@"; do
case "$a" in
--configure) CONFIGURE="--configure" ;;
--monitor) MONITOR="1" ;;
--monitor-only) MONITOR_ONLY="1"; MONITOR="1" ;;
--*) echo "!! unknown flag: $a" >&2; exit 2 ;;
*) POS+=("$a") ;;
esac
done
BITRATE="${POS[0]:?usage: run_test.sh <bitrate> [duration] [master_if] [slave_if] [--configure] [--monitor|--monitor-only]}"
DURATION="${POS[1]:-60}"
MASTER_IF="${POS[2]:-can_com3}"
SLAVE_IF="${POS[3]:-can_com4}"
# One directory per run; the trailing token is the run's HHMMSS (date+time).
RUN_TS="$(date +%Y%m%d-%H%M%S)"
RUN_DIR="$RESULTS/${MASTER_IF}-${SLAVE_IF}_${BITRATE}_${RUN_TS}"
mkdir -p "$RUN_DIR"
echo ">> run directory: $RUN_DIR"
MASTER_BIN="$ROOT/CanMaster/bin/Release/net10.0/CanMaster"
SLAVE_BIN="$ROOT/CanSlave/bin/Release/net10.0/CanSlave"
if [ -n "$CONFIGURE" ] && [ -n "$MONITOR_ONLY" ]; then
echo ">> --monitor-only: ignoring --configure (won't touch a possibly-live interface)"
CONFIGURE=""
fi
if [ -n "$CONFIGURE" ]; then
echo ">> configuring interfaces via setup_can.sh (sudo)"
sudo "$ROOT/scripts/setup_can.sh" "$BITRATE" "$MASTER_IF" "$SLAVE_IF"
fi
# --- optional can-utils monitors (parallel cross-check) --------------------
# Monitor captures land in the same per-run directory as the C# logs.
# Which interfaces to watch: a flood run touches both ends, but --monitor-only
# watches ONLY the master interface (passive single-bus observation).
if [ -n "$MONITOR_ONLY" ]; then
MON_IFS=("$MASTER_IF")
else
MON_IFS=("$MASTER_IF" "$SLAVE_IF")
fi
MON_PIDS=()
start_monitors() {
echo ">> monitor: capturing can-utils for [${MON_IFS[*]}] into $RUN_DIR"
# Driver-level counters BEFORE the run (bus-off / restarts / drops).
for IF in "${MON_IFS[@]}"; do
ip -s -d link show "$IF" > "$RUN_DIR/ipstat_${IF}_before.txt" 2>&1 || true
done
# Real bus load incl. bit-stuffing, timestamped, one line/sec.
local cbl=(); for IF in "${MON_IFS[@]}"; do cbl+=("${IF}@${BITRATE}"); done
canbusload "${cbl[@]}" -t > "$RUN_DIR/canbusload.txt" 2>&1 &
MON_PIDS+=("$!")
# candump with ABSOLUTE date-time stamps (-t A) so lines align with the C#
# logs' wall-clock timestamps.
# * monitor-only : capture ALL frames (see the live CANopen traffic).
# * flood run : capture ERROR frames ONLY ("0~0,#FFFFFFFF" filter) — logging
# every data frame during an 8 kfps flood would be gigabytes.
for IF in "${MON_IFS[@]}"; do
if [ -n "$MONITOR_ONLY" ]; then
candump -e -t A "$IF" > "$RUN_DIR/traffic_${IF}.txt" 2>&1 &
else
candump -e -t A "$IF,0~0,#FFFFFFFF" > "$RUN_DIR/errframes_${IF}.txt" 2>&1 &
fi
MON_PIDS+=("$!")
done
}
stop_monitors() {
[ ${#MON_PIDS[@]} -gt 0 ] && kill "${MON_PIDS[@]}" 2>/dev/null || true
# Driver-level counters AFTER the run — diff against *_before to see faults.
for IF in "${MON_IFS[@]}"; do
ip -s -d link show "$IF" > "$RUN_DIR/ipstat_${IF}_after.txt" 2>&1 || true
done
echo ">> monitor: stopped (files in $RUN_DIR)"
}
# Never leave candump/canbusload running if the script dies mid-way.
trap '[ ${#MON_PIDS[@]} -gt 0 ] && kill "${MON_PIDS[@]}" 2>/dev/null || true' EXIT
# --- monitor-only: observe the bus, transmit nothing, then exit ------------
if [ -n "$MONITOR_ONLY" ]; then
IF="$MASTER_IF"
echo ">> MONITOR-ONLY on $IF for ${DURATION}s — NOT transmitting; live bus load (received traffic):"
ip -s -d link show "$IF" > "$RUN_DIR/ipstat_${IF}_before.txt" 2>&1 || true
# Full traffic capture (incl. CANopen) in the background.
candump -e -t A "$IF" > "$RUN_DIR/traffic_${IF}.txt" 2>&1 &
MON_PIDS+=("$!")
# Live, self-updating bus-load line. canbusload measures the RECEIVED traffic
# (we transmit nothing); raw output is saved, and awk reformats it on screen as:
# <if>@<rate> rx=<frames/s> payload=<Mbps> bus=<pct>%
timeout "$DURATION" canbusload "${IF}@${BITRATE}" -t 2>&1 \
| tee "$RUN_DIR/canbusload.txt" \
| awk '/@/{p=$5; sub(/%/,"",p); printf " %-16s rx=%-6s fps payload=%6.3f Mbps bus=%s%%\n", $1, $2, $4/1000000, p; fflush()}' \
|| true
[ ${#MON_PIDS[@]} -gt 0 ] && kill "${MON_PIDS[@]}" 2>/dev/null || true
ip -s -d link show "$IF" > "$RUN_DIR/ipstat_${IF}_after.txt" 2>&1 || true
echo ">> monitor-only complete. Files in $RUN_DIR"
echo " - traffic_${IF}.txt all frames seen (incl. CANopen) + error frames"
echo " - canbusload.txt raw canbusload output (bus load % over time)"
echo " - ipstat_${IF}_*.txt driver counters before/after"
exit 0
fi
[ -n "$MONITOR" ] && start_monitors
SLAVE_DURATION=$((DURATION + 3))
echo ">> starting slave on $SLAVE_IF (${SLAVE_DURATION}s) ..."
"$SLAVE_BIN" --interface "$SLAVE_IF" --bitrate "$BITRATE" --duration "$SLAVE_DURATION" --results "$RUN_DIR" >/dev/null 2>&1 &
SLAVE_PID=$!
sleep 1
echo ">> starting master on $MASTER_IF (${DURATION}s) ..."
"$MASTER_BIN" --interface "$MASTER_IF" --bitrate "$BITRATE" --duration "$DURATION" --results "$RUN_DIR"
wait "$SLAVE_PID" || true
[ -n "$MONITOR" ] && stop_monitors
echo ">> run complete."
# Merge the two newest summaries into one comparison row (inside the run dir).
"$ROOT/scripts/make_report.sh" "$BITRATE" "$RUN_DIR"