File: orte-iof.c

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (314 lines) | stat: -rw-r--r-- 8,655 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (c) 2004-2010 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2006      Cisco Systems, Inc.  All rights reserved. 
 * Copyright (c) 2007      Sun Microsystems, Inc.  All rights reserved.
 * Copyright (c) 2007      Los Alamos National Security, LLC.  All rights
 *                         reserved. 
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 */

/**
 * @fie
 * ORTE PS command
 *
 */

#include "orte_config.h"
#include "orte/constants.h"

#include <stdio.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif  /* HAVE_UNISTD_H */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif  /*  HAVE_STDLIB_H */
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif  /*  HAVE_SIGNAL_H */
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif  /* HAVE_SYS_STAT_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif  /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif  /* HAVE_SYS_WAIT_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif  /* HAVE_STRING_H */
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif  /* HAVE_DIRENT_H */

#include "opal/event/event.h"
#include "opal/util/cmd_line.h"
#include "opal/util/argv.h"
#include "opal/util/opal_environ.h"
#include "opal/mca/base/base.h"
#include "opal/mca/base/mca_base_param.h"
#include "opal/runtime/opal.h"

#include "orte/runtime/runtime.h"
#include "orte/util/hnp_contact.h"
#include "orte/util/show_help.h"
#include "orte/util/parse_options.h"
#include "orte/util/proc_info.h"
#include "orte/mca/errmgr/errmgr.h"
#include "orte/mca/iof/iof.h"


/*****************************************
 * Global Vars for Command line Arguments
 *****************************************/
static struct {
    bool help;
    int hnppid;
    char *ranks;
    bool stdout_req;
    bool stderr_req;
    bool stddiag_req;
    bool tag;
    orte_hnp_contact_t *target_hnp;
} my_globals;

opal_cmd_line_init_t cmd_line_opts[] = {
    { NULL, NULL, NULL, 
      'h', NULL, "help", 
      0,
      &my_globals.help, OPAL_CMD_LINE_TYPE_BOOL,
      "This help message" },

    { NULL, NULL, NULL, 
      '\0', "pid", "pid", 
      1,
     &my_globals.hnppid, OPAL_CMD_LINE_TYPE_INT,
      "The pid of the mpirun whose output you wish to see" },

    { NULL, NULL, NULL, 
      '\0', "stdout", "stdout", 
      0,
      &my_globals.stdout_req, OPAL_CMD_LINE_TYPE_BOOL,
      "Display stdout from specified process (default)" },
    
    { NULL, NULL, NULL, 
      '\0', "stderr", "stderr", 
      0,
      &my_globals.stderr_req, OPAL_CMD_LINE_TYPE_BOOL,
      "Display stderr from specified process" },
    
    { NULL, NULL, NULL, 
      '\0', "stddiag", "stddiag", 
      0,
      &my_globals.stddiag_req, OPAL_CMD_LINE_TYPE_BOOL,
      "Display stddiag from specified process" },
    
    { NULL, NULL, NULL, 
      '\0', "ranks", "ranks", 
      1,
      &my_globals.ranks, OPAL_CMD_LINE_TYPE_STRING,
     "Ranks whose output is to be displayed (Comma separated list, each element can contain range)" },
    
    { "orte", "tag", "output", 
     '\0', "tag-output", "tag-output", 
     0,
     NULL, OPAL_CMD_LINE_TYPE_BOOL,
     "Tag output with the stream and [job,rank] (default: no tags)" },
    
    /* End of list */
    { NULL, NULL, NULL, 
      '\0', NULL, NULL, 
      0,
      NULL, OPAL_CMD_LINE_TYPE_NULL,
      NULL }
};

/*
 * Local variables & functions
 */
static void abort_exit_callback(int fd, short flags, void *arg);
static struct opal_event term_handler;
static struct opal_event int_handler;
static opal_list_t hnp_list;
static orte_process_name_t target_proc;


int
main(int argc, char *argv[])
{
    int ret, i;
    opal_cmd_line_t cmd_line;
    opal_list_item_t* item = NULL;
    orte_iof_tag_t stream;
    char **ranks=NULL;
    
    /***************
     * Initialize
     ***************/

    /*
     * Make sure to init util before parse_args
     * to ensure installdirs is setup properly
     * before calling mca_base_open();
     */
    if( ORTE_SUCCESS != (ret = opal_init_util(&argc, &argv)) ) {
        return ret;
    }
    
    /* initialize the globals */
    my_globals.help = false;
    my_globals.hnppid = -1;
    my_globals.stdout_req = false;
    my_globals.stderr_req = false;
    my_globals.stddiag_req = false;
    my_globals.ranks = "0";

    /* Parse the command line options */
    opal_cmd_line_create(&cmd_line, cmd_line_opts);
    
    mca_base_open();
    mca_base_cmd_line_setup(&cmd_line);
    ret = opal_cmd_line_parse(&cmd_line, false, argc, argv);
    
    /**
     * Now start parsing our specific arguments
     */
    if (OPAL_SUCCESS != ret || my_globals.help) {
        char *args = NULL;
        args = opal_cmd_line_get_usage_msg(&cmd_line);
        orte_show_help("help-orte-iof.txt", "usage", true, args);
        free(args);
        return ORTE_ERROR;
    }
    
    /*
     * Must specify the mpirun pid
     */
    if(my_globals.hnppid < 0) {
        orte_show_help("help-orte-iof.txt", "pid-required", true);
        return ORTE_ERROR;
    }
    
    /***************************
     * We need all of OPAL and the TOOL portion of ORTE
     ***************************/
    if (ORTE_SUCCESS != orte_init(&argc, &argv, ORTE_PROC_TOOL)) {
        orte_finalize();
        return 1;
    }

    /** setup callbacks for abort signals - from this point
     * forward, we need to abort in a manner that allows us
     * to cleanup
     */
    opal_signal_set(&term_handler, SIGTERM,
                    abort_exit_callback, &term_handler);
    opal_signal_add(&term_handler, NULL);
    opal_signal_set(&int_handler, SIGINT,
                    abort_exit_callback, &int_handler);
    opal_signal_add(&int_handler, NULL);

    /*
     * Get the list of available hnp's and setup contact info
     * to them in the RML
     */
    OBJ_CONSTRUCT(&hnp_list, opal_list_t);
    if (ORTE_SUCCESS != (ret = orte_list_local_hnps(&hnp_list, true) ) ) {
        goto cleanup;
    }

    /*
     * For each hnp in the listing
     */
    while (NULL != (item  = opal_list_remove_first(&hnp_list))) {
        orte_hnp_contact_t *hnp = (orte_hnp_contact_t*)item;
        if (my_globals.hnppid == hnp->pid) {
            /* this is the one we want */
            my_globals.target_hnp = hnp;
            break;
        }
        OBJ_RELEASE(hnp);
    }
    
    /* setup the stream */
    stream = 0;
    if (my_globals.stderr_req) {
        stream |= ORTE_IOF_STDERR;
    }
    if (my_globals.stddiag_req) {
        stream |= ORTE_IOF_STDDIAG;
    }
    if (my_globals.stdout_req) {
        stream |= ORTE_IOF_STDOUT;
    }
    if (0 == stream) {
        /* default to stdout */
        stream |= ORTE_IOF_STDOUT;
    }
    
    /* parse the input ranks */
    orte_util_parse_range_options(my_globals.ranks, &ranks);
    
    /* pull the specified output streams and dump to our stdout */
    for (i=0; i < opal_argv_count(ranks); i++) {
        target_proc.jobid = my_globals.target_hnp->name.jobid + 1;
        target_proc.vpid = strtol(ranks[i], NULL, 10);
        if (ORTE_SUCCESS != (ret = orte_iof.pull(&target_proc, stream, 1))) {
            ORTE_ERROR_LOG(ret);
            goto cleanup;
        }
    }
    
    /* just wait until the abort is fired */
    opal_event_dispatch();

    /***************
     * Cleanup
     ***************/
 cleanup:
    while (NULL != (item  = opal_list_remove_first(&hnp_list))) {
        OBJ_RELEASE(item);
    }
    OBJ_DESTRUCT(&hnp_list);
    opal_argv_free(ranks);
    orte_finalize();

    return ret;
}

static void abort_exit_callback(int fd, short ign, void *arg)
{
    opal_list_item_t *item;
    int ret;
    
    /* Remove the TERM and INT signal handlers */
    opal_signal_del(&term_handler);
    opal_signal_del(&int_handler);

    /* close the outstanding pull */
    if (ORTE_SUCCESS != (ret = orte_iof.close(&target_proc, ORTE_IOF_STDOUT))) {
        ORTE_ERROR_LOG(ret);
    }

    while (NULL != (item  = opal_list_remove_first(&hnp_list))) {
        OBJ_RELEASE(item);
    }
    OBJ_DESTRUCT(&hnp_list);
    orte_finalize();
    exit(1);
}