-
Notifications
You must be signed in to change notification settings - Fork 29
/
psm_utils.c
2661 lines (2358 loc) · 70.9 KB
/
psm_utils.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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.
*/
#include <netdb.h> /* gethostbyname */
#include <malloc.h> /* malloc_usable_size */
#include "psm_user.h"
#include "psm2_hal.h"
#include "psm_am_internal.h"
#include "psm_mq_internal.h"
int psmi_ep_device_is_enabled(const psm2_ep_t ep, int devid);
struct psmi_epid_table psmi_epid_table;
/* Iterator to access the epid table.
* 'ep' can be NULL if remote endpoints from all endpoint handles are requested
*/
void psmi_epid_itor_init(struct psmi_eptab_iterator *itor, psm2_ep_t ep)
{
itor->i = 0;
itor->ep = ep;
pthread_mutex_lock(&psmi_epid_table.tablock);
}
void *psmi_epid_itor_next(struct psmi_eptab_iterator *itor)
{
int i;
struct psmi_epid_tabentry *e;
if (itor->i >= psmi_epid_table.tabsize)
return NULL;
for (i = itor->i; i < psmi_epid_table.tabsize; i++) {
e = &psmi_epid_table.table[i];
if (!e->entry || e->entry == EPADDR_DELETED)
continue;
if (itor->ep && e->ep != itor->ep)
continue;
itor->i = i + 1;
return e->entry;
}
itor->i = psmi_epid_table.tabsize; /* put at end of table */
return NULL;
}
void psmi_epid_itor_fini(struct psmi_eptab_iterator *itor)
{
pthread_mutex_unlock(&psmi_epid_table.tablock);
itor->i = 0;
}
#define mix64(a, b, c) \
{ \
a -= b; a -= c; a ^= (c>>43); \
b -= c; b -= a; b ^= (a<<9); \
c -= a; c -= b; c ^= (b>>8); \
a -= b; a -= c; a ^= (c>>38); \
b -= c; b -= a; b ^= (a<<23); \
c -= a; c -= b; c ^= (b>>5); \
a -= b; a -= c; a ^= (c>>35); \
b -= c; b -= a; b ^= (a<<49); \
c -= a; c -= b; c ^= (b>>11); \
a -= b; a -= c; a ^= (c>>12); \
b -= c; b -= a; b ^= (a<<18); \
c -= a; c -= b; c ^= (b>>22); \
}
psm2_error_t psmi_epid_init()
{
pthread_mutexattr_t attr;
psmi_epid_table.table = NULL, psmi_epid_table.tabsize = 0;
psmi_epid_table.tabsize_used = 0;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&psmi_epid_table.tablock, &attr);
pthread_mutexattr_destroy(&attr);
return PSM2_OK;
};
psm2_error_t psmi_epid_fini()
{
if (psmi_epid_table.table != NULL) {
psmi_free(psmi_epid_table.table);
psmi_epid_table.table = NULL;
}
psmi_epid_table.tabsize = 0;
psmi_epid_table.tabsize_used = 0;
return PSM2_OK;
}
PSMI_ALWAYS_INLINE(
uint64_t
hash_this(const psm2_ep_t ep, const psm2_epid_t epid))
{
uint64_t ep_i = (uint64_t) (uintptr_t) ep;
uint64_t epid_i = (uint64_t) epid;
uint64_t hash = 0x9e3779b97f4a7c13LL;
mix64(ep_i, epid_i, hash);
return hash;
}
PSMI_ALWAYS_INLINE(
void *
psmi_epid_lookup_inner(psm2_ep_t ep, psm2_epid_t epid, int remove))
{
uint64_t key = hash_this(ep, epid);
struct psmi_epid_tabentry *e;
void *entry = NULL;
int idx;
pthread_mutex_lock(&psmi_epid_table.tablock);
if (!psmi_epid_table.table)
goto ret;
idx = (int)(key % psmi_epid_table.tabsize);
while (psmi_epid_table.table[idx].entry != NULL) {
/* An epid can be added twice if there's more than one opened endpoint,
* but really we match on epid *and* on endpoint */
e = &psmi_epid_table.table[idx];
if (e->entry != EPADDR_DELETED && e->key == key) {
entry = e->entry;
if (remove)
psmi_epid_table.table[idx].entry =
EPADDR_DELETED;
goto ret;
}
if (++idx == psmi_epid_table.tabsize)
idx = 0;
}
ret:
pthread_mutex_unlock(&psmi_epid_table.tablock);
return entry;
}
void *psmi_epid_lookup(psm2_ep_t ep, psm2_epid_t epid)
{
void *entry = psmi_epid_lookup_inner(ep, epid, 0);
if (PSMI_EP_HOSTNAME != ep)
_HFI_VDBG("lookup of (%p,%" PRIx64 ") returns %p\n", ep, epid,
entry);
return entry;
}
void *psmi_epid_remove(psm2_ep_t ep, psm2_epid_t epid)
{
if (PSMI_EP_HOSTNAME != ep)
_HFI_VDBG("remove of (%p,%" PRIx64 ")\n", ep, epid);
return psmi_epid_lookup_inner(ep, epid, 1);
}
void psmi_epid_remove_all(psm2_ep_t ep)
{
size_t i;
struct psmi_epid_tabentry *e;
pthread_mutex_lock(&psmi_epid_table.tablock);
for (i = 0; i < psmi_epid_table.tabsize; i++) {
e = &psmi_epid_table.table[i];
if (e->entry == NULL || e->entry == EPADDR_DELETED)
continue;
if (e->ep == ep) {
/* unspecified fields implicitly zeroed */
*e = (struct psmi_epid_tabentry) {
.entry = EPADDR_DELETED
};
}
}
pthread_mutex_unlock(&psmi_epid_table.tablock);
}
psm2_error_t psmi_epid_add(psm2_ep_t ep, psm2_epid_t epid, void *entry)
{
uint64_t key;
int idx, i, newsz;
struct psmi_epid_tabentry *e;
psm2_error_t err = PSM2_OK;
if (PSMI_EP_HOSTNAME != ep)
_HFI_VDBG("add of (%p,%" PRIx64 ") with entry %p\n", ep, epid,
entry);
pthread_mutex_lock(&psmi_epid_table.tablock);
/* Leave this here, mostly for sanity and for the fact that the epid
* table is currently not used in the critical path */
if (++psmi_epid_table.tabsize_used >
(int)(psmi_epid_table.tabsize * PSMI_EPID_TABLOAD_FACTOR)) {
struct psmi_epid_tabentry *newtab;
newsz = psmi_epid_table.tabsize + PSMI_EPID_TABSIZE_CHUNK;
newtab = (struct psmi_epid_tabentry *)
psmi_calloc(ep, PER_PEER_ENDPOINT,
newsz, sizeof(struct psmi_epid_tabentry));
if (newtab == NULL) {
err = PSM2_NO_MEMORY;
goto fail;
}
if (psmi_epid_table.table) { /* rehash the table */
for (i = 0; i < psmi_epid_table.tabsize; i++) {
e = &psmi_epid_table.table[i];
if (e->entry == NULL)
continue;
/* When rehashing, mark deleted as free again */
if (e->entry == EPADDR_DELETED) {
psmi_epid_table.tabsize_used--;
continue;
}
idx = (int)(e->key % newsz);
while (newtab[idx].entry != NULL)
if (++idx == newsz)
idx = 0;
newtab[idx].entry = e->entry;
newtab[idx].key = e->key;
newtab[idx].ep = e->ep;
newtab[idx].epid = e->epid;
}
psmi_free(psmi_epid_table.table);
}
psmi_epid_table.table = newtab;
psmi_epid_table.tabsize = newsz;
}
key = hash_this(ep, epid);
idx = (int)(key % psmi_epid_table.tabsize);
e = &psmi_epid_table.table[idx];
while (e->entry && e->entry != EPADDR_DELETED) {
if (++idx == psmi_epid_table.tabsize)
idx = 0;
e = &psmi_epid_table.table[idx];
}
e->entry = entry;
e->key = key;
e->epid = epid;
e->ep = ep;
fail:
pthread_mutex_unlock(&psmi_epid_table.tablock);
return err;
}
static psmi_lock_t psmi_gethostname_lock;
static void __attribute__ ((constructor)) __psmi_gethostname_lock_constructor(void)
{
psmi_init_lock(&psmi_gethostname_lock);
}
char *psmi_gethostname(void)
{
static char hostname[80] = { '\0' };
char *c;
if (hostname[0] == '\0') {
PSMI_LOCK(psmi_gethostname_lock);
/* CRITICAL SECTION START */
if (hostname[0] == '\0') {
gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0'; /* no guarantee of nul termination */
if ((c = strchr(hostname, '.')))
*c = '\0';
}
PSMI_UNLOCK(psmi_gethostname_lock);
/* CRITICAL SECTION END */
}
return hostname;
}
/*
* Hostname stuff. We really only register the network portion of the epid
* since all epids from the same nid are assumed to have the same hostname.
*/
psm2_error_t
psmi_epid_set_hostname(uint64_t nid, const char *hostname, int overwrite)
{
size_t hlen;
char *h;
psm2_error_t err = PSM2_OK;
if (hostname == NULL)
return PSM2_OK;
/* First see if a hostname already exists */
if ((h = psmi_epid_lookup(PSMI_EP_HOSTNAME, nid)) != NULL) {
if (!overwrite)
return PSM2_OK;
h = psmi_epid_remove(PSMI_EP_HOSTNAME, nid);
if (h != NULL) /* free the previous hostname if so exists */
psmi_free(h);
}
hlen = min(PSMI_EP_HOSTNAME_LEN, strlen(hostname) + 1);
h = (char *)psmi_malloc(PSMI_EP_NONE, PER_PEER_ENDPOINT, hlen);
if (h == NULL)
return PSM2_NO_MEMORY;
snprintf(h, hlen, "%s", hostname);
h[hlen - 1] = '\0';
err = psmi_epid_add(PSMI_EP_HOSTNAME, nid, h);
return err;
}
/* XXX These two functions are not thread safe, we'll use a rotating buffer
* trick whenever we need to make them thread safe */
const char *psmi_epaddr_get_hostname(psm2_epid_t epid)
{
static char hostnamebufs[4][PSMI_EP_HOSTNAME_LEN];
static int bufno;
uint64_t nid = psm2_epid_nid(epid);
char *h, *hostname;
hostname = hostnamebufs[bufno];
bufno = (bufno + 1) % 4;
/* First, if we have registered a host for this epid, just return that, or
* else try to return something with lid and context */
h = psmi_epid_lookup(PSMI_EP_HOSTNAME, nid);
if (h != NULL)
return h;
else {
snprintf(hostname, PSMI_EP_HOSTNAME_LEN - 1, "LID=%d:%d.%d",
(int)PSMI_EPID_GET_LID(epid),
(int)PSMI_EPID_GET_CONTEXT(epid),
(int)PSMI_EPID_GET_SUBCONTEXT(epid));
hostname[PSMI_EP_HOSTNAME_LEN - 1] = '\0';
return hostname;
}
}
/* This one gives the hostname with a lid */
const char *psmi_epaddr_get_name(psm2_epid_t epid)
{
static char hostnamebufs[4][PSMI_EP_HOSTNAME_LEN];
static int bufno;
char *h, *hostname;
hostname = hostnamebufs[bufno];
bufno = (bufno + 1) % 4;
h = psmi_epid_lookup(PSMI_EP_HOSTNAME, psm2_epid_nid(epid));
if (h == NULL)
return psmi_epaddr_get_hostname(epid);
else {
snprintf(hostname, PSMI_EP_HOSTNAME_LEN - 1,
"%s (LID=%d:%d.%d)", h,
(int)PSMI_EPID_GET_LID(epid),
(int)PSMI_EPID_GET_CONTEXT(epid),
(int)PSMI_EPID_GET_SUBCONTEXT(epid));
hostname[PSMI_EP_HOSTNAME_LEN - 1] = '\0';
}
return hostname;
}
/* Wrapper, in case we port to OS xyz that doesn't have sysconf */
uintptr_t psmi_getpagesize(void)
{
static uintptr_t pagesz = (uintptr_t) -1;
long sz;
if (pagesz != (uintptr_t) -1)
return pagesz;
sz = sysconf(_SC_PAGESIZE);
if (sz == -1) {
psmi_handle_error(PSMI_EP_NORETURN, PSM2_INTERNAL_ERR,
"Can't query system page size");
}
pagesz = (uintptr_t) sz;
return pagesz;
}
/* If PSM2_VERBOSE_ENV is set in the environment, we determine
* what its verbose level is and print the environment at "INFO"
* level if the environment's level matches the desired printlevel.
*/
static int psmi_getenv_verblevel = -1;
static int psmi_getenv_is_verblevel(int printlevel)
{
if (psmi_getenv_verblevel == -1) {
char *env = getenv("PSM2_VERBOSE_ENV");
if (env && *env) {
char *ep;
int val = (int)strtol(env, &ep, 0);
if (ep == env)
psmi_getenv_verblevel = 0;
else if (val == 2)
psmi_getenv_verblevel = 2;
else
psmi_getenv_verblevel = 1;
} else
psmi_getenv_verblevel = 0;
}
return (printlevel <= psmi_getenv_verblevel);
}
#define GETENV_PRINTF(_level, _fmt, ...) \
do { \
if ((_level & PSMI_ENVVAR_LEVEL_NEVER_PRINT) == 0) \
{ \
int nlevel = _level; \
if (psmi_getenv_is_verblevel(nlevel)) \
nlevel = 0; \
_HFI_ENVDBG(nlevel, _fmt, ##__VA_ARGS__); \
} \
} while (0)
int
MOCKABLE(psmi_getenv)(const char *name, const char *descr, int level,
int type, union psmi_envvar_val defval,
union psmi_envvar_val *newval)
{
int used_default = 0;
union psmi_envvar_val tval;
char *env = getenv(name);
#if _HFI_DEBUGGING
int ishex = (type == PSMI_ENVVAR_TYPE_ULONG_FLAGS ||
type == PSMI_ENVVAR_TYPE_UINT_FLAGS);
#endif
/* If we're not using the default, always reset the print
* level to '1' so the changed value gets seen at low
* verbosity */
#define _GETENV_PRINT(used_default, fmt, val, defval) \
do { \
if (used_default) \
GETENV_PRINTF(level, "%s%-25s %-40s =>%s" fmt \
"\n", level > 1 ? "*" : " ", name, \
descr, ishex ? "0x" : " ", val); \
else \
GETENV_PRINTF(1, "%s%-25s %-40s =>%s" \
fmt " (default was%s" fmt ")\n", \
level > 1 ? "*" : " ", name, descr, \
ishex ? " 0x" : " ", val, \
ishex ? " 0x" : " ", defval); \
} while (0)
/* _CONSUMED_ALL() is a macro which indicates if strtol() consumed all
of the input passed to it. */
#define _CONSUMED_ALL(CHAR_PTR) (((CHAR_PTR) != NULL) && (*(CHAR_PTR) == 0))
#define _CONVERT_TO_NUM(DEST,TYPE,STRTOL) \
do { \
char *ep; \
/* Avoid base 8 (octal) on purpose, so don't pass in 0 for radix */ \
DEST = (TYPE)STRTOL(env, &ep, 10); \
if (! _CONSUMED_ALL(ep)) { \
DEST = (TYPE)STRTOL(env, &ep, 16); \
if (! _CONSUMED_ALL(ep)) { \
used_default = 1; \
tval = defval; \
} \
} \
} while (0)
switch (type) {
case PSMI_ENVVAR_TYPE_YESNO:
if (!env || *env == '\0') {
tval = defval;
used_default = 1;
} else if (env[0] == 'Y' || env[0] == 'y')
tval.e_int = 1;
else if (env[0] == 'N' || env[0] == 'n')
tval.e_int = 0;
else {
char *ep;
tval.e_ulong = strtoul(env, &ep, 0);
if (ep == env) {
used_default = 1;
tval = defval;
} else if (tval.e_ulong != 0)
tval.e_ulong = 1;
}
_GETENV_PRINT(used_default, "%s", tval.e_long ? "YES" : "NO",
defval.e_int ? "YES" : "NO");
break;
case PSMI_ENVVAR_TYPE_STR:
if (!env || *env == '\0') {
tval = defval;
used_default = 1;
} else
tval.e_str = env;
_GETENV_PRINT(used_default, "%s", tval.e_str, defval.e_str);
break;
case PSMI_ENVVAR_TYPE_INT:
if (!env || *env == '\0') {
tval = defval;
used_default = 1;
} else {
_CONVERT_TO_NUM(tval.e_int,int,strtol);
}
_GETENV_PRINT(used_default, "%d", tval.e_int, defval.e_int);
break;
case PSMI_ENVVAR_TYPE_UINT:
case PSMI_ENVVAR_TYPE_UINT_FLAGS:
if (!env || *env == '\0') {
tval = defval;
used_default = 1;
} else {
_CONVERT_TO_NUM(tval.e_int,unsigned int,strtoul);
}
if (type == PSMI_ENVVAR_TYPE_UINT_FLAGS)
_GETENV_PRINT(used_default, "%x", tval.e_uint,
defval.e_uint);
else
_GETENV_PRINT(used_default, "%u", tval.e_uint,
defval.e_uint);
break;
case PSMI_ENVVAR_TYPE_LONG:
if (!env || *env == '\0') {
tval = defval;
used_default = 1;
} else {
_CONVERT_TO_NUM(tval.e_long,long,strtol);
}
_GETENV_PRINT(used_default, "%ld", tval.e_long, defval.e_long);
break;
case PSMI_ENVVAR_TYPE_ULONG_ULONG:
if (!env || *env == '\0') {
tval = defval;
used_default = 1;
} else {
_CONVERT_TO_NUM(tval.e_ulonglong,unsigned long long,strtoull);
}
_GETENV_PRINT(used_default, "%llu",
tval.e_ulonglong, defval.e_ulonglong);
break;
case PSMI_ENVVAR_TYPE_ULONG:
case PSMI_ENVVAR_TYPE_ULONG_FLAGS:
default:
if (!env || *env == '\0') {
tval = defval;
used_default = 1;
} else {
_CONVERT_TO_NUM(tval.e_ulong,unsigned long,strtoul);
}
if (type == PSMI_ENVVAR_TYPE_ULONG_FLAGS)
_GETENV_PRINT(used_default, "%lx", tval.e_ulong,
defval.e_ulong);
else
_GETENV_PRINT(used_default, "%lu", tval.e_ulong,
defval.e_ulong);
break;
}
#undef _GETENV_PRINT
*newval = tval;
return used_default;
}
MOCK_DEF_EPILOGUE(psmi_getenv);
/*
* Parsing int parameters set in string tuples.
* Output array int *vals should be able to store 'ntup' elements.
* Values are only overwritten if they are parsed.
* Tuples are always separated by colons ':'
*/
int psmi_parse_str_tuples(const char *string, int ntup, int *vals)
{
char *b = (char *)string;
char *e = b;
int tup_i = 0;
int n_parsed = 0;
char *buf = psmi_strdup(NULL, string);
psmi_assert_always(buf != NULL);
while (*e && tup_i < ntup) {
b = e;
while (*e && *e != ':')
e++;
if (e > b) { /* something to parse */
char *ep;
int len = e - b;
long int l;
strncpy(buf, b, len);
buf[len] = '\0';
l = strtol(buf, &ep, 0);
if (ep != buf) { /* successful conversion */
vals[tup_i] = (int)l;
n_parsed++;
}
}
if (*e == ':')
e++; /* skip delimiter */
tup_i++;
}
psmi_free(buf);
return n_parsed;
}
/*
* Memory footprint/usage mode.
*
* This can be used for debug or for separating large installations from
* small/medium ones. The default is to assume a medium installation. Large
* is not that much larger in memory footprint, but we make a conscious effort
* an consuming only the amount of memory we need.
*/
int psmi_parse_memmode(void)
{
union psmi_envvar_val env_mmode;
int used_default =
psmi_getenv("PSM2_MEMORY", "Memory usage mode (normal or large)",
PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_STR,
(union psmi_envvar_val)"normal", &env_mmode);
if (used_default || !strcasecmp(env_mmode.e_str, "normal"))
return PSMI_MEMMODE_NORMAL;
else if (!strcasecmp(env_mmode.e_str, "min"))
return PSMI_MEMMODE_MINIMAL;
else if (!strcasecmp(env_mmode.e_str, "large") ||
!strcasecmp(env_mmode.e_str, "big"))
return PSMI_MEMMODE_LARGE;
else {
_HFI_PRDBG("PSM2_MEMORY env value %s unrecognized, "
"using 'normal' memory mode instead\n",
env_mmode.e_str);
return PSMI_MEMMODE_NORMAL;
}
}
static
const char *psmi_memmode_string(int mode)
{
psmi_assert(mode >= PSMI_MEMMODE_NORMAL && mode < PSMI_MEMMODE_NUM);
switch (mode) {
case PSMI_MEMMODE_NORMAL:
return "normal";
case PSMI_MEMMODE_MINIMAL:
return "minimal";
case PSMI_MEMMODE_LARGE:
return "large";
default:
return "unknown";
}
}
psm2_error_t
psmi_parse_mpool_env(const psm2_mq_t mq, int level,
const struct psmi_rlimit_mpool *rlim,
uint32_t *valo, uint32_t *chunkszo)
{
uint32_t val;
const char *env = rlim->env;
int mode = mq->memmode;
psm2_error_t err = PSM2_OK;
union psmi_envvar_val env_val;
psmi_assert_always(mode >= PSMI_MEMMODE_NORMAL
&& mode < PSMI_MEMMODE_NUM);
psmi_getenv(rlim->env, rlim->descr, rlim->env_level,
PSMI_ENVVAR_TYPE_UINT,
(union psmi_envvar_val)rlim->mode[mode].obj_max, &env_val);
val = env_val.e_uint;
if (val < rlim->minval || val > rlim->maxval) {
err = psmi_handle_error(NULL, PSM2_PARAM_ERR,
"Env. var %s=%u is invalid (valid settings in mode PSM2_MEMORY=%s"
" are inclusively between %u and %u)",
env, val, psmi_memmode_string(mode),
rlim->minval, rlim->maxval);
goto fail;
}
_HFI_VDBG("%s max=%u,chunk=%u (mode=%s(%u),min=%u,max=%u)\n",
env, val, rlim->mode[mode].obj_chunk,
psmi_memmode_string(mode), mode, rlim->minval, rlim->maxval);
*valo = val;
*chunkszo = rlim->mode[mode].obj_chunk;
fail:
return err;
}
uint64_t psmi_cycles_left(uint64_t start_cycles, int64_t timeout_ns)
{
if (timeout_ns < 0)
return 0ULL;
else if (timeout_ns == 0ULL || timeout_ns == ~0ULL)
return ~0ULL;
else {
uint64_t t_end = nanosecs_to_cycles(timeout_ns);
uint64_t t_now = get_cycles() - start_cycles;
if (t_now >= t_end)
return 0ULL;
else
return (t_end - t_now);
}
}
uint32_t psmi_get_ipv4addr()
{
struct hostent *he;
uint32_t addr = 0;
he = gethostbyname(psmi_gethostname());
if (he != NULL && he->h_addrtype == AF_INET && he->h_addr != NULL) {
memcpy(&addr, he->h_addr, sizeof(uint32_t));
return addr;
} else
return 0;
}
#define PSMI_EP_IS_PTR(ptr) ((ptr) != NULL && (ptr) < PSMI_EP_LOGEVENT)
void
psmi_syslog(psm2_ep_t ep, int to_console, int level, const char *format, ...)
{
va_list ap;
/* If we've never syslogged anything from this ep at the PSM level, make
* sure we log context information */
if (PSMI_EP_IS_PTR(ep) && !ep->did_syslog) {
char uuid_str[64];
ep->did_syslog = 1;
memset(&uuid_str, 0, sizeof(uuid_str));
psmi_uuid_unparse(ep->uuid, uuid_str);
hfi_syslog("PSM", 0, LOG_WARNING,
"uuid_key=%s,unit=%d,context=%d,subcontext=%d",
uuid_str,
psmi_hal_get_unit_id(ep->context.psm_hw_ctxt),
psmi_hal_get_context(ep->context.psm_hw_ctxt),
psmi_hal_get_subctxt(ep->context.psm_hw_ctxt));
}
va_start(ap, format);
hfi_vsyslog("PSM", to_console, level, format, ap);
va_end(ap);
}
/* Table of CRCs of all 8-bit messages. */
static uint32_t crc_table[256];
/* Flag: has the table been computed? Initially false. */
static int crc_table_computed;
/* Make the table for a fast CRC. */
static void make_crc_table(void)
{
uint32_t c;
int n, k;
for (n = 0; n < 256; n++) {
c = (uint32_t) n;
for (k = 0; k < 8; k++) {
if (c & 1)
c = 0xedb88320 ^ (c >> 1);
else
c = c >> 1;
}
crc_table[n] = c;
}
crc_table_computed = 1;
}
/* Update a running CRC with the bytes buf[0..len-1]--the CRC
* should be initialized to all 1's, and the transmitted value
* is the 1's complement of the final running CRC (see the
* crc() routine below)).
*/
static uint32_t update_crc(uint32_t crc, unsigned char *buf, int len)
{
uint32_t c = crc;
int n;
if_pf(!crc_table_computed)
make_crc_table();
for (n = 0; n < len; n++) {
c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
}
return c;
}
/* Return the CRC of the bytes buf[0..len-1]. */
uint32_t psmi_crc(unsigned char *buf, int len)
{
return update_crc(0xffffffff, buf, len) ^ 0xffffffff;
}
struct psmi_faultinj_spec {
STAILQ_ENTRY(psmi_faultinj_spec) next;
char spec_name[PSMI_FAULTINJ_SPEC_NAMELEN];
unsigned long long num_faults;
unsigned long long num_calls;
struct drand48_data drand48_data;
int num;
int denom;
};
int psmi_multi_ep_enabled = 0;
void psmi_multi_ep_init()
{
union psmi_envvar_val env_fi;
psmi_getenv("PSM2_MULTI_EP", "PSM2 Multiple Endpoints (yes/no)",
PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_YESNO,
PSMI_ENVVAR_VAL_NO, &env_fi);
psmi_multi_ep_enabled = env_fi.e_uint;
}
#ifdef PSM_FI
int psmi_faultinj_enabled = 0;
int psmi_faultinj_verbose = 0;
char *psmi_faultinj_outfile = NULL;
static struct psmi_faultinj_spec psmi_faultinj_dummy;
static STAILQ_HEAD(, psmi_faultinj_spec) psmi_faultinj_head =
STAILQ_HEAD_INITIALIZER(psmi_faultinj_head);
void psmi_faultinj_init()
{
union psmi_envvar_val env_fi;
psmi_getenv("PSM2_FI", "PSM Fault Injection (yes/no)",
PSMI_ENVVAR_LEVEL_HIDDEN, PSMI_ENVVAR_TYPE_YESNO,
PSMI_ENVVAR_VAL_NO, &env_fi);
psmi_faultinj_enabled = !!env_fi.e_uint;
if (psmi_faultinj_enabled) {
char *def = NULL;
if (!psmi_getenv
("PSM2_FI_TRACEFILE", "PSM Fault Injection output file",
PSMI_ENVVAR_LEVEL_HIDDEN, PSMI_ENVVAR_TYPE_STR,
(union psmi_envvar_val)def, &env_fi)) {
psmi_faultinj_outfile = psmi_strdup(NULL, env_fi.e_str);
}
}
return;
}
void psmi_faultinj_fini()
{
struct psmi_faultinj_spec *fi;
FILE *fp;
int do_fclose = 0;
if (!psmi_faultinj_enabled || psmi_faultinj_outfile == NULL)
return;
if (strncmp(psmi_faultinj_outfile, "stdout", 7) == 0)
fp = stdout;
else if (strncmp(psmi_faultinj_outfile, "stderr", 7) == 0)
fp = stderr;
else {
char *c = psmi_faultinj_outfile;
char buf[192];
int append = 0;
if (*c == '+') {
append = 1;
++c;
}
do_fclose = 1;
snprintf(buf, sizeof(buf) - 1, "%s.%s", c, hfi_get_mylabel());
buf[sizeof(buf) - 1] = '\0';
fp = fopen(buf, append ? "a" : "w");
}
if (fp != NULL) {
STAILQ_FOREACH(fi, &psmi_faultinj_head, next) {
fprintf(fp, "%s:%s PSM2_FI_%-12s %2.3f%% => "
"%2.3f%% %10lld faults/%10lld events\n",
__progname, hfi_get_mylabel(), fi->spec_name,
(double)fi->num * 100.0 / fi->denom,
(double)fi->num_faults * 100.0 / fi->num_calls,
fi->num_faults, fi->num_calls);
}
fflush(fp);
if (do_fclose)
fclose(fp);
}
psmi_free(psmi_faultinj_outfile);
return;
}
/*
* Intended to be used only once, not in the critical path
*/
struct psmi_faultinj_spec *psmi_faultinj_getspec(char *spec_name, int num,
int denom)
{
struct psmi_faultinj_spec *fi;
if (!psmi_faultinj_enabled)
return &psmi_faultinj_dummy;
STAILQ_FOREACH(fi, &psmi_faultinj_head, next) {
if (strcmp(fi->spec_name, spec_name) == 0)
return fi;
}
/* We got here, so no spec -- allocate one */
fi = psmi_malloc(PSMI_EP_NONE, UNDEFINED,
sizeof(struct psmi_faultinj_spec));
psmi_assert_always(fi != NULL);
strncpy(fi->spec_name, spec_name, PSMI_FAULTINJ_SPEC_NAMELEN - 1);
fi->spec_name[PSMI_FAULTINJ_SPEC_NAMELEN - 1] = '\0';
fi->num = num;
fi->denom = denom;
fi->num_faults = 0;
fi->num_calls = 0;
/*
* See if we get a hint from the environment.
* Format is
* <num:denom:initial_seed>
*
* By default, we chose the initial seed to be the 'pid'. If users need
* repeatability, they should set initial_seed to be the 'pid' when the
* error was observed or force the initial_seed to be a constant number in
* each running process. Using 'pid' is useful because core dumps store
* pids and our backtrace format does as well so if a crash is observed for
* a specific seed, programs can reuse the 'pid' to regenerate the same
* error condition.
*/
{
int fvals[3] = { num, denom, (int)getpid() };
union psmi_envvar_val env_fi;
char fvals_str[128];
char fname[128];
char fdesc[300];
snprintf(fvals_str, sizeof(fvals_str) - 1, "%d:%d:1", num,
denom);
fvals_str[sizeof(fvals_str) - 1] = '\0';
snprintf(fname, sizeof(fname) - 1, "PSM2_FI_%s", spec_name);
fname[sizeof(fname) - 1] = '\0';
snprintf(fdesc, sizeof(fdesc) - 1, "Fault Injection %s <%s>",
fname, fvals_str);
if (!psmi_getenv(fname, fdesc, PSMI_ENVVAR_LEVEL_HIDDEN,
PSMI_ENVVAR_TYPE_STR,
(union psmi_envvar_val)fvals_str, &env_fi)) {
/* not using default values */
int n_parsed =
psmi_parse_str_tuples(env_fi.e_str, 3, fvals);
if (n_parsed >= 1)
fi->num = fvals[0];
if (n_parsed >= 2)
fi->denom = fvals[1];
if (n_parsed >= 3)
srand48_r((long int) fvals[2], &fi->drand48_data);