1 /**
2  * Copyright (C) Mellanox Technologies Ltd. 2019.  ALL RIGHTS RESERVED.
3  *
4  * See file LICENSE for terms.
5  */
6 
7 #ifdef HAVE_CONFIG_H
8 #  include "config.h"
9 #endif
10 
11 #include <sys/mman.h>
12 #include <dlfcn.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 
17 #include <ucp/api/ucp.h>
18 
19 #define _QUOTE(x) #x
20 #define QUOTE(x) _QUOTE(x)
21 
22 
test_ucp_init(void * handle)23 int test_ucp_init(void *handle)
24 {
25     typedef ucs_status_t (*ucp_init_version_func_t)(unsigned, unsigned,
26                                                     const ucp_params_t *,
27                                                     const ucp_config_t *,
28                                                     ucp_context_h *);
29     typedef void (*ucp_context_print_info_func_t)(const ucp_context_h, FILE*);
30     typedef void (*ucp_cleanup_func_t)(ucp_context_h);
31 
32     ucp_init_version_func_t ucp_init_version_f;
33     ucp_context_print_info_func_t ucp_context_print_info_f;
34     ucp_cleanup_func_t ucp_cleanup_f;
35     ucp_params_t ucp_params;
36     ucs_status_t status;
37     ucp_context_h ucph;
38 
39     ucp_init_version_f       = (ucp_init_version_func_t)dlsym(handle,
40                                                               "ucp_init_version");
41     ucp_cleanup_f            = (ucp_cleanup_func_t)dlsym(handle, "ucp_cleanup");
42     ucp_context_print_info_f = (ucp_context_print_info_func_t)dlsym(handle,
43                                                                     "ucp_context_print_info");
44 
45     if (!ucp_init_version_f || !ucp_cleanup_f || !ucp_context_print_info_f) {
46         fprintf(stderr, "failed to get UCP function pointers\n");
47         return -1;
48     }
49 
50     ucp_params.field_mask = UCP_PARAM_FIELD_FEATURES;
51     ucp_params.features   = UCP_FEATURE_RMA;
52     status = ucp_init_version_f(UCP_API_MAJOR, UCP_API_MINOR, &ucp_params,
53                                 NULL, &ucph);
54     if (status != UCS_OK) {
55         fprintf(stderr, "ucp_init_version() failed\n");
56         return -1;
57     }
58 
59     ucp_context_print_info_f(ucph, stdout);
60     ucp_cleanup_f(ucph);
61 
62     return 0;
63 }
64 
main(int argc,char ** argv)65 int main(int argc, char **argv)
66 {
67     const char *filename = QUOTE(LIB_PATH);
68     void *handle;
69     void *ptr1, *ptr2;
70     size_t alloc_size;
71     long ret;
72 
73     /* get page size */
74     ret = sysconf(_SC_PAGESIZE);
75     if (ret < 0) {
76         fprintf(stderr, "sysconf(_SC_PAGESIZE) failed: %m\n");
77         return -1;
78     }
79     alloc_size = ret;
80 
81     /* allocate some memory */
82     ptr1 = malloc(alloc_size);
83     if (!ptr1) {
84         fprintf(stderr, "malloc() failed\n");
85         return -1;
86     }
87 
88     ptr2 = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE,
89                 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
90     if (ptr2 == MAP_FAILED) {
91         fprintf(stderr, "mmmap() failed: %m\n");
92         ret = -1;
93         goto failed_mmap;
94     }
95 
96     /* load ucp */
97     printf("opening '%s'\n", filename);
98     handle = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
99     if (handle == NULL) {
100         fprintf(stderr, "failed to open %s: %m\n", filename);
101         ret = -1;
102         goto failed_dlopen;
103     }
104 
105     /* init ucp */
106     ret = test_ucp_init(handle);
107 
108     /* unload ucp */
109     dlclose(handle);
110 
111 failed_dlopen:
112     /* relase the memory - could break if UCM is unloaded */
113     munmap(ptr2, alloc_size);
114 failed_mmap:
115     free(ptr1);
116 
117     printf("done\n");
118     return ret;
119 }
120 
121