|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Usage: ./tracing.sh {start|stop} |
| 4 | + |
| 5 | +if [ $# -ne 1 ]; then |
| 6 | + echo "Usage: $0 {start|stop}" |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | + |
| 10 | +ACTION=$1 |
| 11 | +TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S) |
| 12 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 13 | + |
| 14 | +# List of container name substrings to trace |
| 15 | +# Default container substrings |
| 16 | +DEFAULT_CONTAINER_SUBSTRINGS=( |
| 17 | + "perception.point-cloud-fusion" |
| 18 | + "perception.point-cloud-object-detection" |
| 19 | + "understanding.autoware-multi-object-tracker" |
| 20 | + "understanding.lanelet2-object-list-prediction" |
| 21 | + "planning.simple-planner" |
| 22 | + "planning.trajectory-optimization" |
| 23 | + "control.ackermann-trajectory-control" |
| 24 | +) |
| 25 | + |
| 26 | +# Allow additional container substrings via EXTRA_TRACING_CONTAINERS env var |
| 27 | +# e.g. EXTRA_TRACING_CONTAINERS="my-container-1 my-other-container-1" ./tracing.sh start |
| 28 | +EXTRA_SUBSTRINGS=() |
| 29 | +if [ -n "$EXTRA_TRACING_CONTAINERS" ]; then |
| 30 | + IFS=' ' read -ra EXTRA_SUBSTRINGS <<< "$EXTRA_TRACING_CONTAINERS" |
| 31 | +fi |
| 32 | + |
| 33 | +CONTAINER_SUBSTRINGS=("${DEFAULT_CONTAINER_SUBSTRINGS[@]}" "${EXTRA_SUBSTRINGS[@]}") |
| 34 | + |
| 35 | +DOCKER_USER="dockeruser" |
| 36 | +TRACE_ROOT="/home/dockeruser/.ros/tracing" |
| 37 | + |
| 38 | +format_target_label() { |
| 39 | + local target_type=$1 |
| 40 | + local target_namespace=$2 |
| 41 | + local target_name=$3 |
| 42 | + |
| 43 | + if [ "$target_type" == "docker" ]; then |
| 44 | + echo "$target_name" |
| 45 | + else |
| 46 | + echo "$target_namespace/$target_name" |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +format_target_output_dir() { |
| 51 | + local target_type=$1 |
| 52 | + local target_namespace=$2 |
| 53 | + local target_name=$3 |
| 54 | + |
| 55 | + if [ "$target_type" == "docker" ]; then |
| 56 | + echo "$target_name" |
| 57 | + else |
| 58 | + echo "k8s-${target_namespace}-${target_name}" |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +exec_in_target() { |
| 63 | + local target_type=$1 |
| 64 | + local target_namespace=$2 |
| 65 | + local target_name=$3 |
| 66 | + local command=$4 |
| 67 | + |
| 68 | + if [ "$target_type" == "docker" ]; then |
| 69 | + docker exec --user "$DOCKER_USER" "$target_name" bash -ic "$command" |
| 70 | + else |
| 71 | + kubectl exec -n "$target_namespace" "$target_name" -- bash -ic "$command" |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +copy_trace_from_target() { |
| 76 | + local target_type=$1 |
| 77 | + local target_namespace=$2 |
| 78 | + local target_name=$3 |
| 79 | + local destination_dir=$4 |
| 80 | + |
| 81 | + if [ "$target_type" == "docker" ]; then |
| 82 | + docker cp "$target_name:$TRACE_ROOT/trace" "$destination_dir" |
| 83 | + else |
| 84 | + kubectl cp "$target_namespace/$target_name:/root/.ros/tracing/trace" "$destination_dir" |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +# Arrays to track results |
| 89 | +SUCCESS_TARGETS=() |
| 90 | +FAILED_TARGETS=() |
| 91 | +MISSING_TARGETS=() |
| 92 | + |
| 93 | +# Validate action |
| 94 | +if [ "$ACTION" != "start" ] && [ "$ACTION" != "stop" ]; then |
| 95 | + echo "Usage: $0 {start|stop}" |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | + |
| 99 | +mapfile -t RUNNING_CONTAINERS < <(docker ps --format '{{.Names}}' 2>/dev/null) |
| 100 | +mapfile -t RUNNING_PODS < <(kubectl get pods -A --field-selector=status.phase=Running --no-headers -o custom-columns=NS:.metadata.namespace,NAME:.metadata.name 2>/dev/null) |
| 101 | + |
| 102 | +for substring in "${CONTAINER_SUBSTRINGS[@]}"; do |
| 103 | + MATCHED_TARGETS=() |
| 104 | + |
| 105 | + for container in "${RUNNING_CONTAINERS[@]}"; do |
| 106 | + if [[ "$container" == *"$substring"* ]]; then |
| 107 | + MATCHED_TARGETS+=("docker||$container") |
| 108 | + fi |
| 109 | + done |
| 110 | + |
| 111 | + for pod_entry in "${RUNNING_PODS[@]}"; do |
| 112 | + read -r namespace pod_name <<< "$pod_entry" |
| 113 | + if [[ "$pod_name" == *"$substring"* ]]; then |
| 114 | + MATCHED_TARGETS+=("k8s|$namespace|$pod_name") |
| 115 | + fi |
| 116 | + done |
| 117 | + |
| 118 | + if [ ${#MATCHED_TARGETS[@]} -eq 0 ]; then |
| 119 | + echo "Warning: No running target containing '$substring'." |
| 120 | + MISSING_TARGETS+=("$substring") |
| 121 | + continue |
| 122 | + fi |
| 123 | + |
| 124 | + for target in "${MATCHED_TARGETS[@]}"; do |
| 125 | + target_type=${target%%|*} |
| 126 | + target_remainder=${target#*|} |
| 127 | + target_namespace=${target_remainder%%|*} |
| 128 | + target_name=${target_remainder#*|} |
| 129 | + ( |
| 130 | + TARGET_LABEL=$(format_target_label "$target_type" "$target_namespace" "$target_name") |
| 131 | + |
| 132 | + if [ "$ACTION" == "start" ]; then |
| 133 | + echo "Starting tracing in target: $TARGET_LABEL" |
| 134 | + |
| 135 | + TRACE_COMMAND="ros2 trace start trace --dual-session" |
| 136 | + exec_in_target "$target_type" "$target_namespace" "$target_name" "$TRACE_COMMAND" |
| 137 | + |
| 138 | + if [ $? -eq 0 ]; then |
| 139 | + echo "$TARGET_LABEL" >> /tmp/tracing_success_$$ |
| 140 | + else |
| 141 | + echo "$TARGET_LABEL" >> /tmp/tracing_failed_$$ |
| 142 | + fi |
| 143 | + |
| 144 | + elif [ "$ACTION" == "stop" ]; then |
| 145 | + echo "Stopping tracing in target: $TARGET_LABEL" |
| 146 | + STOP_COMMAND="ros2 trace stop trace --dual-session" |
| 147 | + exec_in_target "$target_type" "$target_namespace" "$target_name" "$STOP_COMMAND" |
| 148 | + |
| 149 | + STOP_STATUS=$? |
| 150 | + |
| 151 | + echo "Copy trace files from target: $TARGET_LABEL" |
| 152 | + TARGET_OUTPUT_DIR="$SCRIPT_DIR/trace/$TIMESTAMP/$(format_target_output_dir "$target_type" "$target_namespace" "$target_name")" |
| 153 | + mkdir -p "$TARGET_OUTPUT_DIR" |
| 154 | + copy_trace_from_target "$target_type" "$target_namespace" "$target_name" "$TARGET_OUTPUT_DIR" |
| 155 | + |
| 156 | + COPY_STATUS=$? |
| 157 | + |
| 158 | + echo "Removing trace files from target: $TARGET_LABEL" |
| 159 | + REMOVE_COMMAND="rm -rf $TRACE_ROOT" |
| 160 | + exec_in_target "$target_type" "$target_namespace" "$target_name" "$REMOVE_COMMAND" |
| 161 | + |
| 162 | + REMOVE_STATUS=$? |
| 163 | + |
| 164 | + if [ $STOP_STATUS -eq 0 ] && [ $COPY_STATUS -eq 0 ] && [ $REMOVE_STATUS -eq 0 ]; then |
| 165 | + echo "$TARGET_LABEL" >> /tmp/tracing_success_$$ |
| 166 | + else |
| 167 | + echo "$TARGET_LABEL" >> /tmp/tracing_failed_$$ |
| 168 | + fi |
| 169 | + fi |
| 170 | + ) & |
| 171 | + done |
| 172 | +done |
| 173 | + |
| 174 | +# Wait for all background jobs to complete |
| 175 | +wait |
| 176 | + |
| 177 | +# Read results from temporary files |
| 178 | +if [ -f /tmp/tracing_success_$$ ]; then |
| 179 | + mapfile -t SUCCESS_TARGETS < /tmp/tracing_success_$$ |
| 180 | + rm /tmp/tracing_success_$$ |
| 181 | +fi |
| 182 | + |
| 183 | +if [ -f /tmp/tracing_failed_$$ ]; then |
| 184 | + mapfile -t FAILED_TARGETS < /tmp/tracing_failed_$$ |
| 185 | + rm /tmp/tracing_failed_$$ |
| 186 | +fi |
| 187 | + |
| 188 | +# Print summary |
| 189 | +echo "" |
| 190 | +echo "============================================" |
| 191 | +echo "Summary - $ACTION operation:" |
| 192 | +echo "============================================" |
| 193 | + |
| 194 | +if [ ${#SUCCESS_TARGETS[@]} -gt 0 ]; then |
| 195 | + echo "✓ Successfully ${ACTION}ed tracing for ${#SUCCESS_TARGETS[@]} target(s):" |
| 196 | + for target in "${SUCCESS_TARGETS[@]}"; do |
| 197 | + echo " - $target" |
| 198 | + done |
| 199 | +else |
| 200 | + echo "✓ No targets ${ACTION}ed successfully" |
| 201 | +fi |
| 202 | + |
| 203 | +echo "" |
| 204 | + |
| 205 | +if [ ${#FAILED_TARGETS[@]} -gt 0 ]; then |
| 206 | + echo "✗ Failed to $ACTION tracing for ${#FAILED_TARGETS[@]} target(s):" |
| 207 | + for target in "${FAILED_TARGETS[@]}"; do |
| 208 | + echo " - $target" |
| 209 | + done |
| 210 | +else |
| 211 | + echo "✗ No failures" |
| 212 | +fi |
| 213 | + |
| 214 | +echo "" |
| 215 | + |
| 216 | +if [ ${#MISSING_TARGETS[@]} -gt 0 ]; then |
| 217 | + echo "⚠ Missing ${#MISSING_TARGETS[@]} target(s) (not running):" |
| 218 | + for substring in "${MISSING_TARGETS[@]}"; do |
| 219 | + echo " - $substring" |
| 220 | + done |
| 221 | +fi |
| 222 | + |
| 223 | +echo "=============================================" |
0 commit comments