koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import time |
| 4 | # import pprint |
| 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 | |
| 39 | def run_tool(cmd, suppress_console=True): |
| 40 | os.system(" ".join(cmd) + " >/dev/null 2>&1 ") |
| 41 | |
| 42 | devices = sys.argv[1].split(',') |
| 43 | cmd = sys.argv[2:] |
| 44 | |
| 45 | th = threading.Thread(None, run_tool, None, (cmd,)) |
| 46 | th.daemon = True |
| 47 | |
| 48 | rstats = read_dstats() |
| 49 | prev_stats = {device: rstats[device] for device in devices} |
| 50 | |
| 51 | th.start() |
| 52 | |
| 53 | wr_compl = "writes complete" |
| 54 | |
| 55 | while 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)) |