1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2 /*
3 * Copyright (c) 2004-2008 The Trustees of Indiana University and Indiana
4 * University Research and Technology
5 * Corporation. All rights reserved.
6 * Copyright (c) 2004-2005 The University of Tennessee and The University
7 * of Tennessee Research Foundation. All rights
8 * reserved.
9 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
10 * University of Stuttgart. All rights reserved.
11 * Copyright (c) 2004-2005 The Regents of the University of California.
12 * All rights reserved.
13 * Copyright (c) 2007-2015 Cisco Systems, Inc. All rights reserved.
14 * Copyright (c) 2010-2015 Los Alamos National Security, LLC.
15 * All rights reserved.
16 * Copyright (c) 2011 NVIDIA Corporation. All rights reserved.
17 * Copyright (c) 2016 Intel, Inc. All rights reserved.
18 * $COPYRIGHT$
19 *
20 * Additional copyrights may follow
21 *
22 * $HEADER$
23 *
24 * These symbols are in a file by themselves to provide nice linker
25 * semantics. Since linkers generally pull in symbols by object
26 * files, keeping these symbols as the only symbols in this file
27 * prevents utility programs such as "ompi_info" from having to import
28 * entire components just to query their version and parameters.
29 */
30
31 #include "opal_config.h"
32
33
34 #include <errno.h>
35 #include <string.h>
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif /* HAVE_SYS_MMAN_H */
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif /* HAVE_UNISTD_H */
42 #ifdef HAVE_NETDB_H
43 #include <netdb.h>
44 #endif /* HAVE_NETDB_H */
45
46 #include "opal/constants.h"
47 #include "opal/util/show_help.h"
48 #include "opal/util/output.h"
49 #include "opal/mca/shmem/base/base.h"
50 #include "opal/mca/shmem/shmem.h"
51 #include "shmem_posix.h"
52 #include "shmem_posix_common_utils.h"
53
54 /* public string showing the shmem ompi_posix component version number */
55 const char *opal_shmem_posix_component_version_string =
56 "OPAL posix shmem MCA component version " OPAL_VERSION;
57
58 /* local functions */
59 static int posix_register (void);
60 static int posix_open(void);
61 static int posix_query(mca_base_module_t **module, int *priority);
62 static int posix_runtime_query(mca_base_module_t **module,
63 int *priority,
64 const char *hint);
65
66 /* local variables */
67 static bool rt_successful = false;
68
69 /* instantiate the public struct with all of our public information
70 * and pointers to our public functions in it
71 */
72 opal_shmem_posix_component_t mca_shmem_posix_component = {
73 .super = {
74 .base_version = {
75 OPAL_SHMEM_BASE_VERSION_2_0_0,
76
77 /* component name and version */
78 .mca_component_name = "posix",
79 MCA_BASE_MAKE_VERSION(component, OPAL_MAJOR_VERSION, OPAL_MINOR_VERSION,
80 OPAL_RELEASE_VERSION),
81
82 .mca_open_component = posix_open,
83 .mca_query_component = posix_query,
84 .mca_register_component_params = posix_register
85 },
86 /* MCA v2.0.0 component meta data */
87 .base_data = {
88 /* the component is checkpoint ready */
89 MCA_BASE_METADATA_PARAM_CHECKPOINT
90 },
91 .runtime_query = posix_runtime_query,
92 },
93 };
94
95
96 /* ////////////////////////////////////////////////////////////////////////// */
97 static int
98 posix_register(void)
99 {
100 /* ////////////////////////////////////////////////////////////////////// */
101 /* (default) priority - set lower than mmap's priority */
102 mca_shmem_posix_component.priority = 40;
103 (void) mca_base_component_var_register(&mca_shmem_posix_component.super.base_version,
104 "priority", "Priority for the shmem posix "
105 "component (default: 40)", MCA_BASE_VAR_TYPE_INT,
106 NULL, 0, MCA_BASE_VAR_FLAG_SETTABLE,
107 OPAL_INFO_LVL_3,
108 MCA_BASE_VAR_SCOPE_ALL_EQ,
109 &mca_shmem_posix_component.priority);
110
111 return OPAL_SUCCESS;
112 }
113
114 /* ////////////////////////////////////////////////////////////////////////// */
115
116 static int
117 posix_open(void)
118 {
119 return OPAL_SUCCESS;
120 }
121
122 /* ////////////////////////////////////////////////////////////////////////// */
123 /**
124 * this routine performs a test that indicates whether or not posix shared
125 * memory can safely be used during this run.
126 * note: that we want to run this test as few times as possible.
127 *
128 * @return OPAL_SUCCESS when posix can safely be used.
129 */
130 static int
131 posix_runtime_query(mca_base_module_t **module,
132 int *priority,
133 const char *hint)
134 {
135 char tmp_buff[OPAL_SHMEM_POSIX_FILE_LEN_MAX];
136 int fd = -1;
137
138 *priority = 0;
139 *module = NULL;
140
141 /* if hint isn't null, then someone else already figured out who is the
142 * best runnable component is AND the caller is relaying that info so we
143 * don't have to perform a run-time query.
144 */
145 if (NULL != hint) {
146 OPAL_OUTPUT_VERBOSE(
147 (70, opal_shmem_base_framework.framework_output,
148 "shmem: posix: runtime_query: "
149 "attempting to use runtime hint (%s)\n", hint)
150 );
151 /* was i selected? if so, then we are done.
152 * otherwise, disqualify myself.
153 */
154 if (0 == strcasecmp(hint,
155 mca_shmem_posix_component.super.base_version.mca_component_name)) {
156 *priority = mca_shmem_posix_component.priority;
157 *module = (mca_base_module_t *)&opal_shmem_posix_module.super;
158 return OPAL_SUCCESS;
159 }
160 else {
161 *priority = 0;
162 *module = NULL;
163 return OPAL_SUCCESS;
164 }
165 }
166 /* if we are here, then perform a run-time query because we didn't get a
167 * hint. it's either up to us to figure it out, or the caller wants us to
168 * re-run the runtime query.
169 */
170 OPAL_OUTPUT_VERBOSE(
171 (70, opal_shmem_base_framework.framework_output,
172 "shmem: posix: runtime_query: NO HINT PROVIDED:"
173 "starting run-time test...\n")
174 );
175 /* shmem_posix_shm_open successfully shm_opened - we can use posix sm! */
176 if (-1 != (fd = shmem_posix_shm_open(tmp_buff,
177 OPAL_SHMEM_POSIX_FILE_LEN_MAX -1))) {
178 /* free up allocated resources before we return */
179 if (0 != shm_unlink(tmp_buff)) {
180 int err = errno;
181 char hn[OPAL_MAXHOSTNAMELEN];
182 gethostname(hn, sizeof(hn));
183 opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1,
184 hn, "shm_unlink(2)", "", strerror(err), err);
185 /* something strange happened, so consider this a run-time test
186 * failure even though shmem_posix_shm_open was successful */
187 }
188 /* all is well */
189 else {
190 *priority = mca_shmem_posix_component.priority;
191 *module = (mca_base_module_t *)&opal_shmem_posix_module.super;
192 rt_successful = true;
193 }
194 }
195
196 return OPAL_SUCCESS;
197 }
198
199 /* ////////////////////////////////////////////////////////////////////////// */
200 static int
201 posix_query(mca_base_module_t **module, int *priority)
202 {
203 *priority = mca_shmem_posix_component.priority;
204 *module = (mca_base_module_t *)&opal_shmem_posix_module.super;
205 return OPAL_SUCCESS;
206 }
207