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>] [-P <max proc_id>] " 9 "[-d <secs>] [-n <num>]\n"); 10 } 11 12 int cpu_status(struct ff_top_args *top) 13 { 14 int ret; 15 struct ff_msg *msg, *retmsg = NULL; 16 17 msg = ff_ipc_msg_alloc(); 18 if (msg == NULL) { 19 errno = ENOMEM; 20 return -1; 21 } 22 23 msg->msg_type = FF_TOP; 24 ret = ff_ipc_send(msg); 25 if (ret < 0) { 26 errno = EPIPE; 27 ff_ipc_msg_free(msg); 28 return -1; 29 } 30 31 do { 32 if (retmsg != NULL) { 33 ff_ipc_msg_free(retmsg); 34 } 35 36 ret = ff_ipc_recv(&retmsg, msg->msg_type); 37 if (ret < 0) { 38 errno = EPIPE; 39 ff_ipc_msg_free(msg); 40 return -1; 41 } 42 } while (msg != retmsg); 43 44 *top = retmsg->top; 45 46 ff_ipc_msg_free(msg); 47 48 return 0; 49 } 50 51 int main(int argc, char **argv) 52 { 53 int ch, delay = 1, n = 0; 54 unsigned int i, j; 55 struct ff_top_args top, otop; 56 struct ff_top_args ptop[RTE_MAX_LCORE], potop[RTE_MAX_LCORE]; 57 int proc_id = 0, max_proc_id = -1; 58 float sys, usr, idle; 59 float psys, pusr, pidle; 60 unsigned long loops, ploops; 61 62 ff_ipc_init(); 63 64 #define TOP_DIFF(member) (top.member - otop.member) 65 #define TOP_DIFF_P(member) (ptop[j].member - potop[j].member) 66 67 while ((ch = getopt(argc, argv, "hp:P:d:n:")) != -1) { 68 switch(ch) { 69 case 'p': 70 proc_id = atoi(optarg); 71 ff_set_proc_id(proc_id); 72 break; 73 case 'P': 74 max_proc_id = atoi(optarg); 75 if (max_proc_id < 0 || max_proc_id >= RTE_MAX_LCORE) { 76 usage(); 77 return -1; 78 } 79 break; 80 case 'd': 81 delay = atoi(optarg) ?: 1; 82 break; 83 case 'n': 84 n = atoi(optarg); 85 break; 86 case 'h': 87 default: 88 usage(); 89 return -1; 90 } 91 } 92 93 for (i = 0; ; i++) { 94 if (max_proc_id == -1) { 95 if (cpu_status(&top)) { 96 printf("fstack ipc message error !\n"); 97 return -1; 98 } 99 100 if (i % 40 == 0) { 101 printf("|---------|---------|---------|---------------|\n"); 102 printf("|%9s|%9s|%9s|%15s|\n", "idle", "sys", "usr", "loop"); 103 printf("|---------|---------|---------|---------------|\n"); 104 } 105 106 if (i) { 107 sys = TOP_DIFF(sys_tsc) / (TOP_DIFF(work_tsc) / 100.0); 108 usr = TOP_DIFF(usr_tsc) / (TOP_DIFF(work_tsc) / 100.0); 109 idle = TOP_DIFF(idle_tsc) / (TOP_DIFF(work_tsc) / 100.0); 110 111 printf("|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", 112 idle, sys, usr, TOP_DIFF(loops)); 113 } 114 }else { 115 /* 116 * get and show cpu usage from proc_id to max_proc_id. 117 */ 118 if (i % (40 / (max_proc_id - proc_id + 2)) == 0) { 119 printf("|---------|---------|---------|" 120 "---------|---------------|\n"); 121 printf("|%9s|%9s|%9s|%9s|%15s|\n", 122 "proc_id", "idle", "sys", "usr", "loop"); 123 printf("|---------|---------|---------|" 124 "---------|---------------|\n"); 125 } 126 127 sys = usr = idle = loops = 0; 128 for (j = proc_id; j <= max_proc_id; j++) { 129 potop[j] = ptop[j]; 130 131 ff_set_proc_id(j); 132 if (cpu_status(&ptop[j])) { 133 printf("fstack ipc message error, proc id:%d!\n", j); 134 return -1; 135 } 136 137 if (i) { 138 psys = TOP_DIFF_P(sys_tsc) / \ 139 (TOP_DIFF_P(work_tsc) / 100.0); 140 pusr = TOP_DIFF_P(usr_tsc) / \ 141 (TOP_DIFF_P(work_tsc) / 100.0); 142 pidle = TOP_DIFF_P(idle_tsc) / \ 143 (TOP_DIFF_P(work_tsc) / 100.0); 144 ploops = TOP_DIFF_P(loops); 145 printf("|%9d|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", 146 j, pidle, psys, pusr, ploops); 147 148 sys += psys; 149 usr += pusr; 150 idle += pidle; 151 loops += ploops; 152 153 if (j == max_proc_id) { 154 printf("|%9s|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", 155 "total", idle, sys, usr, loops); 156 printf("| | | |" 157 " | |\n"); 158 } 159 } 160 } 161 } 162 163 if (n && i >= n) { 164 break; 165 } 166 167 otop = top; 168 sleep(delay); 169 } 170 171 return 0; 172 } 173