1 #include<unistd.h> 2 #include "ff_ipc.h" 3 4 void 5 usage(void) 6 { 7 printf("Usage:\n"); 8 printf(" top [-p <f-stack proc_id>] [-d <secs>] [-n num]\n"); 9 } 10 11 int cpu_status(struct ff_top_args *top) 12 { 13 int ret; 14 struct ff_msg *msg, *retmsg = NULL; 15 16 msg = ff_ipc_msg_alloc(); 17 if (msg == NULL) { 18 errno = ENOMEM; 19 return -1; 20 } 21 22 msg->msg_type = FF_TOP; 23 ret = ff_ipc_send(msg); 24 if (ret < 0) { 25 errno = EPIPE; 26 ff_ipc_msg_free(msg); 27 return -1; 28 } 29 30 do { 31 if (retmsg != NULL) { 32 ff_ipc_msg_free(retmsg); 33 } 34 35 ret = ff_ipc_recv(&retmsg); 36 if (ret < 0) { 37 errno = EPIPE; 38 ff_ipc_msg_free(msg); 39 return -1; 40 } 41 } while (msg != retmsg); 42 43 *top = retmsg->top; 44 45 ff_ipc_msg_free(msg); 46 47 return 0; 48 } 49 50 int main(int argc, char **argv) 51 { 52 int ch, delay = 1, n = 0; 53 unsigned int i; 54 struct ff_top_args top, otop; 55 56 ff_ipc_init(); 57 58 #define TOP_DIFF(member) (top.member - otop.member) 59 60 while ((ch = getopt(argc, argv, "hp:d:n:")) != -1) { 61 switch(ch) { 62 case 'p': 63 ff_set_proc_id(atoi(optarg)); 64 break; 65 case 'd': 66 delay = atoi(optarg) ?: 1; 67 break; 68 case 'n': 69 n = atoi(optarg); 70 break; 71 case 'h': 72 default: 73 usage(); 74 return -1; 75 } 76 } 77 78 for (i = 0; ; i++) { 79 if (cpu_status(&top)) { 80 printf("fstack ipc message error !\n"); 81 return -1; 82 } 83 84 if (i % 40 == 0) { 85 printf("|---------|---------|---------|---------------|\n"); 86 printf("|%9s|%9s|%9s|%15s|\n", "idle", "sys", "usr", "loop"); 87 printf("|---------|---------|---------|---------------|\n"); 88 } 89 90 if (i) { 91 float psys = TOP_DIFF(sys_tsc) / (TOP_DIFF(work_tsc) / 100.0); 92 float pusr = TOP_DIFF(usr_tsc) / (TOP_DIFF(work_tsc) / 100.0); 93 float pidle = TOP_DIFF(idle_tsc) / (TOP_DIFF(work_tsc) / 100.0); 94 95 printf("|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", pidle, psys, pusr, TOP_DIFF(loops)); 96 } 97 98 if (n && i >= n) { 99 break; 100 } 101 102 otop = top; 103 sleep(delay); 104 } 105 106 return 0; 107 } 108