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>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_string_fns.h>
60 #include <rte_metrics.h>
62 /* Maximum long option length for option parsing. */
63 #define MAX_LONG_OPT_SZ 64
64 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
66 #define MAX_STRING_LEN 256
68 /**< mask of enabled ports */
69 static uint32_t enabled_port_mask;
71 static uint32_t enable_stats;
72 /**< Enable xstats. */
73 static uint32_t enable_xstats;
74 /**< Enable collectd format*/
75 static uint32_t enable_collectd_format;
76 /**< FD to send collectd format messages to STDOUT*/
78 /**< Host id process is running on */
79 static char host_id[MAX_LONG_OPT_SZ];
80 /**< Enable metrics. */
81 static uint32_t enable_metrics;
82 /**< Enable stats reset. */
83 static uint32_t reset_stats;
84 /**< Enable xstats reset. */
85 static uint32_t reset_xstats;
86 /**< Enable memory info. */
87 static uint32_t mem_info;
88 /**< Enable displaying xstat name. */
89 static uint32_t enable_xstats_name;
90 static char *xstats_name;
92 /**< Enable xstats by ids. */
93 #define MAX_NB_XSTATS_IDS 1024
94 static uint32_t nb_xstats_ids;
95 static uint64_t xstats_ids[MAX_NB_XSTATS_IDS];
99 proc_info_usage(const char *prgname)
101 printf("%s [EAL options] -- -p PORTMASK\n"
102 " -m to display DPDK memory zones, segments and TAILQ information\n"
103 " -p PORTMASK: hexadecimal bitmask of ports to retrieve stats for\n"
104 " --stats: to display port statistics, enabled by default\n"
105 " --xstats: to display extended port statistics, disabled by "
107 " --metrics: to display derived metrics of the ports, disabled by "
109 " --xstats-name NAME: to display single xstat id by NAME\n"
110 " --xstats-ids IDLIST: to display xstat values by id. "
111 "The argument is comma-separated list of xstat ids to print out.\n"
112 " --stats-reset: to reset port statistics\n"
113 " --xstats-reset: to reset port extended statistics\n"
114 " --collectd-format: to print statistics to STDOUT in expected by collectd format\n"
115 " --host-id STRING: host id used to identify the system process is running on\n",
120 * Parse the portmask provided at run time.
123 parse_portmask(const char *portmask)
130 /* parse hexadecimal string */
131 pm = strtoul(portmask, &end, 16);
132 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') ||
134 printf("%s ERROR parsing the port mask\n", __func__);
146 * Parse ids value list into array
149 parse_xstats_ids(char *list, uint64_t *ids, int limit) {
156 token = strtok_r(list, ",", &ctx);
157 while (token != NULL) {
158 ids[length] = strtoull(token, &endptr, 10);
166 token = strtok_r(NULL, ",", &ctx);
173 proc_info_preparse_args(int argc, char **argv)
175 char *prgname = argv[0];
178 for (i = 0; i < argc; i++) {
179 /* Print stats or xstats to STDOUT in collectd format */
180 if (!strncmp(argv[i], "--collectd-format", MAX_LONG_OPT_SZ)) {
181 enable_collectd_format = 1;
182 stdout_fd = dup(STDOUT_FILENO);
183 close(STDOUT_FILENO);
185 if (!strncmp(argv[i], "--host-id", MAX_LONG_OPT_SZ)) {
186 if ((i + 1) == argc) {
187 printf("Invalid host id or not specified\n");
188 proc_info_usage(prgname);
191 strncpy(host_id, argv[i+1], sizeof(host_id));
195 if (!strlen(host_id)) {
196 int err = gethostname(host_id, MAX_LONG_OPT_SZ-1);
199 strcpy(host_id, "unknown");
205 /* Parse the argument given in the command line of the application */
207 proc_info_parse_args(int argc, char **argv)
211 char *prgname = argv[0];
212 static struct option long_option[] = {
213 {"stats", 0, NULL, 0},
214 {"stats-reset", 0, NULL, 0},
215 {"xstats", 0, NULL, 0},
216 {"metrics", 0, NULL, 0},
217 {"xstats-reset", 0, NULL, 0},
218 {"xstats-name", required_argument, NULL, 1},
219 {"collectd-format", 0, NULL, 0},
220 {"xstats-ids", 1, NULL, 1},
221 {"host-id", 0, NULL, 0},
226 proc_info_usage(prgname);
228 /* Parse command line */
229 while ((opt = getopt_long(argc, argv, "p:m",
230 long_option, &option_index)) != EOF) {
234 enabled_port_mask = parse_portmask(optarg);
235 if (enabled_port_mask == 0) {
236 printf("invalid portmask\n");
237 proc_info_usage(prgname);
246 if (!strncmp(long_option[option_index].name, "stats",
250 else if (!strncmp(long_option[option_index].name, "xstats",
253 else if (!strncmp(long_option[option_index].name,
258 if (!strncmp(long_option[option_index].name, "stats-reset",
262 else if (!strncmp(long_option[option_index].name, "xstats-reset",
267 /* Print xstat single value given by name*/
268 if (!strncmp(long_option[option_index].name,
269 "xstats-name", MAX_LONG_OPT_SZ)) {
270 enable_xstats_name = 1;
271 xstats_name = optarg;
272 printf("name:%s:%s\n",
273 long_option[option_index].name,
275 } else if (!strncmp(long_option[option_index].name,
278 nb_xstats_ids = parse_xstats_ids(optarg,
279 xstats_ids, MAX_NB_XSTATS_IDS);
281 if (nb_xstats_ids <= 0) {
282 printf("xstats-id list parse error.\n");
289 proc_info_usage(prgname);
297 meminfo_display(void)
299 printf("----------- MEMORY_SEGMENTS -----------\n");
300 rte_dump_physmem_layout(stdout);
301 printf("--------- END_MEMORY_SEGMENTS ---------\n");
303 printf("------------ MEMORY_ZONES -------------\n");
304 rte_memzone_dump(stdout);
305 printf("---------- END_MEMORY_ZONES -----------\n");
307 printf("------------- TAIL_QUEUES -------------\n");
308 rte_dump_tailq(stdout);
309 printf("---------- END_TAIL_QUEUES ------------\n");
313 nic_stats_display(uint16_t port_id)
315 struct rte_eth_stats stats;
318 static const char *nic_stats_border = "########################";
320 rte_eth_stats_get(port_id, &stats);
321 printf("\n %s NIC statistics for port %-2d %s\n",
322 nic_stats_border, port_id, nic_stats_border);
324 printf(" RX-packets: %-10"PRIu64" RX-errors: %-10"PRIu64
325 " RX-bytes: %-10"PRIu64"\n", stats.ipackets, stats.ierrors,
327 printf(" RX-nombuf: %-10"PRIu64"\n", stats.rx_nombuf);
328 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64
329 " TX-bytes: %-10"PRIu64"\n", stats.opackets, stats.oerrors,
333 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
334 printf(" Stats reg %2d RX-packets: %-10"PRIu64
335 " RX-errors: %-10"PRIu64
336 " RX-bytes: %-10"PRIu64"\n",
337 i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
341 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
342 printf(" Stats reg %2d TX-packets: %-10"PRIu64
343 " TX-bytes: %-10"PRIu64"\n",
344 i, stats.q_opackets[i], stats.q_obytes[i]);
347 printf(" %s############################%s\n",
348 nic_stats_border, nic_stats_border);
352 nic_stats_clear(uint16_t port_id)
354 printf("\n Clearing NIC stats for port %d\n", port_id);
355 rte_eth_stats_reset(port_id);
356 printf("\n NIC statistics for port %d cleared\n", port_id);
359 static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
360 const char *cnt_name) {
361 char *type_end = strrchr(cnt_name, '_');
363 if ((type_end != NULL) &&
364 (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
365 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
366 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
367 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
368 strncpy(cnt_type, "if_rx_dropped", cnt_type_len);
369 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
370 strncpy(cnt_type, "if_rx_octets", cnt_type_len);
371 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
372 strncpy(cnt_type, "if_rx_packets", cnt_type_len);
373 else if (strncmp(type_end, "_placement",
374 strlen("_placement")) == 0)
375 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
376 else if (strncmp(type_end, "_buff", strlen("_buff")) == 0)
377 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
379 /* Does not fit obvious type: use a more generic one */
380 strncpy(cnt_type, "derive", cnt_type_len);
381 } else if ((type_end != NULL) &&
382 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
383 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
384 strncpy(cnt_type, "if_tx_errors", cnt_type_len);
385 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
386 strncpy(cnt_type, "if_tx_dropped", cnt_type_len);
387 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
388 strncpy(cnt_type, "if_tx_octets", cnt_type_len);
389 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
390 strncpy(cnt_type, "if_tx_packets", cnt_type_len);
392 /* Does not fit obvious type: use a more generic one */
393 strncpy(cnt_type, "derive", cnt_type_len);
394 } else if ((type_end != NULL) &&
395 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
396 if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
397 strncpy(cnt_type, "operations", cnt_type_len);
398 else if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
399 strncpy(cnt_type, "errors", cnt_type_len);
400 else if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
401 strncpy(cnt_type, "filter_result", cnt_type_len);
402 } else if ((type_end != NULL) &&
403 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
404 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
405 strncpy(cnt_type, "errors", cnt_type_len);
407 /* Does not fit obvious type, or strrchr error: */
408 /* use a more generic type */
409 strncpy(cnt_type, "derive", cnt_type_len);
414 nic_xstats_by_name_display(uint16_t port_id, char *name)
418 printf("###### NIC statistics for port %-2d, statistic name '%s':\n",
421 if (rte_eth_xstats_get_id_by_name(port_id, name, &id) == 0)
422 printf("%s: %"PRIu64"\n", name, id);
424 printf("Statistic not found...\n");
429 nic_xstats_by_ids_display(uint16_t port_id, uint64_t *ids, int len)
431 struct rte_eth_xstat_name *xstats_names;
434 static const char *nic_stats_border = "########################";
436 values = malloc(sizeof(*values) * len);
437 if (values == NULL) {
438 printf("Cannot allocate memory for xstats\n");
442 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
443 if (xstats_names == NULL) {
444 printf("Cannot allocate memory for xstat names\n");
449 if (len != rte_eth_xstats_get_names_by_id(
450 port_id, xstats_names, len, ids)) {
451 printf("Cannot get xstat names\n");
455 printf("###### NIC extended statistics for port %-2d #########\n",
457 printf("%s############################\n", nic_stats_border);
458 ret = rte_eth_xstats_get_by_id(port_id, ids, values, len);
459 if (ret < 0 || ret > len) {
460 printf("Cannot get xstats\n");
464 for (i = 0; i < len; i++)
465 printf("%s: %"PRIu64"\n",
466 xstats_names[i].name,
469 printf("%s############################\n", nic_stats_border);
476 nic_xstats_display(uint16_t port_id)
478 struct rte_eth_xstat_name *xstats_names;
481 static const char *nic_stats_border = "########################";
483 len = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
485 printf("Cannot get xstats count\n");
488 values = malloc(sizeof(*values) * len);
489 if (values == NULL) {
490 printf("Cannot allocate memory for xstats\n");
494 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
495 if (xstats_names == NULL) {
496 printf("Cannot allocate memory for xstat names\n");
500 if (len != rte_eth_xstats_get_names_by_id(
501 port_id, xstats_names, len, NULL)) {
502 printf("Cannot get xstat names\n");
506 printf("###### NIC extended statistics for port %-2d #########\n",
508 printf("%s############################\n",
510 ret = rte_eth_xstats_get_by_id(port_id, NULL, values, len);
511 if (ret < 0 || ret > len) {
512 printf("Cannot get xstats\n");
516 for (i = 0; i < len; i++) {
517 if (enable_collectd_format) {
518 char counter_type[MAX_STRING_LEN];
519 char buf[MAX_STRING_LEN];
521 collectd_resolve_cnt_type(counter_type,
522 sizeof(counter_type),
523 xstats_names[i].name);
524 sprintf(buf, "PUTVAL %s/dpdkstat-port.%u/%s-%s N:%"
525 PRIu64"\n", host_id, port_id, counter_type,
526 xstats_names[i].name, values[i]);
527 ret = write(stdout_fd, buf, strlen(buf));
531 printf("%s: %"PRIu64"\n", xstats_names[i].name,
536 printf("%s############################\n",
544 nic_xstats_clear(uint16_t port_id)
546 printf("\n Clearing NIC xstats for port %d\n", port_id);
547 rte_eth_xstats_reset(port_id);
548 printf("\n NIC extended statistics for port %d cleared\n", port_id);
552 metrics_display(int port_id)
554 struct rte_metric_value *metrics;
555 struct rte_metric_name *names;
557 static const char *nic_stats_border = "########################";
559 len = rte_metrics_get_names(NULL, 0);
561 printf("Cannot get metrics count\n");
565 printf("No metrics to display (none have been registered)\n");
569 metrics = rte_malloc("proc_info_metrics",
570 sizeof(struct rte_metric_value) * len, 0);
571 if (metrics == NULL) {
572 printf("Cannot allocate memory for metrics\n");
576 names = rte_malloc(NULL, sizeof(struct rte_metric_name) * len, 0);
578 printf("Cannot allocate memory for metrcis names\n");
583 if (len != rte_metrics_get_names(names, len)) {
584 printf("Cannot get metrics names\n");
590 if (port_id == RTE_METRICS_GLOBAL)
591 printf("###### Non port specific metrics #########\n");
593 printf("###### metrics for port %-2d #########\n", port_id);
594 printf("%s############################\n", nic_stats_border);
595 ret = rte_metrics_get_values(port_id, metrics, len);
596 if (ret < 0 || ret > len) {
597 printf("Cannot get metrics values\n");
604 for (i = 0; i < len; i++)
605 printf("%s: %"PRIu64"\n", names[i].name, metrics[i].value);
607 printf("%s############################\n", nic_stats_border);
613 main(int argc, char **argv)
617 char c_flag[] = "-c1";
618 char n_flag[] = "-n4";
619 char mp_flag[] = "--proc-type=secondary";
620 char *argp[argc + 3];
623 /* preparse app arguments */
624 ret = proc_info_preparse_args(argc, argv);
626 printf("Failed to parse arguments\n");
635 for (i = 1; i < argc; i++)
636 argp[i + 3] = argv[i];
640 ret = rte_eal_init(argc, argp);
642 rte_panic("Cannot init EAL\n");
647 if (!rte_eal_primary_proc_alive(NULL))
648 rte_exit(EXIT_FAILURE, "No primary DPDK process is running.\n");
650 /* parse app arguments */
651 ret = proc_info_parse_args(argc, argv);
653 rte_exit(EXIT_FAILURE, "Invalid argument\n");
660 nb_ports = rte_eth_dev_count();
662 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
664 /* If no port mask was specified*/
665 if (enabled_port_mask == 0)
666 enabled_port_mask = 0xffff;
668 for (i = 0; i < nb_ports; i++) {
669 if (enabled_port_mask & (1 << i)) {
671 nic_stats_display(i);
672 else if (enable_xstats)
673 nic_xstats_display(i);
674 else if (reset_stats)
676 else if (reset_xstats)
678 else if (enable_xstats_name)
679 nic_xstats_by_name_display(i, xstats_name);
680 else if (nb_xstats_ids > 0)
681 nic_xstats_by_ids_display(i, xstats_ids,
683 else if (enable_metrics)
688 /* print port independent stats */
690 metrics_display(RTE_METRICS_GLOBAL);