Skip to content

Commit f5ec549

Browse files
marc-casavantCopilot
andcommitted
fixed valgrind profiling script to automatically generate a callgrind report after profiling is complete
Co-authored-by: Copilot <copilot@github.com>
1 parent e37d1f8 commit f5ec549

3 files changed

Lines changed: 51 additions & 30 deletions

File tree

src/tests/multi-server/scripts/profiling/start_valgrind_profiling.sh

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
# Clear any stale marker from a previous run
66
rm -f /etc/prof-results/.profiling_complete
7+
rm -f /etc/prof-results/start_valgrind_profiling.log
8+
9+
exec > /etc/prof-results/start_valgrind_profiling.log 2>&1
10+
11+
# Ignore SIGTERM — freeradius broadcasts it to the process group on shutdown,
12+
# which would otherwise kill this script before it can touch .profiling_complete
13+
trap '' SIGTERM
714

815
# Load proto_load config to get packet settings
916
source /etc/freeradius/proto_load_config.env
@@ -26,16 +33,26 @@ valgrind \
2633
--keep-debuginfo=yes \
2734
--instr-atstart=no \
2835
freeradius -f -l stdout -S resources.talloc_skip_cleanup=yes 2>&1 | \
29-
tee /tmp/fr.log &
36+
tee /etc/prof-results/freeradius_profiling.log &
37+
VALGRIND_PID=$!
3038

31-
# Wait for server ready
32-
until grep -q "Ready to process requests" /tmp/fr.log; do
33-
sleep 0.1
39+
# Wait for server ready (bail out if freeradius fails to start under valgrind)
40+
STARTUP_TIMEOUT=300
41+
STARTUP_ELAPSED=0
42+
until grep -q "Ready to process requests" /etc/prof-results/freeradius_profiling.log; do
43+
sleep 1
44+
STARTUP_ELAPSED=$(( STARTUP_ELAPSED + 1 ))
45+
if [ ${STARTUP_ELAPSED} -ge ${STARTUP_TIMEOUT} ]; then
46+
echo "ERROR: freeradius did not become ready within ${STARTUP_TIMEOUT}s, aborting"
47+
kill -SIGKILL ${VALGRIND_PID} 2>/dev/null
48+
exit 1
49+
fi
3450
done
3551

3652
# Enable instrumentation. callgrind_control auto-detects the running callgrind
3753
# instance and prints "PID <n>: freeradius ..." — capture that to get the PID
3854
# we need later for the graceful shutdown signal.
55+
echo "INFO: enabling callgrind instrumentation"
3956
CTRL_OUT=$(callgrind_control --instr=on)
4057
printf '%s\n' "$CTRL_OUT"
4158
FR_PID=$(printf '%s\n' "$CTRL_OUT" | grep -oP 'PID \K\d+(?=: freeradius)' | head -1)
@@ -44,9 +61,27 @@ echo "Freeradius PID: ${FR_PID}"
4461
# Wait for approximate send duration
4562
sleep ${SEND_DURATION}
4663

64+
# Stop instrumentation before shutdown so valgrind only flushes already-collected data
65+
echo "INFO: disabling callgrind instrumentation"
66+
CTRL_OUT=$(callgrind_control --instr=off 2>/dev/null || true)
67+
printf '%s\n' "$CTRL_OUT"
68+
4769
# Graceful shutdown (equivalent to Ctrl+C)
48-
kill -SIGINT ${FR_PID}
49-
wait
70+
if [ -z "${FR_PID}" ]; then
71+
echo "WARNING: could not determine freeradius PID from callgrind_control output, sending SIGINT to valgrind pipeline instead"
72+
kill -SIGINT ${VALGRIND_PID} 2>/dev/null || true
73+
else
74+
echo "INFO: killing freeradius process ${FR_PID} with SIGINT for graceful shutdown"
75+
kill -SIGINT ${FR_PID}
76+
fi
77+
78+
# Give valgrind time to write callgrind output after freeradius exits
79+
echo "INFO: sleeping for 5s"
80+
sleep 5
5081

5182
# Signal that valgrind has finished writing all profiling data
83+
echo "INFO: Profiling complete at $(date)"
5284
touch /etc/prof-results/.profiling_complete
85+
86+
# Restore stdout/stderr so subsequent commands in the parent shell
87+
exec > /dev/null 2>&1

src/tests/multi-server/tests/prof-accept/short.ci.test.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ loadgen:
1313
repeat: "no"
1414

1515
# Test framework
16-
test_timeout: 165
17-
test_state1_verify_timeout: 130
18-
test_state2_verify_timeout: 5
16+
# state_1 must cover: valgrind startup (slow under callgrind) + SEND_DURATION sleep (30s) + callgrind dump write
17+
test_timeout: 125
18+
test_state1_verify_timeout: 120
19+
test_state2_verify_timeout: 30

src/tests/multi-server/tests/prof-accept/template.yml.j2

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,17 @@ states:
3030
{%- endfor %}
3131
printf " NUM_MESSAGES: %s\n" "$TEST_LOADGEN_NUM_MESSAGES"
3232

33-
# No profiling
34-
#exec freeradius -f -l stdout
35-
3633
source /etc/freeradius/start_valgrind_profiling.sh
3734

38-
detach: true
39-
- execute_command:
40-
command: |
4135
# Block until valgrind finishes writing all profiling data.
4236
# start_valgrind_profiling.sh touches this file after `wait` returns.
43-
until [ -f /etc/prof-results/.profiling_complete ]; do sleep 5; done
37+
#until [ -f /etc/prof-results/.profiling_complete ]; do sleep 5; done
4438
rm -f /etc/prof-results/.profiling_complete
39+
40+
echo "INFO: running callgrind_annotate to generate report" | tee -a /etc/prof-results/start_valgrind_profiling.log
41+
callgrind_annotate $(find /etc/prof-results -name "callgrind.out.*" -size +0c | sort) > /etc/prof-results/callgrind_report.txt
42+
43+
detach: true
4544
verify:
4645
timeout: {{ test_state1_verify_timeout }}
4746
trigger_mode: unordered
48-
state_2:
49-
description: >
50-
Generate Valgrind/callgrind profiling report
51-
host:
52-
profiling-server:
53-
actions:
54-
- execute_command:
55-
command: |
56-
# Generate report from non-empty callgrind.out.* output files
57-
cd /etc/prof-results
58-
callgrind_annotate $(find . -name "callgrind.out.*" -size +0c | sort) > callgrind_report.txt
59-
verify:
60-
timeout: {{ test_state2_verify_timeout }}
61-
trigger_mode: unordered

0 commit comments

Comments
 (0)