Created
April 29, 2021 22:39
-
-
Save lpereira/9a102e57fc4dcff3cde931e4aadb35fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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/python | |
import sys | |
import time | |
def is_sleeping(pid): | |
with open("/proc/%d/stat" % pid) as stat: | |
state = stat.read().strip().split() | |
return state[2] == 'S' | |
def io_stats(pid): | |
with open("/proc/%d/io" % pid) as io: | |
stats = (stat.split(": ") for stat in io.readlines()) | |
return {stat[0]: int(stat[1]) for stat in stats} | |
if __name__ == '__main__': | |
pid = int(sys.argv[1]) | |
while True: | |
stats = io_stats(pid) | |
last_diff = stats['read_bytes'] - stats['rchar'] | |
while is_sleeping(pid): | |
stats = io_stats(pid) | |
cur_diff = stats['read_bytes'] - stats['rchar'] | |
if cur_diff == last_diff: | |
print("Process seems to be blocked reading the terminal!") | |
else: | |
print("Process is blocked doing something else") | |
last_diff = cur_diff | |
time.sleep(1) | |
print('Process is not sleeping anymore') | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment