koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 1 | from discover import provides |
| 2 | from utils import SensorInfo, is_dev_accepted |
| 3 | |
| 4 | # 1 - major number |
| 5 | # 2 - minor mumber |
| 6 | # 3 - device name |
| 7 | # 4 - reads completed successfully |
| 8 | # 5 - reads merged |
| 9 | # 6 - sectors read |
| 10 | # 7 - time spent reading (ms) |
| 11 | # 8 - writes completed |
| 12 | # 9 - writes merged |
| 13 | # 10 - sectors written |
| 14 | # 11 - time spent writing (ms) |
| 15 | # 12 - I/Os currently in progress |
| 16 | # 13 - time spent doing I/Os (ms) |
| 17 | # 14 - weighted time spent doing I/Os (ms) |
| 18 | |
| 19 | io_values_pos = [ |
| 20 | (3, 'reads_completed', True), |
| 21 | (5, 'sectors_read', True), |
| 22 | (6, 'rtime', True), |
| 23 | (7, 'writes_completed', True), |
| 24 | (9, 'sectors_written', True), |
| 25 | (10, 'wtime', True), |
| 26 | (11, 'io_queue', False), |
| 27 | (13, 'io_time', True) |
| 28 | ] |
| 29 | |
| 30 | |
| 31 | @provides("block-io") |
| 32 | def io_stat(disallowed_prefixes=('ram', 'loop'), allowed_prefixes=None): |
| 33 | results = {} |
| 34 | for line in open('/proc/diskstats'): |
| 35 | vals = line.split() |
| 36 | dev_name = vals[2] |
| 37 | |
| 38 | dev_ok = is_dev_accepted(dev_name, |
| 39 | disallowed_prefixes, |
| 40 | allowed_prefixes) |
| 41 | |
| 42 | if dev_ok: |
| 43 | for pos, name, accum_val in io_values_pos: |
| 44 | sensor_name = "{0}.{1}".format(dev_name, name) |
| 45 | results[sensor_name] = SensorInfo(int(vals[pos]), accum_val) |
| 46 | return results |
Dmitry Yatsushkevich | 1b31f76 | 2015-03-16 13:53:58 -0700 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def get_latency(stat1, stat2): |
koder aka kdanilov | bf83ad9 | 2015-03-18 00:11:00 +0200 | [diff] [blame] | 50 | disks = set(i.split('.', 1)[0] for i in stat1) |
Dmitry Yatsushkevich | 1b31f76 | 2015-03-16 13:53:58 -0700 | [diff] [blame] | 51 | results = {} |
koder aka kdanilov | bf83ad9 | 2015-03-18 00:11:00 +0200 | [diff] [blame] | 52 | |
Dmitry Yatsushkevich | 1b31f76 | 2015-03-16 13:53:58 -0700 | [diff] [blame] | 53 | for disk in disks: |
koder aka kdanilov | bf83ad9 | 2015-03-18 00:11:00 +0200 | [diff] [blame] | 54 | rdc = disk + '.reads_completed' |
| 55 | wrc = disk + '.writes_completed' |
| 56 | rdt = disk + '.rtime' |
| 57 | wrt = disk + '.wtime' |
| 58 | lat = 0.0 |
| 59 | |
| 60 | io_ops1 = stat1[rdc].value + stat1[wrc].value |
| 61 | io_ops2 = stat2[rdc].value + stat2[wrc].value |
| 62 | |
| 63 | diops = io_ops2 - io_ops1 |
| 64 | |
| 65 | if diops != 0: |
| 66 | io1 = stat1[rdt].value + stat1[wrt].value |
| 67 | io2 = stat2[rdt].value + stat2[wrt].value |
| 68 | lat = abs(float(io1 - io2)) / diops |
| 69 | |
| 70 | results[disk + '.latence'] = SensorInfo(lat, False) |
Dmitry Yatsushkevich | 1b31f76 | 2015-03-16 13:53:58 -0700 | [diff] [blame] | 71 | |
| 72 | return results |