-----------接上篇,本文开始介绍cpu占用统计,及memory使用信息。
图中字段表示,如下:
us, user:用户态(un-niced)的任务cpu占用率。
sy, system:内核态任务cpu占用率。
ni, nice:用户态(niced)的任务cpu占用率。
id, idle:空闲的cpu占用率。
wa, IO-wait:等io完成的cpu占用率。
hi : 硬中断cpu占用率。
si : 软中断cpu占用率。
st : 虚拟化相关。一般为0。
1、User
通常我们写的用户态程序,它处于用户态(usr),在没有调整过任务调度策略或优先级的情况下,例如像这样的调用,pthread_create(&tid, NULL, thread_func, NULL),第二个参数设置为NULL,使用线程的默认设置,这样的任务运行起来占用的就是usr利用率。
上程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#define MAX_PTHREAD_NUM 32
void pthread_func(void *args)
{
printf("create pthread task! pid: %d\n", syscall(__NR_getpid));
while(1);
}
int main(int argc, char **argv)
{
int i, n = 0;
pthread_t tid;
pthread_create(&tid, NULL, (void*)pthread_func, NULL);
pthread_join(tid, NULL);
return 0;
}
[root@localhost top]# gcc usr.c -o usr_loop -pthread [root@localhost top]# ./usr_loop create pthread task! pid: 27375 |
程序运行后,我们看到usr_loop程序起来后,usr字段显示cpu占用率100%
2、Sys
Sys表示内核空间占用率,该字段通常是比较低的,如果该值高说明内核态代码耗时严重,或者处于内核态中的任务CPU占比高。通常,我们的用户态程序基本不会占用sys字段,但也有一种情况就是频繁进入内核态,频繁请求内核服务,这样是不是可以占用过多的sys字段呢,来个实验。
int main(int argc, char **argv)
{
While(1)
syscall(__NR_getpid);
return 0;
}
可以看到sy占用率在80%左右(on cpu3),确实我们写了一个频繁进入内核态程序,让代码大部分时间处于内核态执行,但一点,cpu并不是完全占100%,user占用率占17%左右,为什么呢?