#include #include "ff_ipc.h" void usage(void) { printf("Usage:\n"); printf(" top [-p ] [-P ] [-d ] [-n \n"); } int cpu_status(struct ff_top_args *top) { int ret; struct ff_msg *msg, *retmsg = NULL; msg = ff_ipc_msg_alloc(); if (msg == NULL) { errno = ENOMEM; return -1; } msg->msg_type = FF_TOP; ret = ff_ipc_send(msg); if (ret < 0) { errno = EPIPE; ff_ipc_msg_free(msg); return -1; } do { if (retmsg != NULL) { ff_ipc_msg_free(retmsg); } ret = ff_ipc_recv(&retmsg, msg->msg_type); if (ret < 0) { errno = EPIPE; ff_ipc_msg_free(msg); return -1; } } while (msg != retmsg); *top = retmsg->top; ff_ipc_msg_free(msg); return 0; } int main(int argc, char **argv) { int ch, delay = 1, n = 0; unsigned int i, j; struct ff_top_args top = {0, 0, 0, 0, 0}, otop; struct ff_top_args ptop[RTE_MAX_LCORE], potop[RTE_MAX_LCORE];; int proc_id = 0, max_proc_id = -1; float psys, pusr, pidle; ff_ipc_init(); #define TOP_DIFF(member) (top.member - otop.member) #define TOP_ADD_P(member) (top.member += ptop[j].member - potop[j].member) #define TOP_DIFF_P(member) (ptop[j].member - potop[j].member) while ((ch = getopt(argc, argv, "hp:P:d:n:")) != -1) { switch(ch) { case 'p': proc_id = atoi(optarg); ff_set_proc_id(proc_id); break; case 'P': max_proc_id = atoi(optarg); if (max_proc_id < 0 || max_proc_id >= RTE_MAX_LCORE) { usage(); return -1; } break; case 'd': delay = atoi(optarg) ?: 1; break; case 'n': n = atoi(optarg); break; case 'h': default: usage(); return -1; } } for (i = 0; ; i++) { if (max_proc_id == -1) { if (cpu_status(&top)) { printf("fstack ipc message error !\n"); return -1; } if (i % 40 == 0) { printf("|---------|---------|---------|---------------|\n"); printf("|%9s|%9s|%9s|%15s|\n", "idle", "sys", "usr", "loop"); printf("|---------|---------|---------|---------------|\n"); } if (i) { psys = TOP_DIFF(sys_tsc) / (TOP_DIFF(work_tsc) / 100.0); pusr = TOP_DIFF(usr_tsc) / (TOP_DIFF(work_tsc) / 100.0); pidle = TOP_DIFF(idle_tsc) / (TOP_DIFF(work_tsc) / 100.0); printf("|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", pidle, psys, pusr, TOP_DIFF(loops)); } }else { /* * get and show cpu usage from proc_id to max_proc_id. */ for (j = proc_id; j <= max_proc_id; j++) { ff_set_proc_id(j); if (cpu_status(&ptop[j])) { printf("fstack ipc message error, proc id:%d!\n", j); return -1; } TOP_ADD_P(idle_tsc); TOP_ADD_P(loops); TOP_ADD_P(sys_tsc); TOP_ADD_P(usr_tsc); TOP_ADD_P(work_tsc); } if (i % (40 / (max_proc_id - proc_id + 2)) == 0) { printf("|---------|---------|---------|---------|---------------|\n"); printf("|%9s|%9s|%9s|%9s|%15s|\n", "proc_id", "idle", "sys", "usr", "loop"); printf("|---------|---------|---------|---------|---------------|\n"); } if (i) { psys = TOP_DIFF(sys_tsc) / (TOP_DIFF(work_tsc) / 100.0); pusr = TOP_DIFF(usr_tsc) / (TOP_DIFF(work_tsc) / 100.0); pidle = TOP_DIFF(idle_tsc) / (TOP_DIFF(work_tsc) / 100.0); printf("|%9s|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", "total", pidle, psys, pusr, TOP_DIFF(loops)); for (j = proc_id; j <= max_proc_id; j++) { psys = TOP_DIFF_P(sys_tsc) / (TOP_DIFF_P(work_tsc) / 100.0); pusr = TOP_DIFF_P(usr_tsc) / (TOP_DIFF_P(work_tsc) / 100.0); pidle = TOP_DIFF_P(idle_tsc) / (TOP_DIFF_P(work_tsc) / 100.0); printf("|%9d|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", j, pidle, psys, pusr, TOP_DIFF_P(loops)); } printf("| | | | | |\n"); } for (j = proc_id; j <= max_proc_id; j++) { potop[j] = ptop[j]; } } if (n && i >= n) { break; } otop = top; sleep(delay); } return 0; }