xref: /f-stack/tools/top/top.c (revision d02fa78d)
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>] [-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, msg->msg_type);
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, j;
54     struct ff_top_args top = {0, 0, 0, 0, 0}, otop;
55     struct ff_top_args ptop[RTE_MAX_LCORE], potop[RTE_MAX_LCORE];;
56     int proc_id = 0, max_proc_id = -1;
57     float psys, pusr, pidle;
58 
59     ff_ipc_init();
60 
61 #define TOP_DIFF(member) (top.member - otop.member)
62 #define TOP_ADD_P(member) (top.member += ptop[j].member - potop[j].member)
63 #define TOP_DIFF_P(member) (ptop[j].member - potop[j].member)
64 
65     while ((ch = getopt(argc, argv, "hp:P:d:n:")) != -1) {
66         switch(ch) {
67         case 'p':
68             proc_id = atoi(optarg);
69             ff_set_proc_id(proc_id);
70             break;
71         case 'P':
72             max_proc_id = atoi(optarg);
73             if (max_proc_id < 0 || max_proc_id >= RTE_MAX_LCORE) {
74                 usage();
75                 return -1;
76             }
77             break;
78         case 'd':
79             delay = atoi(optarg) ?: 1;
80             break;
81         case 'n':
82             n = atoi(optarg);
83             break;
84         case 'h':
85         default:
86             usage();
87             return -1;
88         }
89     }
90 
91     for (i = 0; ; i++) {
92         if (max_proc_id == -1) {
93             if (cpu_status(&top)) {
94                 printf("fstack ipc message error !\n");
95                 return -1;
96             }
97 
98             if (i % 40 == 0) {
99                 printf("|---------|---------|---------|---------------|\n");
100                 printf("|%9s|%9s|%9s|%15s|\n", "idle", "sys", "usr", "loop");
101                 printf("|---------|---------|---------|---------------|\n");
102             }
103 
104             if (i) {
105                 psys = TOP_DIFF(sys_tsc) / (TOP_DIFF(work_tsc) / 100.0);
106                 pusr = TOP_DIFF(usr_tsc) / (TOP_DIFF(work_tsc) / 100.0);
107                 pidle = TOP_DIFF(idle_tsc) / (TOP_DIFF(work_tsc) / 100.0);
108 
109                 printf("|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", pidle, psys, pusr, TOP_DIFF(loops));
110             }
111         }else {
112             /*
113              * get and show cpu usage from proc_id to max_proc_id.
114              */
115             for (j = proc_id; j <= max_proc_id; j++) {
116                 ff_set_proc_id(j);
117                 if (cpu_status(&ptop[j])) {
118                     printf("fstack ipc message error, proc id:%d!\n", j);
119                     return -1;
120                 }
121 
122                 TOP_ADD_P(idle_tsc);
123                 TOP_ADD_P(loops);
124                 TOP_ADD_P(sys_tsc);
125                 TOP_ADD_P(usr_tsc);
126                 TOP_ADD_P(work_tsc);
127             }
128 
129             if (i % (40 / (max_proc_id - proc_id + 2)) == 0) {
130                 printf("|---------|---------|---------|---------|---------------|\n");
131                 printf("|%9s|%9s|%9s|%9s|%15s|\n", "proc_id", "idle", "sys", "usr", "loop");
132                 printf("|---------|---------|---------|---------|---------------|\n");
133             }
134 
135             if (i) {
136                 psys = TOP_DIFF(sys_tsc) / (TOP_DIFF(work_tsc) / 100.0);
137                 pusr = TOP_DIFF(usr_tsc) / (TOP_DIFF(work_tsc) / 100.0);
138                 pidle = TOP_DIFF(idle_tsc) / (TOP_DIFF(work_tsc) / 100.0);
139                 printf("|%9s|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", "total", pidle, psys, pusr, TOP_DIFF(loops));
140 
141                 for (j = proc_id; j <= max_proc_id; j++) {
142                     psys = TOP_DIFF_P(sys_tsc) / (TOP_DIFF_P(work_tsc) / 100.0);
143                     pusr = TOP_DIFF_P(usr_tsc) / (TOP_DIFF_P(work_tsc) / 100.0);
144                     pidle = TOP_DIFF_P(idle_tsc) / (TOP_DIFF_P(work_tsc) / 100.0);
145                     printf("|%9d|%8.2f%%|%8.2f%%|%8.2f%%|%15lu|\n", j, pidle, psys, pusr, TOP_DIFF_P(loops));
146                 }
147                 printf("|         |         |         |         |               |\n");
148             }
149 
150             for (j = proc_id; j <= max_proc_id; j++) {
151                 potop[j] = ptop[j];
152             }
153         }
154 
155         if (n && i >= n) {
156             break;
157         }
158 
159         otop = top;
160         sleep(delay);
161     }
162 
163     return 0;
164 }
165