Skip to content

Commit 85cf293

Browse files
sdaguecboylan
authored andcommitted
Provide timings for OSC during devstack run
The OSC number remain high, and it's useful to understand how much time we spend making OSC calls, especially to surface it relative to other items. The way we embed this in our code makes it hard to instrument. This patch creates a wrapper function for OSC which collects the timings then aliases `openstack` to that function. This means any invocations of the openstack utility goes through our function while devstack is running. Because this is an alias it only affects the stack.sh shell and any subshells. This also moves the time tracking infrastructure to count in ms, instead of s, because some of these operations are close enough to a second that rounding early is losing way to many significant digits. We divide by 1000 before reporting to the user. Change-Id: Ic5f1844ce732d447ee980b3c9fdc417f72482609
1 parent 6fbd969 commit 85cf293

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

functions-common

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,7 +2459,7 @@ function time_start {
24592459
if [[ -n "$start_time" ]]; then
24602460
die $LINENO "Trying to start the clock on $name, but it's already been started"
24612461
fi
2462-
_TIME_START[$name]=$(date +%s)
2462+
_TIME_START[$name]=$(date +%s%3N)
24632463
}
24642464

24652465
# time_stop $name
@@ -2480,14 +2480,57 @@ function time_stop {
24802480
if [[ -z "$start_time" ]]; then
24812481
die $LINENO "Trying to stop the clock on $name, but it was never started"
24822482
fi
2483-
end_time=$(date +%s)
2483+
end_time=$(date +%s%3N)
24842484
elapsed_time=$(($end_time - $start_time))
24852485
total=${_TIME_TOTAL[$name]:-0}
24862486
# reset the clock so we can start it in the future
24872487
_TIME_START[$name]=""
24882488
_TIME_TOTAL[$name]=$(($total + $elapsed_time))
24892489
}
24902490

2491+
function oscwrap {
2492+
local out
2493+
local rc
2494+
local start
2495+
local end
2496+
# Cannot use timer_start and timer_stop as we run in subshells
2497+
# and those rely on modifying vars in the same process (which cannot
2498+
# happen from a subshell.
2499+
start=$(date +%s%3N)
2500+
out=$(command openstack "$@")
2501+
rc=$?
2502+
end=$(date +%s%3N)
2503+
echo $((end - start)) >> $OSCWRAP_TIMER_FILE
2504+
2505+
echo "$out"
2506+
return $rc
2507+
}
2508+
2509+
function install_oscwrap {
2510+
# File to accumulate our timing data
2511+
OSCWRAP_TIMER_FILE=$(mktemp)
2512+
# Bash by default doesn't expand aliases, allow it for the aliases
2513+
# we want to whitelist.
2514+
shopt -s expand_aliases
2515+
# Remove all aliases that might be expanded to preserve old unexpanded
2516+
# behavior
2517+
unalias -a
2518+
# Add only the alias we want for openstack
2519+
alias openstack=oscwrap
2520+
}
2521+
2522+
function cleanup_oscwrap {
2523+
local total=0
2524+
if python3_enabled ; then
2525+
local python=python3
2526+
else
2527+
local python=python
2528+
fi
2529+
total=$(cat $OSCWRAP_TIMER_FILE | $python -c "import sys; print(sum(int(l) for l in sys.stdin))")
2530+
_TIME_TOTAL["osc"]=$total
2531+
rm $OSCWRAP_TIMER_FILE
2532+
}
2533+
24912534
# time_totals
24922535
# Print out total time summary
24932536
function time_totals {
@@ -2506,6 +2549,8 @@ function time_totals {
25062549
fi
25072550
done
25082551

2552+
cleanup_oscwrap
2553+
25092554
xtrace=$(set +o | grep xtrace)
25102555
set +o xtrace
25112556

@@ -2517,6 +2562,8 @@ function time_totals {
25172562
echo
25182563
for t in ${!_TIME_TOTAL[*]}; do
25192564
local v=${_TIME_TOTAL[$t]}
2565+
# because we're recording in milliseconds
2566+
v=$(($v / 1000))
25202567
printf "%-${len}s %3d\n" "$t" "$v"
25212568
done
25222569
echo "========================="

stack.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ function exit_trap {
493493
kill 2>&1 $jobs
494494
fi
495495

496+
#Remove timing data file
497+
if [ -f "$OSCWRAP_TIMER_FILE" ] ; then
498+
rm "$OSCWRAP_TIMER_FILE"
499+
fi
500+
496501
# Kill the last spinner process
497502
kill_spinner
498503

@@ -903,6 +908,10 @@ else
903908
pip_install_gr python-openstackclient
904909
fi
905910

911+
# Installs alias for osc so that we can collect timing for all
912+
# osc commands. Alias dies with stack.sh.
913+
install_oscwrap
914+
906915
if [[ $TRACK_DEPENDS = True ]]; then
907916
$DEST/.venv/bin/pip freeze > $DEST/requires-post-pip
908917
if ! diff -Nru $DEST/requires-pre-pip $DEST/requires-post-pip > $DEST/requires.diff; then

0 commit comments

Comments
 (0)