root/opal/mca/pmix/pmix4x/pmix/src/tools/plookup/plookup.c

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

DEFINITIONS

This source file includes following definitions.
  1. notification_fn
  2. evhandler_reg_callbk
  3. main

   1 /*
   2  * Copyright (c) 2004-2010 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) 2006-2013 Los Alamos National Security, LLC.
  13  *                         All rights reserved.
  14  * Copyright (c) 2009-2012 Cisco Systems, Inc.  All rights reserved.
  15  * Copyright (c) 2011      Oak Ridge National Labs.  All rights reserved.
  16  * Copyright (c) 2013-2018 Intel, Inc.  All rights reserved.
  17  * Copyright (c) 2015      Mellanox Technologies, Inc.  All rights reserved.
  18  * $COPYRIGHT$
  19  *
  20  * Additional copyrights may follow
  21  *
  22  * $HEADER$
  23  *
  24  */
  25 
  26 #define _GNU_SOURCE
  27 #include <stdio.h>
  28 #include <stdlib.h>
  29 #include <unistd.h>
  30 #include <time.h>
  31 #include <pthread.h>
  32 
  33 #include <pmix_tool.h>
  34 #include "src/mca/base/base.h"
  35 #include "src/mca/pinstalldirs/base/base.h"
  36 #include "src/threads/threads.h"
  37 #include "src/util/cmd_line.h"
  38 #include "src/util/keyval_parse.h"
  39 #include "src/util/show_help.h"
  40 #include "src/runtime/pmix_rte.h"
  41 
  42 typedef struct {
  43     pmix_lock_t lock;
  44     pmix_status_t status;
  45 } mylock_t;
  46 
  47 static pmix_proc_t myproc;
  48 
  49 
  50 /* this is the event notification function we pass down below
  51  * when registering for general events - i.e.,, the default
  52  * handler. We don't technically need to register one, but it
  53  * is usually good practice to catch any events that occur */
  54 static void notification_fn(size_t evhdlr_registration_id,
  55                             pmix_status_t status,
  56                             const pmix_proc_t *source,
  57                             pmix_info_t info[], size_t ninfo,
  58                             pmix_info_t results[], size_t nresults,
  59                             pmix_event_notification_cbfunc_fn_t cbfunc,
  60                             void *cbdata)
  61 {
  62     /* this example doesn't do anything with default events */
  63     if (NULL != cbfunc) {
  64         cbfunc(PMIX_EVENT_ACTION_COMPLETE, NULL, 0, NULL, NULL, cbdata);
  65     }
  66 }
  67 
  68 /* event handler registration is done asynchronously because it
  69  * may involve the PMIx server registering with the host RM for
  70  * external events. So we provide a callback function that returns
  71  * the status of the request (success or an error), plus a numerical index
  72  * to the registered event. The index is used later on to deregister
  73  * an event handler - if we don't explicitly deregister it, then the
  74  * PMIx server will do so when it see us exit */
  75 static void evhandler_reg_callbk(pmix_status_t status,
  76                                  size_t evhandler_ref,
  77                                  void *cbdata)
  78 {
  79     mylock_t *lock = (mylock_t*)cbdata;
  80 
  81     if (PMIX_SUCCESS != status) {
  82         fprintf(stderr, "Client %s:%d EVENT HANDLER REGISTRATION FAILED WITH STATUS %d, ref=%lu\n",
  83                    myproc.nspace, myproc.rank, status, (unsigned long)evhandler_ref);
  84     }
  85     lock->status = status;
  86     PMIX_WAKEUP_THREAD(&lock->lock);
  87 }
  88 
  89 /*****************************************
  90  * Global Vars for Command line Arguments
  91  *****************************************/
  92 typedef struct {
  93     bool help;
  94     bool verbose;
  95     pid_t pid;
  96     bool wait;
  97     int timeout;
  98 } pmix_plookup_globals_t;
  99 
 100 pmix_plookup_globals_t pmix_plookup_globals = {0};
 101 
 102 pmix_cmd_line_init_t cmd_line_opts[] = {
 103     { NULL,
 104       'h', NULL, "help",
 105       0,
 106       &pmix_plookup_globals.help, PMIX_CMD_LINE_TYPE_BOOL,
 107       "This help message" },
 108 
 109     { NULL,
 110       'v', NULL, "verbose",
 111       0,
 112       &pmix_plookup_globals.verbose, PMIX_CMD_LINE_TYPE_BOOL,
 113       "Be Verbose" },
 114 
 115     { NULL,
 116       'p', NULL, "pid",
 117       1,
 118       &pmix_plookup_globals.pid, PMIX_CMD_LINE_TYPE_INT,
 119       "Specify starter pid" },
 120 
 121     { NULL,
 122       'w', NULL, "wait",
 123       0,
 124       &pmix_plookup_globals.wait, PMIX_CMD_LINE_TYPE_BOOL,
 125       "Wait for data to be available" },
 126 
 127     { NULL,
 128       't', NULL, "timeout",
 129       0,
 130       &pmix_plookup_globals.timeout, PMIX_CMD_LINE_TYPE_INT,
 131       "Max number of seconds to wait for data to become available" },
 132 
 133     /* End of list */
 134     { NULL,
 135       '\0', NULL, NULL,
 136       0,
 137       NULL, PMIX_CMD_LINE_TYPE_NULL,
 138       NULL }
 139 };
 140 
 141 int main(int argc, char **argv)
 142 {
 143     pmix_status_t rc;
 144     pmix_info_t *info = NULL;
 145     size_t ninfo = 0, n;
 146     mylock_t mylock;
 147     pmix_cmd_line_t cmd_line;
 148     pmix_pdata_t *pdata = NULL;
 149     size_t ndata;
 150     int count;
 151     char **keys = NULL;
 152 
 153     /* protect against problems if someone passes us thru a pipe
 154      * and then abnormally terminates the pipe early */
 155     signal(SIGPIPE, SIG_IGN);
 156 
 157     /* initialize the output system */
 158     if (!pmix_output_init()) {
 159         return PMIX_ERROR;
 160     }
 161 
 162     /* initialize install dirs code */
 163     if (PMIX_SUCCESS != (rc = pmix_mca_base_framework_open(&pmix_pinstalldirs_base_framework, 0))) {
 164         fprintf(stderr, "pmix_pinstalldirs_base_open() failed -- process will likely abort (%s:%d, returned %d instead of PMIX_SUCCESS)\n",
 165                 __FILE__, __LINE__, rc);
 166         return rc;
 167     }
 168 
 169     /* initialize the help system */
 170     pmix_show_help_init();
 171 
 172     /* keyval lex-based parser */
 173     if (PMIX_SUCCESS != (rc = pmix_util_keyval_parse_init())) {
 174         fprintf(stderr, "pmix_util_keyval_parse_init failed with %d\n", rc);
 175         return PMIX_ERROR;
 176     }
 177 
 178     /* Setup the parameter system */
 179     if (PMIX_SUCCESS != (rc = pmix_mca_base_var_init())) {
 180         fprintf(stderr, "pmix_mca_base_var_init failed with %d\n", rc);
 181         return PMIX_ERROR;
 182     }
 183 
 184     /* register params for pmix */
 185     if (PMIX_SUCCESS != (rc = pmix_register_params())) {
 186         fprintf(stderr, "pmix_register_params failed with %d\n", rc);
 187         return PMIX_ERROR;
 188     }
 189 
 190     /* Parse the command line options */
 191     pmix_cmd_line_create(&cmd_line, cmd_line_opts);
 192 
 193     pmix_mca_base_open();
 194     pmix_mca_base_cmd_line_setup(&cmd_line);
 195     rc = pmix_cmd_line_parse(&cmd_line, false, false, argc, argv);
 196 
 197     if (PMIX_SUCCESS != rc) {
 198         if (PMIX_ERR_SILENT != rc) {
 199             fprintf(stderr, "%s: command line error (%s)\n", argv[0],
 200                     PMIx_Error_string(rc));
 201         }
 202         return rc;
 203     }
 204 
 205     if (pmix_plookup_globals.help) {
 206         char *str, *args = NULL;
 207         args = pmix_cmd_line_get_usage_msg(&cmd_line);
 208         str = pmix_show_help_string("help-plookup.txt", "usage", true,
 209                                     args);
 210         if (NULL != str) {
 211             printf("%s", str);
 212             free(str);
 213         }
 214         free(args);
 215         /* If we show the help message, that should be all we do */
 216         exit(0);
 217     }
 218 
 219     if (pmix_plookup_globals.wait) {
 220         ++ninfo;
 221         if (0 < pmix_plookup_globals.timeout) {
 222             ++ninfo;
 223         }
 224     }
 225 
 226     /* determine how many keys were given */
 227     pmix_cmd_line_get_tail(&cmd_line, &count, &keys);
 228     if (0 == count) {
 229         /* must give us at least one key */
 230         fprintf(stderr, "%s: Must provide at least one key to lookup\n", argv[0]);
 231         exit(1);
 232     }
 233     ndata = count;
 234 
 235     /* if we were given the pid of a starter, then direct that
 236      * we connect to it */
 237 
 238     /* otherwise, use the system connection first, if available */
 239     PMIX_INFO_CREATE(info, 1);
 240     PMIX_INFO_LOAD(&info[0], PMIX_CONNECT_SYSTEM_FIRST, NULL, PMIX_BOOL);
 241     /* init as a tool */
 242     if (PMIX_SUCCESS != (rc = PMIx_tool_init(&myproc, info, 1))) {
 243         fprintf(stderr, "PMIx_tool_init failed: %d\n", rc);
 244         exit(rc);
 245     }
 246     PMIX_INFO_FREE(info, 1);
 247 
 248     /* register a default event handler */
 249     PMIX_CONSTRUCT_LOCK(&mylock.lock);
 250     PMIx_Register_event_handler(NULL, 0, NULL, 0,
 251                                 notification_fn, evhandler_reg_callbk, (void*)&mylock);
 252     PMIX_WAIT_THREAD(&mylock.lock);
 253     if (PMIX_SUCCESS != mylock.status) {
 254         fprintf(stderr, "PMIx_Register_event_handler returned bad status: %d\n", rc);
 255         PMIX_DESTRUCT_LOCK(&mylock.lock);
 256         goto done;
 257     }
 258     PMIX_DESTRUCT_LOCK(&mylock.lock);
 259 
 260     /* setup any info for the lookup */
 261     if (0 < ninfo) {
 262         PMIX_INFO_CREATE(info, ninfo);
 263         PMIX_INFO_LOAD(&info[0], PMIX_WAIT, NULL, PMIX_BOOL);
 264         if (1 < ninfo) {
 265             PMIX_INFO_LOAD(&info[1], PMIX_TIMEOUT, &pmix_plookup_globals.timeout, PMIX_INT);
 266         }
 267     }
 268 
 269     /* setup the keys */
 270     PMIX_PDATA_CREATE(pdata, ndata);
 271     for (n=0; n < ndata; n++) {
 272         pmix_strncpy(pdata[n].key, keys[n], PMIX_MAX_KEYLEN);
 273     }
 274     /* perform the lookup */
 275     rc = PMIx_Lookup(pdata, ndata, info, ninfo);
 276 
 277     if (PMIX_SUCCESS != rc) {
 278         fprintf(stderr, "PMIx_Lookup failed: %d\n", rc);
 279         goto done;
 280     }
 281 
 282     for (n=0; n < ndata; n++) {
 283         fprintf(stderr, "Key: %s\n", pdata[n].key);
 284     }
 285     PMIX_PDATA_FREE(pdata, ndata);
 286 
 287   done:
 288     PMIx_tool_finalize();
 289 
 290     return(rc);
 291 }

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