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

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

DEFINITIONS

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

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