darshan-logutils.c 45.4 KB
Newer Older
1
/*
Shane Snyder's avatar
Shane Snyder committed
2 3 4
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
5 6
 */

7
#define _GNU_SOURCE
8
#include "darshan-util-config.h"
9 10
#include <stdio.h>
#include <string.h>
11
#include <assert.h>
12
#include <stdlib.h>
13
#include <unistd.h>
14
#include <inttypes.h>
15 16 17
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
18
#include <errno.h>
19

20 21
#include "darshan-logutils.h"

22 23 24
/* default input buffer size for decompression algorithm */
#define DARSHAN_DEF_COMP_BUF_SZ (1024*1024) /* 1 MiB */

25
/* special identifers for referring to header, job, and
26 27 28 29 30 31
 * record map regions of the darshan log file
 */
#define DARSHAN_HEADER_REGION_ID    (-3)
#define DARSHAN_JOB_REGION_ID       (-2)
#define DARSHAN_REC_MAP_REGION_ID   (-1)

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
struct darshan_dz_state
{
    /* (libz/bzip2) stream data structure for managing
     * compression and decompression state */
    void *strm;
    /* buffer for staging compressed data to/from log file */
    unsigned char *buf;
    /* size of staging buffer */
    int size;
    /* for reading logs, flag indicating end of log file region */
    int eor;
    /* the region we last tried reading/writing */
    int prev_reg_id;
};

/* internal fd data structure */
struct darshan_fd_int_state
{
    /* posix file descriptor for the log file */
    int fildes;
    /* file pointer position */
    int64_t pos;
    /* flag indicating whether log file was created (and written) */
    int creat_flag;
    /* compression type used on log file (libz or bzip2) */
    enum darshan_comp_type comp_type;
    /* log file path name */
    char logfile_path[PATH_MAX];
    /* pointer to exe & mount data in darshan job data structure */
    char *exe_mnt_data;
    /* whether previous file operations have failed */
    int err;

    /* compression/decompression state */
    struct darshan_dz_state dz;
};

69 70
static int darshan_log_getheader(darshan_fd fd);
static int darshan_log_putheader(darshan_fd fd);
71 72
static int darshan_log_seek(darshan_fd fd, off_t offset);
static int darshan_log_read(darshan_fd fd, void *buf, int len);
73
static int darshan_log_write(darshan_fd fd, void *buf, int len);
74 75
static int darshan_log_dzinit(struct darshan_fd_int_state *state);
static void darshan_log_dzdestroy(struct darshan_fd_int_state *state);
76 77 78 79 80
static int darshan_log_dzread(darshan_fd fd, int region_id, void *buf, int len);
static int darshan_log_dzwrite(darshan_fd fd, int region_id, void *buf, int len);
static int darshan_log_libz_read(darshan_fd fd, int region_id, void *buf, int len);
static int darshan_log_libz_write(darshan_fd fd, int region_id, void *buf, int len);
static int darshan_log_libz_flush(darshan_fd fd, int region_id);
81 82 83 84 85
#ifdef HAVE_LIBBZ2
static int darshan_log_bzip2_read(darshan_fd fd, int region_id, void *buf, int len);
static int darshan_log_bzip2_write(darshan_fd fd, int region_id, void *buf, int len);
static int darshan_log_bzip2_flush(darshan_fd fd, int region_id);
#endif
86 87 88
static int darshan_log_dzload(darshan_fd fd, struct darshan_log_map map);
static int darshan_log_dzunload(darshan_fd fd, struct darshan_log_map *map_p);

Shane Snyder's avatar
Shane Snyder committed
89 90
/* each module's implementation of the darshan logutil functions */
#define X(a, b, c) c,
91 92
struct darshan_mod_logutil_funcs *mod_logutils[DARSHAN_MAX_MODS] =
{
Shane Snyder's avatar
Shane Snyder committed
93
    DARSHAN_MODULE_IDS
94
};
Shane Snyder's avatar
Shane Snyder committed
95
#undef X
96 97 98

/* darshan_log_open()
 *
99
 * open an existing darshan log file for reading only
100
 *
101
 * returns file descriptor on success, NULL on failure
102
 */
103
darshan_fd darshan_log_open(const char *name)
104
{
105
    darshan_fd tmp_fd;
106
    int ret;
107

108
    /* allocate a darshan file descriptor */
109 110 111 112
    tmp_fd = malloc(sizeof(*tmp_fd));
    if(!tmp_fd)
        return(NULL);
    memset(tmp_fd, 0, sizeof(*tmp_fd));
113 114 115 116 117 118 119
    tmp_fd->state = malloc(sizeof(struct darshan_fd_int_state));
    if(!tmp_fd->state)
    {
        free(tmp_fd->state);
        return(NULL);
    }
    memset(tmp_fd->state, 0, sizeof(struct darshan_fd_int_state));
120

121
    /* open the log file in read mode */
122 123
    tmp_fd->state->fildes = open(name, O_RDONLY);
    if(tmp_fd->state->fildes < 0)
124
    {
125
        fprintf(stderr, "Error: failed to open darshan log file %s.\n", name);
126
        free(tmp_fd->state);
127 128 129
        free(tmp_fd);
        return(NULL);
    }
130
    strncpy(tmp_fd->state->logfile_path, name, PATH_MAX);
131 132 133 134 135

    /* read the header from the log file to init fd data structures */
    ret = darshan_log_getheader(tmp_fd);
    if(ret < 0)
    {
136 137
        close(tmp_fd->state->fildes);
        free(tmp_fd->state);
138
        free(tmp_fd);
139 140 141 142
        return(NULL);
    }

    /* initialize compression data structures */
143
    ret = darshan_log_dzinit(tmp_fd->state);
144 145 146
    if(ret < 0)
    {
        fprintf(stderr, "Error: failed to initialize decompression data structures.\n");
147 148
        close(tmp_fd->state->fildes);
        free(tmp_fd->state);
149 150
        free(tmp_fd);
        return(NULL);
151 152
    }

153 154 155 156 157 158 159
    return(tmp_fd);
}

/* darshan_log_create()
 *
 * create a darshan log file for writing with the given compression method
 *
160
 * returns file descriptor on success, NULL on failure
161 162 163 164
 */
darshan_fd darshan_log_create(const char *name, enum darshan_comp_type comp_type)
{
    darshan_fd tmp_fd;
165
    int ret;
166

167
    /* allocate a darshan file descriptor */
168 169 170 171
    tmp_fd = malloc(sizeof(*tmp_fd));
    if(!tmp_fd)
        return(NULL);
    memset(tmp_fd, 0, sizeof(*tmp_fd));
172 173 174 175 176 177 178
    tmp_fd->state = malloc(sizeof(struct darshan_fd_int_state));
    if(!tmp_fd->state)
    {
        free(tmp_fd);
        return(NULL);
    }
    memset(tmp_fd->state, 0, sizeof(struct darshan_fd_int_state));
179

180 181 182
    /* create the log for writing, making sure to not overwrite existing log */
    tmp_fd->state->fildes = creat(name, 0400);
    if(tmp_fd->state->fildes < 0)
183
    {
184
        fprintf(stderr, "Error: failed to open darshan log file %s.\n", name);
185
        free(tmp_fd->state);
186
        free(tmp_fd);
187
        return(NULL);
188
    }
189 190 191
    tmp_fd->state->creat_flag = 1;
    tmp_fd->state->comp_type = comp_type;
    strncpy(tmp_fd->state->logfile_path, name, PATH_MAX);
192

193 194 195 196 197
    /* position file pointer to prealloc space for the log file header
     * NOTE: the header is written at close time, after all internal data
     * structures have been properly set
     */
    ret = darshan_log_seek(tmp_fd, sizeof(struct darshan_header));
198
    if(ret < 0)
199 200
    {
        fprintf(stderr, "Error: unable to seek in darshan log file.\n");
201 202
        close(tmp_fd->state->fildes);
        free(tmp_fd->state);
203 204 205
        free(tmp_fd);
        unlink(name);
        return(NULL);
206 207
    }

208
    /* initialize compression data structures */
209
    ret = darshan_log_dzinit(tmp_fd->state);
210 211
    if(ret < 0)
    {
212
        fprintf(stderr, "Error: failed to initialize compression data structures.\n");
213 214
        close(tmp_fd->state->fildes);
        free(tmp_fd->state);
215 216 217
        free(tmp_fd);
        unlink(name);
        return(NULL);
218 219
    }

220
    return(tmp_fd);
221 222
}

223
/* darshan_log_getjob()
224 225
 *
 * read job level metadata from the darshan log file
226
 *
227
 * returns 0 on success, -1 on failure
228
 */
229
int darshan_log_getjob(darshan_fd fd, struct darshan_job *job)
230
{
231
    struct darshan_fd_int_state *state = fd->state;
232 233
    char job_buf[DARSHAN_JOB_RECORD_SIZE] = {0};
    int job_buf_sz = DARSHAN_JOB_RECORD_SIZE;
234
    int ret;
235

236
    assert(state);
237 238
    assert(fd->job_map.len > 0 && fd->job_map.off > 0);

239
    /* read the compressed job data from the log file */
240 241
    ret = darshan_log_dzread(fd, DARSHAN_JOB_REGION_ID, job_buf, job_buf_sz);
    if(ret <= sizeof(*job))
242
    {
243
        fprintf(stderr, "Error: failed to read darshan log file job data.\n");
244 245
        return(-1);
    }
246 247

    memcpy(job, job_buf, sizeof(*job));
248

249
    if(fd->swap_flag)
250
    {
251 252 253 254 255 256
        /* swap bytes if necessary */
        DARSHAN_BSWAP64(&job->uid);
        DARSHAN_BSWAP64(&job->start_time);
        DARSHAN_BSWAP64(&job->end_time);
        DARSHAN_BSWAP64(&job->nprocs);
        DARSHAN_BSWAP64(&job->jobid);
257 258
    }

259
    /* save trailing exe & mount information, so it can be retrieved later */
260 261 262
    if(!(state->exe_mnt_data))
        state->exe_mnt_data = malloc(DARSHAN_EXE_LEN+1);
    if(!(state->exe_mnt_data))
263
        return(-1);
264
    memcpy(state->exe_mnt_data, &job_buf[sizeof(*job)], DARSHAN_EXE_LEN+1);
265

266 267 268
    return(0);
}

269 270
/* darshan_log_putjob()
 *
271
 * write job level metadata to darshan log file
272 273 274 275 276
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_putjob(darshan_fd fd, struct darshan_job *job)
{
277
    struct darshan_fd_int_state *state = fd->state;
278 279 280 281
    struct darshan_job job_copy;
    int len;
    int ret;

282 283
    assert(state);

284 285 286 287 288 289 290 291 292 293 294 295 296 297
    memset(&job_copy, 0, sizeof(*job));
    memcpy(&job_copy, job, sizeof(*job));

    /* check for newline in existing metadata, add if needed */
    len = strlen(job_copy.metadata);
    if(len > 0 && len < DARSHAN_JOB_METADATA_LEN)
    {
        if(job_copy.metadata[len-1] != '\n')
        {
            job_copy.metadata[len] = '\n';
            job_copy.metadata[len+1] = '\0';
        }
    }

298 299 300
    /* write the compressed job data to log file */
    ret = darshan_log_dzwrite(fd, DARSHAN_JOB_REGION_ID, &job_copy, sizeof(*job));
    if(ret != sizeof(*job))
301
    {
302
        state->err = -1;
303 304 305 306 307 308 309 310 311 312 313 314 315
        fprintf(stderr, "Error: failed to write darshan log file job data.\n");
        return(-1);
    }

    return(0);
}

/* darshan_log_getexe()
 *
 * reads the application exe name from darshan log file
 * 
 * returns 0 on success, -1 on failure 
 */
316 317
int darshan_log_getexe(darshan_fd fd, char *buf)
{
318
    struct darshan_fd_int_state *state = fd->state;
319
    char *newline;
320
    int ret;
321

322 323
    assert(state);

324
    /* if the exe/mount data has not been saved yet, read in the job info */
325
    if(!(state->exe_mnt_data))
326
    {
327 328
        struct darshan_job job;
        ret = darshan_log_getjob(fd, &job);
329

330
        if(ret < 0 || !(state->exe_mnt_data))
331
            return(-1);
332
    }
333

334
    /* exe string is located before the first line break */
335
    newline = strchr(state->exe_mnt_data, '\n');
336 337

    /* copy over the exe string */
338
    if(newline)
339
        memcpy(buf, state->exe_mnt_data, (newline - state->exe_mnt_data));
340 341 342 343

    return (0);
}

344 345 346
/* darshan_log_putexe()
 *
 * wrties the application exe name to darshan log file
347
 * NOTE: this needs to be called immediately following put_job as it
348
 * expects the file pointer to be positioned immediately following
349
 * the darshan job information
350 351 352 353 354
 *
 * returns 0 on success, -1 on failure 
 */
int darshan_log_putexe(darshan_fd fd, char *buf)
{
355 356
    struct darshan_fd_int_state *state = fd->state;
    int len = strlen(buf);
357
    int ret;
358

359
    assert(fd->state);
360

361 362
    ret = darshan_log_dzwrite(fd, DARSHAN_JOB_REGION_ID, buf, len);
    if(ret != len)
363
    {
364
        state->err = -1;
365 366 367 368 369 370 371
        fprintf(stderr, "Error: failed to write exe string to darshan log file.\n");
        return(-1);
    }

    return(0);
}

372 373
/* darshan_log_getmounts()
 * 
374
 * retrieves mount table information from the log. Note that mnt_pts and
375
 * fs_types are arrays that will be allocated by the function and must be
376 377 378
 * freed by the caller. count will indicate the size of the arrays
 *
 * returns 0 on success, -1 on failure
379
 */
380
int darshan_log_getmounts(darshan_fd fd, char*** mnt_pts,
381 382
    char*** fs_types, int* count)
{
383
    struct darshan_fd_int_state *state = fd->state;
384 385
    char *pos;
    int array_index = 0;
386
    int ret;
387

388 389
    assert(state);

390
    /* if the exe/mount data has not been saved yet, read in the job info */
391
    if(!(state->exe_mnt_data))
392
    {
393 394
        struct darshan_job job;
        ret = darshan_log_getjob(fd, &job);
395

396
        if(ret < 0 || !(state->exe_mnt_data))
397
            return(-1);
398
    }
399

400
    /* count entries */
401
    *count = 0;
402
    pos = state->exe_mnt_data;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    while((pos = strchr(pos, '\n')) != NULL)
    {
        pos++;
        (*count)++;
    }

    if(*count == 0)
    {
        /* no mount entries present */
        return(0);
    }

    /* allocate output arrays */
    *mnt_pts = malloc((*count)*sizeof(char*));
    assert(*mnt_pts);
    *fs_types = malloc((*count)*sizeof(char*));
    assert(*fs_types);

    /* work backwards through the table and parse each line (except for
     * first, which holds command line information)
     */
424
    while((pos = strrchr(state->exe_mnt_data, '\n')) != NULL)
425 426
    {
        /* overestimate string lengths */
427
        (*mnt_pts)[array_index] = malloc(DARSHAN_EXE_LEN);
428
        assert((*mnt_pts)[array_index]);
429
        (*fs_types)[array_index] = malloc(DARSHAN_EXE_LEN);
430 431
        assert((*fs_types)[array_index]);

432 433 434
        ret = sscanf(++pos, "%s\t%s", (*fs_types)[array_index],
            (*mnt_pts)[array_index]);
        if(ret != 2)
435
        {
436
            fprintf(stderr, "Error: poorly formatted mount table in darshan log file.\n");
437 438 439 440 441 442 443
            return(-1);
        }
        pos--;
        *pos = '\0';
        array_index++;
    }

444 445 446
    return(0);
}

447 448 449 450 451 452 453 454 455 456 457
/* darshan_log_putmounts()
 *
 * writes mount information to the darshan log file
 * NOTE: this function call should follow immediately after the call
 * to darshan_log_putexe(), as it assumes the darshan log file pointer
 * is pointing to the offset immediately following the exe string
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_putmounts(darshan_fd fd, char** mnt_pts, char** fs_types, int count)
{
458
    struct darshan_fd_int_state *state = fd->state;
459 460
    int i;
    char line[1024];
461 462 463
    char mnt_dat[DARSHAN_EXE_LEN] = {0};
    int mnt_dat_sz = 0;
    char *tmp;
464 465
    int ret;

466 467
    assert(state);

468
    /* write each mount entry to file */
469
    tmp = mnt_dat;
470 471 472
    for(i=count-1; i>=0; i--)
    {
        sprintf(line, "\n%s\t%s", fs_types[i], mnt_pts[i]);
473 474 475 476 477 478

        memcpy(tmp, line, strlen(line));
        tmp += strlen(line);
        mnt_dat_sz += strlen(line);
    }

479 480
    ret = darshan_log_dzwrite(fd, DARSHAN_JOB_REGION_ID, mnt_dat, mnt_dat_sz);
    if (ret != mnt_dat_sz)
481
    {
482
        state->err = -1;
483
        fprintf(stderr, "Error: failed to write darshan log mount data.\n");
484 485 486 487 488 489
        return(-1);
    }

    return(0);
}

490
/* darshan_log_gethash()
491
 *
492
 * read the hash of records from the darshan log file
493 494 495
 *
 * returns 0 on success, -1 on failure
 */
496
int darshan_log_gethash(darshan_fd fd, struct darshan_record_ref **hash)
497
{
498
    struct darshan_fd_int_state *state = fd->state;
499
    char *hash_buf;
500
    int hash_buf_sz;
501
    char *buf_ptr;
502
    darshan_record_id *rec_id_ptr;
503
    uint32_t *path_len_ptr, tmp_path_len;
504 505
    char *path_ptr;
    struct darshan_record_ref *ref;
506 507 508
    int read;
    int read_req_sz;
    int buf_remaining = 0;
509

510 511
    assert(state);

512 513 514 515 516 517 518
    /* just return if there is no record mapping data */
    if(fd->rec_map.len == 0)
    {
        *hash = NULL;
        return(0);
    }

519 520 521
    /* default to hash buffer twice as big as default compression buf */
    hash_buf = malloc(DARSHAN_DEF_COMP_BUF_SZ * 2);
    if(!hash_buf)
522
        return(-1);
523 524
    memset(hash_buf, 0, DARSHAN_DEF_COMP_BUF_SZ * 2);
    hash_buf_sz = DARSHAN_DEF_COMP_BUF_SZ * 2;
525

526
    do
527
    {
528 529
        /* read chunks of the darshan record id -> file name mapping from log file,
         * constructing a hash table in the process
530
         */
531 532 533 534
        read_req_sz = hash_buf_sz - buf_remaining;
        read = darshan_log_dzread(fd, DARSHAN_REC_MAP_REGION_ID,
            hash_buf + buf_remaining, read_req_sz);
        if(read < 0)
535
        {
536 537 538
            fprintf(stderr, "Error: failed to read record hash from darshan log file.\n");
            free(hash_buf);
            return(-1);
539 540
        }

541 542 543 544 545 546 547 548
        /* work through the hash buffer -- deserialize the mapping data and
         * add to the output hash table
         * NOTE: these mapping pairs are variable in length, so we have to be able
         * to handle incomplete mappings temporarily here
         */
        buf_ptr = hash_buf;
        buf_remaining += read;
        while(buf_remaining > (sizeof(darshan_record_id) + sizeof(uint32_t)))
549
        {
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
            /* see if we have enough buf space to read in the next full record */
            tmp_path_len = *(uint32_t *)(buf_ptr + sizeof(darshan_record_id));
            if(fd->swap_flag)
                DARSHAN_BSWAP32(&tmp_path_len);

            /* we need to read more before we continue deserializing */
            if(buf_remaining <
                (sizeof(darshan_record_id) + sizeof(uint32_t) + tmp_path_len))
                break;

            /* get pointers for each field of this darshan record */
            /* NOTE: darshan record hash serialization method: 
             *          ... darshan_record_id | (uint32_t) path_len | path ...
             */
            rec_id_ptr = (darshan_record_id *)buf_ptr;
            buf_ptr += sizeof(darshan_record_id);
            path_len_ptr = (uint32_t *)buf_ptr;
            buf_ptr += sizeof(uint32_t);
            path_ptr = (char *)buf_ptr;

            if(fd->swap_flag)
571
            {
572 573 574
                /* we need to sort out endianness issues before deserializing */
                DARSHAN_BSWAP64(rec_id_ptr);
                DARSHAN_BSWAP32(path_len_ptr);
575
            }
576 577 578

            HASH_FIND(hlink, *hash, rec_id_ptr, sizeof(darshan_record_id), ref);
            if(!ref)
579
            {
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
                ref = malloc(sizeof(*ref));
                if(!ref)
                {
                    free(hash_buf);
                    return(-1);
                }
                ref->rec.name = malloc(*path_len_ptr + 1);
                if(!ref->rec.name)
                {
                    free(ref);
                    free(hash_buf);
                    return(-1);
                }

                /* set the fields for this record */
                ref->rec.id = *rec_id_ptr;
                memcpy(ref->rec.name, path_ptr, *path_len_ptr);
                ref->rec.name[*path_len_ptr] = '\0';

                /* add this record to the hash */
                HASH_ADD(hlink, *hash, rec.id, sizeof(darshan_record_id), ref);
601 602
            }

603 604 605
            buf_ptr += *path_len_ptr;
            buf_remaining -=
                (sizeof(darshan_record_id) + sizeof(uint32_t) + *path_len_ptr);
606
        }
607

608 609 610 611 612 613 614 615
        /* copy any leftover data to beginning of buffer to parse next */
        memcpy(hash_buf, buf_ptr, buf_remaining);

        /* we keep reading until we get a short read informing us we have
         * read all of the record hash
         */
    } while(read == read_req_sz);
    assert(buf_remaining == 0);
616

617
    free(hash_buf);
618 619 620
    return(0);
}

621 622 623
/* darshan_log_puthash()
 *
 * writes the hash table of records to the darshan log file
624 625 626
 * NOTE: this function call should follow immediately after the call
 * to darshan_log_putmounts(), as it assumes the darshan log file pointer
 * is pointing to the offset immediately following the mount information
627 628 629 630 631
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_puthash(darshan_fd fd, struct darshan_record_ref *hash)
{
632
    struct darshan_fd_int_state *state = fd->state;
633
    char *hash_buf;
634
    int hash_buf_sz;
635
    struct darshan_record_ref *ref, *tmp;
636 637 638
    char *buf_ptr;
    int path_len;
    int wrote;
639

640 641
    assert(state);

642 643
    /* allocate memory for largest possible hash record */
    hash_buf_sz = sizeof(darshan_record_id) + sizeof(uint32_t) + PATH_MAX;
644 645 646
    hash_buf = malloc(hash_buf_sz);
    if(!hash_buf)
        return(-1);
647
    memset(hash_buf, 0, hash_buf_sz);
648

649
    /* individually serialize each hash record and write to log file */
650 651
    HASH_ITER(hlink, hash, ref, tmp)
    {
652 653
        buf_ptr = hash_buf;
        path_len = strlen(ref->rec.name);
654

655
        /* the hash buffer has space to serialize this record
656 657 658
         * NOTE: darshan record hash serialization method: 
         *          ... darshan_record_id | (uint32_t) path_len | path ...
         */
659 660 661 662 663 664
        *((darshan_record_id *)buf_ptr) = ref->rec.id;
        buf_ptr += sizeof(darshan_record_id);
        *((uint32_t *)buf_ptr) = path_len;
        buf_ptr += sizeof(uint32_t);
        memcpy(buf_ptr, ref->rec.name, path_len);
        buf_ptr += path_len;
665

666 667 668 669 670
        /* write this hash entry to log file */
        wrote = darshan_log_dzwrite(fd, DARSHAN_REC_MAP_REGION_ID,
            hash_buf, (buf_ptr - hash_buf));
        if(wrote != (buf_ptr - hash_buf))
        {
671
            state->err = -1;
672 673 674 675
            fprintf(stderr, "Error: failed to write record hash to darshan log file.\n");
            free(hash_buf);
            return(-1);
        }
676 677 678 679 680 681 682 683
    }

    free(hash_buf);
    return(0);
}

/* darshan_log_getmod()
 *
684 685
 * get a chunk of module data from the darshan log file
 *
686
 * returns number of bytes read on success, -1 on failure
687 688
 */
int darshan_log_getmod(darshan_fd fd, darshan_module_id mod_id,
689
    void *mod_buf, int mod_buf_sz)
690
{
691
    struct darshan_fd_int_state *state = fd->state;
692
    int ret;
693

694 695
    assert(state);

696
    if(mod_id < 0 || mod_id >= DARSHAN_MAX_MODS)
697
    {
698 699
        fprintf(stderr, "Error: invalid Darshan module id.\n");
        return(-1);
700 701
    }

702 703 704 705
    if(fd->mod_map[mod_id].len == 0)
        return(0); /* no data corresponding to this mod_id */

    /* read this module's data from the log file */
706
    ret = darshan_log_dzread(fd, mod_id, mod_buf, mod_buf_sz);
707
    if(ret < 0)
708
    {
709
        fprintf(stderr,
710
            "Error: failed to read module %s data from darshan log file.\n",
711
            darshan_module_names[mod_id]);
712 713 714
        return(-1);
    }

715
    return(ret);
716 717
}

718 719 720
/* darshan_log_putmod()
 *
 * write a chunk of module data to the darshan log file
Shane Snyder's avatar
Shane Snyder committed
721 722 723 724 725 726
 * NOTE: this function call should be called directly after the
 * put_hash() function, as it expects the file pointer to be
 * positioned directly past the record hash location. Also,
 * for a set of modules with data to write to file, this function
 * should be called in order of increasing module identifiers,
 * as the darshan log file format expects this ordering.
727
 *
728
 * returns number of bytes written on success, -1 on failure
729 730 731 732
 */
int darshan_log_putmod(darshan_fd fd, darshan_module_id mod_id,
    void *mod_buf, int mod_buf_sz)
{
733
    struct darshan_fd_int_state *state = fd->state;
734 735
    int ret;

736 737
    assert(state);

Shane Snyder's avatar
Shane Snyder committed
738
    if(mod_id < 0 || mod_id >= DARSHAN_MAX_MODS)
739
    {
740
        state->err = -1;
Shane Snyder's avatar
Shane Snyder committed
741 742
        fprintf(stderr, "Error: invalid Darshan module id.\n");
        return(-1);
743 744 745
    }

    /* write the module chunk to the log file */
746 747
    ret = darshan_log_dzwrite(fd, mod_id, mod_buf, mod_buf_sz);
    if(ret != mod_buf_sz)
748
    {
749
        state->err = -1;
750 751 752 753 754 755 756 757 758
        fprintf(stderr,
            "Error: failed to write module %s data to darshan log file.\n",
            darshan_module_names[mod_id]);
        return(-1);
    }

    return(0);
}

759 760
/* darshan_log_close()
 *
761
 * close an open darshan file descriptor, freeing any resources
762 763 764
 *
 */
void darshan_log_close(darshan_fd fd)
765
{
766
    struct darshan_fd_int_state *state = fd->state;
767 768
    int ret;

769 770 771 772
    assert(state);

    /* if the file was created for writing */
    if(state->creat_flag)
773 774
    {
        /* flush the last region of the log to file */
775
        switch(state->comp_type)
776 777
        {
            case DARSHAN_ZLIB_COMP:
778
                ret = darshan_log_libz_flush(fd, state->dz.prev_reg_id);
779 780
                if(ret == 0)
                    break;
781 782
#ifdef HAVE_LIBBZ2
            case DARSHAN_BZIP2_COMP:
783
                ret = darshan_log_bzip2_flush(fd, state->dz.prev_reg_id);
784 785 786
                if(ret == 0)
                    break;
#endif 
787 788
            default:
                /* if flush fails, remove the output log file */
789
                state->err = -1;
790 791 792 793 794
                fprintf(stderr, "Error: final flush to log file failed.\n");
                break;
        }

        /* if no errors flushing, write the log header before closing */
795
        if(state->err != -1)
796 797 798
        {
            ret = darshan_log_putheader(fd);
            if(ret < 0)
799
                state->err = -1;
800 801 802
        }
    }

803
    close(state->fildes);
804 805

    /* remove output log file if error writing to it */
806
    if((state->creat_flag) && (state->err == -1))
807 808
    {
        fprintf(stderr, "Unlinking darshan log file %s ...\n",
809 810
            state->logfile_path);
        unlink(state->logfile_path);
811
    }
812

813 814 815 816
    darshan_log_dzdestroy(state);
    if(state->exe_mnt_data)
        free(state->exe_mnt_data);
    free(state);
817
    free(fd);
818 819

    return;
820 821
}

822
/* **************************************************** */
823

824 825 826 827 828 829 830
/* read the header of the darshan log and set internal fd data structures
 * NOTE: this is the only portion of the darshan log that is uncompressed
 *
 * returns 0 on success, -1 on failure
 */
static int darshan_log_getheader(darshan_fd fd)
{
831
    struct darshan_fd_int_state *state = fd->state;
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    struct darshan_header header;
    int i;
    int ret;

    ret = darshan_log_seek(fd, 0);
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to seek in darshan log file.\n");
        return(-1);
    }

    /* read uncompressed header from log file */
    ret = darshan_log_read(fd, &header, sizeof(header));
    if(ret != sizeof(header))
    {
        fprintf(stderr, "Error: failed to read darshan log file header.\n");
        return(-1);
    }

    /* save the version string */
    strncpy(fd->version, header.version_string, 8);

    if(header.magic_nr == DARSHAN_MAGIC_NR)
    {
        /* no byte swapping needed, this file is in host format already */
        fd->swap_flag = 0;
    }
    else
    {
        /* try byte swapping */
        DARSHAN_BSWAP64(&(header.magic_nr));
        if(header.magic_nr == DARSHAN_MAGIC_NR)
        {
            fd->swap_flag = 1;

            /* swap the log map variables in the header */
            DARSHAN_BSWAP64(&(header.rec_map.off));
            DARSHAN_BSWAP64(&(header.rec_map.len));
            for(i = 0; i < DARSHAN_MAX_MODS; i++)
            {
                DARSHAN_BSWAP64(&(header.mod_map[i].off));
                DARSHAN_BSWAP64(&(header.mod_map[i].len));
            }
        }
        else
        {
            /* otherwise this file is just broken */
            fprintf(stderr, "Error: bad magic number in darshan log file.\n");
            return(-1);
        }
    }

884
    state->comp_type = header.comp_type;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

    /* save the mapping of data within log file to this file descriptor */
    fd->job_map.off = sizeof(struct darshan_header);
    fd->job_map.len = header.rec_map.off - fd->job_map.off;
    memcpy(&fd->rec_map, &(header.rec_map), sizeof(struct darshan_log_map));
    memcpy(&fd->mod_map, &(header.mod_map), DARSHAN_MAX_MODS * sizeof(struct darshan_log_map));

    return(0);
}

/* write a darshan header to log file
 *
 * returns 0 on success, -1 on failure
 */
static int darshan_log_putheader(darshan_fd fd)
{
901
    struct darshan_fd_int_state *state = fd->state;
902 903 904 905 906 907 908 909 910 911 912 913 914
    struct darshan_header header;
    int ret;

    ret = darshan_log_seek(fd, 0);
    if(ret < 0)
    {
        fprintf(stderr, "Error: unable to seek in darshan log file.\n");
        return(-1);
    }

    memset(&header, 0, sizeof(header));
    strcpy(header.version_string, DARSHAN_LOG_VERSION);
    header.magic_nr = DARSHAN_MAGIC_NR;
915
    header.comp_type = state->comp_type;
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

    /* copy the mapping information to the header */
    memcpy(&header.rec_map, &fd->rec_map, sizeof(struct darshan_log_map));
    memcpy(&header.mod_map, &fd->mod_map, DARSHAN_MAX_MODS * sizeof(struct darshan_log_map));

    /* write header to file */
    ret = darshan_log_write(fd, &header, sizeof(header));
    if(ret != sizeof(header))
    {
        fprintf(stderr, "Error: failed to write Darshan log file header.\n");
        return(-1);
    }

    return(0);
}

932 933 934 935
/* return 0 on successful seek to offset, -1 on failure.
 */
static int darshan_log_seek(darshan_fd fd, off_t offset)
{
936
    struct darshan_fd_int_state *state = fd->state;
937
    off_t ret_off;
938

939
    if(state->pos == offset)
940 941
        return(0);

942
    ret_off = lseek(state->fildes, offset, SEEK_SET);
943
    if(ret_off == offset)
944
    {
945
        state->pos = offset;
946
        return(0);
947 948 949 950 951
    }

    return(-1);
}

952
/* return amount read on success, 0 on EOF, -1 on failure.
953
 */
954
static int darshan_log_read(darshan_fd fd, void* buf, int len)
955
{
956
    struct darshan_fd_int_state *state = fd->state;
957 958
    int ret;

959
    /* read data from the log file using the given map */
960
    ret = read(state->fildes, buf, len);
961
    if(ret > 0)
962
        state->pos += ret;
963

964
    return(ret);
965 966
}

967
/* return amount written on success, -1 on failure.
968
 */
969
static int darshan_log_write(darshan_fd fd, void* buf, int len)
970
{
971
    struct darshan_fd_int_state *state = fd->state;
972 973
    int ret;

974
    ret = write(state->fildes, buf, len);
975
    if(ret > 0)
976
        state->pos += ret;
977 978 979 980

    return(ret);
}

981
static int darshan_log_dzinit(struct darshan_fd_int_state *state)
982 983 984
{
    int ret;

985
    /* initialize buffers for staging compressed data to/from log file */
986 987
    state->dz.buf = malloc(DARSHAN_DEF_COMP_BUF_SZ);
    if(state->dz.buf == NULL)
988 989
        return(-1);

990
    state->dz.prev_reg_id = DARSHAN_HEADER_REGION_ID;
991

992
    switch(state->comp_type)
993 994
    {
        case DARSHAN_ZLIB_COMP:
995 996 997 998
        {
            z_stream *tmp_zstrm = malloc(sizeof(*tmp_zstrm));
            if(!tmp_zstrm)
            {
999
                free(state->dz.buf);
1000 1001 1002 1003 1004 1005 1006 1007 1008
                return(-1);
            }
            tmp_zstrm->zalloc = Z_NULL;
            tmp_zstrm->zfree = Z_NULL;
            tmp_zstrm->opaque = Z_NULL;
            tmp_zstrm->avail_in = 0;
            tmp_zstrm->next_in = Z_NULL;

            /* TODO: worth using {inflate/deflate}Init2 ?? */
1009
            if(!(state->creat_flag))
1010 1011 1012 1013 1014 1015 1016 1017 1018
            {
                /* read only file, init inflate algorithm */
                ret = inflateInit(tmp_zstrm);
            }
            else
            {
                /* write only file, init deflate algorithm */
                ret = deflateInit(tmp_zstrm, Z_DEFAULT_COMPRESSION);
                tmp_zstrm->avail_out = DARSHAN_DEF_COMP_BUF_SZ;
1019
                tmp_zstrm->next_out = state->dz.buf;
1020 1021 1022 1023
            }
            if(ret != Z_OK)
            {
                free(tmp_zstrm);
1024
                free(state->dz.buf);
1025 1026
                return(-1);
            }
1027
            state->dz.strm = tmp_zstrm;
1028
            break;
1029
        }
1030 1031 1032 1033 1034 1035
#ifdef HAVE_LIBBZ2
        case DARSHAN_BZIP2_COMP:
        {
            bz_stream *tmp_bzstrm = malloc(sizeof(*tmp_bzstrm));
            if(!tmp_bzstrm)
            {
1036
                free(state->dz.buf);
1037 1038 1039 1040 1041 1042 1043 1044
                return(-1);
            }
            tmp_bzstrm->bzalloc = NULL;
            tmp_bzstrm->bzfree = NULL;
            tmp_bzstrm->opaque = NULL;
            tmp_bzstrm->avail_in = 0;
            tmp_bzstrm->next_in = Z_NULL;

1045
            if(state->creat_flag)
1046 1047 1048 1049 1050 1051 1052 1053 1054
            {
                /* read only file, init decompress algorithm */
                ret = BZ2_bzDecompressInit(tmp_bzstrm, 1, 0);
            }
            else
            {
                /* write only file, init compress algorithm */
                ret = BZ2_bzCompressInit(tmp_bzstrm, 9, 1, 30);
                tmp_bzstrm->avail_out = DARSHAN_DEF_COMP_BUF_SZ;
1055
                tmp_bzstrm->next_out = (char *)state->dz.buf;
1056 1057 1058 1059
            }
            if(ret != BZ_OK)
            {
                free(tmp_bzstrm);
1060
                free(state->dz.buf);
1061 1062
                return(-1);
            }
1063
            state->dz.strm = tmp_bzstrm;
1064 1065 1066
            break;
        }
#endif
1067 1068 1069 1070 1071 1072 1073 1074
        default:
            fprintf(stderr, "Error: invalid compression type.\n");
            return(-1);
    }

    return(0);
}

1075
static void darshan_log_dzdestroy(struct darshan_fd_int_state *state)
1076
{
1077
    switch(state->comp_type)
1078 1079
    {
        case DARSHAN_ZLIB_COMP:
1080 1081
            if(!(state->creat_flag))
                inflateEnd(state->dz.strm);
1082
            else
1083 1084
                deflateEnd(state->dz.strm);
            free(state->dz.strm);
1085
            break;
1086 1087
#ifdef HAVE_LIBBZ2
        case DARSHAN_BZIP2_COMP:
1088 1089
            if(!(state->creat_flag))
                BZ2_bzDecompressEnd(state->dz.strm);
1090
            else
1091 1092
                BZ2_bzCompressEnd(state->dz.strm);
            free(state->dz.strm);
1093 1094
            break;
#endif
1095 1096 1097 1098
        default:
            fprintf(stderr, "Error: invalid compression type.\n");
    }

1099
    free(state->dz.buf);
1100 1101 1102 1103 1104
    return;
}

static int darshan_log_dzread(darshan_fd fd, int region_id, void *buf, int len)
{
1105
    struct darshan_fd_int_state *state = fd->state;
1106 1107
    int ret;

1108
    switch(state->comp_type)
1109 1110 1111
    {
        case DARSHAN_ZLIB_COMP:
            ret = darshan_log_libz_read(fd, region_id, buf, len);
1112
            break;
1113 1114 1115 1116 1117
#ifdef HAVE_LIBBZ2
        case DARSHAN_BZIP2_COMP:
            ret = darshan_log_bzip2_read(fd, region_id, buf, len);
            break;
#endif
1118
        default:
1119
            fprintf(stderr, "Error: invalid compression type.\n");
1120 1121 1122 1123 1124 1125
            return(-1);
    }

    return(ret);
}