You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 lines
11 KiB

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