xref: /dpdk/app/test-mldev/test_common.c (revision 854f380fd3b64e63ae143e9cac5bd9c5f0e7ad3e)
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 		close(fd);
38 		return -errno;
39 	}
40 
41 	file_buffer = malloc(file_stat.st_size);
42 	if (file_buffer == NULL) {
43 		ml_err("Failed to allocate memory: %s\n", file);
44 		ret = -ENOMEM;
45 		goto error;
46 	}
47 
48 	file_map = mmap(0, file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
49 	if (file_map == MAP_FAILED) {
50 		ml_err("Failed to map file: %s\n", file);
51 		ret = -errno;
52 		goto error;
53 	}
54 
55 	rte_memcpy(file_buffer, file_map, file_stat.st_size);
56 	munmap(file_map, file_stat.st_size);
57 	close(fd);
58 
59 	*size = file_stat.st_size;
60 	*buffer = file_buffer;
61 
62 	return 0;
63 
64 error:
65 	free(file_buffer);
66 	close(fd);
67 
68 	return ret;
69 }
70 
71 bool
72 ml_test_cap_check(struct ml_options *opt)
73 {
74 	struct rte_ml_dev_info dev_info;
75 
76 	rte_ml_dev_info_get(opt->dev_id, &dev_info);
77 	if (dev_info.max_models == 0) {
78 		ml_err("Not enough mldev models supported = %u", dev_info.max_models);
79 		return false;
80 	}
81 
82 	return true;
83 }
84 
85 int
86 ml_test_opt_check(struct ml_options *opt)
87 {
88 	uint16_t dev_count;
89 	int socket_id;
90 
91 	RTE_SET_USED(opt);
92 
93 	dev_count = rte_ml_dev_count();
94 	if (dev_count == 0) {
95 		ml_err("No ML devices found");
96 		return -ENODEV;
97 	}
98 
99 	if ((opt->dev_id >= dev_count) || (opt->dev_id < 0)) {
100 		ml_err("Invalid option, dev_id = %d", opt->dev_id);
101 		return -EINVAL;
102 	}
103 
104 	socket_id = rte_ml_dev_socket_id(opt->dev_id);
105 	if ((opt->socket_id != SOCKET_ID_ANY) && (opt->socket_id != socket_id)) {
106 		ml_err("Invalid option, socket_id = %d\n", opt->socket_id);
107 		return -EINVAL;
108 	}
109 
110 	return 0;
111 }
112 
113 void
114 ml_test_opt_dump(struct ml_options *opt)
115 {
116 	ml_options_dump(opt);
117 }
118 
119 int
120 ml_test_device_configure(struct ml_test *test, struct ml_options *opt)
121 {
122 	struct test_common *t = ml_test_priv(test);
123 	struct rte_ml_dev_config dev_config;
124 	int ret;
125 
126 	ret = rte_ml_dev_info_get(opt->dev_id, &t->dev_info);
127 	if (ret != 0) {
128 		ml_err("Failed to get mldev info, dev_id = %d\n", opt->dev_id);
129 		return ret;
130 	}
131 
132 	/* configure device */
133 	dev_config.socket_id = opt->socket_id;
134 	dev_config.nb_models = t->dev_info.max_models;
135 	dev_config.nb_queue_pairs = opt->queue_pairs;
136 	ret = rte_ml_dev_configure(opt->dev_id, &dev_config);
137 	if (ret != 0) {
138 		ml_err("Failed to configure ml device, dev_id = %d\n", opt->dev_id);
139 		return ret;
140 	}
141 
142 	return 0;
143 }
144 
145 int
146 ml_test_device_close(struct ml_test *test, struct ml_options *opt)
147 {
148 	struct test_common *t = ml_test_priv(test);
149 	int ret = 0;
150 
151 	RTE_SET_USED(t);
152 
153 	/* close device */
154 	ret = rte_ml_dev_close(opt->dev_id);
155 	if (ret != 0)
156 		ml_err("Failed to close ML device, dev_id = %d\n", opt->dev_id);
157 
158 	return ret;
159 }
160 
161 int
162 ml_test_device_start(struct ml_test *test, struct ml_options *opt)
163 {
164 	struct test_common *t = ml_test_priv(test);
165 	int ret;
166 
167 	RTE_SET_USED(t);
168 
169 	/* start device */
170 	ret = rte_ml_dev_start(opt->dev_id);
171 	if (ret != 0) {
172 		ml_err("Failed to start ml device, dev_id = %d\n", opt->dev_id);
173 		return ret;
174 	}
175 
176 	return 0;
177 }
178 
179 int
180 ml_test_device_stop(struct ml_test *test, struct ml_options *opt)
181 {
182 	struct test_common *t = ml_test_priv(test);
183 	int ret = 0;
184 
185 	RTE_SET_USED(t);
186 
187 	/* stop device */
188 	ret = rte_ml_dev_stop(opt->dev_id);
189 	if (ret != 0)
190 		ml_err("Failed to stop ML device, dev_id = %d\n", opt->dev_id);
191 
192 	return ret;
193 }
194