blob: 754e7a8c98f3e78106eff32607b5541e4f250981 [file] [log] [blame]
koder aka kdanilove4ade1a2015-03-16 20:44:16 +02001import os
2import sys
3import time
koder aka kdanilov8ad6e812015-03-22 14:42:18 +02004import pprint
koder aka kdanilove4ade1a2015-03-16 20:44:16 +02005import threading
6
7
8mapping = [
9 "major number",
10 "minor mumber",
11 "device name",
12 "reads completed successfully",
13 "reads merged",
14 "sectors read",
15 "time spent reading (ms)",
16 "writes complete",
17 "writes merged",
18 "sectors written",
19 "time spent writing (ms)",
20 "I/Os currently in progress",
21 "time spent doing I/Os (ms)",
22 "weighted time spent doing I/Os (ms)"
23]
24
25
26def read_dstats():
27 res = {}
28 for line in open("/proc/diskstats"):
29 stats = dict(zip(mapping, line.split()))
30 name = stats.pop('device name')
31 res[name] = {k: int(v) for k, v in stats.items()}
32 return res
33
34
35def diff_stats(obj1, obj2):
36 return {key: (val - obj2[key]) for key, val in obj1.items()}
37
38
koder aka kdanilov8ad6e812015-03-22 14:42:18 +020039def run_tool(cmd, suppress_console=False):
40 s_cmd = " ".join(cmd)
41 if suppress_console:
42 s_cmd += " >/dev/null 2>&1 "
43 os.system(s_cmd)
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020044
45devices = sys.argv[1].split(',')
46cmd = sys.argv[2:]
47
48th = threading.Thread(None, run_tool, None, (cmd,))
49th.daemon = True
50
51rstats = read_dstats()
52prev_stats = {device: rstats[device] for device in devices}
koder aka kdanilov8ad6e812015-03-22 14:42:18 +020053begin_stats = prev_stats
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020054
55th.start()
56
57wr_compl = "writes complete"
58
59while True:
60 time.sleep(1)
61
62 rstats = read_dstats()
63 new_stats = {device: rstats[device] for device in devices}
64
koder aka kdanilov8ad6e812015-03-22 14:42:18 +020065 # print "Delta writes complete =",
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020066 for device in devices:
67 delta = new_stats[device][wr_compl] - prev_stats[device][wr_compl]
koder aka kdanilov8ad6e812015-03-22 14:42:18 +020068 # print device, delta,
69 # print
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020070
71 prev_stats = new_stats
72
73 if not th.is_alive():
74 break
75
koder aka kdanilov8ad6e812015-03-22 14:42:18 +020076pprint.pprint(diff_stats(new_stats[device], begin_stats[device]))