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 ff_ipc_exit(); 78 return -1; 79 } 80 break; 81 case 'd': 82 delay = atoi(optarg) ?: 1; 83 break; 84 case 'n': 85 n = atoi(optarg); 86 break; 87 case 'h': 88 default: 89 usage(); 90 ff_ipc_exit(); 91 return -1; 92 } 93 } 94 95 for (i = 0; ; i++) { 96 if (max_proc_id == -1) { 97 if (cpu_status(&top)) { 98 printf("fstack ipc message error !\n"); 99 ff_ipc_exit(); 100 return -1; 101 } 102 103 if (i % 40 == 0) { 104 printf("|---------|---------|---------|---------------|\n"); 105 printf("|%9s|%9s|%9s|%15s|\n", "idle", "sys", "usr", "loop"); 106 printf("|---------|---------|---------|---------------|\n"); 107 } 108 109 if (i) { 110 sys = TOP_DIFF(sys_tsc) / (TOP_DIFF(work_tsc) / 100.0); 111 usr = TOP_DIFF(usr_tsc) / (TOP_DIFF(work_tsc) / 100.0); 112 idle = TOP_DIFF(idle_tsc) / (TOP_DIFF(work_tsc) / 100.0); 113 114 printf("|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", 115 idle, sys, usr, TOP_DIFF(loops)); 116 } 117 }else { 118 /* 119 * get and show cpu usage from proc_id to max_proc_id. 120 */ 121 if (i % (40 / (max_proc_id - proc_id + 2)) == 0) { 122 printf("|---------|---------|---------|" 123 "---------|---------------|\n"); 124 printf("|%9s|%9s|%9s|%9s|%15s|\n", 125 "proc_id", "idle", "sys", "usr", "loop"); 126 printf("|---------|---------|---------|" 127 "---------|---------------|\n"); 128 } 129 130 sys = usr = idle = loops = 0; 131 for (j = proc_id; j <= max_proc_id; j++) { 132 potop[j] = ptop[j]; 133 134 ff_set_proc_id(j); 135 if (cpu_status(&ptop[j])) { 136 printf("fstack ipc message error, proc id:%d!\n", j); 137 ff_ipc_exit(); 138 return -1; 139 } 140 141 if (i) { 142 psys = TOP_DIFF_P(sys_tsc) / \ 143 (TOP_DIFF_P(work_tsc) / 100.0); 144 pusr = TOP_DIFF_P(usr_tsc) / \ 145 (TOP_DIFF_P(work_tsc) / 100.0); 146 pidle = TOP_DIFF_P(idle_tsc) / \ 147 (TOP_DIFF_P(work_tsc) / 100.0); 148 ploops = TOP_DIFF_P(loops); 149 printf("|%9d|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", 150 j, pidle, psys, pusr, ploops); 151 152 sys += psys; 153 usr += pusr; 154 idle += pidle; 155 loops += ploops; 156 157 if (j == max_proc_id) { 158 printf("|%9s|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", 159 "total", idle, sys, usr, loops); 160 printf("| | | |" 161 " | |\n"); 162 } 163 } 164 } 165 } 166 167 if (n && i >= n) { 168 break; 169 } 170 171 otop = top; 172 sleep(delay); 173 } 174 175 ff_ipc_exit(); 176 177 return 0; 178 } 179