xref: /dpdk/app/test-mldev/test_common.c (revision fccf444cfe059fad295f4037ba221ddfee694cc0)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2022 Marvell.
3  */
4 
5 #include <errno.h>
6 
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_memory.h>
10 #include <rte_mldev.h>
11 
12 #include "ml_common.h"
13 #include "test_common.h"
14 
15 #include <fcntl.h>
16 #include <sys/mman.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 
20 int
21 ml_read_file(char *file, size_t *size, char **buffer)
22 {
23 	char *file_buffer = NULL;
24 	struct stat file_stat;
25 	char *file_map;
26 	int ret;
27 	int fd;
28 
29 	fd = open(file, O_RDONLY);
30 	if (fd == -1) {
31 		ml_err("Failed to open file: %s\n", file);
32 		return -errno;
33 	}
34 
35 	if (fstat(fd, &file_stat) != 0) {
36 		ml_err("fstat failed for file: %s\n", file);
37 		return -errno;
38 	}
39 
40 	file_buffer = malloc(file_stat.st_size);
41 	if (file_buffer == NULL) {
42 		ml_err("Failed to allocate memory: %s\n", file);
43 		ret = -ENOMEM;
44 		goto error;
45 	}
46 
47 	file_map = mmap(0, file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
48 	if (file_map == MAP_FAILED) {
49 		ml_err("Failed to map file: %s\n", file);
50 		ret = -errno;
51 		goto error;
52 	}
53 
54 	rte_memcpy(file_buffer, file_map, file_stat.st_size);
55 	munmap(file_map, file_stat.st_size);
56 	close(fd);
57 
58 	*size = file_stat.st_size;
59 	*buffer = file_buffer;
60 
61 	return 0;
62 
63 error:
64 	free(file_buffer);
65 	close(fd);
66 
67 	return ret;
68 }
69 
70 bool
71 ml_test_cap_check(struct ml_options *opt)
72 {
73 	struct rte_ml_dev_info dev_info;
74 
75 	rte_ml_dev_info_get(opt->dev_id, &dev_info);
76 	if (dev_info.max_models == 0) {
77 		ml_err("Not enough mldev models supported = %u", dev_info.max_models);
78 		return false;
79 	}
80 
81 	return true;
82 }
83 
84 int
85 ml_test_opt_check(struct ml_options *opt)
86 {
87 	uint16_t dev_count;
88 	int socket_id;
89 
90 	RTE_SET_USED(opt);
91 
92 	dev_count = rte_ml_dev_count();
93 	if (dev_count == 0) {
94 		ml_err("No ML devices found");
95 		return -ENODEV;
96 	}
97 
98 	if (opt->dev_id >= dev_count) {
99 		ml_err("Invalid option dev_id = %d", opt->dev_id);
100 		return -EINVAL;
101 	}
102 
103 	socket_id = rte_ml_dev_socket_id(opt->dev_id);
104 	if (!((opt->socket_id != SOCKET_ID_ANY) || (opt->socket_id != socket_id))) {
105 		ml_err("Invalid option, socket_id = %d\n", opt->socket_id);
106 		return -EINVAL;
107 	}
108 
109 	return 0;
110 }
111 
112 void
113 ml_test_opt_dump(struct ml_options *opt)
114 {
115 	ml_options_dump(opt);
116 }
117 
118 int
119 ml_test_device_configure(struct ml_test *test, struct ml_options *opt)
120 {
121 	struct test_common *t = ml_test_priv(test);
122 	struct rte_ml_dev_config dev_config;
123 	int ret;
124 
125 	ret = rte_ml_dev_info_get(opt->dev_id, &t->dev_info);
126 	if (ret != 0) {
127 		ml_err("Failed to get mldev info, dev_id = %d\n", opt->dev_id);
128 		return ret;
129 	}
130 
131 	/* configure device */
132 	dev_config.socket_id = opt->socket_id;
133 	dev_config.nb_models = t->dev_info.max_models;
134 	dev_config.nb_queue_pairs = opt->queue_pairs;
135 	ret = rte_ml_dev_configure(opt->dev_id, &dev_config);
136 	if (ret != 0) {
137 		ml_err("Failed to configure ml device, dev_id = %d\n", opt->dev_id);
138 		return ret;
139 	}
140 
141 	return 0;
142 }
143 
144 int
145 ml_test_device_close(struct ml_test *test, struct ml_options *opt)
146 {
147 	struct test_common *t = ml_test_priv(test);
148 	int ret = 0;
149 
150 	RTE_SET_USED(t);
151 
152 	/* close device */
153 	ret = rte_ml_dev_close(opt->dev_id);
154 	if (ret != 0)
155 		ml_err("Failed to close ML device, dev_id = %d\n", opt->dev_id);
156 
157 	return ret;
158 }
159 
160 int
161 ml_test_device_start(struct ml_test *test, struct ml_options *opt)
162 {
163 	struct test_common *t = ml_test_priv(test);
164 	int ret;
165 
166 	RTE_SET_USED(t);
167 
168 	/* start device */
169 	ret = rte_ml_dev_start(opt->dev_id);
170 	if (ret != 0) {
171 		ml_err("Failed to start ml device, dev_id = %d\n", opt->dev_id);
172 		return ret;
173 	}
174 
175 	return 0;
176 }
177 
178 int
179 ml_test_device_stop(struct ml_test *test, struct ml_options *opt)
180 {
181 	struct test_common *t = ml_test_priv(test);
182 	int ret = 0;
183 
184 	RTE_SET_USED(t);
185 
186 	/* stop device */
187 	ret = rte_ml_dev_stop(opt->dev_id);
188 	if (ret != 0)
189 		ml_err("Failed to stop ML device, dev_id = %d\n", opt->dev_id);
190 
191 	return ret;
192 }
193