4 * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/queue.h>
46 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_ethdev.h>
49 #include <rte_malloc.h>
50 #include <rte_memory.h>
51 #include <rte_memzone.h>
52 #include <rte_launch.h>
53 #include <rte_tailq.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_debug.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_string_fns.h>
61 #include <rte_metrics.h>
63 /* Maximum long option length for option parsing. */
64 #define MAX_LONG_OPT_SZ 64
65 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
67 #define MAX_STRING_LEN 256
69 /**< mask of enabled ports */
70 static uint32_t enabled_port_mask;
72 static uint32_t enable_stats;
73 /**< Enable xstats. */
74 static uint32_t enable_xstats;
75 /**< Enable collectd format*/
76 static uint32_t enable_collectd_format;
77 /**< FD to send collectd format messages to STDOUT*/
79 /**< Host id process is running on */
80 static char host_id[MAX_LONG_OPT_SZ];
81 /**< Enable metrics. */
82 static uint32_t enable_metrics;
83 /**< Enable stats reset. */
84 static uint32_t reset_stats;
85 /**< Enable xstats reset. */
86 static uint32_t reset_xstats;
87 /**< Enable memory info. */
88 static uint32_t mem_info;
89 /**< Enable displaying xstat name. */
90 static uint32_t enable_xstats_name;
91 static char *xstats_name;
93 /**< Enable xstats by ids. */
94 #define MAX_NB_XSTATS_IDS 1024
95 static uint32_t nb_xstats_ids;
96 static uint64_t xstats_ids[MAX_NB_XSTATS_IDS];
100 proc_info_usage(const char *prgname)
102 printf("%s [EAL options] -- -p PORTMASK\n"
103 " -m to display DPDK memory zones, segments and TAILQ information\n"
104 " -p PORTMASK: hexadecimal bitmask of ports to retrieve stats for\n"
105 " --stats: to display port statistics, enabled by default\n"
106 " --xstats: to display extended port statistics, disabled by "
108 " --metrics: to display derived metrics of the ports, disabled by "
110 " --xstats-name NAME: to display single xstat id by NAME\n"
111 " --xstats-ids IDLIST: to display xstat values by id. "
112 "The argument is comma-separated list of xstat ids to print out.\n"
113 " --stats-reset: to reset port statistics\n"
114 " --xstats-reset: to reset port extended statistics\n"
115 " --collectd-format: to print statistics to STDOUT in expected by collectd format\n"
116 " --host-id STRING: host id used to identify the system process is running on\n",
121 * Parse the portmask provided at run time.
124 parse_portmask(const char *portmask)
131 /* parse hexadecimal string */
132 pm = strtoul(portmask, &end, 16);
133 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') ||
135 printf("%s ERROR parsing the port mask\n", __func__);
147 * Parse ids value list into array
150 parse_xstats_ids(char *list, uint64_t *ids, int limit) {
157 token = strtok_r(list, ",", &ctx);
158 while (token != NULL) {
159 ids[length] = strtoull(token, &endptr, 10);
167 token = strtok_r(NULL, ",", &ctx);
174 proc_info_preparse_args(int argc, char **argv)
176 char *prgname = argv[0];
179 for (i = 0; i < argc; i++) {
180 /* Print stats or xstats to STDOUT in collectd format */
181 if (!strncmp(argv[i], "--collectd-format", MAX_LONG_OPT_SZ)) {
182 enable_collectd_format = 1;
183 stdout_fd = dup(STDOUT_FILENO);
184 close(STDOUT_FILENO);
186 if (!strncmp(argv[i], "--host-id", MAX_LONG_OPT_SZ)) {
187 if ((i + 1) == argc) {
188 printf("Invalid host id or not specified\n");
189 proc_info_usage(prgname);
192 strncpy(host_id, argv[i+1], sizeof(host_id));
196 if (!strlen(host_id)) {
197 int err = gethostname(host_id, MAX_LONG_OPT_SZ-1);
200 strcpy(host_id, "unknown");
206 /* Parse the argument given in the command line of the application */
208 proc_info_parse_args(int argc, char **argv)
212 char *prgname = argv[0];
213 static struct option long_option[] = {
214 {"stats", 0, NULL, 0},
215 {"stats-reset", 0, NULL, 0},
216 {"xstats", 0, NULL, 0},
217 {"metrics", 0, NULL, 0},
218 {"xstats-reset", 0, NULL, 0},
219 {"xstats-name", required_argument, NULL, 1},
220 {"collectd-format", 0, NULL, 0},
221 {"xstats-ids", 1, NULL, 1},
222 {"host-id", 0, NULL, 0},
227 proc_info_usage(prgname);
229 /* Parse command line */
230 while ((opt = getopt_long(argc, argv, "p:m",
231 long_option, &option_index)) != EOF) {
235 enabled_port_mask = parse_portmask(optarg);
236 if (enabled_port_mask == 0) {
237 printf("invalid portmask\n");
238 proc_info_usage(prgname);
247 if (!strncmp(long_option[option_index].name, "stats",
251 else if (!strncmp(long_option[option_index].name, "xstats",
254 else if (!strncmp(long_option[option_index].name,
259 if (!strncmp(long_option[option_index].name, "stats-reset",
263 else if (!strncmp(long_option[option_index].name, "xstats-reset",
268 /* Print xstat single value given by name*/
269 if (!strncmp(long_option[option_index].name,
270 "xstats-name", MAX_LONG_OPT_SZ)) {
271 enable_xstats_name = 1;
272 xstats_name = optarg;
273 printf("name:%s:%s\n",
274 long_option[option_index].name,
276 } else if (!strncmp(long_option[option_index].name,
279 nb_xstats_ids = parse_xstats_ids(optarg,
280 xstats_ids, MAX_NB_XSTATS_IDS);
282 if (nb_xstats_ids <= 0) {
283 printf("xstats-id list parse error.\n");
290 proc_info_usage(prgname);
298 meminfo_display(void)
300 printf("----------- MEMORY_SEGMENTS -----------\n");
301 rte_dump_physmem_layout(stdout);
302 printf("--------- END_MEMORY_SEGMENTS ---------\n");
304 printf("------------ MEMORY_ZONES -------------\n");
305 rte_memzone_dump(stdout);
306 printf("---------- END_MEMORY_ZONES -----------\n");
308 printf("------------- TAIL_QUEUES -------------\n");
309 rte_dump_tailq(stdout);
310 printf("---------- END_TAIL_QUEUES ------------\n");
314 nic_stats_display(uint8_t port_id)
316 struct rte_eth_stats stats;
319 static const char *nic_stats_border = "########################";
321 rte_eth_stats_get(port_id, &stats);
322 printf("\n %s NIC statistics for port %-2d %s\n",
323 nic_stats_border, port_id, nic_stats_border);
325 printf(" RX-packets: %-10"PRIu64" RX-errors: %-10"PRIu64
326 " RX-bytes: %-10"PRIu64"\n", stats.ipackets, stats.ierrors,
328 printf(" RX-nombuf: %-10"PRIu64"\n", stats.rx_nombuf);
329 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64
330 " TX-bytes: %-10"PRIu64"\n", stats.opackets, stats.oerrors,
334 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
335 printf(" Stats reg %2d RX-packets: %-10"PRIu64
336 " RX-errors: %-10"PRIu64
337 " RX-bytes: %-10"PRIu64"\n",
338 i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
342 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
343 printf(" Stats reg %2d TX-packets: %-10"PRIu64
344 " TX-bytes: %-10"PRIu64"\n",
345 i, stats.q_opackets[i], stats.q_obytes[i]);
348 printf(" %s############################%s\n",
349 nic_stats_border, nic_stats_border);
353 nic_stats_clear(uint8_t port_id)
355 printf("\n Clearing NIC stats for port %d\n", port_id);
356 rte_eth_stats_reset(port_id);
357 printf("\n NIC statistics for port %d cleared\n", port_id);
360 static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
361 const char *cnt_name) {
362 char *type_end = strrchr(cnt_name, '_');
364 if ((type_end != NULL) &&
365 (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
366 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
367 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
368 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
369 strncpy(cnt_type, "if_rx_dropped", cnt_type_len);
370 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
371 strncpy(cnt_type, "if_rx_octets", cnt_type_len);
372 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
373 strncpy(cnt_type, "if_rx_packets", cnt_type_len);
374 else if (strncmp(type_end, "_placement",
375 strlen("_placement")) == 0)
376 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
377 else if (strncmp(type_end, "_buff", strlen("_buff")) == 0)
378 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
380 /* Does not fit obvious type: use a more generic one */
381 strncpy(cnt_type, "derive", cnt_type_len);
382 } else if ((type_end != NULL) &&
383 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
384 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
385 strncpy(cnt_type, "if_tx_errors", cnt_type_len);
386 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
387 strncpy(cnt_type, "if_tx_dropped", cnt_type_len);
388 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
389 strncpy(cnt_type, "if_tx_octets", cnt_type_len);
390 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
391 strncpy(cnt_type, "if_tx_packets", cnt_type_len);
393 /* Does not fit obvious type: use a more generic one */
394 strncpy(cnt_type, "derive", cnt_type_len);
395 } else if ((type_end != NULL) &&
396 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
397 if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
398 strncpy(cnt_type, "operations", cnt_type_len);
399 else if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
400 strncpy(cnt_type, "errors", cnt_type_len);
401 else if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
402 strncpy(cnt_type, "filter_result", cnt_type_len);
403 } else if ((type_end != NULL) &&
404 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
405 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
406 strncpy(cnt_type, "errors", cnt_type_len);
408 /* Does not fit obvious type, or strrchr error: */
409 /* use a more generic type */
410 strncpy(cnt_type, "derive", cnt_type_len);
415 nic_xstats_by_name_display(uint8_t port_id, char *name)
419 printf("###### NIC statistics for port %-2d, statistic name '%s':\n",
422 if (rte_eth_xstats_get_id_by_name(port_id, name, &id) == 0)
423 printf("%s: %"PRIu64"\n", name, id);
425 printf("Statistic not found...\n");
430 nic_xstats_by_ids_display(uint8_t port_id, uint64_t *ids, int len)
432 struct rte_eth_xstat_name *xstats_names;
435 static const char *nic_stats_border = "########################";
437 values = malloc(sizeof(*values) * len);
438 if (values == NULL) {
439 printf("Cannot allocate memory for xstats\n");
443 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
444 if (xstats_names == NULL) {
445 printf("Cannot allocate memory for xstat names\n");
450 if (len != rte_eth_xstats_get_names_by_id(
451 port_id, xstats_names, len, ids)) {
452 printf("Cannot get xstat names\n");
456 printf("###### NIC extended statistics for port %-2d #########\n",
458 printf("%s############################\n", nic_stats_border);
459 ret = rte_eth_xstats_get_by_id(port_id, ids, values, len);
460 if (ret < 0 || ret > len) {
461 printf("Cannot get xstats\n");
465 for (i = 0; i < len; i++)
466 printf("%s: %"PRIu64"\n",
467 xstats_names[i].name,
470 printf("%s############################\n", nic_stats_border);
477 nic_xstats_display(uint8_t port_id)
479 struct rte_eth_xstat_name *xstats_names;
482 static const char *nic_stats_border = "########################";
484 len = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
486 printf("Cannot get xstats count\n");
489 values = malloc(sizeof(*values) * len);
490 if (values == NULL) {
491 printf("Cannot allocate memory for xstats\n");
495 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
496 if (xstats_names == NULL) {
497 printf("Cannot allocate memory for xstat names\n");
501 if (len != rte_eth_xstats_get_names_by_id(
502 port_id, xstats_names, len, NULL)) {
503 printf("Cannot get xstat names\n");
507 printf("###### NIC extended statistics for port %-2d #########\n",
509 printf("%s############################\n",
511 ret = rte_eth_xstats_get_by_id(port_id, NULL, values, len);
512 if (ret < 0 || ret > len) {
513 printf("Cannot get xstats\n");
517 for (i = 0; i < len; i++) {
518 if (enable_collectd_format) {
519 char counter_type[MAX_STRING_LEN];
520 char buf[MAX_STRING_LEN];
522 collectd_resolve_cnt_type(counter_type,
523 sizeof(counter_type),
524 xstats_names[i].name);
525 sprintf(buf, "PUTVAL %s/dpdkstat-port.%u/%s-%s N:%"
526 PRIu64"\n", host_id, port_id, counter_type,
527 xstats_names[i].name, values[i]);
528 write(stdout_fd, buf, strlen(buf));
530 printf("%s: %"PRIu64"\n", xstats_names[i].name,
535 printf("%s############################\n",
543 nic_xstats_clear(uint8_t port_id)
545 printf("\n Clearing NIC xstats for port %d\n", port_id);
546 rte_eth_xstats_reset(port_id);
547 printf("\n NIC extended statistics for port %d cleared\n", port_id);
551 metrics_display(int port_id)
553 struct rte_metric_value *metrics;
554 struct rte_metric_name *names;
556 static const char *nic_stats_border = "########################";
558 len = rte_metrics_get_names(NULL, 0);
560 printf("Cannot get metrics count\n");
564 printf("No metrics to display (none have been registered)\n");
568 metrics = rte_malloc("proc_info_metrics",
569 sizeof(struct rte_metric_value) * len, 0);
570 if (metrics == NULL) {
571 printf("Cannot allocate memory for metrics\n");
575 names = rte_malloc(NULL, sizeof(struct rte_metric_name) * len, 0);
577 printf("Cannot allocate memory for metrcis names\n");
582 if (len != rte_metrics_get_names(names, len)) {
583 printf("Cannot get metrics names\n");
589 if (port_id == RTE_METRICS_GLOBAL)
590 printf("###### Non port specific metrics #########\n");
592 printf("###### metrics for port %-2d #########\n", port_id);
593 printf("%s############################\n", nic_stats_border);
594 ret = rte_metrics_get_values(port_id, metrics, len);
595 if (ret < 0 || ret > len) {
596 printf("Cannot get metrics values\n");
603 for (i = 0; i < len; i++)
604 printf("%s: %"PRIu64"\n", names[i].name, metrics[i].value);
606 printf("%s############################\n", nic_stats_border);
612 main(int argc, char **argv)
616 char c_flag[] = "-c1";
617 char n_flag[] = "-n4";
618 char mp_flag[] = "--proc-type=secondary";
619 char *argp[argc + 3];
622 /* preparse app arguments */
623 ret = proc_info_preparse_args(argc, argv);
625 printf("Failed to parse arguments\n");
634 for (i = 1; i < argc; i++)
635 argp[i + 3] = argv[i];
639 ret = rte_eal_init(argc, argp);
641 rte_panic("Cannot init EAL\n");
646 if (!rte_eal_primary_proc_alive(NULL))
647 rte_exit(EXIT_FAILURE, "No primary DPDK process is running.\n");
649 /* parse app arguments */
650 ret = proc_info_parse_args(argc, argv);
652 rte_exit(EXIT_FAILURE, "Invalid argument\n");
659 nb_ports = rte_eth_dev_count();
661 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
663 /* If no port mask was specified*/
664 if (enabled_port_mask == 0)
665 enabled_port_mask = 0xffff;
667 for (i = 0; i < nb_ports; i++) {
668 if (enabled_port_mask & (1 << i)) {
670 nic_stats_display(i);
671 else if (enable_xstats)
672 nic_xstats_display(i);
673 else if (reset_stats)
675 else if (reset_xstats)
677 else if (enable_xstats_name)
678 nic_xstats_by_name_display(i, xstats_name);
679 else if (nb_xstats_ids > 0)
680 nic_xstats_by_ids_display(i, xstats_ids,
682 else if (enable_metrics)
687 /* print port independent stats */
689 metrics_display(RTE_METRICS_GLOBAL);