koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import time |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 4 | import pprint |
koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 5 | import threading |
| 6 | |
| 7 | |
| 8 | mapping = [ |
| 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 | |
| 26 | def 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 | |
| 35 | def diff_stats(obj1, obj2): |
| 36 | return {key: (val - obj2[key]) for key, val in obj1.items()} |
| 37 | |
| 38 | |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 39 | def 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 kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 44 | |
| 45 | devices = sys.argv[1].split(',') |
| 46 | cmd = sys.argv[2:] |
| 47 | |
| 48 | th = threading.Thread(None, run_tool, None, (cmd,)) |
| 49 | th.daemon = True |
| 50 | |
| 51 | rstats = read_dstats() |
| 52 | prev_stats = {device: rstats[device] for device in devices} |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 53 | begin_stats = prev_stats |
koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 54 | |
| 55 | th.start() |
| 56 | |
| 57 | wr_compl = "writes complete" |
| 58 | |
| 59 | while True: |
| 60 | time.sleep(1) |
| 61 | |
| 62 | rstats = read_dstats() |
| 63 | new_stats = {device: rstats[device] for device in devices} |
| 64 | |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 65 | # print "Delta writes complete =", |
koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 66 | for device in devices: |
| 67 | delta = new_stats[device][wr_compl] - prev_stats[device][wr_compl] |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 68 | # print device, delta, |
| 69 | # print |
koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 70 | |
| 71 | prev_stats = new_stats |
| 72 | |
| 73 | if not th.is_alive(): |
| 74 | break |
| 75 | |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 76 | pprint.pprint(diff_stats(new_stats[device], begin_stats[device])) |