blob: 634b20115f1fd61ecc206ad979134748bd59dd89 [file] [log] [blame]
koder aka kdanilove4ade1a2015-03-16 20:44:16 +02001import os
2import sys
3import time
4# import pprint
5import 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
39def run_tool(cmd, suppress_console=True):
40 os.system(" ".join(cmd) + " >/dev/null 2>&1 ")
41
42devices = sys.argv[1].split(',')
43cmd = sys.argv[2:]
44
45th = threading.Thread(None, run_tool, None, (cmd,))
46th.daemon = True
47
48rstats = read_dstats()
49prev_stats = {device: rstats[device] for device in devices}
50
51th.start()
52
53wr_compl = "writes complete"
54
55while True:
56 time.sleep(1)
57
58 rstats = read_dstats()
59 new_stats = {device: rstats[device] for device in devices}
60
61 print "Delta writes complete =",
62 for device in devices:
63 delta = new_stats[device][wr_compl] - prev_stats[device][wr_compl]
64 print device, delta,
65 print
66
67 prev_stats = new_stats
68
69 if not th.is_alive():
70 break
71
72# pprint.pprint(diff_stats(stat2, stat1))