Skip to content

Instantly share code, notes, and snippets.

@goddoe
Created March 9, 2020 13:41
Show Gist options
  • Save goddoe/c5a27026ad405386c46b4aafef3466bc to your computer and use it in GitHub Desktop.
Save goddoe/c5a27026ad405386c46b4aafef3466bc to your computer and use it in GitHub Desktop.
resource check
import argparse
from torch.utils.tensorboard import SummaryWriter
import psutil
import time
def bytesto(bytes, to, bsize=1024):
"""convert bytes to megabytes, etc.
sample code:
print('mb= ' + str(bytesto(314575262000000, 'm')))
sample output:
mb= 300002347.946
"""
a = {'k' : 1, 'm': 2, 'g' : 3, 't' : 4, 'p' : 5, 'e' : 6 }
r = float(bytes)
for i in range(a[to]):
r = r / bsize
return(r)
parser = argparse.ArgumentParser()
parser.add_argument('--pid', type=int, help='pid of target program')
parser.add_argument('--tb-path', type=str, help='tensorboard path')
args = parser.parse_args()
process = psutil.Process(args.pid)
writer = SummaryWriter(args.tb_path)
max_cpu_percent = 0
max_ram_memory = 0
i = 0
while True:
curr_cpu_percent = process.cpu_percent()
curr_ram_memory = round(bytesto(process.memory_info().rss, 'g'), 4)
if curr_cpu_percent > max_cpu_percent:
max_cpu_percent = curr_cpu_percent
if curr_ram_memory > max_ram_memory:
max_ram_memory = curr_ram_memory
print(f"max cpu percent: {max_cpu_percent}")
print(f"max ram memory: {max_ram_memory}")
writer.add_scalar("cpu", curr_cpu_percent, i)
writer.add_scalar("memory", curr_ram_memory, i)
i += 1
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment