No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

179 líneas
4.1 KiB

hace 3 años
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <pwd.h>
  4. #include <curses.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include <termcap.h>
  8. #include <termios.h>
  9. #include <time.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <fcntl.h>
  13. #include <errno.h>
  14. #include <dirent.h>
  15. #include <assert.h>
  16. struct proc {
  17. pid_t pid;
  18. int tick;
  19. };
  20. void print_procs(
  21. struct proc *proc1, struct proc *proc2, int cputimemode)
  22. {
  23. int p, nprocs;
  24. u64_t idleticks = 0;
  25. u64_t kernelticks = 0;
  26. u64_t systemticks = 0;
  27. u64_t userticks = 0;
  28. u64_t total_ticks = 0;
  29. int blockedseen = 0;
  30. static struct proc *tick_procs = NULL;
  31. tick_procs = malloc(nr_total * sizeof(struct proc));
  32. for(p = nprocs = 0; p < nr_total; p++) {
  33. u64_t uticks;
  34. //如果当前进程标志不是used就continue 看下一个进程。
  35. if(!(proc2[p].p_flags & USED))
  36. continue;
  37. tick_procs[nprocs].p = proc2 + p;
  38. tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
  39. //更新实时uticks
  40. uticks = cputicks(&proc1[p], &proc2[p], 1);
  41. //算出总的ticks
  42. total_ticks = total_ticks + uticks;
  43. //判断是否为idletick
  44. //为0一直continue 不用计算
  45. if(p-5 == 317) {
  46. idleticks = uticks;
  47. continue;
  48. }
  49. //判断是否为kerneltick
  50. if(p-5 == ((endpoint_t) -1)) {
  51. kernelticks = uticks;
  52. }
  53. if(!(proc2[p].p_flags & IS_TASK)) {
  54. if(proc2[p].p_flags & IS_SYSTEM)
  55. systemticks = systemticks + tick_procs[nprocs].ticks;
  56. else
  57. userticks = userticks + tick_procs[nprocs].ticks;
  58. }
  59. nprocs++;
  60. }
  61. if (total_ticks == 0)
  62. return;
  63. printf("CPU states: %6.2f%%", 100.0 * usedticks / total_ticks);
  64. printf("\n");
  65. }
  66. int print_memory(void)
  67. {
  68. FILE *fp;
  69. unsigned int pagesize;
  70. unsigned long total, free, largest, cached;
  71. if ((fp = fopen("/proc/meminfo", "r")) == NULL)
  72. return 0;
  73. if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free,
  74. &largest, &cached) != 5) {
  75. fclose(fp);
  76. return 0;
  77. }
  78. fclose(fp);
  79. printf("main memory: %ldK total, %ldK free, %ldK contig free, "
  80. "%ldK cached\n",
  81. (pagesize * total)/1024, (pagesize * free)/1024,
  82. (pagesize * largest)/1024, (pagesize * cached)/1024);
  83. return 1;
  84. }
  85. int nr_procs, nr_tasks, nr_total;
  86. void getkinfo(void)
  87. {
  88. FILE *fp;
  89. if ((fp = fopen("/proc/kinfo", "r")) == NULL) {
  90. exit(1);
  91. }
  92. if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) {
  93. exit(1);
  94. }
  95. fclose(fp);
  96. nr_total = (int) (nr_procs + nr_tasks);
  97. }
  98. void parse_psinfo(pid_t pid, struct proc pro)
  99. {
  100. char path[PATH_MAX];
  101. char name[256], type, state;
  102. int version, endpt, blocked, priority, tick;
  103. FILE *fp;
  104. sprintf(path, "/proc/%d/psinfo", pid);
  105. if ((fp = fopen(path, "r")) == NULL)
  106. return;
  107. if (fscanf(fp, "%d %c %d %s %c %d %d", &version, &type, &endpt, name, &state, &blocked, &priority) != 7)
  108. {
  109. fclose(fp);
  110. return;
  111. }
  112. //读入 滴答
  113. if (fscanf(fp, "%d", &tick) < 0)
  114. {
  115. fclose(fp);
  116. return;
  117. }
  118. //存入进程结构体
  119. pro.pid = pid;
  120. pro.tick = tick;
  121. fclose(fp);
  122. }
  123. void parse_dir(process)
  124. {
  125. DIR *p_dir;
  126. struct dirent *p_ent;
  127. pid_t pid;
  128. char *end;
  129. p_dir = opendir("/proc/")
  130. //readdir(),/proc/pid/psinfo,/proc/内文件名为PID
  131. p_ent=readdir(p_dir);
  132. int i=0;
  133. while(p_ent != NULL)
  134. {
  135. pid=strtol(p_ent->d_name,&end,10);
  136. if(pid!=0 && !end[0])
  137. {
  138. parse_psinfo(pid, process[i]);
  139. i++;
  140. }
  141. p_ent=readdir(p_dir);
  142. }
  143. closedir(p_dir);
  144. }
  145. void get_procs()
  146. {
  147. //struct proc *process;
  148. //process = malloc(nr_total * sizeof(struct proc);
  149. struct proc process[nr_total];
  150. parse_dir(process);
  151. print_procs(process);
  152. }
  153. int mytop()
  154. {
  155. if (chdir("/proc") != 0)
  156. {
  157. perror("chdir to /proc" );
  158. return 1;
  159. }
  160. print_memory();
  161. getkinfo();
  162. get_procs();
  163. }