File: usnic_btl_run_tests.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (101 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (c) 2014      Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2016      IBM Corporation.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

/* A simple test runner program for the usnic BTL unit tests.  See README.test
 * for more information. */

/* for dladdr() */
#define _GNU_SOURCE

#include <assert.h>
#include <libgen.h> /* for dirname() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dlfcn.h>

#define MCA_BTL_USNIC_SO "mca_btl_usnic.so"

typedef void (*run_tests_fn_t)(void);

int main(int argc, char **argv)
{
    void *mpi_handle;
    void *usnic_handle;
    void (*run_tests)(void);
    int (*init)(int *, char ***);
    int (*finalize)(void);
    Dl_info info;
    char *libmpi_path;
    char *path;
    char *to;
    int path_len;

    mpi_handle = dlopen("lib" OMPI_LIBMPI_NAME ".so", RTLD_NOW | RTLD_GLOBAL);
    if (mpi_handle == NULL) {
        fprintf(stderr, "mpi_handle=NULL dlerror()=%s\n", dlerror());
        abort();
    }

    /* casting awfulness needed for GCC's "-pedantic" option :( */
    *(void **) (&init) = dlsym(mpi_handle, "MPI_Init");
    if (init == NULL) {
        fprintf(stderr, "init=NULL dlerror()=%s\n", dlerror());
        abort();
    }
    /* casting awfulness needed for GCC's "-pedantic" option :( */
    *(void **) (&finalize) = dlsym(mpi_handle, "MPI_Finalize");
    if (finalize == NULL) {
        fprintf(stderr, "finalize=%p dlerror()=%s\n", *(void **) (&finalize), dlerror());
        abort();
    }

    /* call MPI_Init this way to avoid build-time dependency issues */
    init(&argc, &argv);

    /* figure out where the usnic BTL shared object is relative to libmpi.so */
    if (!dladdr(*(void **) (&init), &info)) {
        fprintf(stderr, "ERROR: unable to dladdr(init,...)\n");
        abort();
    }
    libmpi_path = strdup(info.dli_fname);
    assert(libmpi_path != NULL);
    path_len = strlen(libmpi_path) + strlen("/openmpi/") + strlen(MCA_BTL_USNIC_SO);
    path = calloc(path_len + 1, 1);
    to = path;
    to = stpcpy(to, dirname(libmpi_path));
    to = stpcpy(to, "/openmpi/");
    to = stpcpy(to, MCA_BTL_USNIC_SO);

    usnic_handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
    if (usnic_handle == NULL) {
        fprintf(stderr, "usnic_handle=%p dlerror()=%s\n", (void *) usnic_handle, dlerror());
        abort();
    }

    free(libmpi_path);
    free(path);

    /* casting awfulness needed for GCC's "-pedantic" option :( */
    *(void **) (&run_tests) = dlsym(usnic_handle, BTL_USNIC_RUN_TESTS_SYMBOL);
    if (run_tests == NULL) {
        fprintf(stderr, "run_tests=%p dlerror()=%s\n", *(void **) (&run_tests), dlerror());
        abort();
    }
    run_tests();

    finalize();

    /* deliberately do not dlclose() either handle so that any valgrind stack
     * traces are more useful */

    return 0;
}