Ubuntu は Python コードを使用して GPU と CPU の使用率と温度を監視します

Ubuntu は Python コードを使用して GPU と CPU の使用率と温度を監視します

質問:

プロジェクト内で起動できないプログラムがあることが判明し、確認したところnvidia-smiコマンドを入力してもNVIDIAグラフィックスカードのドライバー情報が出力されないことが判明しました。

解決策 1:

再起動後、再度 nvidia-smi -l 1 を入力し、通常どおり印刷します。

解決策 2:

原因としては、Ubuntu カーネルのアップグレードにより NVIDIA ドライバーが接続できなくなることが考えられます。次の方法によるカーネルの自動更新を回避するには、https://zhuanlan.zhihu.com/p/611276995?utm_id=0 を参照してください。

次の 2 つのコマンドを渡し、その中のパラメータ値を 0 に変更します

vi /etc/apt/apt.conf.d/10periodic
vi /etc/apt/apt.conf.d/20auto-apgrades

他の、

理由を推測してください:

放熱の問題が原因でドライバーが頻繁にクラッシュする可能性があります。

そこで、GPU と CPU の使用状況と温度を 5 分ごとに txt ファイルに保存する Python コードを作成しました。ソースコードは次のとおりです。

涉及需要安装的python库
# pip3 install nvidia-ml-py3 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
# pip3 install psutil
def print_gpu_info():
    # ref: https://blog.csdn.net/zxc120389574/article/details/106220612
    # 获取GPU的用量和温度
    # 这里假设使用的是NVIDIA显卡,需要安装nvidia-ml-py库
    

    print("---------GPU usage and T---------")
    import pynvml
    pynvml.nvmlInit()
    # Use device index to get gpu_device
    gpu_device = pynvml.nvmlDeviceGetHandleByIndex(0)

    # Use gpu_device to get device stats
    memory_info = pynvml.nvmlDeviceGetMemoryInfo(gpu_device)
    memoInfo = "Memory: Used/Total = {}/{}".format(memory_info.used / 1024 / 1024, memory_info.total / 1024 / 1024)

    powerUsage = pynvml.nvmlDeviceGetPowerUsage(gpu_device)
    powerState = pynvml.nvmlDeviceGetPowerState(gpu_device)

    # get GPU temperature
    gpu_device = pynvml.nvmlDeviceGetHandleByIndex(0)
    temperature = pynvml.nvmlDeviceGetTemperature(gpu_device, pynvml.NVML_TEMPERATURE_GPU)
    tempInfo = "Temperature:{0}°C".format(temperature)

    gpu_info ='gpu: ' + memoInfo + '  ' + tempInfo + '  '
    return gpu_info


def print_cpu_info():
    # 获取CPU的用量和温度 ()
    print("---------CPU usage and T---------")
    import psutil
    cpu_percent = psutil.cpu_percent()
    # psutil.sensors_temperatures() 官方文档这有说的只适用于 linux
    # 此程序在ubuntu上验证pass,
    cpu_temperature = psutil.sensors_temperatures()['coretemp'][0].current
    memoInfo = f"CPU utilization: {cpu_percent}%"
    tempInfo = f"CPU temperature: {cpu_temperature}°C"

    cpu_info = '    ||||    cpu: ' + memoInfo + ' ' + tempInfo + ' '
    return cpu_info
    
    
def write2Info(info):
    from datetime import datetime
    t = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    print('info : ', info)
    info = t + '   '+ info + '\n'
    txtName = './GPUCPU_usage{}.txt'.format(1)
    with open(txtName, 'a', encoding='utf-8') as wf:
        wf.write(info)
        
        
if __name__ == '__main__':
    import time

    while True:
        gpu_info = print_gpu_info()
        cpu_info = print_cpu_info()  
        info = gpu_info + cpu_info
        write2Info(info)
        time.sleep(2)

または(上端と下端のコードは同じかもしれませんが、以下のコードはテスト実行で問題ありません)



import psutil
import time

def print_gpu_info():
    #print("---------GPU usage and T---------")
    import pynvml
    pynvml.nvmlInit()
    # Use device index to get gpu_device
    gpu_device = pynvml.nvmlDeviceGetHandleByIndex(0)

    # Use gpu_device to get device stats
    memory_info = pynvml.nvmlDeviceGetMemoryInfo(gpu_device)
    memoInfo = "Memory: Used/Total = {}/{}".format(memory_info.used / 1024 / 1024, memory_info.total / 1024 / 1024)
    
    
    gpu_device = pynvml.nvmlDeviceGetHandleByIndex(0)
    temperature = pynvml.nvmlDeviceGetTemperature(gpu_device, pynvml.NVML_TEMPERATURE_GPU)
    tempInfo = "Temperature:{0}°C".format(temperature)
    # print(tempInfo)

    gpu_info ='gpu: ' + memoInfo + '  ' + tempInfo + '  '
    return gpu_info
    


def print_cpu_info():
    #print("---------CPU usage and T---------")
    import psutil
    cpu_percent = psutil.cpu_percent()
    # psutil.sensors_temperatures() 官方文档这有说的只适用于 linux
    # 此程序在ubuntu上验证pass,
    cpu_temperature = psutil.sensors_temperatures()['coretemp'][0].current
    memoInfo = f"CPU utilization: {cpu_percent}%"
    # print(memoInfo)
    tempInfo = f"CPU temperature: {cpu_temperature}°C"
    # print(tempInfo)

    cpu_info = '    ||||    cpu: ' + memoInfo + ' ' + tempInfo + ' '
    return cpu_info


def write2Info(info):
    from datetime import datetime
    t = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    print('info : ', info)
    info = t + '   '+ info + '\n'
    txtName = './GPUCPU_usage{}.txt'.format(1)
    with open(txtName, 'a', encoding='utf-8') as wf:
        wf.write(info)


if __name__ == '__main__':
    import time

    while True:
        gpu_info = print_gpu_info()
        cpu_info = print_cpu_info()
        info = gpu_info + cpu_info
        write2Info(info)
        time.sleep(300)




接続する

链接:https://pan.baidu.com/s/1ZoPp9mSeydqNuRobkChWsw 

おすすめ

転載: blog.csdn.net/qq_42835363/article/details/132103860