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

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

DEFINITIONS

This source file includes following definitions.
  1. release_cb
  2. lookup_cb
  3. test_publish
  4. test_lookup
  5. test_unpublish
  6. test_publish_lookup_common
  7. test_publish_lookup

   1 /*
   2  * Copyright (c) 2015-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 #include "test_publish.h"
  14 #include <time.h>
  15 #include <src/include/pmix_globals.h>
  16 
  17 typedef struct {
  18     int in_progress;
  19     size_t npdata;
  20     pmix_pdata_t *pdata;
  21 } lookup_cbdata;
  22 
  23 static void release_cb(pmix_status_t status, void *cbdata)
  24 {
  25     int *ptr = (int*)cbdata;
  26     *ptr = 0;
  27 }
  28 
  29 static void lookup_cb(pmix_status_t status,
  30                       pmix_pdata_t pdata[], size_t npdata,
  31                       void *cbdata)
  32 {
  33     size_t i, j;
  34     lookup_cbdata *cb = (lookup_cbdata*)cbdata;
  35     pmix_pdata_t *tgt = cb->pdata;
  36 
  37     /* find the matching key in the provided info array - error if not found */
  38     for (i=0; i < npdata; i++) {
  39         for (j=0; j < cb->npdata; j++) {
  40             if (0 == strcmp(pdata[i].key, tgt[j].key)) {
  41                 /* transfer the value to the pmix_pdata_t */
  42                 (void)strncpy(tgt[j].proc.nspace, pdata[i].proc.nspace, PMIX_MAX_NSLEN);
  43                 tgt[j].proc.rank = pdata[i].proc.rank;
  44                 pmix_value_xfer(&tgt[j].value, &pdata[i].value);
  45                 break;
  46             }
  47         }
  48     }
  49     cb->in_progress = 0;
  50 }
  51 
  52 static int test_publish(char *my_nspace, int my_rank, int blocking)
  53 {
  54     int rc;
  55     pmix_info_t info;
  56     char data[512];
  57 
  58     PMIX_INFO_CONSTRUCT(&info);
  59     (void)snprintf(info.key, PMIX_MAX_KEYLEN, "%s:%d", my_nspace, my_rank);
  60     (void)snprintf(data, 512, "data from proc %s:%d", my_nspace, my_rank);
  61     info.value.type = PMIX_STRING;
  62     info.value.data.string = strdup(data);
  63     if (blocking) {
  64         rc = PMIx_Publish(&info, 1);
  65     } else {
  66         int in_progress = 1;
  67         rc = PMIx_Publish_nb(&info, 1, release_cb, &in_progress);
  68         if (PMIX_SUCCESS == rc) {
  69             PMIX_WAIT_FOR_COMPLETION(in_progress);
  70         }
  71     }
  72     PMIX_INFO_DESTRUCT(&info);
  73     return rc;
  74 }
  75 
  76 static int test_lookup(char *my_nspace, int my_rank, int blocking)
  77 {
  78     int rc;
  79     pmix_pdata_t pdata;
  80     char data[512];
  81     char *keys[2];
  82 
  83     PMIX_PDATA_CONSTRUCT(&pdata);
  84     (void)snprintf(pdata.key, PMIX_MAX_KEYLEN, "%s:%d", my_nspace, my_rank);
  85     (void)snprintf(data, 512, "data from proc %s:%d", my_nspace, my_rank);
  86 
  87     if (blocking) {
  88         if (PMIX_SUCCESS != (rc = PMIx_Lookup(&pdata, 1, NULL, 0))) {
  89             PMIX_PDATA_DESTRUCT(&pdata);
  90             return rc;
  91         }
  92     } else {
  93         keys[0] = (char*)malloc(PMIX_MAX_KEYLEN * sizeof(char));
  94         (void)snprintf(keys[0], PMIX_MAX_KEYLEN, "%s:%d", my_nspace, my_rank);
  95         keys[1] = NULL;
  96 
  97         lookup_cbdata cbdata;
  98         cbdata.in_progress = 1;
  99         cbdata.npdata = 1;
 100         cbdata.pdata = &pdata;
 101         /* copy the key across */
 102         (void)strncpy(pdata.key, keys[0], PMIX_MAX_KEYLEN);
 103         rc = PMIx_Lookup_nb(keys, NULL, 0, lookup_cb, (void*)&cbdata);
 104         if (PMIX_SUCCESS != rc) {
 105             PMIX_PDATA_DESTRUCT(&pdata);
 106             return rc;
 107         }
 108         PMIX_WAIT_FOR_COMPLETION(cbdata.in_progress);
 109     }
 110 
 111     if (PMIX_STRING != pdata.value.type ||
 112             NULL == pdata.value.data.string) {
 113         PMIX_PDATA_DESTRUCT(&pdata);
 114         return PMIX_ERR_NOT_FOUND;
 115     }
 116 
 117     if (strncmp(data, pdata.value.data.string, strlen(data))) {
 118         PMIX_PDATA_DESTRUCT(&pdata);
 119         return PMIX_ERR_NOT_FOUND;
 120     }
 121     PMIX_PDATA_DESTRUCT(&pdata);
 122     return rc;
 123 }
 124 
 125 static int test_unpublish(char *my_nspace, int my_rank, int blocking)
 126 {
 127     int rc;
 128     char *keys[2];
 129 
 130     keys[0] = (char*)malloc(PMIX_MAX_KEYLEN * sizeof(char));
 131     (void)snprintf(keys[0], PMIX_MAX_KEYLEN, "%s:%d", my_nspace, my_rank);
 132     keys[1] = NULL;
 133 
 134     if (blocking) {
 135         rc = PMIx_Unpublish(keys, NULL, 0);
 136     } else {
 137         int in_progress = 1;
 138         rc = PMIx_Unpublish_nb(keys, NULL, 0, release_cb, &in_progress);
 139         if (PMIX_SUCCESS == rc) {
 140             PMIX_WAIT_FOR_COMPLETION(in_progress);
 141         }
 142     }
 143     free(keys[0]);
 144     return rc;
 145 }
 146 
 147 static int test_publish_lookup_common(char *my_nspace, int my_rank, int blocking)
 148 {
 149     int rc;
 150     rc = test_publish(my_nspace, my_rank, blocking);
 151     if (PMIX_SUCCESS != rc) {
 152         TEST_ERROR(("%s:%d: %s failed.", my_nspace, my_rank, blocking ? "PMIX_Publish" : "PMIX_Publish_nb"));
 153         return PMIX_ERROR;
 154     }
 155     TEST_VERBOSE(("%s:%d: %s succeeded.", my_nspace, my_rank, blocking ? "PMIX_Publish" : "PMIX_Publish_nb"));
 156 
 157     rc = test_lookup(my_nspace, my_rank, blocking);
 158     if (PMIX_SUCCESS != rc) {
 159         TEST_ERROR(("%s:%d: %s failed.", my_nspace, my_rank, blocking ? "PMIX_Lookup" : "PMIX_Lookup_nb"));
 160         return PMIX_ERROR;
 161     }
 162     TEST_VERBOSE(("%s:%d: %s succeeded.\n", my_nspace, my_rank, blocking ? "PMIX_Lookup" : "PMIX_Lookup_nb"));
 163 
 164     rc = test_unpublish(my_nspace, my_rank, blocking);
 165     if (PMIX_SUCCESS != rc) {
 166         TEST_ERROR(("%s:%d: %s failed.", my_nspace, my_rank, blocking ? "PMIX_Unpublish" : "PMIX_Unpublish_nb"));
 167         return PMIX_ERROR;
 168     }
 169     TEST_VERBOSE(("%s:%d: %s succeeded.", my_nspace, my_rank, blocking ? "PMIX_Unpublish" : "PMIX_Unpublish_nb"));
 170 
 171     rc = test_lookup(my_nspace, my_rank, blocking);
 172     if (PMIX_ERR_NOT_FOUND != rc) {
 173         TEST_ERROR(("%s:%d: %s function returned %d instead of PMIX_ERR_NOT_FOUND.", my_nspace, my_rank, blocking ? "PMIX_Lookup" : "PMIX_Lookup_nb", rc));
 174         return PMIX_ERROR;
 175     }
 176     return PMIX_SUCCESS;
 177 }
 178 
 179 int test_publish_lookup(char *my_nspace, int my_rank)
 180 {
 181     int rc;
 182     /* test blocking */
 183     rc = test_publish_lookup_common(my_nspace, my_rank, 1);
 184     if (PMIX_SUCCESS != rc) {
 185         TEST_ERROR(("%s:%d: Publish/Lookup blocking test failed.", my_nspace, my_rank));
 186         return PMIX_ERROR;
 187     }
 188     /* test non-blocking */
 189     rc = test_publish_lookup_common(my_nspace, my_rank, 0);
 190     if (PMIX_SUCCESS != rc) {
 191         TEST_ERROR(("%s:%d: Publish/Lookup non-blocking test failed.", my_nspace, my_rank));
 192         return PMIX_ERROR;
 193     }
 194     return PMIX_SUCCESS;
 195 }

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