linux c 获取进程 cpu占用率 内存占用情况

╰+攻爆jí腚メ 2021-09-03 07:58 1012阅读 0赞

实例如下:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #define VMRSS_LINE 17
  7. #define VMSIZE_LINE 13
  8. #define PROCESS_ITEM 14
  9. typedef struct {
  10. unsigned long user;
  11. unsigned long nice;
  12. unsigned long system;
  13. unsigned long idle;
  14. }Total_Cpu_Occupy_t;
  15. typedef struct {
  16. unsigned int pid;
  17. unsigned long utime; //user time
  18. unsigned long stime; //kernel time
  19. unsigned long cutime; //all user time
  20. unsigned long cstime; //all dead time
  21. }Proc_Cpu_Occupy_t;
  22. //获取第N项开始的指针
  23. const char* get_items(const char*buffer ,unsigned int item){
  24. const char *p =buffer;
  25. int len = strlen(buffer);
  26. int count = 0;
  27. for (int i=0; i<len;i++){
  28. if (' ' == *p){
  29. count ++;
  30. if(count == item -1){
  31. p++;
  32. break;
  33. }
  34. }
  35. p++;
  36. }
  37. return p;
  38. }
  39. //获取总的CPU时间
  40. unsigned long get_cpu_total_occupy(){
  41. FILE *fd;
  42. char buff[1024]={0};
  43. Total_Cpu_Occupy_t t;
  44. fd =fopen("/proc/stat","r");
  45. if (nullptr == fd){
  46. return 0;
  47. }
  48. fgets(buff,sizeof(buff),fd);
  49. char name[64]={0};
  50. sscanf(buff,"%s %ld %ld %ld %ld",name,&t.user,&t.nice,&t.system,&t.idle);
  51. fclose(fd);
  52. return (t.user + t.nice + t.system + t.idle);
  53. }
  54. //获取进程的CPU时间
  55. unsigned long get_cpu_proc_occupy(unsigned int pid){
  56. char file_name[64]={0};
  57. Proc_Cpu_Occupy_t t;
  58. FILE *fd;
  59. char line_buff[1024]={0};
  60. sprintf(file_name,"/proc/%d/stat",pid);
  61. fd = fopen(file_name,"r");
  62. if(nullptr == fd){
  63. return 0;
  64. }
  65. fgets(line_buff,sizeof(line_buff),fd);
  66. sscanf(line_buff,"%u",&t.pid);
  67. const char *q =get_items(line_buff,PROCESS_ITEM);
  68. sscanf(q,"%ld %ld %ld %ld",&t.utime,&t.stime,&t.cutime,&t.cstime);
  69. fclose(fd);
  70. return (t.utime + t.stime + t.cutime + t.cstime);
  71. }
  72. //获取CPU占用率
  73. float get_proc_cpu(unsigned int pid){
  74. unsigned long totalcputime1,totalcputime2;
  75. unsigned long procputime1,procputime2;
  76. totalcputime1=get_cpu_total_occupy();
  77. procputime1=get_cpu_proc_occupy(pid);
  78. usleep(200000);
  79. totalcputime2=get_cpu_total_occupy();
  80. procputime2=get_cpu_proc_occupy(pid);
  81. float pcpu = 0.0;
  82. if(0 != totalcputime2-totalcputime1){
  83. pcpu=100.0 * (procputime2-procputime1)/(totalcputime2-totalcputime1);
  84. }
  85. return pcpu;
  86. }
  87. //获取进程占用内存
  88. unsigned int get_proc_mem(unsigned int pid){
  89. char file_name[64]={0};
  90. FILE *fd;
  91. char line_buff[512]={0};
  92. sprintf(file_name,"/proc/%d/status",pid);
  93. fd =fopen(file_name,"r");
  94. if(nullptr == fd){
  95. return 0;
  96. }
  97. char name[64];
  98. int vmrss;
  99. for (int i=0; i<VMRSS_LINE-1;i++){
  100. fgets(line_buff,sizeof(line_buff),fd);
  101. }
  102. fgets(line_buff,sizeof(line_buff),fd);
  103. sscanf(line_buff,"%s %d",name,&vmrss);
  104. fclose(fd);
  105. return vmrss;
  106. }
  107. //获取进程占用虚拟内存
  108. unsigned int get_proc_virtualmem(unsigned int pid){
  109. char file_name[64]={0};
  110. FILE *fd;
  111. char line_buff[512]={0};
  112. sprintf(file_name,"/proc/%d/status",pid);
  113. fd =fopen(file_name,"r");
  114. if(nullptr == fd){
  115. return 0;
  116. }
  117. char name[64];
  118. int vmsize;
  119. for (int i=0; i<VMSIZE_LINE-1;i++){
  120. fgets(line_buff,sizeof(line_buff),fd);
  121. }
  122. fgets(line_buff,sizeof(line_buff),fd);
  123. sscanf(line_buff,"%s %d",name,&vmsize);
  124. fclose(fd);
  125. return vmsize;
  126. }
  127. //进程本身
  128. int get_pid(const char* process_name, const char* user = nullptr)
  129. {
  130. if(user == nullptr){
  131. user = getlogin();
  132. }
  133. char cmd[512];
  134. if (user){
  135. sprintf(cmd, "pgrep %s -u %s", process_name, user);
  136. }
  137. FILE *pstr = popen(cmd,"r");
  138. if(pstr == nullptr){
  139. return 0;
  140. }
  141. char buff[512];
  142. ::memset(buff, 0, sizeof(buff));
  143. if(NULL == fgets(buff, 512, pstr)){
  144. return 0;
  145. }
  146. return atoi(buff);
  147. }
  148. int main(int argc, char *argv[])
  149. {
  150. if(argc < 2){
  151. printf("Usage:test <process_name> [user]\n");
  152. return 1;
  153. }
  154. unsigned int pid=0;
  155. if(argc > 2){
  156. pid = get_pid(argv[1],argv[2]);
  157. }
  158. else{
  159. pid = get_pid(argv[1]);
  160. }
  161. printf("pid=%d\n",pid);
  162. printf("pcpu=%f\n",get_proc_cpu(pid));
  163. printf("procmem=%d\n",get_proc_mem(pid));
  164. printf("virtualmem=%d\n",get_proc_virtualmem(pid));
  165. return 0;
  166. }

发表评论

表情:
评论列表 (有 0 条评论,1012人围观)

还没有评论,来说两句吧...

相关阅读

    相关 查看LINUX进程内存占用情况

      可以直接使用top命令后,查看%MEM的内容。可以选择按进程查看或者按用户查看,如想查看oracle用户的进程内存使用情况的话可以使用如下的命令:  (1)top