-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson-io-top-raw
297 lines (256 loc) · 7.09 KB
/
json-io-top-raw
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
# -*- tab-width: 4 -*- ;; Emacs
# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
############################################################ IDENT(1)
#
# $Title: dwatch(8) JSON module for dtrace_io(4) $
# $Copyright: 2014-2022 Devin Teske. All rights reserved. $
# $FrauBSD: dwatch-json/json-io-top-raw 2022-08-22 15:36:11 -0700 freebsdfrau $
# $Version: 1.0 $
#
############################################################ DESCRIPTION
#
# Produce JSON custom log format for disk I/O
#
############################################################ PROBE
: "${PROBE_SECONDS:=10}"
: ${PROBE:=profile-${PROBE_SECONDS}s}
############################################################ FUNCTIONS
#
# JSON helpers
#
jfmt(){ printf "\\\\\"%s\\\\\":$1" "$2" "$3"; }
jraw(){ jfmt %s "$1" "$2"; }
jstr(){ jraw "$1" "\\\"$2\\\""; }
_jstr(){ printf ,; jstr "$@"; }
_jraw(){ printf ,; jraw "$@"; }
############################################################ OVERRIDES
MAX_ARGS=0 # -B num
MAX_DEPTH=0 # -K num
#
# Unsupported features
#
unset EXECREGEX # -z regex
unset GROUP # -g group
unset PID # -p pid
unset PROBE_COALESCE # -F
unset PSTREE # -R
unset USER # -u user
############################################################ EVENT ACTION
_EVENT_TEST="${EVENT_TEST:+($EVENT_TEST)}"
_EVENT_TEST="${CUSTOM_TEST:+$CUSTOM_TEST${EVENT_TEST:+ && }}$_EVENT_TEST"
if [ "$JID" ]; then
pr_id="curthread->td_proc->p_ucred->cr_prison->pr_id"
_EVENT_TEST="$pr_id == $JID${_EVENT_TEST:+ && ($_EVENT_TEST)}"
unset JID
fi
EVENT_TEST="cpu == 0"
CUSTOM_TEST=
############################################################ ACTIONS
BIO_COMMANDS="
BIO_CMD0
BIO_CMD1
BIO_CMD2
BIO_DELETE
BIO_FLUSH
BIO_GETATTR
BIO_READ
BIO_WRITE
BIO_ZONE
" # END-QUOTE
BIO_CMD_LIST=$( echo "$BIO_COMMANDS" | awk '
/^[[:space:]]*(#|$)/ { next }
{
sub(/#.*/, "")
sub(/^[[:space:]]*/, "")
sub(/[[:space:]]*$/, "")
n = split($0, f, /[[:space:]]+/)
for (i = 1; i <= n; i++) list = list " " f[i]
}
END { print substr(list, 2) }
' )
bio_cmd_list=$( echo "$BIO_CMD_LIST" | awk '{
sub(/^BIO_/, "")
gsub(/ BIO_/, " ")
print tolower($0)
}' )
bytes_list="read_bytes bytes_read write_bytes bytes_written"
stat_list="start done error"
exec 9<<EOF
this struct bio *bio;
this bufinfo_t bufinfo;
this uint8_t b_error;
this int b_flags;
this int bio_cmd;
this long bio_length;
this devinfo_t devinfo;
this string device_entry;
this string device_if;
this string device_type;
$( IFS=" "
for bytes in $bytes_list; do
printf "uint64_t\t%s;\n" "$bytes"
done
for cmd in $bio_cmd_list unknown; do
for stat in $stat_list; do
printf "uint64_t\t%s_%s;\n" $cmd $stat
done
done
printf "\n"
printf "inline int set_globals_to[int num] =\n"
for bytes in $bytes_list; do
printf "\t%s =\n" "$bytes"
done
for cmd in $bio_cmd_list unknown; do
for stat in $stat_list; do
printf "\t%s_%s =\n" $cmd $stat
done
done
printf "\tnum;\n\n"
)
#pragma D binding "1.13" device_name
inline string device_name[devinfo_t di] = di.dev_name != "" ?
strjoin(di.dev_name, lltostr(di.dev_minor)) :
strjoin("[", strjoin(
strjoin(lltostr(di.dev_major), ","),
strjoin(lltostr(di.dev_minor), "]")));
inline int add_bio_cmd_start_bytes[int bio_cmd, uint64_t bytes] =
bio_cmd == BIO_READ ? read_bytes += bytes :
bio_cmd == BIO_WRITE ? write_bytes += bytes :
0;
inline int add_bio_cmd_done_bytes[int bio_cmd, uint64_t bytes] =
bio_cmd == BIO_READ ? bytes_read += bytes :
bio_cmd == BIO_WRITE ? bytes_written += bytes :
0;
$( IFS=" "
for stat in $stat_list; do
printf "inline int increment_bio_cmd_%s[int bio_cmd] =\n" $stat
for cmd in $BIO_CMD_LIST; do
var=$( echo "${cmd#BIO_}" | awk '{print tolower($0)}' )
printf "\tbio_cmd == %s ? %s_%s++ :\n" $cmd $var $stat
done
printf "\tunknown_%s++;\n\n" $stat
done
)
BEGIN /* probe ID $ID */
{${TRACE:+
printf("<$ID>");}
set_globals_to[0];
}
/****************************** I/O ******************************/
io:::start, io:::done /* probe ID $(( $ID + 1 )) */
{${TRACE:+
printf("<$(( $ID + 1 ))>");}
this->bio = (struct bio *)args[0];
}
io:::start, io:::done /this->bio != NULL/ /* probe ID $(( $ID + 2 )) */
{${TRACE:+
printf("<$(( $ID + 2 ))>");
}
/*
* struct bio *
*/
this->bufinfo = xlate <bufinfo_t> ((struct bio *)this->bio);
this->b_flags = (int)this->bufinfo.b_flags;
this->b_error = this->b_flags & BIO_ERROR == BIO_ERROR ? 1 : 0;
this->bio_cmd = (int)this->bufinfo.b_cmd;
this->bio_length = (long)this->bufinfo.b_bcount;
/*
* struct devstat *
*/
this->devinfo = xlate <devinfo_t> ((struct devstat *)args[1]);
this->device_entry = device_name[this->devinfo];
this->device_if = device_if[(int)this->devinfo.dev_type];
this->device_type = device_type[(int)this->devinfo.dev_type];
/* De-duplicate events */
this->bio = (this->device_entry == "[0,-1]" ? NULL : this->bio);
}
io:::start /this->bio != NULL${_EVENT_TEST:+ &&
$_EVENT_TEST}/
/* probe ID $(( $ID + 3 )) */
{${TRACE:+
printf("<$(( $ID + 3 ))>");
}
increment_bio_cmd_start[this->bio_cmd];
add_bio_cmd_start_bytes[this->bio_cmd, this->bio_length];
}
io:::done /this->bio != NULL${_EVENT_TEST:+ &&
$_EVENT_TEST}/
/* probe ID $(( $ID + 4 )) */
{${TRACE:+
printf("<$(( $ID + 4 ))>");
}
increment_bio_cmd_done[this->bio_cmd];
add_bio_cmd_done_bytes[this->bio_cmd, this->bio_length];
}
io:::start, io:::done /this->bio != NULL &&
this->b_error${_EVENT_TEST:+ &&
$_EVENT_TEST}/
/* probe ID $(( $ID + 5 )) */
{${TRACE:+
printf("<$(( $ID + 5 ))>");
}
increment_bio_cmd_error[this->bio_cmd];
}
EOF
ACTIONS=$( cat <&9 )
ID=$(( $ID + 6 ))
############################################################ EVENT TAG
# The EVENT_TAG is run inside the print action after the timestamp has been
# printed. By default, `UID.GID CMD[PID]: ' of the process is printed.
EVENT_TAG="printf(\"${PROFILE%-raw}: \")"
############################################################ EVENT DETAILS
: ${HOSTNAME:=$( hostname )}
SECONDS="walltimestamp / 1000000000"
exec 9<<EOF
/*
* Print event details
*/
printf("{$( IFS=" "
jstr report_type "${PROFILE%-raw}"
_jstr hostname "$HOSTNAME"
_jraw epoch %u
for cmd in $bio_cmd_list unknown; do
for stat in $stat_list; do
key=${cmd}_$stat
_jraw $key %lu
_jraw ${key}_rate %lu
done
done
for bytes in $bytes_list; do
_jraw $bytes %lu
_jraw ${bytes}_rate %lu
done
_jraw start_bytes %lu
_jraw start_bytes_rate %lu
_jraw bytes_done %lu
_jraw bytes_done_rate %lu
)}",
$SECONDS,
$( IFS=" "
for cmd in $bio_cmd_list unknown; do
for stat in $stat_list; do
key=${cmd}_$stat
printf "\n\t\t%s," $key
printf "\n\t\t%s / %u," \
$key $PROBE_SECONDS
done
done
for bytes in $bytes_list; do
printf "\n\t\t%s," $bytes
printf "\n\t\t%s / %u," $bytes $PROBE_SECONDS
done
)
read_bytes + write_bytes,
(read_bytes + write_bytes) / $PROBE_SECONDS,
bytes_read + bytes_written,
(bytes_read + bytes_written) / $PROBE_SECONDS
);
/*
* Reset counters
*/
set_globals_to[0];
EOF
EVENT_DETAILS=$( cat <&9 )
################################################################################
# END
################################################################################