技术:
import os
import time
import psutil
import pynvml
from multiprocessing import Process, Manager
# MiB
def gpu_statistic(gpus_per):
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
while (True):
used_gpu_mem = pynvml.nvmlDeviceGetMemoryInfo(handle).used
gpus_per.append(used_gpu_mem * 1.0 / 1024 / 1024)
time.sleep(0.5)
# MiB
def mem_statistic(mems_per, pid):
p = psutil.Process(pid)
while (True):
mems_per.append(p.memory_info().rss * 1.0 / 1024 / 1024)
time.sleep(0.5)
# %
def cpu_statistic(cpus_per, pid):
p = psutil.Process(pid)
while (True):
cpus_per.append(p.cpu_times().user)
time.sleep(0.5)
if __name__ == '__main__':
gpus_all = []
mems_all = []
cpus_all = []
manager = Manager()
x = 1
while(x < 5):
x += 1
gpus_per = manager.list()
mems_per = manager.list()
cpus_per = manager.list()
pid = os.getpid()
gpu_process = Process(target = gpu_statistic, args = (gpus_per,))
mem_process = Process(target = mem_statistic, args = (mems_per, pid))
cpu_process = Process(target = cpu_statistic, args = (cpus_per, pid))
gpu_process.start()
mem_process.start()
cpu_process.start()
time.sleep(2)
gpus_all.append(gpus_per[:])
mems_all.append(mems_per[:])
cpus_all.append(cpus_per[:])
gpu_process.terminate()
mem_process.terminate()
cpu_process.terminate()
print gpus_all
print mems_all
print cpus_all