-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_gpu.py
75 lines (60 loc) · 1.84 KB
/
watch_gpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import GPUtil
import os
import time
import psutil
stopped_num = 10000000 # (设置一个最大获取次数,防止记录文本爆炸)
delay = 10 # 采样信息时间间隔
Gpus = GPUtil.getGPUs()
def get_gpu_info():
'''
:return:
'''
gpulist = []
GPUtil.showUtilization()
# 获取多个GPU的信息,存在列表里
gpu = Gpus[4]
# for gpu in Gpus:
# print('gpu.id:', gpu.id)
# print('GPU总量:', gpu.memoryTotal)
# print('GPU使用量:', gpu.memoryUsed)
# print('gpu使用占比:', gpu.memoryUtil * 100)
# 按GPU逐个添加信息
# gpulist.append([gpu.id, gpu.memoryTotal, gpu.memoryUsed, gpu.memoryUtil * 100])
return gpu.memoryUtil
def get_cpu_info():
''' :return:
memtotal: 总内存
memfree: 空闲内存
memused: Linux: total - free,已使用内存
mempercent: 已使用内存占比
cpu: 各个CPU使用占比
'''
mem = psutil.virtual_memory()
memtotal = mem.total
memfree = mem.free
mempercent = mem.percent
memused = mem.used
cpu = psutil.cpu_percent(percpu=True)
return memtotal, memfree, memused, mempercent, cpu
# 主函数
def main():
times = 0
gpu_percentage = 1
while gpu_percentage>0.3:
# 最大循环次数
# 打印当前时间
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
# 获取CPU信息
# cpu_info = get_cpu_info()
# 获取GPU信息
# gpu_info = get_gpu_info()
gpu_percentage = get_gpu_info()
# print(gpu_percentage)
# 添加时间间隙
# print(cpu_info)
# print(gpu_info, '\n')
time.sleep(delay)
times += 1
os.system('./exec.sh')
if __name__ == '__main__':
main()