GitLab maintenance scheduled for Tomorrow, 2020-08-11, from 17:00 to 18:00 CT - Services will be unavailable during this time.

darshan-merge.c 15.2 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
struct darshan_shared_record_ref
{
    darshan_record_id id;
    int ref_cnt;
    char agg_rec[DEF_MOD_BUF_SIZE];
    UT_hash_handle hlink;
};

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

    exit(1);
}

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

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

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

        if(c == -1) break;

        switch(c)
        {
            case 's':
                *shared_redux = 1;
                break;
62 63
            case 'o':
                *outlog_path = optarg;
Shane Snyder's avatar
Shane Snyder committed
64
                break;
65 66 67 68 69 70 71 72
            case 'e':
                *job_end_time = strtol(optarg, &check, 10);
                if(optarg == check)
                {
                    fprintf(stderr, "Error: unable to parse job end time value.\n");
                    exit(1);
                }
                break;
73 74 75 76 77 78 79
            case '?':
            default:
                usage(argv[0]);
                break;
        }
    }

80
    if(*outlog_path == NULL)
81 82 83 84
    {
        usage(argv[0]);
    }

Shane Snyder's avatar
Shane Snyder committed
85 86
    *infile_list = &argv[optind];
    *n_files = argc - optind;
87

Shane Snyder's avatar
Shane Snyder committed
88
    return;
89 90
}

Shane Snyder's avatar
Shane Snyder committed
91 92 93
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)
94 95 96 97
{
    darshan_fd in_fd;
    struct darshan_base_record *base_rec;
    struct darshan_shared_record_ref *ref, *tmp;
98
    int init_rank = -1;
99 100 101 102
    int ret;
    int i;

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

        while((ret = mod_logutils[mod_id]->log_get_record(in_fd, mod_buf)) == 1)
        {
            base_rec = (struct darshan_base_record *)mod_buf;
117 118
            if(init_rank == -1)
                init_rank = base_rec->rank;
119 120

            /* initialize the hash with the first rank's records */
121
            if(base_rec->rank == init_rank)
122 123 124 125 126 127 128 129 130 131
            {
                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);
                }
132
                memset(ref, 0, sizeof(*ref));
133 134 135 136 137 138 139 140 141 142 143 144

                /* 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);
            }
            else
145
           {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                /* 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
161
                darshan_module_names[mod_id], infile_list[i]);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            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);
}

182 183
int main(int argc, char *argv[])
{
Shane Snyder's avatar
Shane Snyder committed
184 185
    char **infile_list;
    int n_infiles;
186
    int shared_redux;
187
    int64_t job_end_time = 0;
188
    char *outlog_path;
Shane Snyder's avatar
Shane Snyder committed
189 190
    darshan_fd in_fd, merge_fd;
    struct darshan_job in_job, merge_job;
191
    char merge_exe[DARSHAN_EXE_LEN+1] = {0};
192
    struct darshan_mnt_info *merge_mnt_array;
Shane Snyder's avatar
Shane Snyder committed
193
    int merge_mnt_count = 0;
194 195 196
    struct darshan_name_record_ref *in_hash = NULL;
    struct darshan_name_record_ref *merge_hash = NULL;
    struct darshan_name_record_ref *ref, *tmp, *found;
197 198 199 200
    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];
201 202
    int i, j;
    int ret;
203 204

    /* grab command line arguments */
205
    parse_args(argc, argv, &infile_list, &n_infiles, &outlog_path, &shared_redux, &job_end_time);
206

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

Shane Snyder's avatar
Shane Snyder committed
209
    /* first pass at merging together logs:
210 211
     *      - compose output job-level metadata structure (including exe & mount data)
     *      - compose output record_id->file_name mapping 
212
     */
Shane Snyder's avatar
Shane Snyder committed
213
    for(i = 0; i < n_infiles; i++)
214 215 216
    {
        memset(&in_job, 0, sizeof(struct darshan_job));

Shane Snyder's avatar
Shane Snyder committed
217
        in_fd = darshan_log_open(infile_list[i]);
218
        if(in_fd == NULL)
219 220
        {
            fprintf(stderr,
221
                "Error: unable to open input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
222
                infile_list[i]);
223 224 225 226
            return(-1);
        }

        /* read job-level metadata from the input file */
227
        ret = darshan_log_get_job(in_fd, &in_job);
228
        if(ret < 0)
229 230
        {
            fprintf(stderr,
231
                "Error: unable to read job data from input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
232
                infile_list[i]);
233
            darshan_log_close(in_fd);
234 235 236
            return(-1);
        }

237 238 239 240 241 242 243 244 245
        /* 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
246
                infile_list[i]);
247 248 249 250
            darshan_log_close(in_fd);
            return(-1);
        }

251 252
        if(i == 0)
        {
253
            /* get job data, exe, & mounts directly from the first input log */
Shane Snyder's avatar
Shane Snyder committed
254
            memcpy(&merge_job, &in_job, sizeof(struct darshan_job));
255

256
            ret = darshan_log_get_exe(in_fd, merge_exe);
257
            if(ret < 0)
258 259
            {
                fprintf(stderr,
Shane Snyder's avatar
Shane Snyder committed
260 261
                    "Error: unable to read exe string from input Darshan log file %s.\n",
                    infile_list[i]);
262
                darshan_log_close(in_fd);
263 264 265
                return(-1);
            }

266
            ret = darshan_log_get_mounts(in_fd, &merge_mnt_array, &merge_mnt_count);
267
            if(ret < 0)
268
            {
269
                fprintf(stderr,
Shane Snyder's avatar
Shane Snyder committed
270 271
                    "Error: unable to read mount info from input Darshan log file %s.\n",
                    infile_list[i]);
272 273
                darshan_log_close(in_fd);
                return(-1);
274 275 276 277
            }
        }
        else
        {
278
            /* potentially update job timestamps using remaining logs */
Shane Snyder's avatar
Shane Snyder committed
279 280 281 282
            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;
283 284
        }

285
        /* read the hash of ids->names for the input log */
286
        ret = darshan_log_get_namehash(in_fd, &in_hash);
287 288 289 290
        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
291
                infile_list[i]);
292 293 294 295
            darshan_log_close(in_fd);
            return(-1);
        }

296
        /* iterate the input hash, copying over record id->name mappings
297 298 299 300
         * that have not already been copied to the output hash
         */
        HASH_ITER(hlink, in_hash, ref, tmp)
        {
301 302
            HASH_FIND(hlink, merge_hash, &(ref->name_record->id),
                sizeof(darshan_record_id), found);
303 304
            if(!found)
            {
305 306
                HASH_ADD(hlink, merge_hash, name_record->id,
                    sizeof(darshan_record_id), ref);
307
            }
308
            else if(strcmp(ref->name_record->name, found->name_record->name))
309 310 311 312 313 314 315 316 317
            {
                fprintf(stderr,
                    "Error: invalid Darshan record table entry.\n");
                darshan_log_close(in_fd);
                return(-1);
            }
        }

        darshan_log_close(in_fd);
318 319
    }

320 321 322 323
    /* if a job end time was passed in, apply it to the output job */
    if(job_end_time > 0)
        merge_job.end_time = job_end_time;

Shane Snyder's avatar
Shane Snyder committed
324 325 326
    /* create the output "merged" log */
    merge_fd = darshan_log_create(outlog_path, DARSHAN_ZLIB_COMP, 1);
    if(merge_fd == NULL)
327 328 329 330 331 332
    {
        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 */
333
    ret = darshan_log_put_job(merge_fd, &merge_job);
334 335 336
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write job data to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
337 338
        darshan_log_close(merge_fd);
        unlink(outlog_path);
339 340 341
        return(-1);
    }

342
    ret = darshan_log_put_exe(merge_fd, merge_exe);
343 344 345
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write exe string to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
346 347
        darshan_log_close(merge_fd);
        unlink(outlog_path);
348 349 350
        return(-1);
    }

351
    ret = darshan_log_put_mounts(merge_fd, merge_mnt_array, merge_mnt_count);
352 353 354
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write mount data to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
355 356
        darshan_log_close(merge_fd);
        unlink(outlog_path);
357 358 359
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
360
    /* write the merged table of records to output file */
361
    ret = darshan_log_put_namehash(merge_fd, merge_hash);
362 363 364
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to write record table to output darshan log.\n");
Shane Snyder's avatar
Shane Snyder committed
365 366
        darshan_log_close(merge_fd);
        unlink(outlog_path);
367 368
        return(-1);
    }
369

370
    /* iterate over active darshan modules and gather module data to write
Shane Snyder's avatar
Shane Snyder committed
371
     * to the merged output log
372
     */
373
    for(i = 0; i < DARSHAN_MAX_MODS; i++)
374 375 376
    {
        if(!mod_logutils[i]) continue;

377
        if(shared_redux)
378
        {
379
            /* build the hash of records shared globally by this module */
Shane Snyder's avatar
Shane Snyder committed
380 381
            ret = build_mod_shared_rec_hash(infile_list, n_infiles, i,
                merge_job.nprocs, mod_buf, &shared_rec_hash);
382
            if(ret < 0)
383
            {
384 385 386
                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
387 388
                darshan_log_close(merge_fd);
                unlink(outlog_path);
389
                return(-1);
390
            }
391 392 393

        }

Shane Snyder's avatar
Shane Snyder committed
394
        for(j = 0; j < n_infiles; j++)
395
        {
Shane Snyder's avatar
Shane Snyder committed
396
            in_fd = darshan_log_open(infile_list[j]);
397 398 399 400
            if(in_fd == NULL)
            {
                fprintf(stderr,
                    "Error: unable to open input Darshan log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
401 402 403
                    infile_list[j]);
                darshan_log_close(merge_fd);
                unlink(outlog_path);
404 405 406
                return(-1);
            }

407 408 409 410 411
            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
412
                    ret = mod_logutils[i]->log_put_record(merge_fd, sref->agg_rec, in_fd->mod_ver[i]);
413 414 415 416 417
                    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
418 419 420
                        darshan_log_close(in_fd);
                        darshan_log_close(merge_fd);
                        unlink(outlog_path);
421 422 423 424 425
                        return(-1);
                    }
                }
            }

426
            /* loop over module records and write them to output file */
427
            while((ret = mod_logutils[i]->log_get_record(in_fd, mod_buf)) == 1)
428
            {
429 430 431 432 433 434
                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
435
                ret = mod_logutils[i]->log_put_record(merge_fd, mod_buf, in_fd->mod_ver[i]);
436 437 438 439
                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
440
                        darshan_module_names[i], infile_list[j]);
441
                    darshan_log_close(in_fd);
Shane Snyder's avatar
Shane Snyder committed
442 443
                    darshan_log_close(merge_fd);
                    unlink(outlog_path);
444 445 446
                    return(-1);
                }
            }
447 448 449
            if(ret < 0)
            {
                fprintf(stderr,
450
                    "Error: unable to read %s module record from input log file %s.\n",
Shane Snyder's avatar
Shane Snyder committed
451
                    darshan_module_names[i], infile_list[j]);
452
                darshan_log_close(in_fd);
Shane Snyder's avatar
Shane Snyder committed
453 454
                darshan_log_close(merge_fd);
                unlink(outlog_path);
455 456 457
                return(-1);
            }

458
            darshan_log_close(in_fd);
459
        }
460 461 462 463 464 465 466 467 468 469

        /* 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);
            }
        }
470 471
    }

Shane Snyder's avatar
Shane Snyder committed
472
    darshan_log_close(merge_fd);
473 474 475

    return(0);
}
Shane Snyder's avatar
Shane Snyder committed
476 477 478 479 480 481 482 483 484

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