+/* *** STATS *** */
+struct cmd_stats_result {
+ cmdline_fixed_string_t stats;
+ cmdline_fixed_string_t bdf;
+ uint16_t qid;
+};
+
+static void cmd_device_stats_parsed(void *parsed_result, struct cmdline *cl,
+ __rte_unused void *data)
+{
+ struct cmd_stats_result *res = parsed_result;
+ struct rte_vdpa_device *vdev = rte_vdpa_find_device_by_name(res->bdf);
+ struct vdpa_port *vport = NULL;
+ uint32_t first, last;
+ int i;
+
+ if (!vdev) {
+ RTE_LOG(ERR, VDPA, "Invalid device: %s.\n",
+ res->bdf);
+ return;
+ }
+ for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, devcnt); i++) {
+ if (vports[i].dev == vdev) {
+ vport = &vports[i];
+ break;
+ }
+ }
+ if (!vport) {
+ RTE_LOG(ERR, VDPA, "Device %s was not created.\n", res->bdf);
+ return;
+ }
+ if (res->qid == 0xFFFF) {
+ first = 0;
+ last = rte_vhost_get_vring_num(vport->vid);
+ if (last == 0) {
+ RTE_LOG(ERR, VDPA, "Failed to get num of actual virtqs"
+ " for device %s.\n", res->bdf);
+ return;
+ }
+ last--;
+ } else {
+ first = res->qid;
+ last = res->qid;
+ }
+ if (!vport->stats_names) {
+ vport->stats_n = rte_vdpa_get_stats_names(vport->dev, NULL, 0);
+ if (vport->stats_n <= 0) {
+ RTE_LOG(ERR, VDPA, "Failed to get names number of "
+ "device %s stats.\n", res->bdf);
+ return;
+ }
+ vport->stats_names = rte_zmalloc(NULL,
+ (sizeof(*vport->stats_names) + sizeof(*vport->stats)) *
+ vport->stats_n, 0);
+ if (!vport->stats_names) {
+ RTE_LOG(ERR, VDPA, "Failed to allocate memory for stat"
+ " names of device %s.\n", res->bdf);
+ return;
+ }
+ i = rte_vdpa_get_stats_names(vport->dev, vport->stats_names,
+ vport->stats_n);
+ if (vport->stats_n != i) {
+ RTE_LOG(ERR, VDPA, "Failed to get names of device %s "
+ "stats.\n", res->bdf);
+ return;
+ }
+ vport->stats = (struct rte_vdpa_stat *)
+ (vport->stats_names + vport->stats_n);
+ }
+ cmdline_printf(cl, "\nDevice %s:\n", res->bdf);
+ for (; first <= last; first++) {
+ memset(vport->stats, 0, sizeof(*vport->stats) * vport->stats_n);
+ if (rte_vdpa_get_stats(vport->dev, (int)first, vport->stats,
+ vport->stats_n) <= 0) {
+ RTE_LOG(ERR, VDPA, "Failed to get vdpa queue statistics"
+ " for device %s qid %d.\n", res->bdf,
+ (int)first);
+ return;
+ }
+ cmdline_printf(cl, "\tVirtq %" PRIu32 ":\n", first);
+ for (i = 0; i < vport->stats_n; ++i) {
+ cmdline_printf(cl, "\t\t%-*s %-16" PRIu64 "\n",
+ RTE_VDPA_STATS_NAME_SIZE,
+ vport->stats_names[vport->stats[i].id].name,
+ vport->stats[i].value);
+ }
+ }
+}
+
+cmdline_parse_token_string_t cmd_device_stats_ =
+ TOKEN_STRING_INITIALIZER(struct cmd_stats_result, stats, "stats");
+cmdline_parse_token_string_t cmd_device_bdf =
+ TOKEN_STRING_INITIALIZER(struct cmd_stats_result, bdf, NULL);
+cmdline_parse_token_num_t cmd_queue_id =
+ TOKEN_NUM_INITIALIZER(struct cmd_stats_result, qid, UINT32);
+
+cmdline_parse_inst_t cmd_device_stats = {
+ .f = cmd_device_stats_parsed,
+ .data = NULL,
+ .help_str = "stats: show device statistics",
+ .tokens = {
+ (void *)&cmd_device_stats_,
+ (void *)&cmd_device_bdf,
+ (void *)&cmd_queue_id,
+ NULL,
+ },
+};
+