Commit c02c5e04 authored by Michal Berger's avatar Michal Berger Committed by Konrad Sztyber
Browse files

scripts/bash-completion: Speed up rpc lookup



Do this by caching completions to avoid costly run through the help
parser. We cache each completion since it's very unlikely that help
for given RPC method may suddenly change.

One exception here is a list of available RPCs methods which may
change depending on if rpc.py "get"s it while connecting through
an actual app socket. Here, we need to reset the cached list whenever
sock changes or when actual list gets cleaned up.

The overall result should be better user experience with faster
responsiveness while tab'ing through the available opts.

Change-Id: Ia2e10957558095e22adc57aaed300e121c682d40
Signed-off-by: default avatarMichal Berger <michal.berger@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/25362


Community-CI: Community CI Samsung <spdk.community.ci.samsung@gmail.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
parent f22e807f
Loading
Loading
Loading
Loading
+26 −5
Original line number Diff line number Diff line
@@ -201,17 +201,38 @@ _rpc() {
	_init_completion || return
	_is_app "$1" || return

	local rpc=$1 rpc_sock="" method=""
	local -A rpc_methods=()
	local rpc=$1 method=""
	local -g rpc_sock="" rpc_sock_ts old_rpc_sock_ts
	local -gA rpc_methods
	local -gA __comps

	_set_rpc_sock
	# Verify sock against the creation time - this is meant to cover cases of different
	# applications re-creating the socket file at the same location.
	rpc_sock_ts=$(stat --print="%W" "$rpc_sock" 2> /dev/null)
	# Always try to refresh list of methods when sock changed or when the list
	# is empty.
	if [[ $old_rpc_sock_ts != "$rpc_sock_ts" ]] || ((${#rpc_methods[@]} == 0)); then
		rpc_methods=()
		_get_rpc_methods "$rpc"
	fi
	old_rpc_sock_ts=$rpc_sock_ts

	if method=$(_method_in_words); then
		if [[ -n ${__comps["$rpc:$method:$cur"]} ]]; then
			COMPREPLY=(${__comps["$rpc:$method:$cur"]})
		else
			COMPREPLY=($(compgen -W '$(_get_help_rpc_method "$rpc" "$method")' -- "$cur"))
			__comps["$rpc:$method:$cur"]=${COMPREPLY[*]}
		fi
		compopt -o nosort
	elif [[ $cur == -* ]]; then
		if [[ -n ${__comps["$rpc:$cur"]} ]]; then
			COMPREPLY=(${__comps["$rpc:$cur"]})
		else
			COMPREPLY=($(compgen -W '$(_parse_help "$rpc")' -- "$cur"))
			__comps["$rpc:$cur"]=${COMPREPLY[*]}
		fi
	elif [[ $prev == --verbose ]]; then
		COMPREPLY=($(compgen -W 'DEBUG INFO ERROR' -- "$cur"))
	elif [[ $prev == -s ]]; then