Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion contrib/linx/flows/tools/run_linx_qemu_vs_pyc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ fi

WORK="$(mktemp -d "${TMPDIR:-/tmp}/linx-diff.XXXXXX")"
KEEP_WORK=0
QEMU_PID=""
cleanup() {
if [[ -n "${QEMU_PID}" ]] && kill -0 "${QEMU_PID}" >/dev/null 2>&1; then
kill -TERM "${QEMU_PID}" >/dev/null 2>&1 || true
wait "${QEMU_PID}" >/dev/null 2>&1 || true
fi
if [[ "${KEEP_WORK}" == "0" ]]; then
rm -rf "${WORK}"
else
Expand Down Expand Up @@ -124,7 +129,30 @@ if [[ "$(basename -- "$QEMU_BIN")" != *bios-none ]]; then
fi

echo "[qemu] commit trace: $QEMU_TRACE"
LINX_COMMIT_TRACE="$QEMU_TRACE" "$QEMU_BIN" "${QEMU_ARGS[@]}" >/dev/null
QEMU_TRACE_MIN_RECORDS="${LINX_QEMU_VS_PYC_TRACE_MIN_RECORDS:-64}"
LINX_COMMIT_TRACE="$QEMU_TRACE" "$QEMU_BIN" "${QEMU_ARGS[@]}" >/dev/null &
QEMU_PID=$!
while kill -0 "${QEMU_PID}" >/dev/null 2>&1; do
if [[ -s "${QEMU_TRACE}" ]] &&
[[ "$(wc -l <"${QEMU_TRACE}")" -ge "${QEMU_TRACE_MIN_RECORDS}" ]]; then
kill -TERM "${QEMU_PID}" >/dev/null 2>&1 || true
break
fi
sleep 0.01
done
Comment on lines +135 to +142

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Polling every 10ms (sleep 0.01) in a tight loop spawns two external processes (wc and sleep) up to 100 times per second. This process-spawning overhead can cause extremely high CPU utilization and significantly slow down execution, especially on macOS or virtualized environments. Additionally, fractional sleep values are not strictly POSIX-compliant and may fail on some minimal environments.

Consider increasing the sleep interval to 0.1 (100ms) or 0.05 (50ms) to drastically reduce CPU thrashing while remaining highly responsive. Also, capturing the output of wc -l in a variable with a default fallback (e.g., ${num_lines:-0}) prevents potential Bash syntax errors if the command substitution returns an empty string.

Suggested change
while kill -0 "${QEMU_PID}" >/dev/null 2>&1; do
if [[ -s "${QEMU_TRACE}" ]] &&
[[ "$(wc -l <"${QEMU_TRACE}")" -ge "${QEMU_TRACE_MIN_RECORDS}" ]]; then
kill -TERM "${QEMU_PID}" >/dev/null 2>&1 || true
break
fi
sleep 0.01
done
while kill -0 "${QEMU_PID}" >/dev/null 2>&1; do
if [[ -s "${QEMU_TRACE}" ]]; then
num_lines=$(wc -l <"${QEMU_TRACE}")
if [[ ${num_lines:-0} -ge "${QEMU_TRACE_MIN_RECORDS}" ]]; then
kill -TERM "${QEMU_PID}" >/dev/null 2>&1 || true
break
fi
fi
sleep 0.1
done

set +e
wait "${QEMU_PID}"
qemu_rc=$?
set -e
QEMU_PID=""
if [[ "${qemu_rc}" -ne 0 && "${qemu_rc}" -ne 143 ]]; then
echo "error: QEMU trace run failed (rc=${qemu_rc})" >&2
exit "${qemu_rc}"
fi
if [[ ! -s "${QEMU_TRACE}" ]]; then
echo "error: qemu trace was not produced: ${QEMU_TRACE}" >&2
exit 2
fi

echo "[pyc] commit trace: $PYC_TRACE"
PYC_KONATA=0 PYC_EXPECT_EXIT=0 PYC_BOOT_PC=0x10000 PYC_COMMIT_TRACE="$PYC_TRACE" \
Expand Down
Loading