-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_latency.lua
265 lines (218 loc) · 6.3 KB
/
socket_latency.lua
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
-- Chisel description
description = "Draw latencies between request and responce, to choosen ip or/and port"
short_description = "Sockets request/responce latency"
category = "Network"
-- Chisel argument list
args =
{
{
name = "direction",
description = "in/out",
argtype = "string"
},
{
name = "ip",
description = "Target IP",
argtype = "string",
optional = true
},
{
name = "port",
description = "Target port",
argtype = "int",
optional = true
},
{
name = "refresh_time",
description = "Chart refresh time in milliseconds",
argtype = "int",
optional = true
},
}
require "common"
terminal = require "ansiterminal"
terminal.enable_color(true)
write_etypes = {}
write_etypes["sendto"] = true
write_etypes["writev"] = true
write_etypes["write"] = true
write_etypes["pwrite"] = true
read_etypes = {}
read_etypes["recvfrom"] = true
read_etypes["readv"] = true
read_etypes["read"] = true
read_etypes["pread"] = true
refresh_time = 1000000000
refresh_per_sec = 1000000000 / refresh_time
time_map = {}
frequencies = {}
freq_version = 0
colpalette = {22, 28, 64, 34, 2, 76, 46, 118, 154, 191, 227, 226, 11, 220, 209, 208, 202, 197, 9, 1}
ip = ""
port = 0
-- Argument notification callback
function on_set_arg(name, val)
if name == "direction" then
direction = val
return true
elseif name == "ip" then
ip = val
return true
elseif name == "port" then
port = val
return true
elseif name == "refresh_time" then
refresh_time = parse_numeric_input(val, name) * 1000000
refresh_per_sec = 1000000000 / refresh_time
return true
end
return false
end
-- Initialization callback
function on_init()
if (port == nil or port == 0) and (ip == nil or ip == "") then
print("IP or port, or both must be selected")
return false
end
is_tty = sysdig.is_tty()
if not is_tty then
print("This chisel only works on ANSI terminals. Aborting.")
return false
end
tinfo = sysdig.get_terminal_info()
w = tinfo.width
h = tinfo.height
terminal.hidecursor()
-- Request the fileds that we need
field_pid = chisel.request_field("proc.pid")
field_fdnum = chisel.request_field("fd.num")
field_etype = chisel.request_field("evt.type")
field_etime = chisel.request_field("evt.rawtime")
field_edir = chisel.request_field("evt.dir")
local filter = "evt.is_io=true and fd.type=ipv4"
if port ~= nil and port ~= 0 then
filter = filter .. " and fd.port=" .. port
end
if ip ~= nil and ip ~= "" then
filter = filter .. " and fd.ip=" .. ip
end
chisel.set_filter(filter)
return true
end
-- Final chisel initialization
function on_capture_start()
chisel.set_interval_ns(refresh_time)
return true
end
function update_frequency(pid, fdnum, etype, etime, edir)
if time_map[pid] ~= nil and time_map[pid][fdnum] ~= nil then
local llatency = math.log10(time_map[pid][fdnum].stop - time_map[pid][fdnum].start)
if(llatency > 11) then
llatency = 11
end
local norm_llatency = math.floor(llatency * w / 11) + 1
if time_map[pid][fdnum].freq_version ~= freq_version then
time_map[pid][fdnum].freq_version = freq_version
else
if frequencies[time_map[pid][fdnum].platency] ~= nil then
frequencies[time_map[pid][fdnum].platency] = frequencies[time_map[pid][fdnum].platency] - 1
end
end
if frequencies[norm_llatency] == nil then
frequencies[norm_llatency] = 1
else
frequencies[norm_llatency] = frequencies[norm_llatency] + 1
end
time_map[pid][fdnum].platency = norm_llatency
end
end
-- Event parsing callback
function on_event()
local pid = evt.field(field_pid)
local fdnum = evt.field(field_fdnum)
local etype = evt.field(field_etype)
local etime = evt.field(field_etime)
local edir = evt.field(field_edir)
if (direction == "out" and write_etypes[etype] == true and edir == ">") or
(direction == "in" and read_etypes[etype] == true and edir == ">") then
if time_map[pid] == nil then
time_map[pid] = {}
end
if time_map[pid][fdnum] ~= nil then
update_frequency(pid, fdnum, etype, etime, edir)
time_map[pid][fdnum].platency = 0
time_map[pid][fdnum].start = etime
time_map[pid][fdnum].stop = etime
else
time_map[pid][fdnum] = {
start = etime,
stop = etime,
platency = 0,
freq_version = freq_version
}
end
elseif (direction == "out" and read_etypes[etype] == true and edir == "<") or
(direction == "in" and write_etypes[etype] == true and edir == "<") then
if time_map[pid] ~= nil and time_map[pid][fdnum] ~= nil then
update_frequency(pid, fdnum, etype, etime, edir)
time_map[pid][fdnum].stop = etime
end
end
return true
end
function mkcol(n)
local col = math.floor(math.log10(n * refresh_per_sec + 1) / math.log10(1.6))
if col < 1 then
col = 1
end
if col > #colpalette then
col = #colpalette
end
return colpalette[col]
end
-- Periodic timeout callback
function on_interval(ts_s, ts_ns, delta)
terminal.moveup(1)
for x = 1, w do
local fr = frequencies[x]
if fr == nil or fr == 0 then
terminal.setbgcol(0)
else
terminal.setbgcol(mkcol(fr))
end
io.write(" ")
end
io.write(terminal.reset .. "\n")
local x = 0
while true do
if x >= w then
break
end
local curtime = math.floor(x * 11 / w)
local prevtime = math.floor((x - 1) * 11 / w)
if curtime ~= prevtime then
io.write("|")
local tstr = format_time_interval(math.pow(10, curtime))
io.write(tstr)
x = x + #tstr + 1
else
io.write(" ")
x = x + 1
end
end
io.write("\n")
frequencies = {}
freq_version = ts_ns
return true
end
-- Called by the engine at the end of the capture (Ctrl-C)
function on_capture_end(ts_s, ts_ns, delta)
if is_tty then
-- Include the last sample
on_interval(ts_s, ts_ns, 0)
-- reset the terminal
print(terminal.reset)
terminal.showcursor()
end
return true
end