configuration.c 19.6 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7 8
#include <stdio.h>
#include <stdlib.h>
9
#include <string.h>
10 11
#include <assert.h>
#include <errno.h>
12
#include <libgen.h>
13
#include "codes/configuration.h"
14
#include <ross.h>
15

16
#include <codes/configfile.h>
17
#include "txt_configfile.h"
18

19 20 21 22 23
/*
 * Global to hold configuration in memory
 */
ConfigHandle config;

24 25 26
/* Global to hold LP configuration */
config_lpgroups_t lpconf;

27 28 29 30 31 32 33
int configuration_load (const char *filepath,
                        MPI_Comm comm,
                        ConfigHandle *handle)
{
    MPI_File   fh;
    MPI_Status status;
    MPI_Offset txtsize;
34 35 36 37 38
    FILE      *f = NULL;
    char      *txtdata = NULL;
    char      *error = NULL;
    int        rc = 0;
    char      *tmp_path = NULL;
39 40

    rc = MPI_File_open(comm, (char*)filepath, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
41
    if (rc != MPI_SUCCESS) goto finalize;
42 43

    rc = MPI_File_get_size(fh, &txtsize);
44
    if (rc != MPI_SUCCESS) goto finalize;
45

46
    txtdata = (char*) malloc(txtsize);
47
    assert(txtdata);
48 49

    rc = MPI_File_read_all(fh, txtdata, txtsize, MPI_BYTE, &status);
50
    if (rc != MPI_SUCCESS) goto finalize;
51

52 53 54
#ifdef __APPLE__
    f = fopen(filepath, "r");
#else
55
    f = fmemopen(txtdata, txtsize, "rb");
56
#endif
57
    if (!f) { rc = 1; goto finalize; }
58 59

    *handle = txtfile_openStream(f, &error);
60
    if (error) { rc = 1; goto finalize; }
61

62 63
    /* NOTE: posix version overwrites argument :(. */
    tmp_path = strdup(filepath);
64
    assert(tmp_path);
65
    (*handle)->config_dir = strdup(dirname(tmp_path));
66 67
    assert((*handle)->config_dir);

68
    rc = configuration_get_lpgroups(handle, "LPGROUPS", &lpconf);
69

70 71 72 73 74 75 76 77 78
finalize:
    if (fh != MPI_FILE_NULL) MPI_File_close(&fh);
    if (f) fclose(f);
    free(txtdata);
    free(tmp_path);
    if (error) {
        fprintf(stderr, "config error: %s\n", error);
        free(error);
    }
79

80 81 82 83 84 85
    return rc;
}

int configuration_get_value(ConfigHandle *handle,
                            const char *section_name,
                            const char *key_name,
86
                            const char *annotation,
87 88 89 90 91
                            char *value,
                            size_t len)
{
    SectionHandle section_handle;
    int           rc;
92 93 94 95 96 97 98 99 100
    // reading directly from the config, so need to inject the annotation
    // directly into the search string
    char key_name_tmp[CONFIGURATION_MAX_NAME];
    char *key_name_full;
    if (annotation==NULL){
        // sorry const type... we promise we won't change you
        key_name_full = (char*) key_name;
    }
    else{
101 102 103 104 105 106 107 108 109
        if (snprintf(key_name_tmp, CONFIGURATION_MAX_NAME, "%s@%s",
                    key_name, annotation) >= CONFIGURATION_MAX_NAME) {
            fprintf(stderr,
                    "config error: name@annotation pair too long: %s@%s\n",
                    key_name, annotation);
            return 1;
        }
        else
            key_name_full = key_name_tmp;
110
    }
111 112

    rc = cf_openSection(*handle, ROOT_SECTION, section_name, &section_handle);
113
    if (rc != 1) return 0;
114

115
    rc = cf_getKey(*handle, section_handle, key_name_full, value, len);
116 117 118 119 120
    (void) cf_closeSection(*handle, section_handle);

    return rc;
}

121 122 123 124
int configuration_get_value_relpath(
        ConfigHandle *handle,
        const char * section_name,
        const char * key_name,
125
        const char *annotation,
126 127
        char *value,
        size_t length){
128
    char *tmp = (char*) malloc(length);
129

130
    int w = configuration_get_value(handle, section_name, key_name, annotation, tmp,
131
            length);
132 133
    if (w <= 0)
        return w;
134 135

    /* concat the configuration value with the directory */
136
    w = snprintf(value, length, "%s/%s", (*handle)->config_dir, tmp);
137 138 139 140 141

    free(tmp);
    return w;
}

142 143 144
int configuration_get_multivalue(ConfigHandle *handle,
                                 const char *section_name,
                                 const char *key_name,
145
                                 const char *annotation,
146 147 148 149 150
                                 char ***values,
                                 size_t *len)
{
    SectionHandle section_handle;
    int           rc;
151 152 153 154 155 156 157 158 159
    // reading directly from the config, so need to inject the annotation
    // directly into the search string
    char key_name_tmp[CONFIGURATION_MAX_NAME];
    char *key_name_full;
    if (annotation==NULL){
        // sorry const type... we promise we won't change you
        key_name_full = (char*) key_name;
    }
    else{
160 161 162 163 164 165 166 167 168
        if (snprintf(key_name_tmp, CONFIGURATION_MAX_NAME, "%s@%s",
                    key_name, annotation) >= CONFIGURATION_MAX_NAME) {
            fprintf(stderr,
                    "config error: name@annotation pair too long: %s@%s\n",
                    key_name, annotation);
            return 1;
        }
        else
            key_name_full = key_name_tmp;
169
    }
170 171

    rc = cf_openSection(*handle, ROOT_SECTION, section_name, &section_handle);
172
    if (rc != 1) return rc;
173

174
    rc = cf_getMultiKey(*handle, section_handle, key_name_full, values, len);
175 176 177 178 179
    (void) cf_closeSection(*handle, section_handle);

    return rc;
}

180 181 182
int configuration_get_value_int (ConfigHandle *handle,
                                 const char *section_name,
                                 const char *key_name,
183
                                 const char *annotation,
184 185 186 187 188 189 190 191 192
                                 int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
193
                                annotation,
194 195 196 197 198 199 200 201 202 203 204 205 206 207
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        *value = atoi(valuestr);
        rc = 0;
    }

    return rc;
}

int configuration_get_value_uint (ConfigHandle *handle,
                                  const char *section_name,
                                  const char *key_name,
208
                                  const char *annotation,
209 210 211 212 213 214 215 216 217
                                  unsigned int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
218
                                annotation,
219 220 221 222 223 224 225 226 227 228 229 230 231 232
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        *value = (unsigned int) atoi(valuestr);
        rc = 0;
    }

    return rc;
}

int configuration_get_value_longint (ConfigHandle *handle,
                                     const char *section_name,
                                     const char *key_name,
233
                                     const char *annotation,
234 235 236 237 238 239 240 241 242
                                     long int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
243
                                annotation,
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        errno = 0;
        *value = strtol(valuestr, NULL, 10);
        rc = errno;
    }

    return rc;
}

int configuration_get_value_double (ConfigHandle *handle,
                                    const char *section_name,
                                    const char *key_name,
259
                                    const char *annotation,
260 261 262 263 264 265 266 267 268
                                    double *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
269
                                annotation,
270 271 272 273 274 275 276 277 278 279 280 281
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        errno = 0;
        *value = strtod(valuestr, NULL);
        rc = errno;
    }

    return rc;
}

282
static void check_add_anno(
283
        int anno_offset,
284
        config_anno_map_t *map){
285
    if (anno_offset == -1) {
286
        map->has_unanno_lp = 1;
287 288
        if (!map->num_annos)
            map->is_unanno_first = 1;
289 290
    }
    else{
291
        int a = 0;
292
        for (; a < map->num_annos; a++){
293
            if (map->annotations[a].offset == anno_offset) {
294 295 296 297 298 299 300
                map->num_anno_lps[a]++;
                break;
            }
        }
        if (a == map->num_annos){
            // we have a new anno!
            assert(a < CONFIGURATION_MAX_ANNOS);
301
            map->annotations[a].offset = anno_offset;
302 303 304 305 306 307
            map->num_annos++;
            map->num_anno_lps[a] = 1;
        } // else anno was already there, do nothing
    }
}
static void check_add_lp_type_anno(
308 309
        int lp_name_offset,
        int anno_offset,
310
        config_lpgroups_t *lpgroups){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
311
    int lpt_anno = 0;
312 313
    for (; lpt_anno < lpgroups->lpannos_count; lpt_anno++){
        config_anno_map_t *map = &lpgroups->lpannos[lpt_anno];
314 315
        if (map->lp_name.offset == lp_name_offset){
            check_add_anno(anno_offset, map);
316
            break;
317 318 319 320 321 322 323
        }
    }
    if (lpt_anno == lpgroups->lpannos_count){
        // we haven't seen this lp type before
        assert(lpt_anno < CONFIGURATION_MAX_TYPES);
        config_anno_map_t *map = &lpgroups->lpannos[lpt_anno];
        // initialize this annotation map
324
        map->lp_name.offset = lp_name_offset;
325
        map->num_annos = 0;
326
        map->has_unanno_lp = 0;
327
        map->is_unanno_first = 0;
328 329
        memset(map->num_anno_lps, 0, 
                CONFIGURATION_MAX_ANNOS*sizeof(*map->num_anno_lps));
330
        check_add_anno(anno_offset, map);
331 332 333 334
        lpgroups->lpannos_count++;
    }
}

335 336 337 338 339 340 341 342 343 344
#define REALLOC_IF(_cap_var, _len_exp, _buf_var) \
    do { \
        while ((_cap_var) <= (_len_exp)) { \
            _cap_var *= 2; \
            _buf_var = realloc(_buf_var, (_cap_var) * sizeof(*_buf_var)); \
            assert(_buf_var); \
        } \
    } while (0)

/* helper for setting up the canonical name mapping */
345 346
static int check_add_uniq_str(
        int ** offset_array,
347 348
        char ** str_buf,
        int * num_strs,
349
        int * offset_array_cap, // buffer capacity for resizing
350 351 352 353 354 355 356
        int * str_buf_len,
        int * str_buf_cap,   // buffer capacity for resizing
        char const * str)
{
    int slen = strlen(str);

    for (int i = 0; i < *num_strs; i++) {
357
        char const * b = (*str_buf) + (*offset_array)[i];
358 359
        int blen = strlen(b);
        if (slen == blen && memcmp(b, str, blen) == 0)
360
            return i;
361 362
    }

363
    REALLOC_IF(*offset_array_cap, *num_strs, *offset_array);
364 365 366 367
    REALLOC_IF(*str_buf_cap, *str_buf_len + slen + 1, *str_buf);

    // include null char
    memcpy(*str_buf + *str_buf_len, str, slen+1);
368
    (*offset_array)[*num_strs] = *str_buf_len;
369 370
    *num_strs += 1;
    *str_buf_len += slen+1;
371
    return *num_strs-1;
372 373
}

374 375 376
int configuration_get_lpgroups (ConfigHandle *handle,
                                const char *section_name,
                                config_lpgroups_t *lpgroups)
377 378 379 380 381 382 383 384
{
    SectionHandle sh;
    SectionHandle subsh;
    SectionEntry se[10];
    SectionEntry subse[10];
    size_t se_count = 10;
    size_t subse_count = 10;
    char data[256];
385
    // buffer mgmt vars
386 387 388 389 390 391 392 393 394 395
    int num_uniq_group_names = 0;
    int group_names_buf_len = 0;
    int lp_names_buf_len = 0;
    int anno_names_buf_len = 0;
    int group_names_cap = 1;
    int lp_names_cap = 1;
    int anno_names_cap = 1;
    int group_names_buf_cap = 1;
    int lp_names_buf_cap = 1;
    int anno_names_buf_cap = 1;
396
    int name_pos;
397

398
    memset (lpgroups, 0, sizeof(*lpgroups));
399

400 401 402 403 404 405
    int *group_names_offsets =
        malloc(sizeof(*group_names_offsets) * group_names_cap);
    int *lp_names_offsets =
        malloc(sizeof(*lp_names_offsets) * lp_names_cap);
    int *anno_names_offsets =
        malloc(sizeof(*anno_names_offsets) * anno_names_cap);
406 407 408 409 410 411
    lpgroups->group_names_buf =
        malloc(sizeof(*lpgroups->group_names_buf) * group_names_buf_cap);
    lpgroups->lp_names_buf =
        malloc(sizeof(*lpgroups->lp_names_buf) * lp_names_buf_cap);
    lpgroups->anno_names_buf =
        malloc(sizeof(*lpgroups->anno_names_buf) * anno_names_buf_cap);
412 413 414
    assert(group_names_offsets != NULL);
    assert(lp_names_offsets != NULL);
    assert(anno_names_offsets != NULL);
415 416 417 418
    assert(lpgroups->group_names_buf != NULL);
    assert(lpgroups->lp_names_buf != NULL);
    assert(lpgroups->anno_names_buf != NULL);

419 420 421
    int ret = cf_openSection(*handle, ROOT_SECTION, section_name, &sh);
    if (ret == -1)
        return -1;
422
    cf_listSection(*handle, sh, se, &se_count); 
423

424 425 426 427 428 429 430 431 432 433 434
#define CHECKED_STRTOL(_val, _field, _data) \
    do{ \
        errno = 0; \
        long int _rd = strtol(_data, NULL, 10); \
        if (_rd <= 0 || errno) \
            tw_error(TW_LOC, "bad value (expected positive integer) for " \
                    "\"%s\": %s\n", _field, _data); \
        else \
            _val = _rd; \
    }while(0);

435
    for (size_t i = 0; i < se_count; i++)
436
    {
437
        //printf("section: %s type: %d\n", se[i].name, se[i].type);
438 439 440
        if (se[i].type == SE_SECTION)
        {
            subse_count = 10;
441 442
            cf_openSection(*handle, sh, se[i].name, &subsh);
            cf_listSection(*handle, subsh, subse, &subse_count);
443
            name_pos = check_add_uniq_str(&group_names_offsets,
444 445 446
                    &lpgroups->group_names_buf, &num_uniq_group_names,
                    &group_names_cap, &group_names_buf_len,
                    &group_names_buf_cap, se[i].name);
447
            lpgroups->lpgroups[i].name.offset = group_names_offsets[name_pos];
448 449
            lpgroups->lpgroups[i].repetitions = 1;
            lpgroups->lpgroups_count++;
450 451 452 453
            if (num_uniq_group_names != lpgroups->lpgroups_count)
                tw_error(TW_LOC,
                        "config error: non-unique group names detected\n");

454
            for (size_t j = 0, lpt = 0; j < subse_count; j++)
455 456 457
            {
                if (subse[j].type == SE_KEY)
                {
458 459
                   cf_getKey(*handle, subsh, subse[j].name, data, sizeof(data));
                   //printf("key: %s value: %s\n", subse[j].name, data);
460 461
                   if (strcmp("repetitions", subse[j].name) == 0)
                   {
462 463
                       CHECKED_STRTOL(lpgroups->lpgroups[i].repetitions,
                               "repetitions", data);
464
		       //printf("\n Repetitions: %ld ", lpgroups->lpgroups[i].repetitions);
465 466 467
                   }
                   else
                   {
468 469 470 471 472
                       // to avoid copy, find a possible annotation, change the
                       // string in the config structure itself, then change
                       // back for posterity
                       char *c = strchr(subse[j].name, '@');
                       if (c != NULL) {
473
                           *c = '\0';
474 475 476 477 478 479 480 481 482 483
                           name_pos = check_add_uniq_str(
                                   &anno_names_offsets,
                                   &lpgroups->anno_names_buf,
                                   &lpgroups->num_uniq_annos,
                                   &anno_names_cap,
                                   &anno_names_buf_len,
                                   &anno_names_buf_cap,
                                   c+1);
                           lpgroups->lpgroups[i].lptypes[lpt].anno.offset =
                                   anno_names_offsets[name_pos];
484
                       }
485 486
                       else
                           lpgroups->lpgroups[i].lptypes[lpt].anno.offset = -1;
487

488 489
                       name_pos = check_add_uniq_str(
                               &lp_names_offsets,
490 491 492 493 494
                               &lpgroups->lp_names_buf,
                               &lpgroups->num_uniq_lptypes,
                               &lp_names_cap,
                               &lp_names_buf_len,
                               &lp_names_buf_cap,
495 496 497 498 499 500
                               subse[j].name);
                       lpgroups->lpgroups[i].lptypes[lpt].name.offset =
                               lp_names_offsets[name_pos];

                       if (c != NULL)
                           *c = '@';
501

502
                       // add to anno map
503 504 505 506
                       check_add_lp_type_anno(
                               lpgroups->lpgroups[i].lptypes[lpt].name.offset,
                               lpgroups->lpgroups[i].lptypes[lpt].anno.offset,
                               lpgroups);
507
                       CHECKED_STRTOL(lpgroups->lpgroups[i].lptypes[lpt].count,
508
                               lpgroups->lpgroups[i].lptypes[lpt].name, data);
509
                       lpgroups->lpgroups[i].lptypes_count++;
510 511 512 513
                       lpt++;
                   }
                }
            }
514
            cf_closeSection(*handle, subsh);
515 516 517
        }
    }

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    // set up the string pointers
    lpgroups->group_names =
        malloc(lpgroups->lpgroups_count * sizeof(*lpgroups->group_names));
    lpgroups->lp_names =
        malloc(lpgroups->num_uniq_lptypes * sizeof(*lpgroups->lp_names));
    assert(lpgroups->group_names);
    assert(lpgroups->lp_names);

    for (int i = 0; i < lpgroups->lpgroups_count; i++) {
        lpgroups->group_names[i] = lpgroups->group_names_buf +
            group_names_offsets[i];
    }
    for (int i = 0; i < lpgroups->num_uniq_lptypes; i++) {
        lpgroups->lp_names[i] = lpgroups->lp_names_buf +
            lp_names_offsets[i];
    }
    if (lpgroups->num_uniq_annos == 0) {
535 536 537 538
        free(lpgroups->anno_names_buf);
        lpgroups->anno_names = NULL;
        lpgroups->anno_names_buf = NULL;
    }
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
    else {
        lpgroups->anno_names =
            malloc(lpgroups->num_uniq_annos * sizeof(*lpgroups->anno_names));
        assert(lpgroups->anno_names);
        for (int i = 0; i < lpgroups->num_uniq_annos; i++) {
            lpgroups->anno_names[i] = lpgroups->anno_names_buf +
                anno_names_offsets[i];
        }
    }

    // everything is set up in offset mode, now make a second pass and convert
    // to pointers
    for (int g = 0; g < lpgroups->lpgroups_count; g++) {
        config_lpgroup_t *lpg = &lpgroups->lpgroups[g];
        lpg->name.ptr = lpgroups->group_names_buf + lpg->name.offset;
        for (int t = 0; t < lpg->lptypes_count; t++) {
            config_lptype_t *lpt = &lpg->lptypes[t];
            lpt->name.ptr = lpgroups->lp_names_buf + lpt->name.offset;
            lpt->anno.ptr =
                (lpt->anno.offset == -1)
                    ? NULL
                    : lpgroups->anno_names_buf + lpt->anno.offset;
        }
    }
    for (int m = 0; m < lpgroups->lpannos_count; m++) {
        config_anno_map_t *map = &lpgroups->lpannos[m];
        map->lp_name.ptr = lpgroups->lp_names_buf + map->lp_name.offset;
        for (int a = 0; a < map->num_annos; a++) {
            map->annotations[a].ptr =
                (map->annotations[a].offset == -1)
                    ? NULL
                    : lpgroups->anno_names_buf + map->annotations[a].offset;
        }
    }
573

574
    cf_closeSection(*handle, sh);
575 576 577 578 579

    free(group_names_offsets);
    free(lp_names_offsets);
    free(anno_names_offsets);

580 581
    return 0;
}
Philip Carns's avatar
Philip Carns committed
582

583 584 585 586 587
/*
 * Helper function - get the position in the LP annotation list of the
 * given annotation. Used for configuration schemes where an array of
 * configuration values is generated based on the annotations in
 * config_anno_map_t
588 589
 * If anno is not found or a NULL anno is passed in,
 * -1 is returned */
590 591
int configuration_get_annotation_index(const char *              anno,
                                       const config_anno_map_t * anno_map){
592
    if (anno == NULL) return -1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
593
    for (int i = 0; i < anno_map->num_annos; i++){
594
        if (!strcmp(anno, anno_map->annotations[i].ptr)){
595 596 597 598 599 600
            return (int)i;
        }
    }
    return -1;
}

Philip Carns's avatar
Philip Carns committed
601 602 603 604 605 606 607 608
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */