root/oshmem/info/info.c

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

DEFINITIONS

This source file includes following definitions.
  1. oshmem_info_init
  2. oshmem_info_finalize
  3. oshmem_info_value_to_bool
  4. oshmem_info_value_to_int
  5. oshmem_info_get_heap_size
  6. oshmem_info_get_library_version

   1 /*
   2  * Copyright (c) 2015      Mellanox Technologies, Inc.
   3  *                         All rights reserved.
   4  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
   5  * Copyright (c) 2019      Research Organization for Information Science
   6  *                         and Technology (RIST).  All rights reserved.
   7  * $COPYRIGHT$
   8  *
   9  * Additional copyrights may follow
  10  *
  11  * $HEADER$
  12  */
  13 
  14 #include "oshmem_config.h"
  15 
  16 #include <stdlib.h>
  17 #include <string.h>
  18 #include <errno.h>
  19 
  20 #include "opal/util/opal_environ.h"
  21 #include "opal/util/show_help.h"
  22 #include "opal/util/output.h"
  23 #include "opal/util/printf.h"
  24 
  25 #include "ompi/mca/rte/rte.h"
  26 
  27 #include "oshmem/version.h"
  28 #include "oshmem/constants.h"
  29 #include "oshmem/info/info.h"
  30 #include "oshmem/shmem/shmem_api_logger.h"
  31 
  32 
  33 /*
  34  * Global variables
  35  */
  36 oshmem_info_t oshmem_shmem_info_env = {
  37     false,
  38     false,
  39     false,
  40     256 * 1024 * 1024
  41 };
  42 
  43 #define OSHMEM_MAX_LIBRARY_VERSION_STRING 256
  44 
  45 /*
  46  * Local functions
  47  */
  48 static int oshmem_info_value_to_bool(char *value, bool *interp);
  49 static int oshmem_info_value_to_int(char *value, int *interp);
  50 static int oshmem_info_get_heap_size(char *value, size_t *interp);
  51 static int oshmem_info_get_library_version(char *version, int *len);
  52 
  53 
  54 /*
  55  * This function is called during oshmem_init.
  56  */
  57 int oshmem_info_init(void)
  58 {
  59     int ret = OSHMEM_SUCCESS;
  60     char *cptr;
  61 
  62     /* fill the env info object */
  63 
  64     if (NULL != (cptr = getenv(OSHMEM_ENV_VERSION))) {
  65         ret = oshmem_info_value_to_bool(cptr, &oshmem_shmem_info_env.print_version);
  66         if (OSHMEM_SUCCESS != ret) {
  67             goto out;
  68         }
  69     }
  70     if (oshmem_shmem_info_env.print_version && 0 == OMPI_PROC_MY_NAME->vpid) {
  71         char version[OSHMEM_MAX_LIBRARY_VERSION_STRING];
  72         int len;
  73 
  74         ret = oshmem_info_get_library_version(version, &len);
  75         if (OSHMEM_SUCCESS != ret || 0 == len) {
  76             goto out;
  77         }
  78         opal_show_help("help-shmem-runtime.txt",
  79                        "oshmem_init:print-version",
  80                        true,
  81                        version);
  82     }
  83 
  84     if (NULL != (cptr = getenv(OSHMEM_ENV_INFO))) {
  85         ret = oshmem_info_value_to_bool(cptr, &oshmem_shmem_info_env.print_info);
  86         if (OSHMEM_SUCCESS != ret) {
  87             goto out;
  88         }
  89     }
  90     if (oshmem_shmem_info_env.print_info && 0 == OMPI_PROC_MY_NAME->vpid) {
  91         opal_show_help("help-shmem-runtime.txt",
  92                        "oshmem_init:print-info",
  93                        true,
  94                        OSHMEM_ENV_VERSION,
  95                        OSHMEM_ENV_INFO,
  96                        OSHMEM_ENV_SYMMETRIC_SIZE,
  97                        OSHMEM_ENV_DEBUG);
  98     }
  99 
 100     if (NULL != (cptr = getenv(OSHMEM_ENV_DEBUG))) {
 101         ret = oshmem_info_value_to_bool(cptr, &oshmem_shmem_info_env.debug);
 102         if (OSHMEM_SUCCESS != ret) {
 103             goto out;
 104         }
 105     }
 106 
 107     if (NULL != (cptr = getenv(OSHMEM_ENV_SYMMETRIC_SIZE))) {
 108         char *p1 = getenv(SHMEM_HEAP_SIZE);
 109         if (p1 && strcmp(cptr, p1)) {
 110             SHMEM_API_ERROR("Found conflict between env '%s' and '%s'.\n",
 111                           OSHMEM_ENV_SYMMETRIC_SIZE, SHMEM_HEAP_SIZE);
 112             ret = OSHMEM_ERR_BAD_PARAM;
 113             goto out;
 114         }
 115         ret = oshmem_info_get_heap_size(cptr, &oshmem_shmem_info_env.symmetric_heap_size);
 116         if (OSHMEM_SUCCESS != ret) {
 117             goto out;
 118         }
 119     } else if (NULL != (cptr = getenv(SHMEM_HEAP_SIZE))) {
 120         ret = oshmem_info_get_heap_size(cptr, &oshmem_shmem_info_env.symmetric_heap_size);
 121         if (OSHMEM_SUCCESS != ret) {
 122             goto out;
 123         }
 124     }
 125 
 126     /* All done */
 127 
 128     return OSHMEM_SUCCESS;
 129 
 130 out:
 131     return ret;
 132 }
 133 
 134 
 135 /*
 136  * Shut down oshmem_info handling
 137  */
 138 int oshmem_info_finalize(void)
 139 {
 140 
 141     /* All done -- destroy the table */
 142 
 143     return OSHMEM_SUCCESS;
 144 }
 145 
 146 
 147 /**
 148  * Convert value string to boolean
 149  *
 150  * Convert value string \c value into a boolean.  The
 151  * strings "true", "false", and integer numbers can be converted
 152  * into booleans.  All others will return \c OSHMEM_ERR_BAD_PARAM
 153  *
 154  * @param value Value string for info key to interpret
 155  * @param interp returned interpretation of the value key
 156  *
 157  * @retval OSHMEM_SUCCESS string was successfully interpreted
 158  * @retval OSHMEM_ERR_BAD_PARAM string was not able to be interpreted
 159  */
 160 static int oshmem_info_value_to_bool(char *value, bool *interp)
 161 {
 162     int tmp;
 163 
 164     /* idiot case */
 165     if (NULL == value || NULL == interp) return OSHMEM_ERR_BAD_PARAM;
 166 
 167     /* is it true / false? */
 168     if (0 == strcmp(value, "true")) {
 169         *interp = true;
 170         return OSHMEM_SUCCESS;
 171     } else if (0 == strcmp(value, "false")) {
 172         *interp = false;
 173         return OSHMEM_SUCCESS;
 174 
 175     /* is it a number? */
 176     } else if (OSHMEM_SUCCESS == oshmem_info_value_to_int(value, &tmp)) {
 177         if (tmp == 0) {
 178             *interp = false;
 179         } else {
 180             *interp = true;
 181         }
 182         return OSHMEM_SUCCESS;
 183     }
 184 
 185     return OSHMEM_ERR_BAD_PARAM;
 186 }
 187 
 188 
 189 /**
 190  * Convert value string to integer
 191  *
 192  * Convert value string \c value into a integer.
 193  * All others will return \c OSHMEM_ERR_BAD_PARAM
 194  *
 195  * @param value Value string for info key to interpret
 196  * @param interp returned interpretation of the value key
 197  *
 198  * @retval OSHMEM_SUCCESS string was successfully interpreted
 199  * @retval OSHMEM_ERR_BAD_PARAM string was not able to be interpreted
 200  */
 201 static int oshmem_info_value_to_int(char *value, int *interp)
 202 {
 203     long tmp;
 204     char *endp;
 205 
 206     if (NULL == value || '\0' == value[0]) return OSHMEM_ERR_BAD_PARAM;
 207 
 208     errno = 0;
 209     tmp = strtol(value, &endp, 10);
 210     /* we found something not a number */
 211     if (*endp != '\0') return OSHMEM_ERR_BAD_PARAM;
 212     /* underflow */
 213     if (tmp == 0 && errno == EINVAL) return OSHMEM_ERR_BAD_PARAM;
 214 
 215     *interp = (int) tmp;
 216 
 217     return OSHMEM_SUCCESS;
 218 }
 219 
 220 static int oshmem_info_get_heap_size(char *value, size_t *interp)
 221 {
 222     char *p;
 223     long long factor = 1;
 224     int idx;
 225     long long size = 0;
 226 
 227     p = value;
 228     *interp = 0;
 229 
 230     if (!p) {
 231         return OSHMEM_ERR_BAD_PARAM;
 232     }
 233 
 234     /* Sanity check for buffer overflow attack (coverity issue: Use of untrusted string value) */
 235     if (16 < strlen(p)) {
 236         return OSHMEM_ERR_BAD_PARAM;
 237     }
 238 
 239     if (1 == sscanf(p, "%lld%n", &size, &idx)) {
 240         if (p[idx] != '\0') {
 241             if (p[idx + 1] == '\0') {
 242                 if (p[idx] == 'k' || p[idx] == 'K') {
 243                     factor = 1024;
 244                 } else if (p[idx] == 'm' || p[idx] == 'M') {
 245                     factor = 1024 * 1024;
 246                 } else if (p[idx] == 'g' || p[idx] == 'G') {
 247                     factor = 1024 * 1024 * 1024;
 248                 } else if (p[idx] == 't' || p[idx] == 'T') {
 249                     factor = 1024UL * 1024UL * 1024UL * 1024UL;
 250                 } else {
 251                     size = 0;
 252                 }
 253             } else {
 254                 size = 0;
 255             }
 256         }
 257     }
 258 
 259     if (size <= 0) {
 260         return OSHMEM_ERR_BAD_PARAM;
 261     } else {
 262         opal_setenv(OSHMEM_ENV_SYMMETRIC_SIZE, p, true, &environ);
 263         opal_setenv(SHMEM_HEAP_SIZE, p, true, &environ);
 264 /* Probably needless code */
 265 #if 0
 266         char *tmp = p;
 267 
 268         if(!p) {
 269             opal_asprintf(&tmp, "%lld", size * factor);
 270         }
 271 
 272         if (tmp) {
 273             opal_setenv(OSHMEM_ENV_SYMMETRIC_SIZE, p, true, &environ);
 274             opal_setenv(SHMEM_HEAP_SIZE, p, true, &environ);
 275         }
 276 
 277         if (!p && tmp) {
 278             free(tmp);
 279         }
 280 #endif
 281     }
 282 
 283     *interp = size * factor;
 284 
 285     return OSHMEM_SUCCESS;
 286 }
 287 
 288 static int oshmem_info_get_library_version(char *version, int *resultlen)
 289 {
 290     int len_left;
 291     char *ptr, tmp[OSHMEM_MAX_LIBRARY_VERSION_STRING];
 292 
 293     ptr = tmp;
 294     len_left = sizeof(tmp);
 295     memset(tmp, 0, OSHMEM_MAX_LIBRARY_VERSION_STRING);
 296 
 297     snprintf(tmp, OSHMEM_MAX_LIBRARY_VERSION_STRING, "Open SHMEM v%d.%d.%d",
 298              OSHMEM_MAJOR_VERSION, OSHMEM_MINOR_VERSION, OSHMEM_RELEASE_VERSION);
 299     ptr += strlen(tmp);
 300     len_left -= strlen(tmp);
 301 
 302     if (strlen(OSHMEM_GREEK_VERSION) > 0) {
 303         snprintf(ptr, len_left, "%s", OSHMEM_GREEK_VERSION);
 304         ptr = tmp + strlen(tmp);
 305         len_left = OSHMEM_MAX_LIBRARY_VERSION_STRING - strlen(tmp);
 306     }
 307 
 308     /* Package name */
 309     if (strlen(OPAL_PACKAGE_STRING) > 0) {
 310         snprintf(ptr, len_left, ", package: %s", OPAL_PACKAGE_STRING);
 311         ptr = tmp + strlen(tmp);
 312         len_left = OSHMEM_MAX_LIBRARY_VERSION_STRING - strlen(tmp);
 313     }
 314 
 315     /* Ident string */
 316     if (strlen(OSHMEM_IDENT_STRING) > 0) {
 317         snprintf(ptr, len_left, ", ident: %s", OSHMEM_IDENT_STRING);
 318         ptr = tmp + strlen(tmp);
 319         len_left = OSHMEM_MAX_LIBRARY_VERSION_STRING - strlen(tmp);
 320     }
 321 
 322     /* Repository revision */
 323     if (strlen(OSHMEM_REPO_REV) > 0) {
 324         snprintf(ptr, len_left, ", repo rev: %s", OSHMEM_REPO_REV);
 325         ptr = tmp + strlen(tmp);
 326         len_left = OSHMEM_MAX_LIBRARY_VERSION_STRING - strlen(tmp);
 327     }
 328 
 329     /* Release date */
 330     if (strlen(OSHMEM_RELEASE_DATE) > 0) {
 331         snprintf(ptr, len_left, ", %s", OSHMEM_RELEASE_DATE);
 332         ptr = tmp + strlen(tmp);
 333         len_left = OSHMEM_MAX_LIBRARY_VERSION_STRING - strlen(tmp);
 334     }
 335 
 336     memcpy(version, tmp, strlen(tmp) + 1);
 337     *resultlen = strlen(tmp) + 1;
 338 
 339     return OSHMEM_SUCCESS;
 340 }

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