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