3)计算进程processCPUUse的CPU使用率:
processCPUUse = processTime / totalCPUTime;
2. 4. 4、计算线程的CPU使用率threadCPUUse
1)对两个足够短的CPU快照和相应的线程快照进行采样,即读取/ proc / stat文件以在两个时间点获取以下数据:
读取/ proc / [PID] / task / [TID] / stat文件在两个时间点获取以下数据:
2)计算总CPU时间totalCPUTime和线程时间threadTime:
totalCPUTime = CPUTime2 – CPUTime1;
threadTime = threadTime2 – threadTime1;
3)计算该线程的CPU使用率threadCPUUse:
threadCPUUse = threadTime / totalCPUTime;
2. 5、计算多核条件下的CPU使用率
2. 5. 1、基本概念
首先,通过读取/ proc / stat文件获取总的CPU时间,读取/ proc / [PID] / stat获取进程的CPU时间,然后读取/ proc / [PID] / task / [TID] / stat获取线程的CPU时间,读取/ proc / cpuinfo以获取CPU数量。
在有多个内核的情况下,计算进程或线程的CPU使用率。上面的方法通常是相对于CPU所有内核的总时间占用率而言的。我们通常用于获取单个内核的进程或线程的CPU使用率。入住率。因此,我们可以按照上述方法计算CPU占用率,然后将结果乘以CPU内核数即可得出相对于单个内核的进程或线程的占用率。
2. 5. 2、计算总CPU使用率
与2. 4. 2相同。
2. 5. 3、计算进程mProcessCPUUse的CPU使用率

1)与2. 4. 3相同,计算进程processCPUUse的CPU使用率;
2)读取/ proc / cpuinfo文件以获取逻辑CPU(处理器)的数量(请参阅1. 1):processorNum
3)在多核情况mProcessCPUUse中,进程的CPU使用率:
mProcessCPUUse = processCPUUse * processorNum;
2. 5. 4、计算线程mThreadCPUUse的CPU使用率
1)与2. 4. 4相同计算线程的CPU使用率threadCPUUse;
2)读取/ proc / cpuinfo文件以获取逻辑CPU(处理器)的数量(请参阅1. 1):processorNum
3)在多核情况下该线程的CPU使用情况mThreadCPUUse:
mThreadCPUUse = threadCPUUse * processorNum;
2. 6、问题2) / proc / [PID] /任务目录是仅在Linux 2. 6. 0-test6之后存在的功能。 3)关于CPU使用率为负的情况,解决方案是在出现负值之前继续采样并计算CPU使用率,直到非负值为止。 4)一些线程的生命周期很短,并且可能在采样期间死亡。 2. 7、一个用于计算CPU总使用量的小程序
#include#include #include #include<time.h> typedefstruct procstat { char processorName[20]; unsignedint user; unsignedint nice; unsignedint system; unsignedint idle; unsignedint iowait; unsignedint irq; unsignedint softirq; unsignedint stealstolen; unsignedint guest; }Procstat; Procstat getCPUStatus(){ // Get "/proc/stat" info. FILE* inputFile = NULL; chdir("/proc"); inputFile = fopen("stat","r"); if(!inputFile){ perror("error: Can not open file.\n"); } char buff[1024]; fgets(buff,sizeof(buff), inputFile);// Read 1 line. printf(buff); Procstat ps; sscanf(buff,"%s %u %u %u %u %u %u %u %u %u", ps.processorName,&ps.user,&ps.nice,&ps.system,&ps.idle,&ps.iowait,&ps.irq,&ps.softirq,&ps.stealstolen,&ps.guest);// Scan from "buff". printf("user: %u\n", ps.user); fclose(inputFile); return ps; } float calculateCPUUse(Procstat ps1,Procstat ps2){ unsignedint totalCPUTime =(ps2.user + ps2.nice + ps2.system + ps2.idle + ps2.iowait + ps2.irq + ps2.softirq + ps2.stealstolen + ps2.guest)-(ps1.user + ps1.nice + ps1.system + ps1.idle + ps1.iowait + ps1.irq + ps1.softirq + ps1.stealstolen + ps1.guest); unsignedint idleCPUTime = ps2.idle - ps1.idle; floatCPUUse=((float) totalCPUTime -(float) idleCPUTime)/(float) totalCPUTime; printf("totalCPUTime: %u\nidleCPUTime: %u\n", totalCPUTime, idleCPUTime); returnCPUUse; } int main(int argc,char* argv[]){ printf("Test CPU\n"); // Get processor num. int processorNum = sysconf(_SC_NPROCESSORS_CONF);// "unistd.h" is required. printf("Processors: %d\n", processorNum); // Test Procstat ps1, ps2; int i =0; for(i =0; i <=100000; i++){ srand((unsigned) time(NULL)); int m = rand()%100000; int n =1+ rand()%100000; int k = m / n; if(i ==10){ ps1 = getCPUStatus(); } if(i ==10000){ ps2 = getCPUStatus(); } } floatCPUUse= calculateCPUUse(ps1, ps2); printf("CPUUse: %f\n",CPUUse); return0; }
3、 Linux环境查看进程运行的相关信息3. 1、使用ps命令查看进程信息
几个常用参数:
示例1)列出了当前内存中的所有进程
[root@rh ~]$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 10.00.0193561536?SsApr220:01/sbin/init root 20.00.000? S Apr220:00[kthreadd] root 30.00.000? S Apr220:00[migration/0] root 40.00.000? S Apr220:00[ksoftirqd/0] root 50.00.000? S Apr220:00[migration/0] root 60.00.000? S Apr220:00[watchdog/0] root 70.00.000? S Apr220:00[migration/1] root 80.00.000? S Apr220:00[migration/1] ......
示例2)列出了进程号为13560的进程的所有线程和CPU使用率
[root@rh ~]$ ps -eLo pid,lwp,u | grep 13560 135601356049.5
3. 2、使用top命令查看过程信息
几个常用参数:
示例1)执行两次top命令,然后将结果输出到/top_result.data。
[root@rh ~]$ top -b -n 2>/top_result.data示例2)监视过程13620
[root@rh ~]$ top -d 2-p 13620 top -16:27:35 up 4 days,7:43,2 users, load average:0.35,0.47,0.44 Tasks:1 total,1 running,0 sleeping,0 stopped,0 zombie Cpu(s):0.1%us,3.1%sy,0.0%ni,96.5%id,0.0%wa,0.0%hi,0.3%si,0.0%st Mem:16320632k total,1790796k used,14529836k free,233168k buffers Swap:8232952k total,0k used,8232952k free,941540k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 13620 test1370 20011060944760 R 53.40.00:04.78 netperf
参考本文
“鸟兄弟Linux私人厨房”
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shoujiruanjian/article-375750-2.html
伊拉克能打赢