持续获取io

This commit is contained in:
cppla 2022-04-01 15:37:43 +08:00
parent 5176cb0340
commit 47cf7a1818

@ -222,56 +222,57 @@ def _disk_io():
磁盘读写有误差4k8k https://stackoverflow.com/questions/34413926/psutil-vs-dd-monitoring-disk-i-o 磁盘读写有误差4k8k https://stackoverflow.com/questions/34413926/psutil-vs-dd-monitoring-disk-i-o
:return: :return:
''' '''
# pre pid snapshot while True:
snapshot_first = {} # pre pid snapshot
# next pid snapshot snapshot_first = {}
snapshot_second = {} # next pid snapshot
# read count snapshot snapshot_second = {}
snapshot_read = 0 # read count snapshot
# write count snapshot snapshot_read = 0
snapshot_write = 0 # write count snapshot
# process snapshot snapshot_write = 0
pid_snapshot = [str(i) for i in os.listdir("/proc") if i.isdigit() is True] # process snapshot
for pid in pid_snapshot: pid_snapshot = [str(i) for i in os.listdir("/proc") if i.isdigit() is True]
try: for pid in pid_snapshot:
with open("/proc/{}/io".format(pid)) as f: try:
pid_io = {} with open("/proc/{}/io".format(pid)) as f:
for line in f.readlines(): pid_io = {}
if "read_bytes" in line: for line in f.readlines():
pid_io["read"] = int(line.split("read_bytes:")[-1].strip()) if "read_bytes" in line:
elif "write_bytes" in line and "cancelled_write_bytes" not in line: pid_io["read"] = int(line.split("read_bytes:")[-1].strip())
pid_io["write"] = int(line.split("write_bytes:")[-1].strip()) elif "write_bytes" in line and "cancelled_write_bytes" not in line:
pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip() pid_io["write"] = int(line.split("write_bytes:")[-1].strip())
snapshot_first[pid] = pid_io pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip()
except: snapshot_first[pid] = pid_io
if pid in snapshot_first: except:
snapshot_first.pop(pid) if pid in snapshot_first:
snapshot_first.pop(pid)
time.sleep(INTERVAL) time.sleep(INTERVAL)
for pid in pid_snapshot: for pid in pid_snapshot:
try: try:
with open("/proc/{}/io".format(pid)) as f: with open("/proc/{}/io".format(pid)) as f:
pid_io = {} pid_io = {}
for line in f.readlines(): for line in f.readlines():
if "read_bytes" in line: if "read_bytes" in line:
pid_io["read"] = int(line.split("read_bytes:")[-1].strip()) pid_io["read"] = int(line.split("read_bytes:")[-1].strip())
elif "write_bytes" in line and "cancelled_write_bytes" not in line: elif "write_bytes" in line and "cancelled_write_bytes" not in line:
pid_io["write"] = int(line.split("write_bytes:")[-1].strip()) pid_io["write"] = int(line.split("write_bytes:")[-1].strip())
pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip() pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip()
snapshot_second[pid] = pid_io snapshot_second[pid] = pid_io
except: except:
if pid in snapshot_first: if pid in snapshot_first:
snapshot_first.pop(pid) snapshot_first.pop(pid)
if pid in snapshot_second: if pid in snapshot_second:
snapshot_second.pop(pid) snapshot_second.pop(pid)
for k, v in snapshot_first.items(): for k, v in snapshot_first.items():
if snapshot_first[k]["name"] == snapshot_second[k]["name"] and snapshot_first[k]["name"] != "bash": if snapshot_first[k]["name"] == snapshot_second[k]["name"] and snapshot_first[k]["name"] != "bash":
snapshot_read += (snapshot_second[k]["read"] - snapshot_first[k]["read"]) snapshot_read += (snapshot_second[k]["read"] - snapshot_first[k]["read"])
snapshot_write += (snapshot_second[k]["write"] - snapshot_first[k]["write"]) snapshot_write += (snapshot_second[k]["write"] - snapshot_first[k]["write"])
diskIO["read"] = snapshot_read diskIO["read"] = snapshot_read
diskIO["write"] = snapshot_write diskIO["write"] = snapshot_write
def get_realtime_data(): def get_realtime_data():
''' '''