2021-07-18 00:38:43 -04:00
|
|
|
local Util = require 'Util'
|
|
|
|
local Common = require 'Common'
|
2021-07-16 23:25:44 -04:00
|
|
|
local Geometry = require 'Geometry'
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
return function(update_freq)
|
|
|
|
local PLOT_SEC_BREAK = 20
|
|
|
|
local PLOT_HEIGHT = 56
|
2021-07-18 00:38:43 -04:00
|
|
|
local DEVICES = {'sda', 'nvme0n1'}
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-18 00:38:43 -04:00
|
|
|
-- the sector size of any block device in linux is 512 bytes
|
|
|
|
-- see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/types.h?id=v4.4-rc6#n121
|
|
|
|
local BLOCK_SIZE_BYTES = 512
|
|
|
|
|
|
|
|
-- fields 3 and 7 (sectors read and written)
|
|
|
|
local RW_REGEX = '%s+%d+%s+%d+%s+(%d+)%s+%d+%s+%d+%s+%d+%s+(%d+)'
|
|
|
|
|
|
|
|
local __tonumber = tonumber
|
2021-07-18 00:03:44 -04:00
|
|
|
local __string_match = string.match
|
|
|
|
local __math_floor = math.floor
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- header
|
2021-07-11 20:26:02 -04:00
|
|
|
|
2021-07-17 16:43:00 -04:00
|
|
|
local header = Common.Header(
|
|
|
|
Geometry.CENTER_LEFT_X,
|
|
|
|
Geometry.TOP_Y,
|
|
|
|
Geometry.SECTION_WIDTH,
|
|
|
|
'INPUT / OUTPUT'
|
|
|
|
)
|
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- reads
|
2021-07-05 23:27:43 -04:00
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
local io_label_function = function(bytes)
|
|
|
|
local new_unit, new_value = Util.convert_data_val(bytes)
|
|
|
|
return __math_floor(new_value)..' '..new_unit..'B/s'
|
|
|
|
end
|
|
|
|
|
|
|
|
local format_value_function = function(bps)
|
|
|
|
local unit, value = Util.convert_data_val(bps)
|
|
|
|
return Util.precision_round_to_string(value, 3)..' '..unit..'B/s'
|
|
|
|
end
|
|
|
|
|
|
|
|
local build_plot = function(y, label)
|
|
|
|
return Common.initLabeledScalePlot(
|
|
|
|
Geometry.CENTER_LEFT_X,
|
|
|
|
y,
|
|
|
|
Geometry.SECTION_WIDTH,
|
|
|
|
PLOT_HEIGHT,
|
|
|
|
format_value_function,
|
|
|
|
io_label_function,
|
|
|
|
PLOT_SEC_BREAK,
|
|
|
|
label,
|
|
|
|
2,
|
|
|
|
update_freq
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
local reads = build_plot(header.bottom_y, 'Reads')
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- writes
|
|
|
|
|
|
|
|
local writes = build_plot(
|
|
|
|
header.bottom_y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2,
|
|
|
|
'Writes'
|
2021-07-17 16:43:00 -04:00
|
|
|
)
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- update function
|
|
|
|
|
2021-07-18 00:38:43 -04:00
|
|
|
local DEVICE_PATHS = {}
|
|
|
|
for i = 1, #DEVICES do
|
|
|
|
DEVICE_PATHS[i] = string.format('/sys/block/%s/stat', DEVICES[i])
|
|
|
|
end
|
2021-07-18 00:03:44 -04:00
|
|
|
|
2021-07-18 00:38:43 -04:00
|
|
|
local read_devices = function()
|
|
|
|
local read_bytes = 0
|
|
|
|
local write_bytes = 0
|
|
|
|
for _, path in pairs(DEVICE_PATHS) do
|
|
|
|
local r, w = __string_match(Util.read_file(path), RW_REGEX)
|
|
|
|
read_bytes = read_bytes + __tonumber(r)
|
|
|
|
write_bytes = write_bytes + __tonumber(w)
|
|
|
|
end
|
|
|
|
return read_bytes * BLOCK_SIZE_BYTES, write_bytes * BLOCK_SIZE_BYTES
|
2021-07-18 00:03:44 -04:00
|
|
|
end
|
|
|
|
|
2021-07-18 01:31:51 -04:00
|
|
|
local prev_read_bytes, prev_write_bytes = read_devices()
|
|
|
|
|
|
|
|
local compute_rate = function(x0, x1)
|
|
|
|
-- mask overflow
|
|
|
|
if x1 > x0 then
|
|
|
|
return (x1 - x0) * update_freq
|
|
|
|
else
|
|
|
|
return 0
|
2021-07-17 00:17:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local update = function(cr)
|
2021-07-18 01:31:51 -04:00
|
|
|
local read_bytes, write_bytes = read_devices()
|
|
|
|
Common.annotated_scale_plot_set(
|
|
|
|
reads,
|
|
|
|
cr,
|
|
|
|
compute_rate(prev_read_bytes, read_bytes)
|
|
|
|
)
|
|
|
|
Common.annotated_scale_plot_set(
|
|
|
|
writes,
|
|
|
|
cr,
|
|
|
|
compute_rate(prev_write_bytes, write_bytes)
|
|
|
|
)
|
|
|
|
prev_read_bytes = read_bytes
|
|
|
|
prev_write_bytes = write_bytes
|
2021-07-17 00:17:22 -04:00
|
|
|
end
|
|
|
|
|
2021-07-18 00:03:44 -04:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- main drawing functions
|
|
|
|
|
2021-07-17 16:43:00 -04:00
|
|
|
local draw_static = function(cr)
|
|
|
|
Common.drawHeader(cr, header)
|
|
|
|
Common.annotated_scale_plot_draw_static(reads, cr)
|
|
|
|
Common.annotated_scale_plot_draw_static(writes, cr)
|
|
|
|
end
|
|
|
|
|
2021-07-17 00:17:22 -04:00
|
|
|
local draw_dynamic = function(cr)
|
|
|
|
update(cr)
|
|
|
|
Common.annotated_scale_plot_draw_dynamic(reads, cr)
|
|
|
|
Common.annotated_scale_plot_draw_dynamic(writes, cr)
|
|
|
|
end
|
|
|
|
|
|
|
|
return {static = draw_static, dynamic = draw_dynamic}
|
2017-07-19 00:36:15 -04:00
|
|
|
end
|