1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
11 #include <sys/queue.h>
17 #include <rte_common.h>
18 #include <rte_debug.h>
19 #include <rte_ethdev.h>
20 #include <rte_malloc.h>
21 #include <rte_memory.h>
22 #include <rte_memzone.h>
23 #include <rte_launch.h>
24 #include <rte_tailq.h>
25 #include <rte_per_lcore.h>
26 #include <rte_lcore.h>
28 #include <rte_atomic.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_string_fns.h>
31 #include <rte_metrics.h>
33 /* Maximum long option length for option parsing. */
34 #define MAX_LONG_OPT_SZ 64
35 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
37 #define MAX_STRING_LEN 256
39 /**< mask of enabled ports */
40 static uint32_t enabled_port_mask;
42 static uint32_t enable_stats;
43 /**< Enable xstats. */
44 static uint32_t enable_xstats;
45 /**< Enable collectd format*/
46 static uint32_t enable_collectd_format;
47 /**< FD to send collectd format messages to STDOUT*/
49 /**< Host id process is running on */
50 static char host_id[MAX_LONG_OPT_SZ];
51 /**< Enable metrics. */
52 static uint32_t enable_metrics;
53 /**< Enable stats reset. */
54 static uint32_t reset_stats;
55 /**< Enable xstats reset. */
56 static uint32_t reset_xstats;
57 /**< Enable memory info. */
58 static uint32_t mem_info;
59 /**< Enable displaying xstat name. */
60 static uint32_t enable_xstats_name;
61 static char *xstats_name;
63 /**< Enable xstats by ids. */
64 #define MAX_NB_XSTATS_IDS 1024
65 static uint32_t nb_xstats_ids;
66 static uint64_t xstats_ids[MAX_NB_XSTATS_IDS];
70 proc_info_usage(const char *prgname)
72 printf("%s [EAL options] -- -p PORTMASK\n"
73 " -m to display DPDK memory zones, segments and TAILQ information\n"
74 " -p PORTMASK: hexadecimal bitmask of ports to retrieve stats for\n"
75 " --stats: to display port statistics, enabled by default\n"
76 " --xstats: to display extended port statistics, disabled by "
78 " --metrics: to display derived metrics of the ports, disabled by "
80 " --xstats-name NAME: to display single xstat id by NAME\n"
81 " --xstats-ids IDLIST: to display xstat values by id. "
82 "The argument is comma-separated list of xstat ids to print out.\n"
83 " --stats-reset: to reset port statistics\n"
84 " --xstats-reset: to reset port extended statistics\n"
85 " --collectd-format: to print statistics to STDOUT in expected by collectd format\n"
86 " --host-id STRING: host id used to identify the system process is running on\n",
91 * Parse the portmask provided at run time.
94 parse_portmask(const char *portmask)
101 /* parse hexadecimal string */
102 pm = strtoul(portmask, &end, 16);
103 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') ||
105 printf("%s ERROR parsing the port mask\n", __func__);
117 * Parse ids value list into array
120 parse_xstats_ids(char *list, uint64_t *ids, int limit) {
127 token = strtok_r(list, ",", &ctx);
128 while (token != NULL) {
129 ids[length] = strtoull(token, &endptr, 10);
137 token = strtok_r(NULL, ",", &ctx);
144 proc_info_preparse_args(int argc, char **argv)
146 char *prgname = argv[0];
149 for (i = 0; i < argc; i++) {
150 /* Print stats or xstats to STDOUT in collectd format */
151 if (!strncmp(argv[i], "--collectd-format", MAX_LONG_OPT_SZ)) {
152 enable_collectd_format = 1;
153 stdout_fd = dup(STDOUT_FILENO);
154 close(STDOUT_FILENO);
156 if (!strncmp(argv[i], "--host-id", MAX_LONG_OPT_SZ)) {
157 if ((i + 1) == argc) {
158 printf("Invalid host id or not specified\n");
159 proc_info_usage(prgname);
162 snprintf(host_id, sizeof(host_id), "%s", argv[i+1]);
166 if (!strlen(host_id)) {
167 int err = gethostname(host_id, MAX_LONG_OPT_SZ-1);
170 strcpy(host_id, "unknown");
176 /* Parse the argument given in the command line of the application */
178 proc_info_parse_args(int argc, char **argv)
182 char *prgname = argv[0];
183 static struct option long_option[] = {
184 {"stats", 0, NULL, 0},
185 {"stats-reset", 0, NULL, 0},
186 {"xstats", 0, NULL, 0},
187 {"metrics", 0, NULL, 0},
188 {"xstats-reset", 0, NULL, 0},
189 {"xstats-name", required_argument, NULL, 1},
190 {"collectd-format", 0, NULL, 0},
191 {"xstats-ids", 1, NULL, 1},
192 {"host-id", 0, NULL, 0},
197 proc_info_usage(prgname);
199 /* Parse command line */
200 while ((opt = getopt_long(argc, argv, "p:m",
201 long_option, &option_index)) != EOF) {
205 enabled_port_mask = parse_portmask(optarg);
206 if (enabled_port_mask == 0) {
207 printf("invalid portmask\n");
208 proc_info_usage(prgname);
217 if (!strncmp(long_option[option_index].name, "stats",
221 else if (!strncmp(long_option[option_index].name, "xstats",
224 else if (!strncmp(long_option[option_index].name,
229 if (!strncmp(long_option[option_index].name, "stats-reset",
233 else if (!strncmp(long_option[option_index].name, "xstats-reset",
238 /* Print xstat single value given by name*/
239 if (!strncmp(long_option[option_index].name,
240 "xstats-name", MAX_LONG_OPT_SZ)) {
241 enable_xstats_name = 1;
242 xstats_name = optarg;
243 printf("name:%s:%s\n",
244 long_option[option_index].name,
246 } else if (!strncmp(long_option[option_index].name,
249 nb_xstats_ids = parse_xstats_ids(optarg,
250 xstats_ids, MAX_NB_XSTATS_IDS);
252 if (nb_xstats_ids <= 0) {
253 printf("xstats-id list parse error.\n");
260 proc_info_usage(prgname);
268 meminfo_display(void)
270 printf("----------- MEMORY_SEGMENTS -----------\n");
271 rte_dump_physmem_layout(stdout);
272 printf("--------- END_MEMORY_SEGMENTS ---------\n");
274 printf("------------ MEMORY_ZONES -------------\n");
275 rte_memzone_dump(stdout);
276 printf("---------- END_MEMORY_ZONES -----------\n");
278 printf("------------- TAIL_QUEUES -------------\n");
279 rte_dump_tailq(stdout);
280 printf("---------- END_TAIL_QUEUES ------------\n");
284 nic_stats_display(uint16_t port_id)
286 struct rte_eth_stats stats;
289 static const char *nic_stats_border = "########################";
291 rte_eth_stats_get(port_id, &stats);
292 printf("\n %s NIC statistics for port %-2d %s\n",
293 nic_stats_border, port_id, nic_stats_border);
295 printf(" RX-packets: %-10"PRIu64" RX-errors: %-10"PRIu64
296 " RX-bytes: %-10"PRIu64"\n", stats.ipackets, stats.ierrors,
298 printf(" RX-nombuf: %-10"PRIu64"\n", stats.rx_nombuf);
299 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64
300 " TX-bytes: %-10"PRIu64"\n", stats.opackets, stats.oerrors,
304 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
305 printf(" Stats reg %2d RX-packets: %-10"PRIu64
306 " RX-errors: %-10"PRIu64
307 " RX-bytes: %-10"PRIu64"\n",
308 i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
312 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
313 printf(" Stats reg %2d TX-packets: %-10"PRIu64
314 " TX-bytes: %-10"PRIu64"\n",
315 i, stats.q_opackets[i], stats.q_obytes[i]);
318 printf(" %s############################%s\n",
319 nic_stats_border, nic_stats_border);
323 nic_stats_clear(uint16_t port_id)
325 printf("\n Clearing NIC stats for port %d\n", port_id);
326 rte_eth_stats_reset(port_id);
327 printf("\n NIC statistics for port %d cleared\n", port_id);
330 static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
331 const char *cnt_name) {
332 char *type_end = strrchr(cnt_name, '_');
334 if ((type_end != NULL) &&
335 (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
336 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
337 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
338 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
339 strncpy(cnt_type, "if_rx_dropped", cnt_type_len);
340 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
341 strncpy(cnt_type, "if_rx_octets", cnt_type_len);
342 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
343 strncpy(cnt_type, "if_rx_packets", cnt_type_len);
344 else if (strncmp(type_end, "_placement",
345 strlen("_placement")) == 0)
346 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
347 else if (strncmp(type_end, "_buff", strlen("_buff")) == 0)
348 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
350 /* Does not fit obvious type: use a more generic one */
351 strncpy(cnt_type, "derive", cnt_type_len);
352 } else if ((type_end != NULL) &&
353 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
354 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
355 strncpy(cnt_type, "if_tx_errors", cnt_type_len);
356 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
357 strncpy(cnt_type, "if_tx_dropped", cnt_type_len);
358 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
359 strncpy(cnt_type, "if_tx_octets", cnt_type_len);
360 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
361 strncpy(cnt_type, "if_tx_packets", cnt_type_len);
363 /* Does not fit obvious type: use a more generic one */
364 strncpy(cnt_type, "derive", cnt_type_len);
365 } else if ((type_end != NULL) &&
366 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
367 if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
368 strncpy(cnt_type, "operations", cnt_type_len);
369 else if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
370 strncpy(cnt_type, "errors", cnt_type_len);
371 else if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
372 strncpy(cnt_type, "filter_result", cnt_type_len);
373 } else if ((type_end != NULL) &&
374 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
375 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
376 strncpy(cnt_type, "errors", cnt_type_len);
378 /* Does not fit obvious type, or strrchr error: */
379 /* use a more generic type */
380 strncpy(cnt_type, "derive", cnt_type_len);
385 nic_xstats_by_name_display(uint16_t port_id, char *name)
389 printf("###### NIC statistics for port %-2d, statistic name '%s':\n",
392 if (rte_eth_xstats_get_id_by_name(port_id, name, &id) == 0)
393 printf("%s: %"PRIu64"\n", name, id);
395 printf("Statistic not found...\n");
400 nic_xstats_by_ids_display(uint16_t port_id, uint64_t *ids, int len)
402 struct rte_eth_xstat_name *xstats_names;
405 static const char *nic_stats_border = "########################";
407 values = malloc(sizeof(*values) * len);
408 if (values == NULL) {
409 printf("Cannot allocate memory for xstats\n");
413 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
414 if (xstats_names == NULL) {
415 printf("Cannot allocate memory for xstat names\n");
420 if (len != rte_eth_xstats_get_names_by_id(
421 port_id, xstats_names, len, ids)) {
422 printf("Cannot get xstat names\n");
426 printf("###### NIC extended statistics for port %-2d #########\n",
428 printf("%s############################\n", nic_stats_border);
429 ret = rte_eth_xstats_get_by_id(port_id, ids, values, len);
430 if (ret < 0 || ret > len) {
431 printf("Cannot get xstats\n");
435 for (i = 0; i < len; i++)
436 printf("%s: %"PRIu64"\n",
437 xstats_names[i].name,
440 printf("%s############################\n", nic_stats_border);
447 nic_xstats_display(uint16_t port_id)
449 struct rte_eth_xstat_name *xstats_names;
452 static const char *nic_stats_border = "########################";
454 len = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
456 printf("Cannot get xstats count\n");
459 values = malloc(sizeof(*values) * len);
460 if (values == NULL) {
461 printf("Cannot allocate memory for xstats\n");
465 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
466 if (xstats_names == NULL) {
467 printf("Cannot allocate memory for xstat names\n");
471 if (len != rte_eth_xstats_get_names_by_id(
472 port_id, xstats_names, len, NULL)) {
473 printf("Cannot get xstat names\n");
477 printf("###### NIC extended statistics for port %-2d #########\n",
479 printf("%s############################\n",
481 ret = rte_eth_xstats_get_by_id(port_id, NULL, values, len);
482 if (ret < 0 || ret > len) {
483 printf("Cannot get xstats\n");
487 for (i = 0; i < len; i++) {
488 if (enable_collectd_format) {
489 char counter_type[MAX_STRING_LEN];
490 char buf[MAX_STRING_LEN];
492 collectd_resolve_cnt_type(counter_type,
493 sizeof(counter_type),
494 xstats_names[i].name);
495 sprintf(buf, "PUTVAL %s/dpdkstat-port.%u/%s-%s N:%"
496 PRIu64"\n", host_id, port_id, counter_type,
497 xstats_names[i].name, values[i]);
498 ret = write(stdout_fd, buf, strlen(buf));
502 printf("%s: %"PRIu64"\n", xstats_names[i].name,
507 printf("%s############################\n",
515 nic_xstats_clear(uint16_t port_id)
517 printf("\n Clearing NIC xstats for port %d\n", port_id);
518 rte_eth_xstats_reset(port_id);
519 printf("\n NIC extended statistics for port %d cleared\n", port_id);
523 metrics_display(int port_id)
525 struct rte_metric_value *metrics;
526 struct rte_metric_name *names;
528 static const char *nic_stats_border = "########################";
530 len = rte_metrics_get_names(NULL, 0);
532 printf("Cannot get metrics count\n");
536 printf("No metrics to display (none have been registered)\n");
540 metrics = rte_malloc("proc_info_metrics",
541 sizeof(struct rte_metric_value) * len, 0);
542 if (metrics == NULL) {
543 printf("Cannot allocate memory for metrics\n");
547 names = rte_malloc(NULL, sizeof(struct rte_metric_name) * len, 0);
549 printf("Cannot allocate memory for metrcis names\n");
554 if (len != rte_metrics_get_names(names, len)) {
555 printf("Cannot get metrics names\n");
561 if (port_id == RTE_METRICS_GLOBAL)
562 printf("###### Non port specific metrics #########\n");
564 printf("###### metrics for port %-2d #########\n", port_id);
565 printf("%s############################\n", nic_stats_border);
566 ret = rte_metrics_get_values(port_id, metrics, len);
567 if (ret < 0 || ret > len) {
568 printf("Cannot get metrics values\n");
575 for (i = 0; i < len; i++)
576 printf("%s: %"PRIu64"\n", names[i].name, metrics[i].value);
578 printf("%s############################\n", nic_stats_border);
584 main(int argc, char **argv)
588 char c_flag[] = "-c1";
589 char n_flag[] = "-n4";
590 char mp_flag[] = "--proc-type=secondary";
591 char *argp[argc + 3];
594 /* preparse app arguments */
595 ret = proc_info_preparse_args(argc, argv);
597 printf("Failed to parse arguments\n");
606 for (i = 1; i < argc; i++)
607 argp[i + 3] = argv[i];
611 ret = rte_eal_init(argc, argp);
613 rte_panic("Cannot init EAL\n");
618 if (!rte_eal_primary_proc_alive(NULL))
619 rte_exit(EXIT_FAILURE, "No primary DPDK process is running.\n");
621 /* parse app arguments */
622 ret = proc_info_parse_args(argc, argv);
624 rte_exit(EXIT_FAILURE, "Invalid argument\n");
631 nb_ports = rte_eth_dev_count_avail();
633 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
635 /* If no port mask was specified*/
636 if (enabled_port_mask == 0)
637 enabled_port_mask = 0xffff;
639 RTE_ETH_FOREACH_DEV(i) {
640 if (enabled_port_mask & (1 << i)) {
642 nic_stats_display(i);
643 else if (enable_xstats)
644 nic_xstats_display(i);
645 else if (reset_stats)
647 else if (reset_xstats)
649 else if (enable_xstats_name)
650 nic_xstats_by_name_display(i, xstats_name);
651 else if (nb_xstats_ids > 0)
652 nic_xstats_by_ids_display(i, xstats_ids,
654 else if (enable_metrics)
659 /* print port independent stats */
661 metrics_display(RTE_METRICS_GLOBAL);
663 ret = rte_eal_cleanup();
665 printf("Error from rte_eal_cleanup(), %d\n", ret);