https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c
关键结构体:
struct rusage { struct timeval ru_utime; /* user time used */ struct timeval ru_stime; /* system time used */ long ru_maxrss; /* max resident set size */ long ru_ixrss; /* integral shared text memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims */ long ru_majflt; /* page faults */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* messages sent */ long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ };
程序:
/** * Author: Yaowen Xu * Github: https://github.com/yaowenxu * Organization: 北航系统结构研究所 * Date: 2019-08-18 13:31:34 * LastEditTime: 2019-08-18 13:51:26 * Description: 使用系统 函数 getrusage 获取程序运行相关信息 * 此程序主要是关注与总时间和内核空间运行时间与用户 * 空间运行时间,使用此函数可大致对程序运行时间计算; * 查看: 具体使用信息可以在控制台以 man getrusage 命令查看 */ #include <stdio.h> #include <math.h> #include <sys/time.h> #include <sys/resource.h> int str2int(char* str){ char *p = str; int sum = 0; while (*p != ‘\0‘) { sum = sum*10 + (*p-‘0‘); p++; } return sum; } int main(int argc, char* argv[]){ int def = 1000; if (argc == 2) { def = str2int(argv[argc-1]); } for (int i = 0; i < def ; i++) { float tmp = sqrt(i); } struct rusage usage; getrusage(RUSAGE_SELF, &usage); //getrusage(RUSAGE_CHILDREN, &usage); long user = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec; // user time used long sys = usage.ru_stime.tv_sec * 1e6 + usage.ru_stime.tv_usec; // sys time used printf("User: %ld us\n", user); // 用户空间使用的时间 printf("Sys: %ld us\n", sys); // 内核空间使用的时间 printf("Total: %ld us\n", user+sys); // 总共使用的时钟 return 0; }
保持更新,如果对您有帮助请点击推荐!更多关于Linux 相关的知识,请关注 cnblogs.com/xuyaowen
原文地址:https://www.cnblogs.com/xuyaowen/p/c-sys-getrusage.html
时间: 2024-10-09 08:53:50