def get_infos(): """Get all information about all your graphics cards. Returns: dict: The returned result is a dict with 3 keys: count, driver_version and devices: count: Number of gpus found driver_version: The version of the system’s graphics driver devices: It's a list and every item is a namedtuple Device which has 10 fields, for exzample id, name and fan_speed etc. It should be noted that the Process field is also a namedtuple which has 11 fields. """ infos = {} Device = namedtuple( "Device", [ "id", "name", "free", "used", "total", "temperature", "fan_speed", "power_usage", "power_state", "process", ], ) Process = namedtuple( "Process", [ "pid", "memory_percent", "status", "username", "num_threads", "cpu_num", "cpu_percent", "name", "cmdline", "used_gpu_mem", "create_time", ], ) driver_version = pynvml.nvmlSystemGetDriverVersion().decode() device_count = pynvml.nvmlDeviceGetCount() devices = [] for i in range(device_count): handle = pynvml.nvmlDeviceGetHandleByIndex(i) name = pynvml.nvmlDeviceGetName(handle).decode() mem_info = pynvml.nvmlDeviceGetMemoryInfo(handle) power_usage = pynvml.nvmlDeviceGetPowerUsage( handle) # Power usage in milliwatts mW processes = pynvml.nvmlDeviceGetComputeRunningProcesses( handle) # Which processes are using the GPU # process_info = [(item.pid, item.usedGpuMemory) for item in process_info] process_info = [] for p in processes: # append Process object to process_info pid = p.pid used_gpu_mem = p.usedGpuMemory p = psutil.Process(pid=pid) _ = p.cpu_percent() time.sleep(0.05) process_info.append( Process( pid=pid, memory_percent=p.memory_percent(), status=p.status(), username=p.username(), num_threads=p.num_threads(), cpu_num=p.cpu_num(), cpu_percent=p.cpu_percent(), name=p.name(), cmdline=" ".join(p.cmdline()), used_gpu_mem=used_gpu_mem, create_time=p.create_time(), )) try: fan_speed = pynvml.nvmlDeviceGetFanSpeed(handle) except pynvml.NVMLError_NotSupported as e: fan_speed = None power_usage = pynvml.nvmlDeviceGetPowerUsage(handle) power_state = pynvml.nvmlDeviceGetPowerState(handle) temperature = pynvml.nvmlDeviceGetTemperature( handle, pynvml.NVML_TEMPERATURE_GPU) devices.append( Device( id=i, name=name, free=mem_info.free, used=mem_info.used, total=mem_info.total, temperature=temperature, fan_speed=fan_speed, power_usage=power_usage, power_state=power_state, process=process_info, )) infos["count"] = device_count infos["driver_version"] = driver_version infos["devices"] = devices return infos
def pwr_state(self): """Get the Device current Power State""" return pynvml.nvmlDeviceGetPowerState(self.dev)
print(' 共 %s 块 GPU,名称为:'%deviceCount) for i in range(deviceCount): handle = pynvml.nvmlDeviceGetHandleByIndex(i) print(" GPU", i, ":", pynvml.nvmlDeviceGetName(handle)) print('--------------') for i in range(deviceCount): print('查看第 %s 块GPU的显存、温度、风扇、电源: '%i) handle = pynvml.nvmlDeviceGetHandleByIndex(i) info = pynvml.nvmlDeviceGetMemoryInfo(handle) print("Memory Total: %0.2f G"%(info.total/1024/1024/1024)) # 总的显存大小 print("Memory Free: %0.2f G "%(info.free/1024/1024/1024)) # 剩余显存大小 print("Memory Used: %0.2f G "%(info.used/1024/1024/1024)) print("Memory Used percent: %0.2f %% "%(info.used/info.total*100)) print("Temperature is %d C"%(pynvml.nvmlDeviceGetTemperature(handle,0))) print("Fan speed is ",pynvml.nvmlDeviceGetFanSpeed(handle)) print("Power ststus",pynvml.nvmlDeviceGetPowerState(handle)) print('--------------') #最后要关闭管理工具 pynvml.nvmlShutdown() #%% ----------------- os ---------------------- os.listdir(r'c:\windows') os.getcwd() # 当前工作目录 os.chdir('C:\Users\Python_Folder') # 改变工作目录到dirname os.curdir # 返回当前工作目录 os.__file__ # D:\envs\py27\lib\os.pyc os.rename("python26","python21") shutil.move("python21","python20") os.path.dirname(os.__file__) # 获取路径名 D:\software\Anaconda3\envs\py27\lib
memory_max = 0 temprature_max = 0 GPU_load_max = 0 while(1): info = psutil.virtual_memory() memory_max = max(memory_max, info.percent) print(u'内存占比:%4.1f/%4.1f' % (info.percent, memory_max), end=" ") meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle) print("GPU memory: %dMB/%dMB" % (meminfo.used / pow(1024, 2), meminfo.total / pow(1024, 2)), end=" ") temprature = pynvml.nvmlDeviceGetTemperature(handle, 0) temprature_max = max(temprature_max, temprature) print("temprature: %d℃/%d℃" % (temprature, temprature_max), end=" ") PowerState = pynvml.nvmlDeviceGetPowerState(handle) print("PowerState: %d" % (PowerState), end=" ") PowerUsage = pynvml.nvmlDeviceGetPowerUsage(handle) PowerManagementDefaultLimit = pynvml.nvmlDeviceGetPowerManagementDefaultLimit(handle) print("PowerUsage: %d/%d" % (PowerUsage, PowerManagementDefaultLimit), end=" ") GPU_load = PowerUsage / PowerManagementDefaultLimit * 100 GPU_load_max = max(GPU_load_max, GPU_load) print("Power load: %4.1f/%4.1f%%" % (GPU_load, GPU_load_max)) time.sleep(1) if 0: print(u'cpu个数:',psutil.cpu_count()) print(u'内存使用:', psutil.Process(os.getpid()).memory_info().rss)
except np.NVMLError, err: temp = 'NA' try: powMan = pn.nvmlDeviceGetPowerManagementMode(handle) #获取设备当前的电源管理模式 except pn.NVMLError, err: powMan = 'NA' try: graphics_clock = pn.nvmlDeviceGetClockInfo(handle, pn.NVML_CLOCK_GRAPHICS) #检索设备的当前时钟速度 except pn.NVMLError, err: graphics_clock = 'NA' try: mem_clock = pn.nvmlDeviceGetClockInfo(handle, pn.NVML_CLOCK_MEM) except np.NVMLError, err: mem_clock = 'NA' try: perf_stat = pn.nvmlDeviceGetPowerState(handle) #检索设备的当前性能状 except np.NVMLError, err: perf_stat = 'NA' tmp_dict['Gpu_Id'] = gpu_id tmp_dict['Product_Name'] = product_name tmp_dict['Mode'] = mode tmp_dict['Current_Driver_Model'] = Current_driver_model tmp_dict['Total_Memory'] = mem_total tmp_dict['Used_Memory'] = mem_used tmp_dict['Free_Memory'] = mem_free tmp_dict['GPU_Util'] = gpu_util+'%' tmp_dict['Memory_Util'] = mem_util+'%' tmp_dict['Temperature'] = str(temp)+'C' tmp_dict['Graphics_Clock'] = graphics_clock tmp_dict['Memory_Clock'] = mem_clock tmp_dict['Perf_State'] = perf_stat
def _get_device_power_state(gpu): power_state = "P{}".format(pynvml.nvmlDeviceGetPowerState(gpu)) return {'power_state': power_state}