root/opal/mca/pmix/pmix4x/pmix/src/util/name_fns.c

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

DEFINITIONS

This source file includes following definitions.
  1. buffer_cleanup
  2. get_print_name_buffer
  3. pmix_util_print_name_args
  4. pmix_util_print_rank

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2011 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2010      Oracle and/or its affiliates.  All rights reserved.
  13  * Copyright (c) 2014-2016 Research Organization for Information Science
  14  *                         and Technology (RIST). All rights reserved.
  15  * Copyright (c) 2016-2018 Intel, Inc. All rights reserved.
  16  * $COPYRIGHT$
  17  *
  18  * Additional copyrights may follow
  19  *
  20  * $HEADER$
  21  */
  22 #include "pmix_config.h"
  23 
  24 #include <stdio.h>
  25 #include <string.h>
  26 
  27 #include "pmix_common.h"
  28 
  29 #include "src/threads/tsd.h"
  30 #include "src/util/error.h"
  31 #include "src/util/name_fns.h"
  32 #include "src/util/printf.h"
  33 
  34 #define PMIX_PRINT_NAME_ARGS_MAX_SIZE   300
  35 #define PMIX_PRINT_NAME_ARG_NUM_BUFS    16
  36 
  37 #define PMIX_SCHEMA_DELIMITER_CHAR      '.'
  38 #define PMIX_SCHEMA_WILDCARD_CHAR       '*'
  39 #define PMIX_SCHEMA_WILDCARD_STRING     "*"
  40 #define PMIX_SCHEMA_INVALID_CHAR        '$'
  41 #define PMIX_SCHEMA_INVALID_STRING      "$"
  42 
  43 static bool fns_init=false;
  44 
  45 static pmix_tsd_key_t print_args_tsd_key;
  46 char* pmix_print_args_null = "NULL";
  47 typedef struct {
  48     char *buffers[PMIX_PRINT_NAME_ARG_NUM_BUFS];
  49     int cntr;
  50 } pmix_print_args_buffers_t;
  51 
  52 static void
  53 buffer_cleanup(void *value)
  54 {
  55     int i;
  56     pmix_print_args_buffers_t *ptr;
  57 
  58     if (NULL != value) {
  59         ptr = (pmix_print_args_buffers_t*)value;
  60         for (i=0; i < PMIX_PRINT_NAME_ARG_NUM_BUFS; i++) {
  61             free(ptr->buffers[i]);
  62         }
  63         free (ptr);
  64     }
  65 }
  66 
  67 static pmix_print_args_buffers_t*
  68 get_print_name_buffer(void)
  69 {
  70     pmix_print_args_buffers_t *ptr;
  71     int ret, i;
  72 
  73     if (!fns_init) {
  74         /* setup the print_args function */
  75         if (PMIX_SUCCESS != (ret = pmix_tsd_key_create(&print_args_tsd_key, buffer_cleanup))) {
  76             PMIX_ERROR_LOG(ret);
  77             return NULL;
  78         }
  79         fns_init = true;
  80     }
  81 
  82     ret = pmix_tsd_getspecific(print_args_tsd_key, (void**)&ptr);
  83     if (PMIX_SUCCESS != ret) return NULL;
  84 
  85     if (NULL == ptr) {
  86         ptr = (pmix_print_args_buffers_t*)malloc(sizeof(pmix_print_args_buffers_t));
  87         for (i=0; i < PMIX_PRINT_NAME_ARG_NUM_BUFS; i++) {
  88             ptr->buffers[i] = (char *) malloc((PMIX_PRINT_NAME_ARGS_MAX_SIZE+1) * sizeof(char));
  89         }
  90         ptr->cntr = 0;
  91         ret = pmix_tsd_setspecific(print_args_tsd_key, (void*)ptr);
  92     }
  93 
  94     return (pmix_print_args_buffers_t*) ptr;
  95 }
  96 
  97 char* pmix_util_print_name_args(const pmix_proc_t *name)
  98 {
  99     pmix_print_args_buffers_t *ptr;
 100     char *rank;
 101 
 102     /* get the next buffer */
 103     ptr = get_print_name_buffer();
 104     if (NULL == ptr) {
 105         PMIX_ERROR_LOG(PMIX_ERR_OUT_OF_RESOURCE);
 106         return pmix_print_args_null;
 107     }
 108     /* cycle around the ring */
 109     if (PMIX_PRINT_NAME_ARG_NUM_BUFS == ptr->cntr) {
 110         ptr->cntr = 0;
 111     }
 112 
 113     /* protect against NULL names */
 114     if (NULL == name) {
 115         snprintf(ptr->buffers[ptr->cntr++], PMIX_PRINT_NAME_ARGS_MAX_SIZE, "[NO-NAME]");
 116         return ptr->buffers[ptr->cntr-1];
 117     }
 118 
 119     rank = pmix_util_print_rank(name->rank);
 120 
 121     snprintf(ptr->buffers[ptr->cntr++],
 122              PMIX_PRINT_NAME_ARGS_MAX_SIZE,
 123              "[%s,%s]", name->nspace, rank);
 124 
 125     return ptr->buffers[ptr->cntr-1];
 126 }
 127 
 128 char* pmix_util_print_rank(const pmix_rank_t vpid)
 129 {
 130     pmix_print_args_buffers_t *ptr;
 131 
 132     ptr = get_print_name_buffer();
 133 
 134     if (NULL == ptr) {
 135         PMIX_ERROR_LOG(PMIX_ERR_OUT_OF_RESOURCE);
 136         return pmix_print_args_null;
 137     }
 138 
 139     /* cycle around the ring */
 140     if (PMIX_PRINT_NAME_ARG_NUM_BUFS == ptr->cntr) {
 141         ptr->cntr = 0;
 142     }
 143 
 144     if (PMIX_RANK_UNDEF == vpid) {
 145         snprintf(ptr->buffers[ptr->cntr++], PMIX_PRINT_NAME_ARGS_MAX_SIZE, "UNDEF");
 146     } else if (PMIX_RANK_WILDCARD == vpid) {
 147         snprintf(ptr->buffers[ptr->cntr++], PMIX_PRINT_NAME_ARGS_MAX_SIZE, "WILDCARD");
 148     } else {
 149         snprintf(ptr->buffers[ptr->cntr++],
 150                  PMIX_PRINT_NAME_ARGS_MAX_SIZE,
 151                  "%ld", (long)vpid);
 152     }
 153     return ptr->buffers[ptr->cntr-1];
 154 }

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