44
55# Clear any stale marker from a previous run
66rm -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
916source /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
3450done
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"
3956CTRL_OUT=$( callgrind_control --instr=on)
4057printf ' %s\n' " $CTRL_OUT "
4158FR_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
4562sleep ${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) "
5284touch /etc/prof-results/.profiling_complete
85+
86+ # Restore stdout/stderr so subsequent commands in the parent shell
87+ exec > /dev/null 2>&1
0 commit comments