Source

mpi3-fortran / ompi / mca / btl / wv / btl_wv_ini.c

Full commit
  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
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2006-2009 Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2008      Mellanox Technologies. All rights reserved.
 * Copyright (c) 2012      Los Alamos National Security, LLC.  All rights
 *                         reserved. 
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "ompi_config.h"

#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "orte/util/show_help.h"
#include "opal/mca/base/mca_base_param.h"

#include "btl_wv.h"
#include "btl_wv_lex.h"
#include "btl_wv_ini.h"

static const char *ini_filename = NULL;
static bool initialized = false;
static opal_list_t devices;
static char *key_buffer = NULL;
static size_t key_buffer_len = 0;


/*
 * Struct to hold the section name, vendor ID, and list of vendor part
 * ID's and a corresponding set of values (parsed from an INI file).
 */
typedef struct parsed_section_values_t {
    char *name;

    uint32_t *vendor_ids;
    int vendor_ids_len;

    uint32_t *vendor_part_ids;
    int vendor_part_ids_len;

    ompi_btl_wv_ini_values_t values;
} parsed_section_values_t;

/*
 * Struct to hold the final values.   Different from above in a few ways:
 *
 * - The vendor and part IDs will always be set properly
 * - There will only be one part ID (i.e., the above struct is
 *   exploded into multiple of these for each of searching)
 * - There is a super of opal_list_item_t so that we can have a list
 *   of these
 */
typedef struct wv_device_values_t {
    opal_list_item_t super;

    char *section_name;
    uint32_t vendor_id;
    uint32_t vendor_part_id;

    ompi_btl_wv_ini_values_t values;
} wv_device_values_t;

static void device_values_constructor(wv_device_values_t *s);
static void device_values_destructor(wv_device_values_t *s);

OBJ_CLASS_INSTANCE(wv_device_values_t,
                   opal_list_item_t,
                   device_values_constructor,
                   device_values_destructor);


/*
 * Local functions
 */
static int parse_file(char *filename);
static int parse_line(parsed_section_values_t *item);
static void reset_section(bool had_previous_value, parsed_section_values_t *s);
static void reset_values(ompi_btl_wv_ini_values_t *v);
static int save_section(parsed_section_values_t *s);
static inline void show_help(const char *topic);


/*
 * Read the INI files for device-specific values and save them in
 * internal data structures for later lookup.
 */
int ompi_btl_wv_ini_init(void)
{
    int ret = OMPI_ERR_NOT_FOUND;
    char *colon;

    char separator = ';';

    OBJ_CONSTRUCT(&devices, opal_list_t);

    colon = strchr(mca_btl_wv_component.device_params_file_names, separator);
    if (NULL == colon) {
        /* If we've only got 1 file (i.e., no colons found), parse it
           and be done */
        ret = parse_file(mca_btl_wv_component.device_params_file_names);
    } else {
        /* Otherwise, loop over all the files and parse them */
        char *orig = strdup(mca_btl_wv_component.device_params_file_names);
        char *str = orig;

        while (NULL != (colon = strchr(str, ':'))) {
            *colon = '\0';
            ret = parse_file(str);
            /* Note that NOT_FOUND and SUCCESS are not fatal errors
               and we keep going.  Other errors are treated as
               fatal */
            if (OMPI_ERR_NOT_FOUND != ret && OMPI_SUCCESS != ret) {
                break;
            }
            str = colon + 1;
        }
        /* Parse the last file if we didn't have a fatal error above */
        if (OMPI_ERR_NOT_FOUND != ret && OMPI_SUCCESS != ret) {
            ret = parse_file(str);
        }

        /* All done */
        free(orig);
    }

    /* Return SUCCESS unless we got a fatal error */

    initialized = true;
    return (OMPI_SUCCESS == ret || OMPI_ERR_NOT_FOUND == ret) ?
        OMPI_SUCCESS : ret;
}


/*
 * The component found a device and is querying to see if an INI file
 * specified any parameters for it.
 */
int ompi_btl_wv_ini_query(uint32_t vendor_id, uint32_t vendor_part_id,
                              ompi_btl_wv_ini_values_t *values)
{
    int ret;
    wv_device_values_t *h;
    opal_list_item_t *item;

    if (!initialized) {
        if (OMPI_SUCCESS != (ret = ompi_btl_wv_ini_init())) {
            return ret;
        }
    }

    if (mca_btl_wv_component.verbose) {
        BTL_OUTPUT(("Querying INI files for vendor 0x%04x, part ID %d",
                    vendor_id, vendor_part_id));
    }

    reset_values(values);

    /* Iterate over all the saved devices */
    for (item = opal_list_get_first(&devices);
         item != opal_list_get_end(&devices);
         item = opal_list_get_next(item)) {
        h = (wv_device_values_t*) item;
        if (vendor_id == h->vendor_id &&
            vendor_part_id == h->vendor_part_id) {
            /* Found it! */
            /* NOTE: There is a bug in the PGI 6.2 series that causes
               the compiler to choke when copying structs containing
               bool members by value.  So do a memcpy here instead. */
            memcpy(values, &h->values, sizeof(h->values));
            if (mca_btl_wv_component.verbose) {
                BTL_OUTPUT(("Found corresponding INI values: %s",
                            h->section_name));
            }
            return OMPI_SUCCESS;
        }
    }

    /* If we fall through to here, we didn't find it */
    if (mca_btl_wv_component.verbose) {
        BTL_OUTPUT(("Did not find corresponding INI values"));
    }
    return OMPI_ERR_NOT_FOUND;
}


/*
 * The component is shutting down; release all internal state
 */
int ompi_btl_wv_ini_finalize(void)
{
    opal_list_item_t *item;

    if (initialized) {
        for (item = opal_list_remove_first(&devices);
             NULL != item;
             item = opal_list_remove_first(&devices)) {
            OBJ_RELEASE(item);
        }
        OBJ_DESTRUCT(&devices);
        initialized = true;
    }

    return OMPI_SUCCESS;
}

/**************************************************************************/

/*
 * Parse a single file
 */
static int parse_file(char *filename)
{
    int val;
    int ret = OMPI_SUCCESS;
    bool showed_no_section_warning = false;
    bool showed_unexpected_tokens_warning = false;
    parsed_section_values_t section;

    reset_section(false, &section);

    /* Open the file */
    ini_filename = filename;
    btl_wv_ini_yyin = fopen(filename, "r");
    if (NULL == btl_wv_ini_yyin) {
        orte_show_help("help-mpi-btl-wv.txt", "ini file:file not found",
                       true, filename);
        ret = OMPI_ERR_NOT_FOUND;
        goto cleanup;
    }

    /* Do the parsing */
    btl_wv_ini_parse_done = false;
    btl_wv_ini_yynewlines = 1;
    btl_wv_ini_init_buffer(btl_wv_ini_yyin);
    while (!btl_wv_ini_parse_done) {
        val = btl_wv_ini_yylex();
        switch (val) {
        case BTL_WV_INI_PARSE_DONE:
            /* This will also set btl_wv_ini_parse_done to true, so just
               break here */
            break;

        case BTL_WV_INI_PARSE_NEWLINE:
            /* blank line!  ignore it */
            break;

        case BTL_WV_INI_PARSE_SECTION:
            /* We're starting a new section; if we have previously
               parsed a section, go see if we can use its values. */
            save_section(&section);

            reset_section(true, &section);
            section.name = strdup(btl_wv_ini_yytext);
            break;

        case BTL_WV_INI_PARSE_SINGLE_WORD:
            if (NULL == section.name) {
                /* Warn that there is no current section, and ignore
                   this parameter */
                if (!showed_no_section_warning) {
                    show_help("ini file:not in a section");
                    showed_no_section_warning = true;
                }
                /* Parse it and then dump it */
                parse_line(&section);
                reset_section(true, &section);
            } else {
                parse_line(&section);
            }
            break;

        default:
            /* anything else is an error */
            if (!showed_unexpected_tokens_warning) {
                show_help("ini file:unexpected token");
                showed_unexpected_tokens_warning = true;
            }
            break;
        }
    }
    save_section(&section);
    fclose(btl_wv_ini_yyin);
    btl_wv_ini_yylex_destroy ();

cleanup:
    reset_section(true, &section);
    if (NULL != key_buffer) {
        free(key_buffer);
        key_buffer = NULL;
        key_buffer_len = 0;
    }
    return ret;
}


/*
 * Parse a single line in the INI file
 */
static int parse_line(parsed_section_values_t *sv)
{
    int val, ret = OMPI_SUCCESS;
    char *value = NULL;
    bool showed_unknown_field_warning = false;

    /* Save the name name */
    if (key_buffer_len < strlen(btl_wv_ini_yytext) + 1) {
        char *tmp;
        key_buffer_len = strlen(btl_wv_ini_yytext) + 1;
        tmp = (char *) realloc(key_buffer, key_buffer_len);
        if (NULL == tmp) {
            free(key_buffer);
            key_buffer_len = 0;
            key_buffer = NULL;
            return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
        }
        key_buffer = tmp;
    }
    strncpy(key_buffer, btl_wv_ini_yytext, key_buffer_len);

    /* The first thing we have to see is an "=" */
    val = btl_wv_ini_yylex();
    if (btl_wv_ini_parse_done || BTL_WV_INI_PARSE_EQUAL != val) {
        show_help("ini file:expected equals");
        return OMPI_ERROR;
    }

    /* Next we get the value */
    val = btl_wv_ini_yylex();
    if (BTL_WV_INI_PARSE_SINGLE_WORD == val ||
        BTL_WV_INI_PARSE_VALUE == val) {
        value = strdup(btl_wv_ini_yytext);

        /* Now we need to see the newline */
        val = btl_wv_ini_yylex();
        if (BTL_WV_INI_PARSE_NEWLINE != val &&
            BTL_WV_INI_PARSE_DONE != val) {
            show_help("ini file:expected newline");
            free(value);
            return OMPI_ERROR;
        }
    }

    /* If we did not get EOL or EOF, something is wrong */
    else if (BTL_WV_INI_PARSE_DONE != val &&
             BTL_WV_INI_PARSE_NEWLINE != val) {
        show_help("ini file:expected newline");
        return OMPI_ERROR;
    }

    /* Ok, we got a good parse.  Now figure out what it is and save
       the value.  Note that the flex already took care of trimming
       all whitespace at the beginning and ending of the value. */

    if (0 == strcasecmp(key_buffer, "vendor_id")) {
        if (OMPI_SUCCESS != (ret = ompi_btl_wv_ini_intify_list(value, &sv->vendor_ids,
                                               &sv->vendor_ids_len))) {
            return ret;
        }
    }

    else if (0 == strcasecmp(key_buffer, "vendor_part_id")) {
        if (OMPI_SUCCESS != (ret = ompi_btl_wv_ini_intify_list(value, &sv->vendor_part_ids,
                                               &sv->vendor_part_ids_len))) {
            return ret;
        }
    }

    else if (0 == strcasecmp(key_buffer, "mtu")) {
        /* Single value */
        sv->values.mtu = (uint32_t) ompi_btl_wv_ini_intify(value);
        sv->values.mtu_set = true;
    }

    else if (0 == strcasecmp(key_buffer, "use_eager_rdma")) {
        /* Single value */
        sv->values.use_eager_rdma = (uint32_t) ompi_btl_wv_ini_intify(value);
        sv->values.use_eager_rdma_set = true;
    }

    else if (0 == strcasecmp(key_buffer, "receive_queues")) {
        /* Single value (already strdup'ed) */
        sv->values.receive_queues = value;
        value = NULL;
    }

    else if (0 == strcasecmp(key_buffer, "max_inline_data")) {
        /* Single value */
        sv->values.max_inline_data = (int32_t) ompi_btl_wv_ini_intify(value);
        sv->values.max_inline_data_set = true;
    }

    else if (0 == strcasecmp(key_buffer, "rdmacm_reject_causes_connect_error")) {
        /* Single value */
        sv->values.rdmacm_reject_causes_connect_error = 
            (bool) ompi_btl_wv_ini_intify(value);
        sv->values.rdmacm_reject_causes_connect_error_set = true;
    }

    else {
        /* Have no idea what this parameter is.  Not an error -- just
           ignore it */
        if (!showed_unknown_field_warning) {
            orte_show_help("help-mpi-btl-wv.txt",
                           "ini file:unknown field", true,
                           ini_filename, btl_wv_ini_yynewlines,
                           key_buffer);
            showed_unknown_field_warning = true;
        }
    }

    /* All done */

    if (NULL != value) {
        free(value);
    }
    return ret;
}


/*
 * Construct an wv_device_values_t and set all of its values to known states
 */
static void device_values_constructor(wv_device_values_t *s)
{
    s->section_name = NULL;
    s->vendor_id = 0;
    s->vendor_part_id = 0;
    reset_values(&s->values);
}


/*
 * Destruct an wv_device_values_t and free any memory that it has
 */
static void device_values_destructor(wv_device_values_t *s)
{
    if (NULL != s->section_name) {
        free(s->section_name);
    }
    if (NULL != s->values.receive_queues) {
        free(s->values.receive_queues);
    }
}


/*
 * Reset a parsed section; free any memory that it may have had
 */
static void reset_section(bool had_previous_value, parsed_section_values_t *s)
{
    if (had_previous_value) {
        if (NULL != s->name) {
            free(s->name);
        }
        if (NULL != s->vendor_ids) {
            free(s->vendor_ids);
        }
        if (NULL != s->vendor_part_ids) {
            free(s->vendor_part_ids);
        }
    }

    s->name = NULL;
    s->vendor_ids = NULL;
    s->vendor_ids_len = 0;
    s->vendor_part_ids = NULL;
    s->vendor_part_ids_len = 0;

    reset_values(&s->values);
}


/*
 * Reset the values to known states
 */
static void reset_values(ompi_btl_wv_ini_values_t *v)
{
    v->mtu = 0;
    v->mtu_set = false;

    v->use_eager_rdma = 0;
    v->use_eager_rdma_set = false;

    v->receive_queues = NULL;

    v->max_inline_data = 0;
    v->max_inline_data_set = false;

    v->rdmacm_reject_causes_connect_error = false;
    v->rdmacm_reject_causes_connect_error_set = false;
}


/*
 * If we have a valid section, see if we have a matching section
 * somewhere (i.e., same vendor ID and vendor part ID).  If we do,
 * update the values.  If not, save the values in a new instance and
 * add it to the list.
 */
static int save_section(parsed_section_values_t *s)
{
    int i, j;
    opal_list_item_t *item;
    wv_device_values_t *h;
    bool found;

    /* Is the parsed section valid? */
    if (NULL == s->name || 0 == s->vendor_ids_len ||
        0 == s->vendor_part_ids_len) {
        return OMPI_ERR_BAD_PARAM;
    }

    /* Iterate over each of the vendor/part IDs in the parsed
       values */
    for (i = 0; i < s->vendor_ids_len; ++i) {
        for (j = 0; j < s->vendor_part_ids_len; ++j) {
            found = false;

            /* Iterate over all the saved devices */
            for (item = opal_list_get_first(&devices);
                 item != opal_list_get_end(&devices);
                 item = opal_list_get_next(item)) {
                h = (wv_device_values_t*) item;
                if (s->vendor_ids[i] == h->vendor_id &&
                    s->vendor_part_ids[j] == h->vendor_part_id) {
                    /* Found a match.  Update any newly-set values. */
                    if (s->values.mtu_set) {
                        h->values.mtu = s->values.mtu;
                        h->values.mtu_set = true;
                    }

                    if (s->values.use_eager_rdma_set) {
                        h->values.use_eager_rdma = s->values.use_eager_rdma;
                        h->values.use_eager_rdma_set = true;
                    }

                    if (NULL != s->values.receive_queues) {
                        h->values.receive_queues = 
                            strdup(s->values.receive_queues);
                    }

                    if (s->values.max_inline_data_set) {
                        h->values.max_inline_data = s->values.max_inline_data;
                        h->values.max_inline_data_set = true;
                    }

                    if (s->values.rdmacm_reject_causes_connect_error_set) {
                        h->values.rdmacm_reject_causes_connect_error = 
                            s->values.rdmacm_reject_causes_connect_error;
                        h->values.rdmacm_reject_causes_connect_error_set = 
                            true;
                    }

                    found = true;
                    break;
                }
            }

            /* Did we find/update it in the exising list?  If not,
               create a new one. */
            if (!found) {
                h = OBJ_NEW(wv_device_values_t);
                h->section_name = strdup(s->name);
                h->vendor_id = s->vendor_ids[i];
                h->vendor_part_id = s->vendor_part_ids[j];
                /* NOTE: There is a bug in the PGI 6.2 series that
                   causes the compiler to choke when copying structs
                   containing bool members by value.  So do a memcpy
                   here instead. */
                memcpy(&h->values, &s->values, sizeof(s->values));
                /* Need to strdup the string, though */
                if (NULL != h->values.receive_queues) {
                    h->values.receive_queues = strdup(s->values.receive_queues);
                }
                opal_list_append(&devices, &h->super);
            }
        }
    }

    /* All done */

    return OMPI_SUCCESS;
}


/*
 * Do string-to-integer conversion, for both hex and decimal numbers
 */
int ompi_btl_wv_ini_intify(char *str)
{
    while (isspace(*str)) {
        ++str;
    }

    /* If it's hex, use sscanf() */
    if (strlen(str) > 3 && 0 == strncasecmp("0x", str, 2)) {
        unsigned int i;
        sscanf(str, "%X", &i);
        return (int) i;
    }

    /* Nope -- just decimal, so use atoi() */
    return atoi(str);
}


/*
 * Take a comma-delimited list and infity them all
 */
int ompi_btl_wv_ini_intify_list(char *value, uint32_t **values, int *len)
{
    char *comma;
    char *str = value;

    *len = 0;

    /* Comma-delimited list of values */
    comma = strchr(str, ',');
    if (NULL == comma) {
        /* If we only got one value (i.e., no comma found), then
           just make an array of one value and save it */
        *values = (uint32_t *) malloc(sizeof(uint32_t));
        if (NULL == *values) {
            return OMPI_ERR_OUT_OF_RESOURCE;
        }
        *values[0] = (uint32_t) ompi_btl_wv_ini_intify(str);
        *len = 1;
    } else {
        int newsize = 1;

        /* Count how many values there are and allocate enough space
           for them */
        while (NULL != comma) {
            ++newsize;
            str = comma + 1;
            comma = strchr(str, ',');
        }
        *values = (uint32_t *) malloc(sizeof(uint32_t) * newsize);
        if (NULL == *values) {
            return OMPI_ERR_OUT_OF_RESOURCE;
        }

        /* Iterate over the values and save them */
        str = value;
        comma = strchr(str, ',');
        do {
            *comma = '\0';
            (*values)[*len] = (uint32_t) ompi_btl_wv_ini_intify(str);
            ++(*len);
            str = comma + 1;
            comma = strchr(str, ',');
        } while (NULL != comma);
        /* Get the last value (i.e., the value after the last
           comma, because it won't have been snarfed in the
           loop) */
        (*values)[*len] = (uint32_t) ompi_btl_wv_ini_intify(str);
        ++(*len);
    }

    return OMPI_SUCCESS;
}

/*
 * Trival helper function
 */
static inline void show_help(const char *topic)
{
    char *save = btl_wv_ini_yytext;
    if (0 == strcmp("\n", btl_wv_ini_yytext)) {
        btl_wv_ini_yytext = "<end of line>";
    }
    orte_show_help("help-mpi-btl-wv.txt", topic, true,
                   ini_filename, btl_wv_ini_yynewlines,
                   btl_wv_ini_yytext);
    btl_wv_ini_yytext = save;
}