xref: /f-stack/tools/traffic/traffic.c (revision 93240043)
1 #include<unistd.h>
2 #include "ff_ipc.h"
3 
4 void
usage(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] [-s]\n");
10 }
11 
traffic_status(struct ff_traffic_args * traffic)12 int traffic_status(struct ff_traffic_args *traffic)
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_TRAFFIC;
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             return -1;
40         }
41     } while (msg != retmsg);
42 
43     *traffic = retmsg->traffic;
44 
45     ff_ipc_msg_free(msg);
46 
47     return 0;
48 }
49 
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52     int ch, delay = 1, n = 0;
53     int single = 0;
54     unsigned int i, j;
55     struct ff_traffic_args traffic = {0, 0, 0, 0}, otr;
56     struct ff_traffic_args ptraffic[RTE_MAX_LCORE], potr[RTE_MAX_LCORE];
57     int proc_id = 0, max_proc_id = -1;
58     uint64_t rxp, rxb, txp, txb;
59     uint64_t prxp, prxb, ptxp, ptxb;
60     int title_line = 40;
61 
62     ff_ipc_init();
63 
64 #define DIFF(member) (traffic.member - otr.member)
65 #define DIFF_P(member) (ptraffic[j].member - potr[j].member)
66 #define ADD_S(member) (traffic.member += ptraffic[j].member)
67 
68     while ((ch = getopt(argc, argv, "hp:P:d:n:s")) != -1) {
69         switch(ch) {
70         case 'p':
71             proc_id = atoi(optarg);
72             ff_set_proc_id(proc_id);
73             break;
74         case 'P':
75             max_proc_id = atoi(optarg);
76             if (max_proc_id < 0 || max_proc_id >= RTE_MAX_LCORE) {
77                 usage();
78                 ff_ipc_exit();
79                 return -1;
80             }
81             if (max_proc_id > title_line - 2)
82                 title_line = max_proc_id + 2;
83             break;
84         case 'd':
85             delay = atoi(optarg) ?: 1;
86             break;
87         case 'n':
88             n = atoi(optarg);
89             break;
90         case 's':
91             single = 1;
92             break;
93         case 'h':
94         default:
95             usage();
96             ff_ipc_exit();
97             return -1;
98         }
99     }
100 
101     if (single) {
102         if (max_proc_id == -1) {
103             if (traffic_status(&traffic)) {
104                 printf("fstack ipc message error !\n");
105                 ff_ipc_exit();
106                 return -1;
107             }
108 
109             printf("%lu,%lu,%lu,%lu\n", traffic.rx_packets, traffic.rx_bytes,
110                 traffic.tx_packets, traffic.tx_bytes);
111         } else {
112             for (j = proc_id; j <= max_proc_id; j++) {
113                 ff_set_proc_id(j);
114                 if (traffic_status(&ptraffic[j])) {
115                     printf("fstack ipc message error, proc id:%d!\n", j);
116                     ff_ipc_exit();
117                     return -1;
118                 }
119 
120                 printf("%9d,%20lu,%20lu,%20lu,%20lu,\n",
121                     j, ptraffic[j].rx_packets, ptraffic[j].rx_bytes,
122                     ptraffic[j].tx_packets, ptraffic[j].tx_bytes);
123 
124                 ADD_S(rx_packets);
125                 ADD_S(rx_bytes);
126                 ADD_S(tx_packets);
127                 ADD_S(tx_bytes);
128             }
129 
130             printf("%9s,%20lu,%20lu,%20lu,%20lu,\n",
131                 "total", traffic.rx_packets, traffic.rx_bytes,
132                 traffic.tx_packets, traffic.tx_bytes);
133         }
134         ff_ipc_exit();
135         return 0;
136     }
137 
138     for (i = 0; ; i++) {
139         if (max_proc_id == -1) {
140             if (traffic_status(&traffic)) {
141                 printf("fstack ipc message error !\n");
142                 ff_ipc_exit();
143                 return -1;
144             }
145 
146             if (i % title_line == 0) {
147                 printf("|--------------------|--------------------|");
148                 printf("--------------------|--------------------|\n");
149                 printf("|%20s|%20s|%20s|%20s|\n", "rx packets", "rx bytes",
150                     "tx packets", "tx bytes");
151                 printf("|--------------------|--------------------|");
152                 printf("--------------------|--------------------|\n");
153             }
154 
155             if (i) {
156                 rxp = DIFF(rx_packets);
157                 rxb = DIFF(rx_bytes);
158                 txp = DIFF(tx_packets);
159                 txb = DIFF(tx_bytes);
160 
161                 printf("|%20lu|%20lu|%20lu|%20lu|\n", rxp, rxb, txp, txb);
162             }
163         } else {
164             /*
165              * get and show traffic from proc_id to max_proc_id.
166              */
167             if (i % (title_line / (max_proc_id - proc_id + 2)) == 0) {
168                 printf("|---------|--------------------|--------------------|"
169                     "--------------------|--------------------|\n");
170                 printf("|%9s|%20s|%20s|%20s|%20s|\n",
171                     "proc_id", "rx packets", "rx bytes",
172                     "tx packets", "tx bytes");
173                 printf("|---------|--------------------|--------------------|"
174                     "--------------------|--------------------|\n");
175             }
176 
177             rxp = rxb = txp = txb = 0;
178             for (j = proc_id; j <= max_proc_id; j++) {
179                 potr[j] = ptraffic[j];
180 
181                 ff_set_proc_id(j);
182                 if (traffic_status(&ptraffic[j])) {
183                     printf("fstack ipc message error, proc id:%d!\n", j);
184                     ff_ipc_exit();
185                     return -1;
186                 }
187 
188                 if (i) {
189                     prxp = DIFF_P(rx_packets);
190                     prxb = DIFF_P(rx_bytes);
191                     ptxp = DIFF_P(tx_packets);
192                     ptxb = DIFF_P(tx_bytes);
193                     printf("|%9d|%20lu|%20lu|%20lu|%20lu|\n",
194                         j, prxp, prxb, ptxp, ptxb);
195 
196                     rxp += prxp;
197                     rxb += prxb;
198                     txp += ptxp;
199                     txb += ptxb;
200 
201                     if (j == max_proc_id) {
202                         printf("|%9s|%20lu|%20lu|%20lu|%20lu|\n",
203                             "total", rxp, rxb, txp, txb);
204                         printf("|         |                    |"
205                             "                    |                    |"
206                             "                    |\n");
207                     }
208                 }
209             }
210         }
211 
212         if (n && i >= n) {
213             break;
214         }
215 
216         otr = traffic;
217         sleep(delay);
218     }
219 
220     ff_ipc_exit();
221     return 0;
222 }
223