-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_agent.sh
More file actions
executable file
·159 lines (137 loc) · 4.51 KB
/
run_agent.sh
File metadata and controls
executable file
·159 lines (137 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
# PerfLens Agent Launcher
# Interactive script to start the PerfLens agent on a target device.
# Copy this file + agent/perflens_agent.py to the target device and run it.
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
AGENT_SCRIPT="$SCRIPT_DIR/agent/perflens_agent.py"
# Also check if agent is alongside this script (when copied to device)
if [ ! -f "$AGENT_SCRIPT" ]; then
AGENT_SCRIPT="$SCRIPT_DIR/perflens_agent.py"
fi
if [ ! -f "$AGENT_SCRIPT" ]; then
AGENT_SCRIPT="$(dirname "$0")/perflens_agent.py"
fi
DEFAULT_PORT=9999
DEFAULT_FREQ=99
DEFAULT_DURATION=8
BOLD='\033[1m'
DIM='\033[2m'
GREEN='\033[32m'
CYAN='\033[36m'
YELLOW='\033[33m'
RED='\033[31m'
RESET='\033[0m'
header() {
echo ""
echo -e "${BOLD}${CYAN} PerfLens Agent${RESET}"
echo -e " ${DIM}Profiling agent for target devices${RESET}"
echo ""
}
prompt() {
local var_name=$1 prompt_text=$2 default=$3
if [ -n "$default" ]; then
echo -ne " ${BOLD}${prompt_text}${RESET} ${DIM}[$default]${RESET}: "
else
echo -ne " ${BOLD}${prompt_text}${RESET}: "
fi
read -r value
value="${value:-$default}"
eval "$var_name=\"$value\""
}
check_perf() {
if ! command -v perf &>/dev/null; then
echo -e " ${RED}Error: 'perf' not found. Install linux-tools or perf.${RESET}"
exit 1
fi
}
pick_process() {
echo -e " ${BOLD}Select process to profile:${RESET}"
echo ""
# Show interesting user processes (skip kernel threads, short-lived, and this script)
local procs
procs=$(ps -eo pid,user,%cpu,comm --sort=-%cpu 2>/dev/null \
| grep -v -E '^\s*(PID|$)' \
| grep -v -E '\[(.*)\]' \
| grep -v -E '(bash|sh|ssh|sshd|ps|grep|run_agent|perflens)' \
| head -15)
if [ -z "$procs" ]; then
prompt PID " PID to profile" ""
return
fi
echo -e " ${DIM} PID USER %CPU COMMAND${RESET}"
local i=1
local pids=()
while IFS= read -r line; do
local pid=$(echo "$line" | awk '{print $1}')
pids+=("$pid")
echo -e " ${GREEN}${i})${RESET} $line"
((i++))
done <<< "$procs"
echo -e " ${GREEN}${i})${RESET} Enter PID manually"
echo ""
prompt choice " Choice" "1"
if [ "$choice" -eq "$i" ] 2>/dev/null; then
prompt PID " PID" ""
elif [ "$choice" -ge 1 ] && [ "$choice" -lt "$i" ] 2>/dev/null; then
PID="${pids[$((choice - 1))]}"
else
PID="${pids[0]}"
fi
}
header
check_perf
if [ ! -f "$AGENT_SCRIPT" ]; then
echo -e " ${RED}Error: Cannot find perflens_agent.py${RESET}"
echo -e " ${DIM}Expected at: $SCRIPT_DIR/agent/perflens_agent.py${RESET}"
echo -e " ${DIM}Or alongside this script: $(dirname "$0")/perflens_agent.py${RESET}"
exit 1
fi
# --- Process selection ---
pick_process
if [ -z "$PID" ]; then
echo -e " ${RED}Error: No PID selected${RESET}"
exit 1
fi
if ! kill -0 "$PID" 2>/dev/null; then
echo -e " ${YELLOW}Warning: PID $PID doesn't exist or no permission${RESET}"
fi
# --- Server connection ---
echo ""
prompt SERVER_IP "Server IP" ""
if [ -z "$SERVER_IP" ]; then
echo -e " ${RED}Error: Server IP is required${RESET}"
exit 1
fi
prompt SERVER_PORT "Server port" "$DEFAULT_PORT"
# --- Advanced options ---
echo ""
echo -e " ${DIM}Advanced (press Enter for defaults):${RESET}"
prompt FREQ "Sample frequency (Hz)" "$DEFAULT_FREQ"
prompt DURATION "Sample duration (seconds)" "$DEFAULT_DURATION"
# --- Summary ---
PROC_NAME=$(ps -p "$PID" -o comm= 2>/dev/null || echo "unknown")
echo ""
echo -e " ${DIM}──────────────────────────────────────────${RESET}"
echo -e " ${BOLD}Process :${RESET} $PROC_NAME (PID $PID)"
echo -e " ${BOLD}Server :${RESET} $SERVER_IP:$SERVER_PORT"
echo -e " ${BOLD}Frequency :${RESET} ${FREQ} Hz"
echo -e " ${BOLD}Duration :${RESET} ${DURATION}s per sample"
echo -e " ${DIM}──────────────────────────────────────────${RESET}"
echo ""
CMD=(python3 "$AGENT_SCRIPT"
--pid "$PID"
--server "$SERVER_IP"
--port "$SERVER_PORT"
--frequency "$FREQ"
--duration "$DURATION")
# Check if we need sudo for perf
if [ "$(id -u)" -ne 0 ]; then
echo -e " ${YELLOW}Note: perf usually requires root. Running with sudo.${RESET}"
CMD=(sudo "${CMD[@]}")
fi
echo -e " ${DIM}${CMD[*]}${RESET}"
echo ""
echo -e " ${GREEN}Starting profiler...${RESET} (Ctrl+C to stop)"
echo ""
exec "${CMD[@]}"