root/opal/mca/pmix/pmix4x/pmix/test/pmi_client.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. log_fatal
  2. log_error
  3. log_info
  4. random_value
  5. main
  6. test_item1
  7. test_item2
  8. test_item3
  9. test_item4
  10. test_item5
  11. test_item6
  12. test_item7

   1 /*
   2  * Copyright (c) 2013-2017 Intel, Inc.  All rights reserved.
   3  * Copyright (c) 2015      Mellanox Technologies, Inc.
   4  *                         All rights reserved.
   5  * $COPYRIGHT$
   6  *
   7  * Additional copyrights may follow
   8  *
   9  * $HEADER$
  10  *
  11  */
  12 
  13 #define _GNU_SOURCE
  14 #include <stdio.h>
  15 #include <stdlib.h>
  16 #include <string.h>
  17 #include <time.h>
  18 #include <stdarg.h>
  19 
  20 #include "pmi.h"
  21 
  22 /* Target is legacy SLURM pmi library implementation */
  23 static int _legacy = 0;
  24 /* Verbose level 0-silent, 1-fatal, 2-error, 3+ debug*/
  25 static int _verbose = 1;
  26 
  27 static void log_fatal(const char *format, ...)
  28 {
  29     va_list arglist;
  30     char *output = NULL;
  31 
  32     va_start(arglist, format);
  33     if (_verbose > 0) {
  34         if (0 > vasprintf(&output, format, arglist) ||
  35             NULL == output) {
  36             va_end(arglist);
  37             return;
  38         }
  39         fprintf(stderr, "FATAL: %s", output);
  40         free(output);
  41     }
  42     va_end(arglist);
  43 }
  44 
  45 static void log_error(const char *format, ...)
  46 {
  47     va_list arglist;
  48     char *output = NULL;
  49 
  50     va_start(arglist, format);
  51     if (_verbose > 0) {
  52         if (0 > vasprintf(&output, format, arglist) ||
  53             NULL == output) {
  54             va_end(arglist);
  55             return;
  56         }
  57         fprintf(stderr, "ERROR: %s", output);
  58         free(output);
  59     }
  60     va_end(arglist);
  61 }
  62 
  63 static void log_info(const char *format, ...)
  64 {
  65     va_list arglist;
  66     char *output = NULL;
  67 
  68     va_start(arglist, format);
  69     if (_verbose > 0) {
  70         if (0 > vasprintf(&output, format, arglist) ||
  71             NULL == output) {
  72             va_end(arglist);
  73             return;
  74         }
  75         fprintf(stderr, "INFO: %s", output);
  76         free(output);
  77     }
  78     va_end(arglist);
  79 }
  80 
  81 #define log_assert(e, msg) \
  82     do {                                                                \
  83         if (!(e)) {                                                     \
  84             log_fatal("%s at %s:%d\n", msg, __func__, __LINE__);    \
  85             rc = -1;                                                    \
  86         }                                                               \
  87     } while (0)
  88 
  89 static inline long random_value(long min_value, long max_value)
  90 {
  91    return ((min_value >= max_value) ? min_value : min_value + (rand() % (max_value - min_value + 1)));
  92 }
  93 
  94 static int test_item1(void);
  95 static int test_item2(void);
  96 static int test_item3(void);
  97 static int test_item4(void);
  98 static int test_item5(void);
  99 static int test_item6(void);
 100 static int test_item7(void);
 101 
 102 static int spawned, size, rank, appnum;
 103 static char jobid[255];
 104 
 105 
 106 int main(int argc, char **argv)
 107 {
 108     int ret = 0;
 109     int rc;
 110     char *str = NULL;
 111     int ti = (argc > 1 ? atoi(argv[1]) : 0);
 112 
 113     srand(time(NULL));
 114     str = getenv("VERBOSE");
 115     _verbose = (str ? atoi(str) : _verbose);
 116 
 117     spawned = random_value(10, 20);
 118     size = random_value(10, 20);
 119     rank = random_value(10, 20);
 120     appnum = random_value(10, 20);
 121     if (PMI_SUCCESS != (rc = PMI_Init(&spawned))) {
 122         log_fatal("PMI_Init failed: %d\n", rc);
 123         return rc;
 124     }
 125 
 126     str = getenv("PMIX_NAMESPACE");
 127     _legacy = (str ? 0 : 1);
 128 
 129     /* this test should be always run */
 130     if (1) {
 131         rc = test_item1();
 132         ret += (rc ? 1 : 0);
 133         log_info("TI1  : %s\n", (rc ? "FAIL" : "PASS"));
 134     }
 135 
 136     if (!ti || 2 == ti) {
 137         rc = test_item2();
 138         ret += (rc ? 1 : 0);
 139         log_info("TI2  : %s\n", (rc ? "FAIL" : "PASS"));
 140     }
 141 
 142     if (!ti || 3 == ti) {
 143         rc = test_item3();
 144         ret += (rc ? 1 : 0);
 145         log_info("TI3  : %s\n", (rc ? "FAIL" : "PASS"));
 146     }
 147 
 148     if (!ti || 4 == ti) {
 149         rc = test_item4();
 150         ret += (rc ? 1 : 0);
 151         log_info("TI4  : %s\n", (rc ? "FAIL" : "PASS"));
 152     }
 153 
 154     if (!ti || 5 == ti) {
 155         rc = test_item5();
 156         ret += (rc ? 1 : 0);
 157         log_info("TI5  : %s\n", (rc ? "FAIL" : "PASS"));
 158     }
 159 
 160     if (!ti || 6 == ti) {
 161         rc = test_item6();
 162         ret += (rc ? 1 : 0);
 163         log_info("TI6  : %s\n", (rc ? "FAIL" : "PASS"));
 164     }
 165 
 166     if (!ti || 7 == ti) {
 167         rc = test_item7();
 168         ret += (rc ? 1 : 0);
 169         log_info("TI7  : %s\n", (rc ? "FAIL" : "PASS"));
 170     }
 171 
 172     if (PMI_SUCCESS != (rc = PMI_Finalize())) {
 173         log_fatal("PMI_Finalize failed: %d\n", rc);
 174         return rc;
 175     }
 176 
 177     return ret;
 178 }
 179 
 180 static int test_item1(void)
 181 {
 182     int rc = 0;
 183     int val = 0;
 184 
 185     log_assert(spawned == PMI_FALSE || spawned == PMI_TRUE, "");
 186 
 187     if (PMI_SUCCESS != (rc = PMI_Get_size(&size))) {
 188         log_fatal("PMI_Get_Size failed: %d\n", rc);
 189         return rc;
 190     }
 191     log_assert(size >= 0, "");
 192 
 193     if (PMI_SUCCESS != (rc = PMI_Get_rank(&rank))) {
 194         log_fatal("PMI_Get_Rank failed: %d\n", rc);
 195         return rc;
 196     }
 197     log_assert(rank >= 0, "");
 198     log_assert(rank < size, "");
 199 
 200     if (PMI_SUCCESS != (rc = PMI_Get_appnum(&appnum))) {
 201         log_fatal("PMI_Get_appnum failed: %d\n", rc);
 202         return rc;
 203     }
 204 
 205     log_info("spawned=%d size=%d rank=%d appnum=%d\n", spawned, size, rank, appnum);
 206 
 207     val = random_value(10, 100);
 208     if (PMI_SUCCESS != (rc = PMI_Get_universe_size(&val))) {
 209         log_fatal("PMI_Get_universe_size failed: %d\n", rc);
 210         return rc;
 211     }
 212     log_assert(size == val, "");
 213 
 214     val = random_value(10, 100);
 215     if (PMI_SUCCESS != (rc = PMI_Get_id_length_max(&val))) {
 216         log_fatal("PMI_Get_id_length_max failed: %d\n", rc);
 217         return rc;
 218     }
 219     log_info("PMI_Get_id_length_max=%d\n", val);
 220     if (!_legacy) {
 221         log_assert(sizeof(jobid) == val, "Check PMIX_MAX_NSLEN value in pmix_common.h");
 222     }
 223 
 224     sprintf(jobid, "%s", __func__);
 225     if (PMI_SUCCESS != (rc = PMI_Get_id(jobid, sizeof(jobid)))) {
 226         log_fatal("PMI_Get_id failed: %d\n", rc);
 227         return rc;
 228     }
 229 
 230     log_info("jobid=%s\n", jobid);
 231     log_assert(memcmp(jobid, __func__, sizeof(__func__)), "");
 232 
 233     sprintf(jobid, "%s", __func__);
 234     if (PMI_SUCCESS != (rc = PMI_Get_kvs_domain_id(jobid, sizeof(jobid)))) {
 235         log_fatal("PMI_Get_kvs_domain_id failed: %d\n", rc);
 236         return rc;
 237     }
 238 
 239     log_info("PMI_Get_kvs_domain_id=%s\n", jobid);
 240     log_assert(memcmp(jobid, __func__, sizeof(__func__)), "");
 241 
 242     sprintf(jobid, "%s", __func__);
 243     if (PMI_SUCCESS != (rc = PMI_KVS_Get_my_name(jobid, sizeof(jobid)))) {
 244         log_fatal("PMI_KVS_Get_my_name failed: %d\n", rc);
 245         return rc;
 246     }
 247 
 248     log_info("PMI_KVS_Get_my_name=%s\n", jobid);
 249     log_assert(memcmp(jobid, __func__, sizeof(__func__)), "");
 250 
 251     return rc;
 252 }
 253 
 254 static int test_item2(void)
 255 {
 256     int rc = 0;
 257     PMI_BOOL val;
 258 
 259     if (PMI_SUCCESS != (rc = PMI_Initialized(&val))) {
 260         log_fatal("PMI_Initialized failed: %d\n", rc);
 261         return rc;
 262     }
 263     log_assert(PMI_TRUE == val, "");
 264 
 265     return rc;
 266 }
 267 
 268 static int test_item3(void)
 269 {
 270     int rc = 0;
 271     int val = 0;
 272 
 273     val = random_value(10, 100);
 274     if (PMI_SUCCESS != (rc = PMI_KVS_Get_key_length_max(&val))) {
 275         log_fatal("PMI_KVS_Get_key_length_max failed: %d\n", rc);
 276         return rc;
 277     }
 278     log_info("PMI_KVS_Get_key_length_max=%d\n", val);
 279     if (!_legacy) {
 280         log_assert(511 == val, "Check PMIX_MAX_KEYLEN value in pmix_common.h");
 281     }
 282 
 283     val = random_value(10, 100);
 284     if (PMI_SUCCESS != (rc = PMI_KVS_Get_value_length_max(&val))) {
 285         log_fatal("PMI_KVS_Get_value_length_max failed: %d\n", rc);
 286         return rc;
 287     }
 288     log_info("PMI_KVS_Get_value_length_max=%d\n", val);
 289     if (!_legacy) {
 290         log_assert(4096 == val, "Check limitation for a value");
 291     }
 292 
 293     return rc;
 294 }
 295 
 296 static int test_item4(void)
 297 {
 298     int rc = 0;
 299     int val = 0;
 300     int *ranks = NULL;
 301     int i = 0;
 302 
 303     val = -1;
 304     if (PMI_SUCCESS != (rc = PMI_Get_clique_size(&val))) {
 305         log_fatal("PMI_Get_clique_size failed: %d\n", rc);
 306         return rc;
 307     }
 308     log_info("PMI_Get_clique_size=%d\n", val);
 309     log_assert((0 < val) && (val <= size), "");
 310 
 311     ranks = alloca(val*sizeof(int));
 312     if (!ranks) {
 313         return PMI_FAIL;
 314     }
 315 
 316     memset(ranks, (-1), val*sizeof(int));
 317     if (PMI_SUCCESS != (rc = PMI_Get_clique_ranks(ranks, val))) {
 318         log_fatal("PMI_Get_clique_ranks failed: %d\n", rc);
 319         return rc;
 320     }
 321 
 322     for (i = 0; i < val; i++) {
 323         if (!((0 <= ranks[i]) && (ranks[i] < size))) {
 324             log_fatal("found invalid value in ranks array: ranks[%d]=%d\n", i, ranks[i]);
 325             return rc;
 326         }
 327     }
 328 
 329     return rc;
 330 }
 331 
 332 static int test_item5(void)
 333 {
 334     int rc = 0;
 335     char *val = NULL;
 336     int val_size = 0;
 337     /* Predefined Job attributes */
 338     const char *tkeys[] = {
 339             "PMI_process_mapping",
 340             NULL
 341     };
 342     const char **ptr = tkeys;
 343 
 344     if (!_legacy) {
 345         log_error("%s\n", "PMIx and SLURM/PMI1 do not set 'PMI_process_mapping' (Do not mark test as failed)");
 346         return rc;
 347     }
 348 
 349     if (PMI_SUCCESS != (rc = PMI_KVS_Get_value_length_max(&val_size))) {
 350         log_fatal("PMI_KVS_Get_value_length_max failed: %d\n", rc);
 351         return rc;
 352     }
 353 
 354     val = alloca(val_size);
 355     if (!val) {
 356         return PMI_FAIL;
 357     }
 358 
 359     while (*ptr) {
 360         if (PMI_SUCCESS != (rc = PMI_KVS_Get(jobid, *ptr, val, val_size))) {
 361             log_fatal("PMI_KVS_Get: [%s] %d\n", *ptr, rc);
 362             return rc;
 363         }
 364         log_info("key=%s value=%.80s\n", *ptr, val);
 365         ptr++;
 366     }
 367 
 368     return rc;
 369 }
 370 
 371 static int test_item6(void)
 372 {
 373     int rc = 0;
 374     char val[100];
 375     const char *tkey = __func__;
 376     const char *tval = __FILE__;
 377 
 378     if (PMI_SUCCESS != (rc = PMI_KVS_Put(jobid, tkey, tval))) {
 379         log_fatal("PMI_KVS_Put %d\n", rc);
 380         return rc;
 381     }
 382 
 383     if (PMI_SUCCESS != (rc = PMI_KVS_Get(jobid, tkey, val, sizeof(val)))) {
 384         log_fatal("PMI_KVS_Get %d\n", rc);
 385         return rc;
 386     }
 387 
 388     log_info("tkey=%s tval=%s val=%s\n", tkey, tval, val);
 389 
 390     log_assert(!strcmp(tval, val), "value does not meet expectation");
 391 
 392     return rc;
 393 }
 394 
 395 static int test_item7(void)
 396 {
 397     int rc = 0;
 398     char tkey[100];
 399     char tval[100];
 400     char val[100];
 401     int i = 0;
 402 
 403     for (i = 0; i < size; i++) {
 404         sprintf(tkey, "KEY-%d", i);
 405         sprintf(tval, "VALUE-%d", i);
 406         if (i == rank) {
 407             if (PMI_SUCCESS != (rc = PMI_KVS_Put(jobid, tkey, tval))) {
 408                 log_fatal("PMI_KVS_Put [%s=%s] %d\n", tkey, tval, rc);
 409                 return rc;
 410             }
 411         }
 412     }
 413 
 414     if (PMI_SUCCESS != (rc = PMI_KVS_Commit(jobid))) {
 415         log_fatal("PMI_KVS_Commit %d\n", rc);
 416         return rc;
 417     }
 418 
 419     if (PMI_SUCCESS != (rc = PMI_Barrier())) {
 420         log_fatal("PMI_Barrier %d\n", rc);
 421         return rc;
 422     }
 423 
 424     for (i = 0; i < size; i++) {
 425         sprintf(tkey, "KEY-%d", i);
 426         sprintf(tval, "VALUE-%d", i);
 427         if (PMI_SUCCESS != (rc = PMI_KVS_Get(jobid, tkey, val, sizeof(val)))) {
 428             log_fatal("PMI_KVS_Get [%s=?] %d\n", tkey, rc);
 429             return rc;
 430         }
 431 
 432         log_info("tkey=%s tval=%s val=%s\n", tkey, tval, val);
 433 
 434         log_assert(!strcmp(tval, val), "value does not meet expectation");
 435     }
 436 
 437     return rc;
 438 }

/* [<][>][^][v][top][bottom][index][help] */