2017-08-22 09:12:53 +00:00
|
|
|
#include<unistd.h>
|
|
|
|
#include "ff_ipc.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage:\n");
|
2019-07-25 10:50:07 +00:00
|
|
|
printf(" top [-p <f-stack proc_id>] [-P <max proc_id>] [-d <secs>] [-n <num]>\n");
|
2017-08-22 09:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-25 09:19:28 +00:00
|
|
|
ret = ff_ipc_recv(&retmsg, msg->msg_type);
|
2017-08-22 09:12:53 +00:00
|
|
|
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;
|
2019-07-25 10:50:07 +00:00
|
|
|
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;
|
2017-08-22 09:12:53 +00:00
|
|
|
|
2017-09-06 02:16:53 +00:00
|
|
|
ff_ipc_init();
|
|
|
|
|
2017-08-22 09:12:53 +00:00
|
|
|
#define TOP_DIFF(member) (top.member - otop.member)
|
2019-07-25 10:50:07 +00:00
|
|
|
#define TOP_ADD_P(member) (top.member += ptop[j].member - potop[j].member)
|
|
|
|
#define TOP_DIFF_P(member) (ptop[j].member - potop[j].member)
|
2017-08-22 09:12:53 +00:00
|
|
|
|
2019-07-25 10:50:07 +00:00
|
|
|
while ((ch = getopt(argc, argv, "hp:P:d:n:")) != -1) {
|
2017-08-22 09:12:53 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'p':
|
2019-07-25 10:50:07 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-08-22 09:12:53 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
delay = atoi(optarg) ?: 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
n = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
2019-07-25 10:50:07 +00:00
|
|
|
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];
|
|
|
|
}
|
2017-08-22 09:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n && i >= n) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
otop = top;
|
|
|
|
sleep(delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|