APUE 3.9节中关于系统调用read给出了不同大小的缓冲区会导致读取效率的差异。这里stevens用三种时间表示读取文件过程所花费的时间。这三种时间分别为真实/时钟时间(real / clock time),系统cpu时间(system cpu time),用户cpu时间(user cpu time )。其意义如下: 真实时间:进程从开始执行到最后结束的时间,包括阻塞+就绪(排队等待)+运行的时间。也即我们能够真实感受到的时间。 系统cpu时间:进程运行时,在系统区执行的时间,如(write,read等系统调用),运行的地方位于系统内存中。 用户cpu时间:进程运行时,在用户区执行的时间。这里主要是我们自己编写的代码,运行在用户内存中。
程序从用户态到系统态需要消耗一定的时间,频繁的切换会导致系统运行的效率低下。但缓冲区又不宜太大,会浪费用户内存 。所以合适的缓冲区大小很有必要 。那我们如何确定一个最佳的缓冲区呢?stevens就用上面的三种时间去测试系统效率。他选用的bufsize从1,2,4,8 ... 524888 这20个值进行测试,最后得出最佳大小。那我们如何获得这些时间呢?
其实linux提供了很多关于时间的系统调用,如time,gettime ofday ,clock,times等等。其中前三个很常用,但无法获得system cpu time 和 user cpu time。所以这里主要介绍times函数。它的函数原型为:
#include<sys/times> clock_t times( struct tms * buf); struct tms 的成员结构如下: struct tms { clock_t tms_utime; clock_t tms_stime; clock_t tms_cutime; clock_t tms_cstime; };
通过上面的介绍你会发现原来这个函数非常好用,只要在程序的末尾调用一下,就可以知道这个进程的system cpu time 和 user cpu time ,很方便吧,而返回值只要不等于(clock_t) -1,就说明返回成功。
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<fcntl.h> #include<string.h> #include<errno.h> #include<time.h> #include<sys/times.h> #include<sys/time.h> #define BUF_SIZE 512 #define FILE_NAME "demo.txt" #define err_exit(m) {perror(m); exit(1);} int main(){ int i,fd,clocks_per_sec; char buf[BUF_SIZE]; struct tms st_tms; clock_t start = clock(); fd = open(FILE_NAME, O_RDONLY | O_CREAT, 0664); if (fd == -1) err_exit( "open error" ); while (read(fd, buf, BUF_SIZE) != 0); for (i = 0; i < 1000000000; i++); if (times(&st_tms) == -1) err_exit( "times error" ); clock_t end = clock(); clocks_per_sec = sysconf(_SC_CLK_TCK); printf("real times: %7dus\n" , (end-start)/CLOCKS_PER_SEC*1000000); printf("user times: %7dus\n" , st_tms.tms_utime*1000000/(clocks_per_sec)); printf("syst times: %7dus\n" , st_tms.tms_stime*1000000/(clocks_per_sec)); return 0; }
上面的代码需要注意以下两点:
1.为了demo.txt文件足够大,方便看出系统态下的执行时间,可以用下面的语句生成:
dd if=/dev/zero of=demo.txt bs=1M count=512 --可以生成512M的文件,很方便的!
2.times和clock都有clock_t类型的变量,将它们转化为秒要分别除以system(_SC_CLK_TCK)和宏CLOCKS_PER_SEC,它们的值是不同的,具体可以man一下这两个函数。
讲到这里就把struct tms 的最后两个成员变量也讲一下吧,tms_cutime和tms_cstime是用来记录子进程(们)的system cpu time 和 user cpu time。他们的值是在父进程中执行wait 或 waitpid 时开始记录,等到wait返回后才停止记录。看如下代码:
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<fcntl.h> #include<string.h> #include<errno.h> #include<time.h> #include<sys/times.h> #include<sys/time.h> #define BUF_SIZE 512 #define FILE_NAME "demo.txt" #define err_exit(m) {perror(m); exit(1);} int main(){ int i,fd,clocks_per_sec; char buf[BUF_SIZE]; struct tms st_tms; clock_t start = clock(); fd = open(FILE_NAME, O_RDONLY | O_CREAT, 0664); if (fd == -1) err_exit( "open error" ); while (read(fd, buf, BUF_SIZE) != 0); for (i = 0; i < 1000000000; i++); int pid; if ((pid = fork()) < 0){ err_exit("fork error" ); }else if (pid == 0){ int i = 0; for ( ; i < 1000000000; i++); lseek(fd, 0, SEEK_SET); while (read(fd, buf, BUF_SIZE) > 0); exit(0); }else { wait(-1); } if (times(&st_tms) == -1) err_exit( "times error" ); clock_t end = clock(); clocks_per_sec = sysconf(_SC_CLK_TCK); printf("real times: %7dus\n" , (end-start)/CLOCKS_PER_SEC*1000000); printf("user times: %7dus\n" , st_tms.tms_utime*1000000/(clocks_per_sec)); printf("syst times: %7dus\n" , st_tms.tms_stime*1000000/(clocks_per_sec)); printf("child user times: %7dus\n" , st_tms.tms_cutime*1000000/(clocks_per_sec)); printf("child syst times: %7dus\n" , st_tms.tms_cstime*1000000/(clocks_per_sec)); return 0; }
注意:linux中clock函数并不能统计子进程(们)的时间哦,所以最后那个real time 只是父进程的时间,如果要统计可以使用time函数啦,time返回的时间点,所以当然足够精确啦!编译执行,看一下结果是否符合我们的预期值。
相关链接:https://stackoverflow.com/questions/17432502/how-can-i-measure-cpu-time-and-wall-clock-time-on-both-linux-windows#