Created
April 24, 2016 12:46
-
-
Save andresmrm/7a58b5efe9d7a008d974ad4604323dc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
---------------------------------------------------------------------- | |
Copyright (C) 2016 Andrés M. R. Martano | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
---------------------------------------------------------------------- | |
Iotop displays accumulated writes to disks by process, but I couldn't | |
configure it to solve two problems: | |
- It stops displaying process that terminated. | |
- It doesn't show to which HD writes are going to. | |
So I made this small dirty script to try to solve this points. | |
It displays process even after they terminated, and keeps accumulating | |
values if they are restarted. | |
It can't tell to which partition they wrote to, but it also displays | |
total writes to a set partition (see bellow) so you can try to guess | |
what's going on (at least if you only have 2 relevant partitions like | |
me =P). | |
Run this script like this: | |
iotop -boqqq | <path to this script> | |
(iotop requires root) | |
Values are in MBs. | |
The "Writes Iotop" are the total writes accumulated from Iotop data. | |
The "Writes SysFs" are the total changes to the set partition | |
"lifetime_write_kbytes" file since the script started running. | |
**YOU NEED TO EDIT THE "PATH_TO_HD_FILE" VAR BELLOW.** | |
---------------------------------------------------------------------- | |
''' | |
import sys | |
sizes = { | |
'B': 1, | |
'K': 1e3, | |
'M': 1e6, | |
'G': 1e9, | |
} | |
uses = {} | |
# Change the line bellow to the correct path to the partition | |
# you want to monitor: | |
PATH_TO_HD_FILE = '/sys/fs/ext4/<partition>/lifetime_write_kbytes' | |
def get_hd_total_write(): | |
arq = open(PATH_TO_HD_FILE, 'r') | |
total = arq.read() | |
arq.close() | |
return int(total.strip()) | |
init_writes = get_hd_total_write() | |
def print_all(): | |
in_order = sorted(uses.items(), key=lambda x: x[1]['write'], reverse=False) | |
total_w = 0 | |
total_r = 0 | |
for name, entry in in_order: | |
total_w += entry['write'] | |
total_r += entry['read'] | |
print(name) | |
w = str(entry['write']/1e6).rjust(15) | |
r = str(entry['read']/1e6).rjust(15) | |
print(w, r) | |
os_write_diff = get_hd_total_write() - init_writes | |
print('----------------') | |
print('Writes Iotop: ', total_w/1e6) | |
print('Writes SysFs: ', os_write_diff/1e3) | |
print('Diff Iotop/Sysfs: ', round((os_write_diff-total_w/1e3)/1e3, 3)) | |
print('Reads: ', total_r/1e6) | |
print('----------------') | |
for line in sys.stdin: | |
try: | |
a, _, b = line.partition('%') | |
b, _, name = b.partition('%') | |
a = a + b | |
id_, prio, user, read, read_size, write, write_size, swapin, io_, = a.split() | |
name = name.strip() | |
except: | |
print(line) | |
print(a) | |
print(name) | |
raise | |
read = float(read) * sizes[read_size[0]] | |
write = float(write) * sizes[write_size[0]] | |
entry = uses.get(name) | |
if not entry: | |
entry = { | |
'write': 0, | |
'read': 0, | |
} | |
uses[name] = entry | |
entry['write'] += write | |
entry['read'] += read | |
print_all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment