forked from cornelisnetworks/opa-psm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psm.c
539 lines (454 loc) · 14.5 KB
/
psm.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
This file is provided under a dual BSD/GPLv2 license. When using or
redistributing this file, you may do so under either license.
GPL LICENSE SUMMARY
Copyright(c) 2015 Intel Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Contact Information:
Intel Corporation, www.intel.com
BSD LICENSE
Copyright(c) 2015 Intel Corporation.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2003-2014 Intel Corporation. All rights reserved. */
#include <dlfcn.h>
#include "psm_user.h"
static int psmi_verno_major = PSM2_VERNO_MAJOR;
static int psmi_verno_minor = PSM2_VERNO_MINOR;
static int psmi_verno = PSMI_VERNO_MAKE(PSM2_VERNO_MAJOR, PSM2_VERNO_MINOR);
static int psmi_verno_client_val;
#define PSMI_NOT_INITIALIZED 0
#define PSMI_INITIALIZED 1
#define PSMI_FINALIZED -1 /* Prevent the user from calling psm2_init
* once psm_finalize has been called. */
static int psmi_isinit = PSMI_NOT_INITIALIZED;
int psmi_verno_client()
{
return psmi_verno_client_val;
}
#ifdef PSMI_PLOCK_IS_SPINLOCK
psmi_spinlock_t psmi_progress_lock;
#elif defined(PSMI_PLOCK_IS_MUTEXLOCK)
pthread_mutex_t psmi_progress_lock = PTHREAD_MUTEX_INITIALIZER;
#elif defined(PSMI_PLOCK_IS_MUTEXLOCK_DEBUG)
pthread_mutex_t psmi_progress_lock = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
pthread_t psmi_progress_lock_owner = PSMI_PLOCK_NO_OWNER;
#endif
/* This function is used to determine whether the current library build can
* successfully communicate with another library that claims to be version
* 'verno'.
*
* PSM 2.x is always ABI compatible, but this checks to see if two different
* versions of the library can coexist.
*/
int psmi_verno_isinteroperable(uint16_t verno)
{
if (PSMI_VERNO_GET_MAJOR(verno) != PSM2_VERNO_MAJOR)
return 0;
return 1;
}
int psmi_isinitialized()
{
return (psmi_isinit == PSMI_INITIALIZED);
}
extern char psmi_hfi_revision[];
psm2_error_t __psm2_init(int *major, int *minor)
{
psm2_error_t err = PSM2_OK;
union psmi_envvar_val env_tmask;
PSM2_LOG_MSG("entering");
if (psmi_isinit == PSMI_INITIALIZED)
goto update;
if (psmi_isinit == PSMI_FINALIZED) {
err = PSM2_IS_FINALIZED;
goto fail;
}
if (major == NULL || minor == NULL) {
err = PSM2_PARAM_ERR;
goto fail;
}
#ifdef PSM_DEBUG
if (!getenv("PSM2_NO_WARN"))
fprintf(stderr,
"!!! WARNING !!! You are running an internal-only PSM *DEBUG* build.\n");
#endif
#ifdef PSM_PROFILE
if (!getenv("PSM2_NO_WARN"))
fprintf(stderr,
"!!! WARNING !!! You are running an internal-only PSM *PROFILE* build.\n");
#endif
/* Make sure we complain if fault injection is enabled */
if (getenv("PSM2_FI") && !getenv("PSM2_NO_WARN"))
fprintf(stderr,
"!!! WARNING !!! You are running with fault injection enabled!\n");
/* Make sure, as an internal check, that this version knows how to detect
* compatibility with other library versions it may communicate with */
if (psmi_verno_isinteroperable(psmi_verno) != 1) {
err = psmi_handle_error(PSMI_EP_NORETURN, PSM2_INTERNAL_ERR,
"psmi_verno_isinteroperable() not updated for current version!");
goto fail;
}
/* The only way to not support a client is if the major number doesn't
* match */
if (*major != PSM2_VERNO_MAJOR && *major != PSM2_VERNO_COMPAT_MAJOR) {
err = psmi_handle_error(NULL, PSM2_INIT_BAD_API_VERSION,
"This library does not implement version %d.%d",
*major, *minor);
goto fail;
}
/* Make sure we don't keep track of a client that claims a higher version
* number than we are */
psmi_verno_client_val =
min(PSMI_VERNO_MAKE(*major, *minor), psmi_verno);
/* Check to see if we need to set Architecture flags to something
* besides big core Xeons */
cpuid_t id;
psmi_cpu_model = CPUID_MODEL_UNDEFINED;
/* First check to ensure Genuine Intel */
get_cpuid(0x0, 0, &id);
if(id.ebx == CPUID_GENUINE_INTEL_EBX
&& id.ecx == CPUID_GENUINE_INTEL_ECX
&& id.edx == CPUID_GENUINE_INTEL_EDX)
{
/* Use cpuid with EAX=1 to get processor info */
get_cpuid(0x1, 0, &id);
psmi_cpu_model = CPUID_GENUINE_INTEL;
}
if( (psmi_cpu_model == CPUID_GENUINE_INTEL) &&
(id.eax & CPUID_FAMILY_MASK) == CPUID_FAMILY_XEON)
{
psmi_cpu_model = ((id.eax & CPUID_MODEL_MASK) >> 4) |
((id.eax & CPUID_EXMODEL_MASK) >> 12);
}
psmi_isinit = PSMI_INITIALIZED;
/* hfi_debug lives in libhfi.so */
psmi_getenv("PSM2_TRACEMASK",
"Mask flags for tracing",
PSMI_ENVVAR_LEVEL_USER,
PSMI_ENVVAR_TYPE_ULONG_FLAGS,
(union psmi_envvar_val)hfi_debug, &env_tmask);
hfi_debug = (long)env_tmask.e_ulong;
/* The "real thing" is done in hfi_proto.c as a constructor function, but
* we getenv it here to report what we're doing with the setting */
{
extern int __hfi_malloc_no_mmap;
union psmi_envvar_val env_mmap;
char *env = getenv("HFI_DISABLE_MMAP_MALLOC");
int broken = (env && *env && !__hfi_malloc_no_mmap);
psmi_getenv("HFI_DISABLE_MMAP_MALLOC",
broken ? "Skipping mmap disable for malloc()" :
"Disable mmap for malloc()",
PSMI_ENVVAR_LEVEL_USER,
PSMI_ENVVAR_TYPE_YESNO,
(union psmi_envvar_val)0, &env_mmap);
if (broken)
_HFI_ERROR
("Couldn't successfully disable mmap in mallocs "
"with mallopt()\n");
}
if (getenv("PSM2_IDENTIFY")) {
Dl_info info_psm, info_hfi;
_HFI_INFO("PSM2: %s version: %d.%d, from %s:%s\n", psmi_hfi_revision,
PSM2_VERNO_MAJOR,PSM2_VERNO_MINOR,
dladdr(psm2_init, &info_psm) ? info_psm.dli_fname :
"libpsm2 not available",
dladdr(hfi_userinit, &info_hfi) ? info_hfi.dli_fname :
"libhfi not available");
}
#ifdef PSMI_PLOCK_IS_SPINLOCK
psmi_spin_init(&psmi_progress_lock);
#endif
if (getenv("PSM2_DIAGS")) {
_HFI_INFO("Running diags...\n");
psmi_diags();
}
psmi_faultinj_init();
/* Initialize the unexpected system buffer allocator */
err = psmi_sysbuf_init();
if (err != PSM2_OK)
goto fail;
psmi_epid_init();
update:
*major = (int)psmi_verno_major;
*minor = (int)psmi_verno_minor;
fail:
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_init)
psm2_error_t __psm2_finalize(void)
{
struct psmi_eptab_iterator itor;
char *hostname;
psm2_ep_t ep;
extern psm2_ep_t psmi_opened_endpoint; /* in psm_endpoint.c */
PSM2_LOG_MSG("entering");
PSMI_ERR_UNLESS_INITIALIZED(NULL);
ep = psmi_opened_endpoint;
while (ep != NULL) {
psmi_opened_endpoint = ep->user_ep_next;
psm2_ep_close(ep, PSM2_EP_CLOSE_GRACEFUL,
2 * PSMI_MIN_EP_CLOSE_TIMEOUT);
ep = psmi_opened_endpoint;
}
psmi_epid_fini();
psmi_faultinj_fini();
/* De-allocate memory for any allocated space to store hostnames */
psmi_epid_itor_init(&itor, PSMI_EP_HOSTNAME);
while ((hostname = psmi_epid_itor_next(&itor)))
psmi_free(hostname);
psmi_epid_itor_fini(&itor);
char buf[128];
psmi_sysbuf_getinfo(buf, sizeof(buf));
_HFI_VDBG("%s", buf);
psmi_sysbuf_fini();
psmi_isinit = PSMI_FINALIZED;
PSM2_LOG_MSG("leaving");
return PSM2_OK;
}
PSMI_API_DECL(psm2_finalize)
/*
* Function exposed in >= 1.05
*/
psm2_error_t
__psm2_map_nid_hostname(int num, const uint64_t *nids, const char **hostnames)
{
int i;
psm2_error_t err = PSM2_OK;
PSM2_LOG_MSG("entering");
PSMI_ERR_UNLESS_INITIALIZED(NULL);
PSMI_PLOCK();
if (nids == NULL || hostnames == NULL) {
err = PSM2_PARAM_ERR;
goto fail;
}
for (i = 0; i < num; i++) {
if ((err = psmi_epid_set_hostname(nids[i], hostnames[i], 1)))
break;
}
fail:
PSMI_PUNLOCK();
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_map_nid_hostname)
void __psm2_epaddr_setlabel(psm2_epaddr_t epaddr, char const *epaddr_label)
{
PSM2_LOG_MSG("entering");
PSM2_LOG_MSG("leaving");
return; /* ignore this function */
}
PSMI_API_DECL(psm2_epaddr_setlabel)
void __psm2_epaddr_setctxt(psm2_epaddr_t epaddr, void *ctxt)
{
/* Eventually deprecate this API to use set/get opt as this is unsafe. */
PSM2_LOG_MSG("entering");
psm2_setopt(PSM2_COMPONENT_CORE, (const void *)epaddr,
PSM2_CORE_OPT_EP_CTXT, (const void *)ctxt, sizeof(void *));
PSM2_LOG_MSG("leaving");
}
PSMI_API_DECL(psm2_epaddr_setctxt)
void *__psm2_epaddr_getctxt(psm2_epaddr_t epaddr)
{
psm2_error_t err;
uint64_t optlen = sizeof(void *);
void *result = NULL;
PSM2_LOG_MSG("entering");
/* Evetually deprecate this API to use set/get opt as this is unsafe. */
err = psm2_getopt(PSM2_COMPONENT_CORE, (const void *)epaddr,
PSM2_CORE_OPT_EP_CTXT, (void *)&result, &optlen);
PSM2_LOG_MSG("leaving");
if (err == PSM2_OK)
return result;
else
return NULL;
}
PSMI_API_DECL(psm2_epaddr_getctxt)
psm2_error_t
__psm2_setopt(psm2_component_t component, const void *component_obj,
int optname, const void *optval, uint64_t optlen)
{
psm2_error_t rv;
PSM2_LOG_MSG("entering");
switch (component) {
case PSM2_COMPONENT_CORE:
rv = psmi_core_setopt(component_obj, optname, optval, optlen);
PSM2_LOG_MSG("leaving");
return rv;
break;
case PSM2_COMPONENT_MQ:
/* Use the deprecated MQ set/get opt for now which does not use optlen */
rv = psm2_mq_setopt((psm2_mq_t) component_obj, optname, optval);
PSM2_LOG_MSG("leaving");
return rv;
break;
case PSM2_COMPONENT_AM:
/* Hand off to active messages */
rv = psmi_am_setopt(component_obj, optname, optval, optlen);
PSM2_LOG_MSG("leaving");
return rv;
break;
case PSM2_COMPONENT_IB:
/* Hand off to IPS ptl to set option */
rv = psmi_ptl_ips.setopt(component_obj, optname, optval,
optlen);
PSM2_LOG_MSG("leaving");
return rv;
break;
}
/* Unrecognized/unknown component */
rv = psmi_handle_error(NULL, PSM2_PARAM_ERR, "Unknown component %u",
component);
PSM2_LOG_MSG("leaving");
return rv;
}
PSMI_API_DECL(psm2_setopt);
psm2_error_t
__psm2_getopt(psm2_component_t component, const void *component_obj,
int optname, void *optval, uint64_t *optlen)
{
psm2_error_t rv;
PSM2_LOG_MSG("entering");
switch (component) {
case PSM2_COMPONENT_CORE:
rv = psmi_core_getopt(component_obj, optname, optval, optlen);
PSM2_LOG_MSG("leaving");
return rv;
break;
case PSM2_COMPONENT_MQ:
/* Use the deprecated MQ set/get opt for now which does not use optlen */
rv = psm2_mq_getopt((psm2_mq_t) component_obj, optname, optval);
PSM2_LOG_MSG("leaving");
return rv;
break;
case PSM2_COMPONENT_AM:
/* Hand off to active messages */
rv = psmi_am_getopt(component_obj, optname, optval, optlen);
PSM2_LOG_MSG("leaving");
return rv;
break;
case PSM2_COMPONENT_IB:
/* Hand off to IPS ptl to set option */
rv = psmi_ptl_ips.getopt(component_obj, optname, optval,
optlen);
PSM2_LOG_MSG("leaving");
return rv;
break;
}
/* Unrecognized/unknown component */
rv = psmi_handle_error(NULL, PSM2_PARAM_ERR, "Unknown component %u",
component);
PSM2_LOG_MSG("leaving");
return rv;
}
PSMI_API_DECL(psm2_getopt);
psm2_error_t __psmi_poll_noop(ptl_t *ptl, int replyonly)
{
PSM2_LOG_MSG("entering");
PSM2_LOG_MSG("leaving");
return PSM2_OK_NO_PROGRESS;
}
PSMI_API_DECL(psmi_poll_noop)
psm2_error_t __psm2_poll(psm2_ep_t ep)
{
psm2_error_t err1 = PSM2_OK, err2 = PSM2_OK;
psm2_ep_t tmp;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
PSMI_PLOCK();
tmp = ep;
do {
err1 = ep->ptl_amsh.ep_poll(ep->ptl_amsh.ptl, 0); /* poll reqs & reps */
if (err1 > PSM2_OK_NO_PROGRESS) { /* some error unrelated to polling */
PSMI_PUNLOCK();
PSM2_LOG_MSG("leaving");
return err1;
}
err2 = ep->ptl_ips.ep_poll(ep->ptl_ips.ptl, 0); /* get into ips_do_work */
if (err2 > PSM2_OK_NO_PROGRESS) { /* some error unrelated to polling */
PSMI_PUNLOCK();
PSM2_LOG_MSG("leaving");
return err2;
}
ep = ep->mctxt_next;
} while (ep != tmp);
/* This is valid because..
* PSM2_OK & PSM2_OK_NO_PROGRESS => PSM2_OK
* PSM2_OK & PSM2_OK => PSM2_OK
* PSM2_OK_NO_PROGRESS & PSM2_OK => PSM2_OK
* PSM2_OK_NO_PROGRESS & PSM2_OK_NO_PROGRESS => PSM2_OK_NO_PROGRESS */
PSMI_PUNLOCK();
PSM2_LOG_MSG("leaving");
return (err1 & err2);
}
PSMI_API_DECL(psm2_poll)
psm2_error_t __psmi_poll_internal(psm2_ep_t ep, int poll_amsh)
{
psm2_error_t err1 = PSM2_OK_NO_PROGRESS;
psm2_error_t err2;
psm2_ep_t tmp;
PSM2_LOG_MSG("entering");
PSMI_PLOCK_ASSERT();
tmp = ep;
do {
if (poll_amsh) {
err1 = ep->ptl_amsh.ep_poll(ep->ptl_amsh.ptl, 0); /* poll reqs & reps */
if (err1 > PSM2_OK_NO_PROGRESS) { /* some error unrelated to polling */
PSM2_LOG_MSG("leaving");
return err1;
}
}
err2 = ep->ptl_ips.ep_poll(ep->ptl_ips.ptl, 0); /* get into ips_do_work */
if (err2 > PSM2_OK_NO_PROGRESS) { /* some error unrelated to polling */
PSM2_LOG_MSG("leaving");
return err2;
}
ep = ep->mctxt_next;
} while (ep != tmp);
PSM2_LOG_MSG("leaving");
return (err1 & err2);
}
PSMI_API_DECL(psmi_poll_internal)
#ifdef PSM_PROFILE
/* These functions each have weak symbols */
void psmi_profile_block()
{
; /* empty for profiler */
}
void psmi_profile_unblock()
{
; /* empty for profiler */
}
void psmi_profile_reblock(int did_no_progress)
{
; /* empty for profiler */
}
#endif