xref: /dpdk/app/test-mldev/test_stats.c (revision 77fefa0a129e661caf0f3628f48204ed3886ab5f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2023 Marvell.
3  */
4 
5 #include "test_stats.h"
6 #include "test_inference_common.h"
7 #include "test_model_ops.h"
8 
9 int
ml_stats_get(struct ml_test * test,struct ml_options * opt,enum rte_ml_dev_xstats_mode mode,int32_t fid)10 ml_stats_get(struct ml_test *test, struct ml_options *opt, enum rte_ml_dev_xstats_mode mode,
11 	     int32_t fid)
12 {
13 	struct test_common *t = ml_test_priv(test);
14 	int32_t model_id;
15 	int ret;
16 	int i;
17 
18 	if (!opt->stats)
19 		return 0;
20 
21 	if (mode == RTE_ML_DEV_XSTATS_MODEL)
22 		model_id = ((struct test_inference *)t)->model[fid].id;
23 	else
24 		model_id = -1;
25 
26 	/* get xstats size */
27 	t->xstats_size = rte_ml_dev_xstats_names_get(opt->dev_id, mode, model_id, NULL, 0);
28 	if (t->xstats_size > 0) {
29 		/* allocate for xstats_map and values */
30 		t->xstats_map = rte_malloc(
31 			"ml_xstats_map", t->xstats_size * sizeof(struct rte_ml_dev_xstats_map), 0);
32 		if (t->xstats_map == NULL) {
33 			ret = -ENOMEM;
34 			goto error;
35 		}
36 
37 		t->xstats_values =
38 			rte_malloc("ml_xstats_values", t->xstats_size * sizeof(uint64_t), 0);
39 		if (t->xstats_values == NULL) {
40 			ret = -ENOMEM;
41 			goto error;
42 		}
43 
44 		ret = rte_ml_dev_xstats_names_get(opt->dev_id, mode, model_id, t->xstats_map,
45 						  t->xstats_size);
46 		if (ret != t->xstats_size) {
47 			printf("Unable to get xstats names, ret = %d\n", ret);
48 			ret = -1;
49 			goto error;
50 		}
51 
52 		for (i = 0; i < t->xstats_size; i++)
53 			rte_ml_dev_xstats_get(opt->dev_id, mode, model_id, &t->xstats_map[i].id,
54 					      &t->xstats_values[i], 1);
55 	}
56 
57 	/* print xstats*/
58 	printf("\n");
59 	ml_print_line(80);
60 	if (mode == RTE_ML_DEV_XSTATS_MODEL)
61 		printf(" Model Statistics: %s\n",
62 		       ((struct test_inference *)t)->model[fid].info.name);
63 	else
64 		printf(" Device Statistics\n");
65 	ml_print_line(80);
66 	for (i = 0; i < t->xstats_size; i++)
67 		printf(" %-64s = %" PRIu64 "\n", t->xstats_map[i].name, t->xstats_values[i]);
68 	ml_print_line(80);
69 
70 	rte_free(t->xstats_map);
71 	rte_free(t->xstats_values);
72 
73 	return 0;
74 
75 error:
76 	rte_free(t->xstats_map);
77 	rte_free(t->xstats_values);
78 
79 	return ret;
80 }
81 
82 int
ml_throughput_get(struct ml_test * test,struct ml_options * opt)83 ml_throughput_get(struct ml_test *test, struct ml_options *opt)
84 {
85 	struct test_inference *t = ml_test_priv(test);
86 	uint64_t total_cycles = 0;
87 	uint32_t nb_filelist;
88 	uint64_t throughput;
89 	uint64_t avg_e2e;
90 	uint32_t qp_id;
91 	uint64_t freq;
92 
93 	if (!opt->stats)
94 		return 0;
95 
96 	/* print inference throughput */
97 	if (strcmp(opt->test_name, "inference_ordered") == 0)
98 		nb_filelist = 1;
99 	else
100 		nb_filelist = opt->nb_filelist;
101 
102 	/* Print model end-to-end latency and throughput */
103 	freq = rte_get_tsc_hz();
104 	for (qp_id = 0; qp_id < RTE_MAX_LCORE; qp_id++)
105 		total_cycles += t->args[qp_id].end_cycles - t->args[qp_id].start_cycles;
106 
107 	avg_e2e = total_cycles / (opt->repetitions * nb_filelist);
108 	if (freq == 0) {
109 		printf(" %-64s = %" PRIu64 "\n", "Average End-to-End Latency (cycles)", avg_e2e);
110 	} else {
111 		avg_e2e = (avg_e2e * NS_PER_S) / freq;
112 		printf(" %-64s = %" PRIu64 "\n", "Average End-to-End Latency (ns)", avg_e2e);
113 	}
114 
115 	/* Print model throughput */
116 	if (freq == 0) {
117 		throughput = 1000000 / avg_e2e;
118 		printf(" %-64s = %" PRIu64 "\n", "Average Throughput (inferences / million cycles)",
119 		       throughput);
120 	} else {
121 		throughput = freq / avg_e2e;
122 		printf(" %-64s = %" PRIu64 "\n", "Average Throughput (inferences / second)",
123 		       throughput);
124 	}
125 
126 	ml_print_line(80);
127 
128 	return 0;
129 }
130