darshan-merge.c 14.4 KB
Newer Older
1
#include <stdio.h>
2
3
4
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
5
6
#include <getopt.h>
#include <glob.h>
7

8
9
#include "uthash-1.9.2/src/uthash.h"

10
11
#include "darshan-logutils.h"

12
13
#define DEF_MOD_BUF_SIZE 1024 /* 1 KiB is enough for all current mod records ... */

14
15
16
17
18
19
20
21
22
23
/* TODO: set job end timestamp? */

struct darshan_shared_record_ref
{
    darshan_record_id id;
    int ref_cnt;
    char agg_rec[DEF_MOD_BUF_SIZE];
    UT_hash_handle hlink;
};

24
25
void usage(char *exename)
{
26
    fprintf(stderr, "Usage: %s --output <output_path> [options] <input-logs>\n", exename);
Shane Snyder's avatar
Shane Snyder committed
27
28
    fprintf(stderr, "This utility merges multiple Darshan log files into a single output log file.\n");
    fprintf(stderr, "Options:\n");
29
    fprintf(stderr, "\t--output\t(REQUIRED) Full path of the output darshan log file.\n");
Shane Snyder's avatar
Shane Snyder committed
30
    fprintf(stderr, "\t--shared-redux\tReduce globally shared records into a single record.\n");
31
32
33
34

    exit(1);
}

Shane Snyder's avatar
Shane Snyder committed
35
void parse_args(int argc, char **argv, char ***infile_list, int *n_files,
36
    char **outlog_path, int *shared_redux)
37
38
39
40
41
{
    int index;
    static struct option long_opts[] =
    {
        {"shared-redux", no_argument, NULL, 's'},
42
        {"output", required_argument, NULL, 'o'},
43
44
45
46
        {0, 0, 0, 0}
    };

    *shared_redux = 0;
47
    *outlog_path = NULL;
48
49
50
51
52
53
54
55
56
57
58
59

    while(1)
    {
        int c = getopt_long(argc, argv, "", long_opts, &index);

        if(c == -1) break;

        switch(c)
        {
            case 's':
                *shared_redux = 1;
                break;
60
61
            case 'o':
                *outlog_path = optarg;
Shane Snyder's avatar
Shane Snyder committed
62
                break;
63
64
65
66
67
68
69
            case '?':
            default:
                usage(argv[0]);
                break;
        }
    }

70
    if(*outlog_path == NULL)
71
72
73
74
    {
        usage(argv[0]);
    }

Shane Snyder's avatar
Shane Snyder committed
75
76
    *infile_list = &argv[optind];
    *n_files = argc - optind;
77

Shane Snyder's avatar
Shane Snyder committed
78
    return;
79
80
}

Shane Snyder's avatar
Shane Snyder committed
81
82
83
int build_mod_shared_rec_hash(char **infile_list, int n_infiles,
    darshan_module_id mod_id, int nprocs, char *mod_buf,
    struct darshan_shared_record_ref **shared_rec_hash)
84
85
86
87
88
89
90
91
92
{
    darshan_fd in_fd;
    struct darshan_base_record *base_rec;
    struct darshan_shared_record_ref *ref, *tmp;
    int init = 0;
    int ret;
    int i;

    /* loop over each input log file */
Shane Snyder's avatar
Shane Snyder committed
93
    for(i = 0; i < n_infiles; i++)
94
    {
Shane Snyder's avatar
Shane Snyder committed
95
        in_fd = darshan_log_open(infile_list[i]);
96
97
98
99
        if(in_fd == NULL)
        {
            fprintf(stderr,
                "Error: unable to open input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
100
                infile_list[i]);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
            return(-1);
        }

        while((ret = mod_logutils[mod_id]->log_get_record(in_fd, mod_buf)) == 1)
        {
            base_rec = (struct darshan_base_record *)mod_buf;

            /* initialize the hash with the first rank's records */
            if(!init)
            {
                struct darshan_base_record *agg_base;

                /* create a new ref and add to the hash */
                ref = malloc(sizeof(*ref));
                if(!ref)
                {
                    darshan_log_close(in_fd);
                    return(-1);
                }
Shane Snyder's avatar
Shane Snyder committed
120
                memset(ref, 0, sizeof(*ref));
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

                /* initialize the aggregate record with this rank's record */
                agg_base = (struct darshan_base_record *)ref->agg_rec;
                agg_base->id = base_rec->id;
                agg_base->rank = -1;
                mod_logutils[mod_id]->log_agg_records(mod_buf, ref->agg_rec, 1);

                ref->id = base_rec->id;
                ref->ref_cnt = 1;
                HASH_ADD(hlink, *shared_rec_hash, id, sizeof(darshan_record_id), ref);
                init = 1;
            }
            else
            {
                /* search for this record in shared record hash */
                HASH_FIND(hlink, *shared_rec_hash, &(base_rec->id),
                    sizeof(darshan_record_id), ref);
                if(ref)
                {
                    /* if found, aggregate this rank's record into the shared record */
                    mod_logutils[mod_id]->log_agg_records(mod_buf, ref->agg_rec, 0);
                    ref->ref_cnt++;
                }
            }
        }
        if(ret < 0)
        {
            fprintf(stderr,
                "Error: unable to read %s module record from input log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
150
                darshan_module_names[mod_id], infile_list[i]);
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
            darshan_log_close(in_fd);
            return(-1);
        }

        darshan_log_close(in_fd);
    }

    /* prune any non-shared records from the hash one last time */
    HASH_ITER(hlink, *shared_rec_hash, ref, tmp)
    {
        if(ref->ref_cnt != nprocs)
        {
            HASH_DELETE(hlink, *shared_rec_hash, ref);
            free(ref);
        }
    }

    return(0);
}

171
172
int main(int argc, char *argv[])
{
Shane Snyder's avatar
Shane Snyder committed
173
174
    char **infile_list;
    int n_infiles;
175
    int shared_redux;
176
    char *outlog_path;
Shane Snyder's avatar
Shane Snyder committed
177
178
    darshan_fd in_fd, merge_fd;
    struct darshan_job in_job, merge_job;
179
    char merge_exe[DARSHAN_EXE_LEN+1] = {0};
Shane Snyder's avatar
Shane Snyder committed
180
181
182
    char **merge_mnt_pts;
    char **merge_fs_types;
    int merge_mnt_count = 0;
183
    struct darshan_record_ref *in_hash = NULL;
Shane Snyder's avatar
Shane Snyder committed
184
    struct darshan_record_ref *merge_hash = NULL;
185
    struct darshan_record_ref *ref, *tmp, *found;
186
187
188
189
    struct darshan_shared_record_ref *shared_rec_hash = NULL;
    struct darshan_shared_record_ref *sref, *stmp;
    struct darshan_base_record *base_rec;
    char mod_buf[DEF_MOD_BUF_SIZE];
190
191
    int i, j;
    int ret;
192
193

    /* grab command line arguments */
194
    parse_args(argc, argv, &infile_list, &n_infiles, &outlog_path, &shared_redux);
195

Shane Snyder's avatar
Shane Snyder committed
196
    memset(&merge_job, 0, sizeof(struct darshan_job));
197

Shane Snyder's avatar
Shane Snyder committed
198
    /* first pass at merging together logs:
199
200
     *      - compose output job-level metadata structure (including exe & mount data)
     *      - compose output record_id->file_name mapping 
201
     */
Shane Snyder's avatar
Shane Snyder committed
202
    for(i = 0; i < n_infiles; i++)
203
204
205
    {
        memset(&in_job, 0, sizeof(struct darshan_job));

Shane Snyder's avatar
Shane Snyder committed
206
        in_fd = darshan_log_open(infile_list[i]);
207
        if(in_fd == NULL)
208
209
        {
            fprintf(stderr,
210
                "Error: unable to open input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
211
                infile_list[i]);
212
213
214
215
            return(-1);
        }

        /* read job-level metadata from the input file */
216
217
        ret = darshan_log_getjob(in_fd, &in_job);
        if(ret < 0)
218
219
        {
            fprintf(stderr,
220
                "Error: unable to read job data from input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
221
                infile_list[i]);
222
            darshan_log_close(in_fd);
223
224
225
            return(-1);
        }

226
227
228
229
230
231
232
233
234
        /* if the input darshan log has metadata set indicating the darshan
         * shutdown procedure was called on the log, then we error out. if the
         * shutdown procedure was started, then it's possible the log has
         * incomplete or corrupt data, so we just throw out the data for now.
         */
        if(strstr(in_job.metadata, "darshan_shutdown=yes"))
        {
            fprintf(stderr,
                "Error: potentially corrupt data found in input log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
235
                infile_list[i]);
236
237
238
239
            darshan_log_close(in_fd);
            return(-1);
        }

240
241
        if(i == 0)
        {
242
            /* get job data, exe, & mounts directly from the first input log */
Shane Snyder's avatar
Shane Snyder committed
243
            memcpy(&merge_job, &in_job, sizeof(struct darshan_job));
244

Shane Snyder's avatar
Shane Snyder committed
245
            ret = darshan_log_getexe(in_fd, merge_exe);
246
            if(ret < 0)
247
248
            {
                fprintf(stderr,
Shane Snyder's avatar
Shane Snyder committed
249
250
                    "Error: unable to read exe string from input Darshan log file %s.\n",
                    infile_list[i]);
251
                darshan_log_close(in_fd);
252
253
254
                return(-1);
            }

Shane Snyder's avatar
Shane Snyder committed
255
256
            ret = darshan_log_getmounts(in_fd, &merge_mnt_pts,
                &merge_fs_types, &merge_mnt_count);
257
            if(ret < 0)
258
            {
259
                fprintf(stderr,
Shane Snyder's avatar
Shane Snyder committed
260
261
                    "Error: unable to read mount info from input Darshan log file %s.\n",
                    infile_list[i]);
262
263
                darshan_log_close(in_fd);
                return(-1);
264
265
266
267
            }
        }
        else
        {
268
            /* potentially update job timestamps using remaining logs */
Shane Snyder's avatar
Shane Snyder committed
269
270
271
272
            if(in_job.start_time < merge_job.start_time)
                merge_job.start_time = in_job.start_time;
            if(in_job.end_time > merge_job.end_time)
                merge_job.end_time = in_job.end_time;
273
274
        }

275
        /* read the hash of ids->names for the input log */
276
277
278
279
280
        ret = darshan_log_gethash(in_fd, &in_hash);
        if(ret < 0)
        {
            fprintf(stderr,
                "Error: unable to read job data from input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
281
                infile_list[i]);
282
283
284
285
286
287
288
289
290
            darshan_log_close(in_fd);
            return(-1);
        }

        /* iterate the input hash, copying over record_id->file_name mappings
         * that have not already been copied to the output hash
         */
        HASH_ITER(hlink, in_hash, ref, tmp)
        {
Shane Snyder's avatar
Shane Snyder committed
291
            HASH_FIND(hlink, merge_hash, &(ref->id), sizeof(darshan_record_id), found);
292
293
            if(!found)
            {
Shane Snyder's avatar
Shane Snyder committed
294
                HASH_ADD(hlink, merge_hash, id, sizeof(darshan_record_id), ref);
295
            }
296
            else if(strcmp(ref->name, found->name))
297
298
299
300
301
302
303
304
305
            {
                fprintf(stderr,
                    "Error: invalid Darshan record table entry.\n");
                darshan_log_close(in_fd);
                return(-1);
            }
        }

        darshan_log_close(in_fd);
306
307
    }

Shane Snyder's avatar
Shane Snyder committed
308
309
310
    /* create the output "merged" log */
    merge_fd = darshan_log_create(outlog_path, DARSHAN_ZLIB_COMP, 1);
    if(merge_fd == NULL)
311
312
313
314
315
316
    {
        fprintf(stderr, "Error: unable to create output darshan log.\n");
        return(-1);
    }

    /* write the darshan job info, exe string, and mount data to output file */
Shane Snyder's avatar
Shane Snyder committed
317
    ret = darshan_log_putjob(merge_fd, &merge_job);
318
319
320
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write job data to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
321
322
        darshan_log_close(merge_fd);
        unlink(outlog_path);
323
324
325
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
326
    ret = darshan_log_putexe(merge_fd, merge_exe);
327
328
329
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write exe string to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
330
331
        darshan_log_close(merge_fd);
        unlink(outlog_path);
332
333
334
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
335
    ret = darshan_log_putmounts(merge_fd, merge_mnt_pts, merge_fs_types, merge_mnt_count);
336
337
338
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write mount data to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
339
340
        darshan_log_close(merge_fd);
        unlink(outlog_path);
341
342
343
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
344
345
    /* write the merged table of records to output file */
    ret = darshan_log_puthash(merge_fd, merge_hash);
346
347
348
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write record table to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
349
350
        darshan_log_close(merge_fd);
        unlink(outlog_path);
351
352
        return(-1);
    }
353

354
    /* iterate over active darshan modules and gather module data to write
Shane Snyder's avatar
Shane Snyder committed
355
     * to the merged output log
356
     */
Shane Snyder's avatar
Shane Snyder committed
357
    for(i = 0; i < DARSHAN_MAX_MODS; i++)
358
359
360
    {
        if(!mod_logutils[i]) continue;

361
        if(shared_redux)
362
        {
363
            /* build the hash of records shared globally by this module */
Shane Snyder's avatar
Shane Snyder committed
364
365
            ret = build_mod_shared_rec_hash(infile_list, n_infiles, i,
                merge_job.nprocs, mod_buf, &shared_rec_hash);
366
            if(ret < 0)
367
            {
368
369
370
                fprintf(stderr,
                    "Error: unable to build list of %s module's shared records.\n",
                    darshan_module_names[i]);
Shane Snyder's avatar
Shane Snyder committed
371
372
                darshan_log_close(merge_fd);
                unlink(outlog_path);
373
                return(-1);
374
            }
375
376
377

        }

Shane Snyder's avatar
Shane Snyder committed
378
        for(j = 0; j < n_infiles; j++)
379
        {
Shane Snyder's avatar
Shane Snyder committed
380
            in_fd = darshan_log_open(infile_list[j]);
381
382
383
384
            if(in_fd == NULL)
            {
                fprintf(stderr,
                    "Error: unable to open input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
385
386
387
                    infile_list[j]);
                darshan_log_close(merge_fd);
                unlink(outlog_path);
388
389
390
                return(-1);
            }

Shane Snyder's avatar
Shane Snyder committed
391
392
393
394
395
            if(j == 0 && shared_rec_hash)
            {
                /* write out the shared records first */
                HASH_ITER(hlink, shared_rec_hash, sref, stmp)
                {
Shane Snyder's avatar
Shane Snyder committed
396
                    ret = mod_logutils[i]->log_put_record(merge_fd, sref->agg_rec, in_fd->mod_ver[i]);
Shane Snyder's avatar
Shane Snyder committed
397
398
399
400
401
                    if(ret < 0)
                    {
                        fprintf(stderr,
                            "Error: unable to write %s module record to output darshan log.\n",
                            darshan_module_names[i]);
Shane Snyder's avatar
Shane Snyder committed
402
403
404
                        darshan_log_close(in_fd);
                        darshan_log_close(merge_fd);
                        unlink(outlog_path);
Shane Snyder's avatar
Shane Snyder committed
405
406
407
408
409
                        return(-1);
                    }
                }
            }

410
            /* loop over module records and write them to output file */
411
            while((ret = mod_logutils[i]->log_get_record(in_fd, mod_buf)) == 1)
412
            {
413
414
415
416
417
418
                base_rec = (struct darshan_base_record *)mod_buf;

                HASH_FIND(hlink, shared_rec_hash, &(base_rec->id), sizeof(darshan_record_id), sref);
                if(sref)
                    continue; /* skip shared records */

Shane Snyder's avatar
Shane Snyder committed
419
                ret = mod_logutils[i]->log_put_record(merge_fd, mod_buf, in_fd->mod_ver[i]);
420
421
422
423
                if(ret < 0)
                {
                    fprintf(stderr,
                        "Error: unable to write %s module record to output log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
424
                        darshan_module_names[i], infile_list[j]);
425
                    darshan_log_close(in_fd);
Shane Snyder's avatar
Shane Snyder committed
426
427
                    darshan_log_close(merge_fd);
                    unlink(outlog_path);
428
429
430
                    return(-1);
                }
            }
431
432
433
            if(ret < 0)
            {
                fprintf(stderr,
434
                    "Error: unable to read %s module record from input log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
435
                    darshan_module_names[i], infile_list[j]);
436
                darshan_log_close(in_fd);
Shane Snyder's avatar
Shane Snyder committed
437
438
                darshan_log_close(merge_fd);
                unlink(outlog_path);
439
440
441
                return(-1);
            }

442
            darshan_log_close(in_fd);
443
        }
444
445
446
447
448
449
450
451
452
453

        /* clear the shared record hash for the next module */
        if(shared_redux)
        {
            HASH_ITER(hlink, shared_rec_hash, sref, stmp)
            {
                HASH_DELETE(hlink, shared_rec_hash, sref);
                free(sref);
            }
        }
454
455
    }

Shane Snyder's avatar
Shane Snyder committed
456
    darshan_log_close(merge_fd);
457
458
459

    return(0);
}
Shane Snyder's avatar
Shane Snyder committed
460
461
462
463
464
465
466
467
468

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */