GCC Code Coverage Report


Directory: src/
File: src/talp/backend_manager.c
Date: 2026-11-17 15:36:32
Exec Total Coverage
Lines: 44 125 35.2%
Functions: 6 11 54.5%
Branches: 28 94 29.8%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2026 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #include "talp/backend_manager.h"
21
22 #include "apis/dlb_errors.h"
23 #include "support/debug.h"
24 #include "talp/backend.h"
25 #include "talp/talp_gpu.h"
26 #include "talp/talp_hwc.h"
27
28 #include <dirent.h>
29 #include <dlfcn.h>
30 #include <libgen.h>
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35
36 // Internal Core interface that plugins may use
37 const core_api_t core_api = {
38 .abi_version = DLB_BACKEND_ABI_VERSION,
39 .struct_size = sizeof(core_api_t),
40 .gpu = {
41 .enter_runtime = talp_gpu_enter_runtime,
42 .exit_runtime = talp_gpu_exit_runtime,
43 },
44 };
45
46 // Loaded plugins. We only accept one plugin per capability
47 typedef struct plugin_t {
48 const backend_api_t *api;
49 void *handle;
50 } plugin_t;
51
52
53 /* --- GPU plguins ------------------------------------------------------------ */
54
55 // List of possible GPU backend plugin names
56 static const char *gpu_plugins[] = {
57 "cupti",
58 "rocprofiler-sdk",
59 "rocprofilerv2",
60 };
61
62 enum { num_gpu_plugins = sizeof(gpu_plugins) / sizeof(gpu_plugins[0]) };
63
64 static plugin_t loaded_gpu_plugin = {};
65
66
67 /* --- HWC plguins ------------------------------------------------------------ */
68
69 // List of possible HWC backend plugin names
70 static const char *hwc_plugins[] = {
71 "papi",
72 "perf",
73 };
74
75 enum { num_hwc_plugins = sizeof(hwc_plugins) / sizeof(hwc_plugins[0]) };
76
77 static plugin_t loaded_hwc_plugin = {};
78
79
80 /* --- Energy plugins --------------------------------------------------------- */
81
82 // List of possible energy backend plugin names
83 static const char *energy_plugins[] = {
84 "ear",
85 "seanerbus",
86 };
87
88 enum { num_energy_plugins = sizeof(energy_plugins) / sizeof(energy_plugins[0]) };
89
90 static plugin_t loaded_energy_plugin = {};
91
92
93 /* ----------------------------------------------------------------------------- */
94
95 /* Obtain full path to libdlb.so (or whatever variant is loaded) */
96 15 static const char* get_dlb_lib_path(void) {
97
98 static char dlb_lib_path[PATH_MAX] = "";
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (dlb_lib_path[0] != '\0') return dlb_lib_path;
101
102 // Get information of the dlb shared object currently loaded
103 Dl_info info;
104
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 if (dladdr((void*)__func__, &info)) {
105 15 snprintf(dlb_lib_path, sizeof(dlb_lib_path), "%s", dirname((char*)info.dli_fname));
106 } else {
107 ✗ debug_warning("dladdr failed to obtain information");
108 ✗ return NULL;
109 }
110
111 15 return dlb_lib_path;
112 }
113
114 /* Obtain DLB_LIB_DIR environment variable */
115 156 static const char* get_dlb_plugin_path(void) {
116
117 static char dlb_plugin_path[PATH_MAX] = "";
118
119
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 15 times.
156 if (dlb_plugin_path[0] != '\0') return dlb_plugin_path;
120
121 // Try env var first
122 15 const char *dlb_plugin_path_env = getenv("DLB_LIB_DIR");
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (dlb_plugin_path_env != NULL) {
124 ✗ DIR* dir = opendir(dlb_plugin_path_env);
125 ✗ if (dir) {
126 ✗ snprintf(dlb_plugin_path, sizeof(dlb_plugin_path), "%s", dlb_plugin_path_env);
127 ✗ closedir(dir);
128 } else {
129 ✗ dlb_plugin_path_env = NULL;
130 }
131 }
132
133 // Try same directoy as dlb library
134
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (dlb_plugin_path_env == NULL) {
135 15 snprintf(dlb_plugin_path, sizeof(dlb_plugin_path), "%s", get_dlb_lib_path());
136 }
137
138 15 return dlb_plugin_path;
139 }
140
141 /* Load exact plugin name, return error otherwise */
142 156 static int talp_backend_manager_load(const char* plugin_name) {
143
144 156 const char *dlb_plugin_path = get_dlb_plugin_path();
145
2/4
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 156 times.
156 if (dlb_plugin_path == NULL || dlb_plugin_path[0] == '\0') {
146 ✗ return DLB_ERR_UNKNOWN;
147 }
148
149 #ifndef DLB_TEST_LIB
150 156 const char *plugin_suffix = "";
151 156 int dlopen_flag = RTLD_LAZY;
152 #else
153 /* stub libraries contain symbols that will be called during testing,
154 * so we'll load the plugin and all symbols from its linked librarties. */
155 const char *plugin_suffix = "_test";
156 int dlopen_flag = RTLD_NOW | RTLD_GLOBAL;
157 #endif
158
159 // Load the plugin
160 char plugin_path[PATH_MAX];
161 156 int not_truncated_len = snprintf(plugin_path, PATH_MAX, "%s/libdlb_%s%s.so",
162 dlb_plugin_path, plugin_name, plugin_suffix);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (not_truncated_len >= PATH_MAX) {
164 // really unlikely, but avoids GCC's format-truncation warning
165 ✗ return DLB_ERR_UNKNOWN;
166 }
167 156 void *handle = dlopen(plugin_path, dlopen_flag);
168
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (handle == NULL) {
169 156 debug_warning("Failed to load plugin %s: %s", plugin_name, dlerror());
170 156 return DLB_ERR_UNKNOWN;
171 }
172
173 // Load the plugin public function
174 typedef backend_api_t* (*backend_get_api_func_t)(void);
175 ✗ backend_get_api_func_t get_api = (backend_get_api_func_t)dlsym(handle, "DLB_Get_Backend_API");
176 ✗ if (get_api == NULL) {
177 ✗ debug_warning("Failed to find function: %s", dlerror());
178 ✗ goto close_handle_and_error;
179 }
180
181 // Check that DLB_Get_Backend_API returns a valid API
182 ✗ const backend_api_t *backend_api = get_api();
183 ✗ if (backend_api == NULL) {
184 ✗ debug_warning("DLB_Get_Backend_API returned NULL in plugin %s", plugin_name);
185 ✗ goto close_handle_and_error;
186 }
187
188 // Check ABI version
189 ✗ if (backend_api->abi_version != DLB_BACKEND_ABI_VERSION
190 ✗ || backend_api->struct_size != sizeof(backend_api_t)) {
191 ✗ debug_warning("ABI compatibility check failed in plugin %s", plugin_name);
192 ✗ goto close_handle_and_error;
193 }
194
195 // Check that plugin return its own name
196 ✗ if (strcmp(backend_api->name, plugin_name) != 0) {
197 ✗ debug_warning("Plugin %s did not return an equivalent plugin name during initialization."
198 " Deactivating...", plugin_name);
199 ✗ goto close_handle_and_error;
200 }
201
202 // Check plugin capabilities
203 ✗ if (backend_api->capabilities.gpu) {
204 ✗ loaded_gpu_plugin.api = backend_api;
205 ✗ loaded_gpu_plugin.handle = handle;
206 ✗ } else if (backend_api->capabilities.hwc) {
207 ✗ loaded_hwc_plugin.api = backend_api;
208 ✗ loaded_hwc_plugin.handle = handle;
209 ✗ } else if (backend_api->capabilities.energy) {
210 ✗ loaded_energy_plugin.api = backend_api;
211 ✗ loaded_energy_plugin.handle = handle;
212 } else {
213 ✗ debug_warning("Unkown capabilities for plugin %s", plugin_name);
214 ✗ goto close_handle_and_error;
215 }
216
217 ✗ return DLB_SUCCESS;
218
219 ✗ close_handle_and_error:
220 ✗ dlclose(handle);
221 ✗ return DLB_ERR_UNKNOWN;
222 }
223
224 20 const backend_api_t* talp_backend_manager_load_gpu_backend(const char *name) {
225
226
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (loaded_gpu_plugin.handle == NULL) {
227
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 if (name == NULL || name[0] == '\0') {
228 // Try all possible GPU plugins until one succeeds
229
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (size_t i = 0; i < num_gpu_plugins; ++i) {
230 60 const char *p = gpu_plugins[i];
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
60 if (talp_backend_manager_load(p) == DLB_SUCCESS) {
232 ✗ break;
233 }
234 }
235 } else {
236 ✗ talp_backend_manager_load(name);
237 }
238 }
239
240 // Note that it may be NULL if no GPU plugin could be loaded
241 20 return loaded_gpu_plugin.api;
242 }
243
244 ✗ void talp_backend_manager_unload_gpu_backend(void) {
245
246 ✗ if (loaded_gpu_plugin.handle != NULL) {
247 ✗ dlclose(loaded_gpu_plugin.handle);
248 ✗ loaded_gpu_plugin = (const plugin_t){};
249 }
250 }
251
252 28 const backend_api_t* talp_backend_manager_load_hwc_backend(const char *name) {
253
254
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (loaded_hwc_plugin.handle == NULL) {
255
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 if (name == NULL || name[0] == '\0') {
256 // Try all possible HWC plugins until one succeeds
257
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
84 for (size_t i = 0; i < num_hwc_plugins; ++i) {
258 56 const char *p = hwc_plugins[i];
259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if (talp_backend_manager_load(p) == DLB_SUCCESS) {
260 ✗ break;
261 }
262 }
263 } else {
264 ✗ talp_backend_manager_load(name);
265 }
266 }
267
268 // Note that it may be NULL if no HWC plugin could be loaded
269 28 return loaded_hwc_plugin.api;
270 }
271
272 ✗ void talp_backend_manager_unload_hwc_backend(void) {
273
274 ✗ if (loaded_hwc_plugin.handle != NULL) {
275 ✗ dlclose(loaded_hwc_plugin.handle);
276 ✗ loaded_hwc_plugin = (const plugin_t){};
277 }
278 }
279
280 20 const backend_api_t* talp_backend_manager_load_energy_backend(const char *name) {
281
282
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (loaded_energy_plugin.handle == NULL) {
283
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 if (name == NULL || name[0] == '\0') {
284 // Try all possible energy plugins until one succeeds
285
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
60 for (size_t i = 0; i < num_energy_plugins; ++i) {
286 40 const char *p = energy_plugins[i];
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
40 if (talp_backend_manager_load(p) == DLB_SUCCESS) {
288 ✗ break;
289 }
290 }
291 } else {
292 ✗ talp_backend_manager_load(name);
293 }
294 }
295
296 // Note that it may be NULL if no energy plugin could be loaded
297 20 return loaded_energy_plugin.api;
298 }
299
300 ✗ void talp_backend_manager_unload_energy_backend(void) {
301
302 ✗ if (loaded_energy_plugin.handle != NULL) {
303 ✗ dlclose(loaded_energy_plugin.handle);
304 ✗ loaded_energy_plugin = (const plugin_t){};
305 }
306 }
307
308 ✗ void* talp_backend_manager_get_symbol_from_plugin(const char *symbol, const char *plugin_name) {
309
310 ✗ if (loaded_gpu_plugin.handle != NULL
311 ✗ && strcmp(loaded_gpu_plugin.api->name, plugin_name) == 0) {
312 ✗ return dlsym(loaded_gpu_plugin.handle, symbol);
313 }
314
315 ✗ if (loaded_hwc_plugin.handle != NULL
316 ✗ && strcmp(loaded_hwc_plugin.api->name, plugin_name) == 0) {
317 ✗ return dlsym(loaded_hwc_plugin.handle, symbol);
318 }
319
320 ✗ if (loaded_energy_plugin.handle != NULL
321 ✗ && strcmp(loaded_energy_plugin.api->name, plugin_name) == 0) {
322 ✗ return dlsym(loaded_energy_plugin.handle, symbol);
323 }
324
325
326 ✗ return NULL;
327 }
328
329 ✗ int talp_backend_manager_get_gpu_affinity(char *buffer, size_t buffer_size, bool full_uuid) {
330
331 // This function is only called from `dlb` utilility, so we don't expect
332 // having the plugin already loaded, but we can support it just in case
333 ✗ bool plugin_needs_loading = loaded_gpu_plugin.handle == NULL;
334
335 ✗ if (plugin_needs_loading) {
336 ✗ talp_backend_manager_load_gpu_backend(NULL);
337 }
338
339 ✗ int error = DLB_ERR_UNKNOWN;
340 ✗ if (loaded_gpu_plugin.handle != NULL
341 ✗ && loaded_gpu_plugin.api->gpu.get_uuids) {
342 ✗ int backend_error = loaded_gpu_plugin.api->gpu.get_uuids(buffer, buffer_size, full_uuid);
343 ✗ if (backend_error == DLB_BACKEND_SUCCESS) {
344 ✗ error = DLB_SUCCESS;
345 }
346 }
347
348 ✗ if (plugin_needs_loading) {
349 ✗ talp_backend_manager_unload_gpu_backend();
350 }
351
352 ✗ return error;
353 }
354