Linux —— Ubuntu下C++获取CPU使用率、GPU使用率、GPU温度

本文介绍了如何在Ubuntu操作系统中,不通过代码方式获取CPU和GPU的使用率以及GPU的温度。提供了一种纯Linux C++实现的方法来监控这些系统指标。

非代码

     首先介绍在Ubuntu下不使用代码查看的方式来查看CPU使用率、GPU使用率、GPU温度。

查看CPU使用率:
	top
	
查看GPU使用率及温度:
	nvidia-smi

在这里插入图片描述

代码

     介绍在Ubuntu下不使用代码查看的方式来查看CPU使用率、GPU使用率、GPU温度。

     纯linux c++获取CPU使用率

typedef struct cpu_occupy_          //定义一个cpu occupy的结构体
{
    char name[20];                  //定义一个char类型的数组名name有20个元素
    unsigned int user;              //定义一个无符号的int类型的user
    unsigned int nice;              //定义一个无符号的int类型的nice
    unsigned int system;            //定义一个无符号的int类型的system
    unsigned int idle;              //定义一个无符号的int类型的idle
    unsigned int iowait;
    unsigned int irq;
    unsigned int softirq;
}cpu_occupy_t;

double cal_cpuoccupy (cpu_occupy_t *o, cpu_occupy_t *n)
{
    double od = (double) (o->user + o->nice + o->system +o->idle+o->softirq+o->iowait+o->irq);//第一次(用户+优先级+系统+空闲)的时间再赋给od
    double nd = (double) (n->user + n->nice + n->system +n->idle+n->softirq+n->iowait+n->irq);//第二次(用户+优先级+系统+空闲)的时间再赋给od
    double id = (double) (n->idle);    //用户第一次和第二次的时间之差再赋给id
    double sd = (double) (o->idle) ;    //系统第一次和第二次的时间之差再赋给sd

    double cpu_use=0;
    if((nd-od) != 0)
        cpu_use =100.0 - ((id-sd))/(nd-od)*100.00; //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used
    return cpu_use;
}

void get_cpuoccupy (cpu_occupy_t *cpu)
{
    FILE *fp = fopen ("/proc/stat", "r");
    if(!fp){return;}

    char buff[256]={0};
    fgets (buff, sizeof(buff), fp);
    fclose(fp);fp=nullptr;

    cpu_occupy_t *tCPU = cpu;
    sscanf (buff, "%s %u %u %u %u %u %u %u", tCPU->name, &tCPU->user, &tCPU->nice,&tCPU->system, &tCPU->idle ,&tCPU->iowait,&tCPU->irq,&tCPU->softirq);
    tCPU=nullptr;;
}

int main()
{
    cpu_occupy_t cpu_stat1;
    get_cpuoccupy((cpu_occupy_t *)&cpu_stat1);

    QThread::sleep(1);

    cpu_occupy_t cpu_stat2;
    get_cpuoccupy((cpu_occupy_t *)&cpu_stat2);

    double cpu = cal_cpuoccupy ((cpu_occupy_t *)&cpu_stat1, (cpu_occupy_t *)&cpu_stat2);
    
    return 0;
}


     纯linux c++获取CPU使用率

QProcess Process;
start("nvidia-smi");
waitForFinished();

connect(&Process,&QProcess::readyRead,[&]()
{
	while(bytesAvailable())
    {
        QByteArray data = readLine();
        if(data.isEmpty()){continue;}

        QString strData=data.data();
        if(strData.contains("%") && strData.contains("Default"))  // nvidia-smi
        {
            QStringList dataList = strData.split('|');
            if(dataList.size() < 4){continue;}

            QString temp=dataList[1];
            QStringList tempList=temp.split(" ");
            if(tempList.size()>=2)
            {
	            // gpu温度
                QString Temp=tempList[4];
                Temp=Temp.left(Temp.size());
            }

            // rate
            QString gpu=dataList[3];
            gpu.remove(QRegExp("\\s"));
            short index=gpu.indexOf("%");
            if(index!=-1)
            {
	            // gpu使用率
                QString Rate = gpu.mid(0,index);
            }
        }
    }
});

关注

笔者 - jxd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信必诺

嗨,支持下哥们呗。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值