From 61e4f2855b8d901f66869950f5db1a8dcd9a5a2a Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 20 Sep 2011 22:37:22 -0400 Subject: [PATCH] cros_sdk: avoid useless forks The cros_sdk tool runs a lot of helper programs which can be replaced with bash builtins, or condensed multiple calls into a single one. By themselves they aren't that slow, but add them all up and run them a whole lot, and it starts to make a difference. External programs to bash internals: - dirname $f -> ${f%/*} - f=$(cat $f) -> f=$(<$f) - which f -> type -P f Simpler expressions: - [[ ( ... ) ]] -> [[ ... ]] - eval v=\$$f -> v=${!f} Common/clearer expressions: - ! var=$(cmd) -> var=$(cmd || :) Condensed tools: - sort | uniq -> uniq -u BUG=None TEST=`cros_sdk --enter` still works with simultaneous runs TEST=`cros_sdk --enter -- ls` still works Change-Id: Ice5e5e252237082a2249990644e051895d61d1fd Reviewed-on: http://gerrit.chromium.org/gerrit/8029 Commit-Ready: Mike Frysinger Reviewed-by: Mike Frysinger Tested-by: Mike Frysinger --- sdk_lib/enter_chroot.sh | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh index 8da0ab5fa9..8b7db51586 100755 --- a/sdk_lib/enter_chroot.sh +++ b/sdk_lib/enter_chroot.sh @@ -217,7 +217,7 @@ function setup_env { sudo rm -f "${SYNCERPIDFILE}"; fi; if ! [ -f "${SYNCERPIDFILE}" ] || \ - ! [ -d /proc/$(cat "${SYNCERPIDFILE}") ]; then + ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then debug "Starting sync process" env_sync_proc & echo $! > "${SYNCERPIDFILE}" @@ -233,27 +233,26 @@ function setup_env { ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then - TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then + TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh" mkdir -p "${TARGET_DIR}" - cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}" - cp -r ${HOME}/.ssh/*.pub "${TARGET_DIR}" + cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" copy_ssh_config "${TARGET_DIR}" - ASOCK="$(dirname "${SSH_AUTH_SOCK}")" + ASOCK=${SSH_AUTH_SOCK%/*} ensure_mounted "${ASOCK}" "--bind" "${ASOCK}" fi fi MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" + CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)" if [ -z "$CHROME_ROOT" ]; then - ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ - 2>/dev/null)" + CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ + 2>/dev/null || :)" CHROME_ROOT_AUTO=1 fi - if [[ ( -n "$CHROME_ROOT" ) ]]; then - if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then + if [[ -n "$CHROME_ROOT" ]]; then + if [[ ! -d "${CHROME_ROOT}/src" ]]; then error "Not mounting chrome source" sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" if [[ ! "$CHROME_ROOT_AUTO" ]]; then @@ -272,9 +271,9 @@ function setup_env { MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - if [ $(which gclient 2>/dev/null) ]; then + if DEPOT_TOOLS=$(type -P gclient) ; then + DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname debug "Mounting depot_tools" - DEPOT_TOOLS=$(dirname "$(which gclient)") mkdir -p "$MOUNTED_PATH" if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then warn "depot_tools failed to mount; perhaps it's on NFS?" @@ -394,7 +393,7 @@ function teardown_env { fi done # Remove any dups from lock file while installing new one - sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE" + sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE" SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") if [ "${SAVED_PREF}" != "false" ]; then @@ -421,7 +420,7 @@ function teardown_env { # has been reused by another process; make sure we don't skip # starting the syncer process when this occurs by deleting the # PID file. - kill $(cat "${SYNCERPIDFILE}") && \ + kill $(<"${SYNCERPIDFILE}") && \ sudo rm -f "${SYNCERPIDFILE}" || \ debug "Unable to clean up syncer process."; @@ -466,9 +465,8 @@ CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}" # Pass proxy variables into the environment. for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do - eval value=\$${type} - if [ -n "${value}" ]; then - CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${value}" + if [ -n "${!type}" ]; then + CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${!type}" fi done