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