-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathnio-diagnose
executable file
·405 lines (331 loc) · 8.14 KB
/
nio-diagnose
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
set -u # do _NOT_ set -e here, some output is better than none...
PATH=/bin:/sbin:/usr/bin:/usr/sbin
output_file=""
function usage() {
echo >&2 "Usage: $0 [OPTIONS] PIDS..."
echo >&2
echo >&2 "Options:"
echo >&2 " -o OUTPUT_FILE"
echo >&2
echo >&2 "Examples:"
echo >&2 " $0 \$(pgrep MyService) # if your binary is called MyService"
echo >&2 " $0 12334 3731 313 # if your NIO binaries have pid 12234, 3731 and 313"
}
function log() {
echo >&2 "$*"
}
function info() {
log "info: $*"
}
function warning() {
log "WARNING: $*"
}
function die() {
log "ERROR: $*"
exit 1
}
function is_linux() {
if [[ "$(uname -s)" == Linux ]]; then
return 0
else
return 1
fi
}
complained_about_missing=()
function where_is() {
local where_is_var
local where_from
local found
found=false
for f in "${complained_about_missing[@]:+"${complained_about_missing[@]}"}"; do
if [[ "$f" == "$1" ]]; then
found=true
fi
done
if "$found"; then
return 0
fi
if is_linux; then
local where_is_netstat="sudo apt-get update && sudo apt-get install net-tools"
true "$where_is_netstat" # silence the unused warning
local where_is_lsof="sudo apt-get update && sudo apt-get install lsof"
true "$where_is_lsof" # silence the unused warning
fi
where_is_var="where_is_$1"
where_from=""
where_is_info="${!where_is_var:-}"
if [[ -n "$where_is_info" ]]; then
where_from=" (you might want to get it via \`${where_is_info}\`)"
fi
warning "\`$1\` not found$where_from"
complained_about_missing+=("$1")
}
while getopts "o:" opt; do
case "$opt" in
o)
output_file="$OPTARG"
true > "$output_file" # truncate the file
;;
/?)
usage
exit 1
;;
esac
done
shift $(( OPTIND - 1 ))
if ! is_linux; then
if [[ $# -lt 1 ]]; then
usage
die "At least one pid required"
fi
fi
if [[ -z "$output_file" ]]; then
output_file=$(mktemp "/tmp/nio-diagnose_$(date +%s)_XXXXXX")
fi
function output() {
if [[ "$output_file" == - ]]; then
echo "$*"
else
echo "$*" >> "$output_file"
fi
}
function stream_output() {
if [[ "$output_file" == - ]]; then
cat
else
cat >> "$output_file"
fi
}
function guess_nio_pids() {
declare -a nio_pids
if is_linux; then
nio_pids=()
while IFS=/ read -r _ _ pid _; do
nio_pids+=("$pid")
done < <(grep ^NIO-ELT /proc/*/task/*/comm 2> /dev/null || true)
for pid in "${nio_pids[@]+"${nio_pids[@]}"}"; do
echo "$pid"
done | sort | uniq
else
die "Sorry, can only guess the NIO pids on Linux"
fi
}
function nio_pids() {
if [[ $# -gt 0 ]]; then
for nio_pid in "$@"; do
echo "$nio_pid"
done
else
info "No pids provided, guessing NIO pids."
info "It's recommended that you pass the pids of your NIO programs as arguments."
guess_nio_pids
fi
}
function run_and_print() {
declare -ra command=( "$@" )
if ! command -v "${command[0]}" > /dev/null 2>&1; then
where_is "${command[0]}"
fi
output 'Command: `'"${command[*]}"'`'
output
output '```'
"${command[@]}" 2>&1 | stream_output
output '```'
output
}
function analyse_open_fds() {
local pid
declare -a command
pid=$1
command=( lsof -Pnp "$pid" )
output "### Open file descriptors (pid $pid)"
output
run_and_print "${command[@]}"
if ! which -v lsof > /dev/null 2> /dev/null; then
run_and_print ls -la "/proc/$pid/fd"
fi
}
function composite_command_ls_epoll() {
for fd_file in "/proc/$pid/fd"/*; do
if [[ "$(readlink "$fd_file")" =~ eventpoll ]]; then
fd=$(basename "$fd_file")
echo "epoll fd $fd"
echo "==========="
cat "/proc/$pid/fdinfo/$fd"
echo
fi
done
}
function composite_command_system_versions_darwin() {
date
uname -a
sw_vers
swift -version || true
id
}
function composite_command_system_versions_linux() {
date
uname -a
swift -version || true
id
}
function analyse_eventing_fds() {
local pid
declare -a command
pid=$1
if is_linux; then
command=( composite_command_ls_epoll "$pid" )
else
command=( lskq -p "$pid" )
fi
output "### Eventing fds state (pid $pid)"
output
run_and_print "${command[@]}"
}
function analyse_ulimit() {
declare -a command
command=( ulimit -a )
output "### ulimits"
output
run_and_print "${command[@]}"
}
function analyse_procs() {
declare -a command
if is_linux; then
command=( bash -c 'TERM=dumb top -H -n 1' )
else
command=( top -l 1 )
fi
output "### processes"
output
output "#### top"
output
run_and_print "${command[@]}"
output
output "#### ps"
output
run_and_print ps auxw
}
function analyse_system_versions() {
declare -a command
if is_linux; then
command=( composite_command_system_versions_linux )
else
command=( composite_command_system_versions_darwin )
fi
output "### System versions"
output
run_and_print "${command[@]}"
}
function analyse_syscalls() {
declare -a command
if is_linux; then
command=( grep -H ^ "/proc/$pid/task"/*/syscall )
else
command=( echo "Unsupported on Darwin" )
fi
output "### Syscalls by thread"
output
run_and_print "${command[@]}"
}
function analyse_thread_names() {
declare -a command
if is_linux; then
command=( grep -H ^ "/proc/$pid/task"/*/comm )
else
command=( echo "Unsupported on Darwin" )
fi
output "### Thread names"
output
run_and_print "${command[@]}"
}
function analyse_tcp_conns() {
declare -a command
if is_linux; then
command=( netstat --tcp -peona )
else
command=( netstat -n -p tcp -a )
fi
output "### TCP connections"
output
run_and_print "${command[@]}"
}
function analyse_udp() {
declare -a command
if is_linux; then
command=( netstat --udp -peona )
else
command=( netstat -n -p udp -a )
fi
output "### UDP"
output
run_and_print "${command[@]}"
}
function analyse_uds() {
declare -a command
if is_linux; then
command=( netstat --protocol=unix -peona )
else
command=( netstat -n -a -f unix )
fi
output "### Unix Domain Sockets"
output
run_and_print "${command[@]}"
}
function analyse_process() {
local pid
pid=$1
output "## Analysing pid $pid"
output
if ! kill -0 "$pid" 2> /dev/null; then # ignore-unacceptable-language
output "pid $pid is dead, skipping"
return 0
fi
analyse_open_fds "$pid"
analyse_eventing_fds "$pid"
analyse_thread_names "$pid"
analyse_syscalls "$pid"
}
function produce_network_stats() {
declare -a command
command=( netstat -s )
output "### System-wide network statistics"
output
run_and_print "${command[@]}"
}
number_of_nio_pids=0
output "# NIO diagnose ($(shasum "${BASH_SOURCE[0]}"))"
output
output "## System information"
output
analyse_system_versions
analyse_tcp_conns
analyse_udp
analyse_uds
produce_network_stats
analyse_procs
analyse_ulimit
while read -r nio_pid; do
number_of_nio_pids=$(( number_of_nio_pids + 1 ))
analyse_process "$nio_pid"
done < <(nio_pids "$@")
info "output written to $output_file"
if [[ "$number_of_nio_pids" -gt 0 ]]; then
exit 0
else
exit 1
fi