1919usage () {
2020 echo " This script downloads all of the files listed in the mappings section of a heap profile."
2121 echo " "
22- echo " Usage: $0 <heap_profile> <node_name> [<gcloud ssh opts>...]"
23- echo " Common gcloud ssh options include --project."
22+ echo " Usage: $0 <heap_profile> <node_name> [--gcloud-common-args] [--gcloud-ssh-args...]"
23+ echo " <heap_profile> : the path to the heap profile file (in pprof format) to analyze."
24+ echo " <node_name> : the name of the node to connect to (e.g., the name of a Vizier agent node)."
25+ echo " [--gcloud-common-args] : common arguments to pass to gcloud commands, such as --project."
26+ echo " [--gcloud-ssh-args] : additional arguments to pass to gcloud compute ssh commands, such as --internal-ip."
2427 exit 1
2528}
2629
27- heap_profile=" $1 "
28- node_name=" $2 "
29- output_dir=" ${heap_profile% .txt} "
30+ parse_args () {
31+ if [ $# -lt 2 ]; then
32+ usage
33+ fi
34+
35+ heap_profile=" $1 "
36+ shift
37+
38+ node_name=" $1 "
39+ shift
40+
41+ while test $# -gt 0; do
42+ case " $1 " in
43+ --gcloud-common-args=* )
44+ GCLOUD_COMMON_ARGS=" ${1#* =} "
45+ shift
46+ ;;
47+ --gcloud-common-args)
48+ GCLOUD_COMMON_ARGS=" $2 "
49+ shift
50+ shift
51+ ;;
52+ --gcloud-ssh-args=* )
53+ GCLOUD_SSH_ARGS=" ${1#* =} "
54+ shift
55+ ;;
56+ --gcloud-ssh-args)
57+ GCLOUD_SSH_ARGS=" $2 "
58+ shift
59+ shift
60+ ;;
61+ * ) usage ;;
62+ esac
63+ done ;
64+ }
65+
3066
31- if [ -z " $heap_profile " ] || [ -z " $node_name " ]; then
32- usage
33- fi
67+ check_args () {
68+ if [ -z " $heap_profile " ] || [ -z " $node_name " ]; then
69+ usage
70+ fi
71+ }
72+
73+ parse_args " ${@ } "
74+ check_args
75+
76+ output_dir=" ${heap_profile% .txt} "
3477
3578# Create the output directory at the beginning of the script.
3679mkdir -p " $output_dir "
@@ -44,16 +87,16 @@ mkdir -p "$output_dir"
4487mappings=$( awk ' BEGIN{m=0} /MAPPED_LIBRARIES/{m=1} { if(m) { print $6 }}' " $heap_profile " | grep " ^/" | sort | uniq)
4588
4689err_file=" $output_dir /gcloud_error.log"
47- zone=$( gcloud compute instances list " ${@: 3 } " --filter=" $node_name " --format=" table(name, zone)" | tail -n 1 | awk ' {print $2}' )
48- procs=$( gcloud compute ssh --zone " $zone " --command=' ps ax' " $node_name " " ${@: 3 } " 2> " $err_file " ) || cat " $err_file " && rm " $err_file "
90+ zone=$( gcloud compute instances list " ${GCLOUD_COMMON_ARGS } " --filter=" $node_name " --format=" table(name, zone)" | tail -n 1 | awk ' {print $2}' )
91+ procs=$( gcloud compute ssh --zone " $zone " --command=' ps ax' " $node_name " " ${GCLOUD_COMMON_ARGS} " " ${GCLOUD_SSH_ARGS }" 2> " $err_file " ) || cat " $err_file " && rm " $err_file "
4992
5093# Find the mapping that corresponds to a process on the node.
5194# We assume that the process was started by running one of the files in the mappings
5295# (which is commonly the case and certainly the case for the binaries we care about, eg. pem, kelvin).
5396for fname in $mappings
5497do
5598 bname=$( basename " $fname " )
56- matching_proc=$( echo " $procs " | grep " $bname " || true)
99+ matching_proc=$( echo " $procs " | grep -E " $bname $ " || true)
57100 if [[ -n " $matching_proc " ]]; then
58101 pid=$( echo " $matching_proc " | awk ' { print $1 }' )
59102 matched_file=" $fname "
@@ -80,13 +123,13 @@ output_on_err() {
80123}
81124
82125# Create tar archive on node.
83- output_on_err gcloud compute ssh --zone " $zone " --command=" $create_tar_cmd " " $node_name " " ${@: 3 } "
126+ output_on_err gcloud compute ssh --zone " $zone " --command=" $create_tar_cmd " " $node_name " " ${GCLOUD_COMMON_ARGS} " " ${GCLOUD_SSH_ARGS }"
84127
85128# Copy archive to local machine.
86- output_on_err gcloud compute scp --zone " $zone " " ${@: 3 } " " $USER @$node_name :~/$tar_file " " ${output_dir} /$tar_file "
129+ output_on_err gcloud compute scp --zone " $zone " " ${GCLOUD_COMMON_ARGS} " " ${GCLOUD_SSH_ARGS }" " $USER @$node_name :~/$tar_file " " ${output_dir} /$tar_file "
87130
88131# Cleanup tar archive on node.
89- output_on_err gcloud compute ssh --zone " $zone " --command=" rm ~/$tar_file " " $node_name " " ${@: 3 } "
132+ output_on_err gcloud compute ssh --zone " $zone " --command=" rm ~/$tar_file " " $node_name " " ${GCLOUD_COMMON_ARGS} " " ${GCLOUD_SSH_ARGS }"
90133
91134tar --strip-components=1 -C " $output_dir " -xzf " ${output_dir} /$tar_file "
92135
0 commit comments