Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

401 lignes
11 KiB

il y a 3 ans
il y a 3 ans
  1. #define mytop_h
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <pwd.h>
  5. #include <curses.h>
  6. //#include <timers.h>
  7. #include <stdlib.h>
  8. #include <limits.h>
  9. #include <termcap.h>
  10. #include <termios.h>
  11. #include <time.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <dirent.h>
  17. #include <assert.h>
  18. typedef int endpoint_t;
  19. typedef uint64_t u64_t;
  20. typedef long unsigned int vir_bytes;
  21. #define USED 0x1
  22. #define IS_TASK 0x2
  23. #define IS_SYSTEM 0x4
  24. #define BLOCKED 0x8
  25. #define TYPE_TASK 'T'
  26. #define TYPE_SYSTEM 'S'
  27. #define STATE_RUN 'R'
  28. #define MAX_NR_TASKS 1023
  29. #define SELF ((endpoint_t) 0x8ace)
  30. #define _MAX_MAGIC_PROC (SELF)
  31. #define _ENDPOINT_GENERATION_SIZE (MAX_NR_TASKS+_MAX_MAGIC_PROC+1)
  32. #define _ENDPOINT_P(e) \
  33. ((((e)+MAX_NR_TASKS) % _ENDPOINT_GENERATION_SIZE) - MAX_NR_TASKS)
  34. #define SLOT_NR(e) (_ENDPOINT_P(e) + 5)
  35. #define _PATH_PROC "/proc"
  36. #define CPUTIME(m, i) (m & (1L << (i)))
  37. const char *cputimenames[] = { "user", "ipc", "kernelcall" };
  38. #define CPUTIMENAMES (sizeof(cputimenames)/sizeof(cputimenames[0]))
  39. unsigned int nr_procs, nr_tasks;
  40. int nr_total=0;
  41. //int slot_a=0;
  42. //int pronum=0;
  43. //int filenum=0;
  44. //proc 结构体
  45. struct proc {
  46. int p_flags;
  47. endpoint_t p_endpoint;
  48. pid_t p_pid;
  49. u64_t p_cpucycles[CPUTIMENAMES];
  50. int p_priority;
  51. endpoint_t p_blocked;
  52. time_t p_user_time;
  53. vir_bytes p_memory;
  54. uid_t p_effuid;
  55. int p_nice;
  56. char p_name[16+1];
  57. };
  58. struct proc *proc = NULL, *prev_proc = NULL;
  59. //u64_t 64位 high和low32位 拼接成64位 high+low
  60. static inline u64_t make64(unsigned long lo, unsigned long hi)
  61. {
  62. return ((u64_t)hi << 32) | (u64_t)lo;
  63. }
  64. //把每个pid/psinfo的信息读出来
  65. //判断读取信息是否可用
  66. void parse_file(pid_t pid)
  67. {
  68. char path[PATH_MAX], name[256], type, state;
  69. int version, endpt, effuid;
  70. unsigned long cycles_hi, cycles_lo;
  71. FILE *fp;
  72. struct proc *p;
  73. int slot;
  74. int i;
  75. sprintf(path, "/proc/%d/psinfo", pid);
  76. //按照/proc/%d/psinfo打开path中的文件
  77. if ((fp = fopen(path, "r")) == NULL)
  78. return;
  79. //version是否为1,如果不是该进程不需要记录
  80. if (fscanf(fp, "%d", &version) != 1) {
  81. fclose(fp);
  82. return;
  83. }
  84. //versions错误处理
  85. if (version != 0) {
  86. fputs("procfs version mismatch!\n", stderr);
  87. exit(1);
  88. }
  89. //读入类型和端点 判断是否读入的是两个
  90. if (fscanf(fp, " %c %d", &type, &endpt) != 2) {
  91. fclose(fp);
  92. return;
  93. }
  94. //统计总file数
  95. //filenum+=1;
  96. //原来的slot超出了nr_total
  97. slot = SLOT_NR(endpt);
  98. slot++;
  99. //slot=slot_a;
  100. //slot_a+=1;//赋值需保证在数组中不会重复
  101. //判断endpoint的值是否合理 在0到nr_total的范围内
  102. if(slot < 0 || slot >= nr_total) {
  103. fprintf(stderr, "top: unreasonable endpoint number %d\n", endpt);
  104. fclose(fp);
  105. return;
  106. }
  107. //slot为该进程结构体在数组中的位置
  108. p = &proc[slot];//把slot地址赋值给p
  109. if (type == TYPE_TASK)
  110. //标示task进程
  111. p->p_flags |= IS_TASK;
  112. else if (type == TYPE_SYSTEM)
  113. //标示system进程
  114. p->p_flags |= IS_SYSTEM;
  115. //将endpt和pid存入对应进程结构体
  116. p->p_endpoint = endpt;
  117. p->p_pid = pid;
  118. //读入名字 状态 阻塞状态 动态优先级 进程时间 高周期 低周期
  119. if (fscanf(fp, " %255s %c %d %d %lu %*u %lu %lu",
  120. name, &state, &p->p_blocked, &p->p_priority,
  121. &p->p_user_time, &cycles_hi, &cycles_lo) != 7) {
  122. fclose(fp);
  123. return;
  124. }
  125. //将指定长度的字符串复制到字符数组中
  126. strncpy(p->p_name, name, sizeof(p->p_name)-1);
  127. //数组置0
  128. p->p_name[sizeof(p->p_name)-1] = 0;
  129. if (state != STATE_RUN)//如果不是run的进程
  130. p->p_flags |= BLOCKED;//标志阻塞
  131. //拼接成64位,放在p_cpucycles[]数组中
  132. p->p_cpucycles[0] = make64(cycles_lo, cycles_hi);
  133. p->p_memory = 0L;
  134. //判断是否为有效用户ID
  135. if (!(p->p_flags & IS_TASK)) {
  136. int j;
  137. //读如内存 有效用户ID 和静态优先级
  138. if ((j=fscanf(fp, " %lu %*u %*u %*c %*d %*u %u %*u %d %*c %*d %*u",
  139. &p->p_memory, &effuid, &p->p_nice)) != 3) {
  140. fclose(fp);
  141. return;
  142. }
  143. p->p_effuid = effuid;
  144. } else p->p_effuid = 0;
  145. //连续读CPUTIMENAMES次cycles_hi,cycle_lo
  146. for(i = 1; i < CPUTIMENAMES; i++) {
  147. if(fscanf(fp, " %lu %lu",
  148. &cycles_hi, &cycles_lo) == 2) {
  149. //拼接成64位,放在p_cpucycles[]数组中
  150. p->p_cpucycles[i] = make64(cycles_lo, cycles_hi);
  151. } else {
  152. p->p_cpucycles[i] = 0;
  153. }
  154. }
  155. //读如内存 存入进程结构体
  156. if ((p->p_flags & IS_TASK)) {
  157. if(fscanf(fp, " %lu", &p->p_memory) != 1) {
  158. p->p_memory = 0;
  159. }
  160. }
  161. //按位或
  162. p->p_flags |= USED;
  163. fclose(fp);
  164. }
  165. void parse_dir(void)
  166. {
  167. DIR *p_dir;
  168. struct dirent *p_ent;
  169. pid_t pid;
  170. char *end;
  171. //打开/proc
  172. if ((p_dir = opendir("/proc/")) == NULL) {
  173. perror("opendir on /proc");
  174. exit(1);
  175. }
  176. //readdir()返回参数p_dir 目录流的下个目录进入点。
  177. p_ent=readdir(p_dir);
  178. while(p_ent != NULL){
  179. // if(strncpy(p_ent->d_name,"/proc",1)==0)
  180. // continue;
  181. //分析出里面所有pid
  182. pid=strtol(p_ent->d_name,&end,10);
  183. if(pid!=0 && !end[0]){
  184. //printf("%l\n",pid);
  185. //一个pid调用一次parse_file
  186. parse_file(pid);
  187. }
  188. p_ent=readdir(p_dir);
  189. }
  190. closedir(p_dir);
  191. }
  192. int print_memory(void)
  193. {
  194. FILE *fp;
  195. unsigned int pagesize;
  196. unsigned long total, free, largest, cached;
  197. //打开meminfo
  198. if ((fp = fopen("/proc/meminfo", "r")) == NULL)
  199. return 0;
  200. //读输入
  201. if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free,
  202. &largest, &cached) != 5) {
  203. fclose(fp);
  204. return 0;
  205. }
  206. fclose(fp);
  207. //打印memory信息
  208. printf("main memory: %ldK total, %ldK free, %ldK contig free, "
  209. "%ldK cached\n",
  210. (pagesize * total)/1024, (pagesize * free)/1024,
  211. (pagesize * largest)/1024, (pagesize * cached)/1024);
  212. return 1;
  213. }
  214. //tp 结构体
  215. //包含了进程指针p和ticks,对应某个进程和滴答
  216. struct tp {
  217. struct proc *p;
  218. u64_t ticks;
  219. };
  220. //计算cputicks 用到当前进程和其他进程的,还涉及CPUTIME
  221. //滴答并不是简单的结构体中的滴答,因为在写文件的时候需要更新。需要通过当前进程来和该进程一起计算
  222. u64_t cputicks(struct proc *p1, struct proc *p2, int timemode)
  223. {
  224. int i;
  225. u64_t t = 0;
  226. //计算每个进程proc的滴答,通过proc和当前进程prev_proc做比较,如果endpoint相等,则在循环中分别计算
  227. for(i = 0; i < CPUTIMENAMES; i++) {
  228. if(!CPUTIME(timemode, i))
  229. continue;
  230. if(p1->p_endpoint == p2->p_endpoint) {
  231. t = t + p2->p_cpucycles[i] - p1->p_cpucycles[i];
  232. } else {
  233. t = t + p2->p_cpucycles[i];
  234. }
  235. }
  236. // for(i = 0; i < CPUTIMENAMES; i++) {
  237. // if(!CPUTIME(timemode, i))
  238. // continue;
  239. // if(proc->p_endpoint == prev_proc->p_endpoint) {
  240. // t = t + prev_proc->p_cpucycles[i] - proc->p_cpucycles[i];
  241. // } else {
  242. // t = t + prev_proc->p_cpucycles[i];
  243. // }
  244. // }
  245. return t;
  246. }
  247. void print_procs(
  248. struct proc *proc1, struct proc *proc2, int cputimemode)
  249. {
  250. int p, nprocs;
  251. u64_t idleticks = 0;
  252. u64_t kernelticks = 0;
  253. u64_t systemticks = 0;
  254. u64_t userticks = 0;
  255. u64_t total_ticks = 0;
  256. int blockedseen = 0;
  257. static struct tp *tick_procs = NULL;
  258. if (tick_procs == NULL) {
  259. //给tick_procs分配内存
  260. //创建tp结构体tick_procs
  261. tick_procs = malloc(nr_total * sizeof(tick_procs[0]));
  262. //tick procs错误处理
  263. if (tick_procs == NULL) {
  264. fprintf(stderr, "Out of memory!\n");
  265. exit(1);
  266. }
  267. }
  268. for(p = nprocs = 0; p < nr_total; p++) {
  269. u64_t uticks;
  270. //如果当前进程标志不是used就continue 看下一个进程。
  271. if(!(proc2[p].p_flags & USED))
  272. continue;
  273. tick_procs[nprocs].p = proc2 + p;
  274. tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
  275. //更新实时uticks
  276. uticks = cputicks(&proc1[p], &proc2[p], 1);
  277. //算出总的ticks
  278. total_ticks = total_ticks + uticks;
  279. //判断是否为idletick
  280. //为0一直continue 不用计算
  281. if(p-5 == 317) {
  282. idleticks = uticks;
  283. continue;
  284. }
  285. //判断是否为kerneltick
  286. if(p-5 == ((endpoint_t) -1)) {
  287. kernelticks = uticks;
  288. }
  289. // if(!(proc2[p].p_flags & IS_TASK)) {
  290. // if(proc2[p].p_flags & IS_SYSTEM)
  291. // systemticks = systemticks + tick_procs[nprocs].ticks;
  292. // else
  293. // userticks = userticks + tick_procs[nprocs].ticks;
  294. // }
  295. //判断是否为systemtick和usertick
  296. if(!(proc2[p].p_flags & IS_TASK)) {
  297. if(proc2[p].p_flags & IS_SYSTEM)
  298. systemticks = systemticks + tick_procs[nprocs].ticks;
  299. else
  300. userticks = userticks + tick_procs[nprocs].ticks;
  301. }
  302. nprocs++;
  303. }
  304. if (total_ticks == 0)
  305. return;
  306. //打印user system kernel idle的情况
  307. printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks);
  308. printf("%6.2f%% system, ", 100.0 * systemticks / total_ticks);
  309. printf("%6.2f%% kernel, ", 100.0 * kernelticks/ total_ticks);
  310. printf("%6.2f%% idle",100.00-(100.0 * (kernelticks+userticks+systemticks)/ total_ticks));
  311. printf("\n");
  312. }
  313. //get_procs将所有需要的信息放在结构体数组proc[]中,每个元素都是一个进程结构体。
  314. void get_procs(void)
  315. {
  316. struct proc *p;
  317. int i;
  318. p = prev_proc;
  319. //记录当前进程,赋值给prev_proc
  320. prev_proc = proc;
  321. proc = p;
  322. if (proc == NULL) {
  323. //分配内存
  324. //每个进程分配一个结构体
  325. //分配nr_total个单位proc结构体内存空间,并把指针赋予proc
  326. proc = malloc(nr_total * sizeof(proc[0]));
  327. //错误处理
  328. if (proc == NULL) {
  329. fprintf(stderr, "Out of memory!\n");
  330. exit(1);
  331. }
  332. }
  333. //先将所有flag置0
  334. for (i = 0; i < nr_total; i++)
  335. proc[i].p_flags = 0;
  336. //调用parse_dir分析pid
  337. parse_dir();
  338. }
  339. void getkinfo(void)
  340. {
  341. FILE *fp;
  342. if ((fp = fopen("/proc/kinfo", "r")) == NULL) {
  343. exit(1);
  344. }
  345. //读如nr_procs,nr_tasks
  346. if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) {
  347. exit(1);
  348. }
  349. fclose(fp);
  350. //算出总的nr_total
  351. nr_total = (int) (nr_procs + nr_tasks);
  352. }
  353. int mytop(){
  354. //跳转到/proc
  355. if (chdir("/proc") != 0) {
  356. perror("chdir to /proc" );
  357. return 1;
  358. }
  359. print_memory();
  360. getkinfo();
  361. get_procs();
  362. //当前进程为空的话 就要再调用get_procs
  363. if(prev_proc==NULL)
  364. get_procs();
  365. print_procs(prev_proc,proc,1);
  366. //fflush(NULL);
  367. return 0;
  368. }