GitLab maintenance scheduled form Friday, 2021-06-18 5:00pm to Satursday, 2021-06-19 10:00pm CT - Services will be unavailable during this time.

darshan-logutils.c 20.6 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
static int darshan_log_seek(darshan_fd fd, off_t offset);
static int darshan_log_read(darshan_fd fd, void *buf, int len);
24
static int darshan_log_write(darshan_fd fd, void *buf, int len);
25 26 27 28

/* TODO: can we make this s.t. we don't care about ordering (i.e., X macro it ) */
struct darshan_mod_logutil_funcs *mod_logutils[DARSHAN_MAX_MODS] =
{
29
    NULL,               /* NULL */
30
    &posix_logutils,    /* POSIX */
31 32 33
    &mpiio_logutils,    /* MPI-IO */
    &hdf5_logutils,     /* HDF5 */
    &pnetcdf_logutils,  /* PNETCDF */
34 35 36 37 38 39 40 41 42 43 44 45
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};
46 47 48 49 50 51 52

/* darshan_log_open()
 *
 * open a darshan log file for reading/writing
 *
 * returns 0 on success, -1 on failure
 */
53
darshan_fd darshan_log_open(const char *name, const char *mode)
54
{
55
    darshan_fd tmp_fd;
56

57 58 59 60
    /* we only allows "w" or "r" modes, nothing fancy */
    assert(strlen(mode) == 1);
    assert(mode[0] == 'r' || mode[0] == 'w');

61
    tmp_fd = malloc(sizeof(*tmp_fd));
62 63 64 65
    if(!tmp_fd)
        return(NULL);
    memset(tmp_fd, 0, sizeof(*tmp_fd));

66 67
    tmp_fd->gzf = gzopen(name, mode);
    if(!tmp_fd->gzf)
68 69
    {
        free(tmp_fd);
70
        tmp_fd = NULL;
71
    }
72 73

    return(tmp_fd);
74 75
}

76 77 78 79 80 81 82 83
/* darshan_log_getheader()
 *
 * read the header of the darshan log and set internal data structures
 * NOTE: this function must be called before reading other portions of the log
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_getheader(darshan_fd fd, struct darshan_header *header)
84
{
85
    int i;
86
    int ret;
87

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

95
    /* read header from log file */
96
    ret = darshan_log_read(fd, header, sizeof(*header));
97
    if(ret != sizeof(*header))
98
    {
99
        fprintf(stderr, "Error: failed to read darshan log file header.\n");
100 101 102
        return(-1);
    }

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

106
    if(header->magic_nr == DARSHAN_MAGIC_NR)
107
    {
108
        /* no byte swapping needed, this file is in host format already */
109 110 111 112 113 114
        fd->swap_flag = 0;
    }
    else
    {
        /* try byte swapping */
        DARSHAN_BSWAP64(&header->magic_nr);
115
        if(header->magic_nr == DARSHAN_MAGIC_NR)
116 117
        {
            fd->swap_flag = 1;
118 119 120 121

            /* swap the log map variables in the header */
            DARSHAN_BSWAP64(&header->rec_map.off);
            DARSHAN_BSWAP64(&header->rec_map.len);
122
            for(i = 0; i < DARSHAN_MAX_MODS; i++)
123 124 125 126
            {
                DARSHAN_BSWAP64(&header->mod_map[i].off);
                DARSHAN_BSWAP64(&header->mod_map[i].len);
            }
127 128 129 130 131 132 133
        }
        else
        {
            /* otherwise this file is just broken */
            fprintf(stderr, "Error: bad magic number in darshan log file.\n");
            return(-1);
        }
134 135
    }

136 137 138
    /* save the mapping of data within log file to this file descriptor */
    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));
139 140

    return(0);
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
/* darshan_log_putheader()
 *
 * write a darshan header to log file
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_putheader(darshan_fd fd, 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);
    }

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

    /* copy the mapping information to the file descriptor */
    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);
}

175
/* darshan_log_getjob()
176 177
 *
 * read job level metadata from the darshan log file
178
 *
179
 * returns 0 on success, -1 on failure
180
 */
181
int darshan_log_getjob(darshan_fd fd, struct darshan_job *job)
182
{
183
    int ret;
184

185
    ret = darshan_log_seek(fd, sizeof(struct darshan_header));
186
    if(ret < 0)
187
    {
188
        fprintf(stderr, "Error: unable to seek in darshan log file.\n");
189
        return(-1);
190 191
    }

192
    /* read the job data from the log file */
193
    ret = darshan_log_read(fd, job, sizeof(*job));
194
    if(ret != sizeof(*job))
195
    {
196
        fprintf(stderr, "Error: failed to read darshan log file job data.\n");
197 198 199
        return(-1);
    }

200
    if(fd->swap_flag)
201
    {
202 203 204 205 206 207
        /* 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);
208 209
    }

210 211 212
    return(0);
}

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
/* darshan_log_putjob()
 *
 * write job level metadat to darshan log file
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_putjob(darshan_fd fd, struct darshan_job *job)
{
    struct darshan_job job_copy;
    int len;
    int ret;

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

    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';
        }
    }

    /* write job data to file */
    ret = darshan_log_write(fd, job, sizeof(*job));
    if(ret != sizeof(*job))
    {
        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 
 */
263 264
int darshan_log_getexe(darshan_fd fd, char *buf)
{
265 266
    int tmp_off = sizeof(struct darshan_header) + sizeof(struct darshan_job);
    int ret;
267 268
    char *newline;

269 270
    ret = darshan_log_seek(fd, tmp_off);
    if(ret < 0)
271
    {
272 273
        fprintf(stderr, "Error: unable to seek in darshan log file.\n");
        return(-1);
274
    }
275

276 277
    /* read the trailing exe data from the darshan log */
    ret = darshan_log_read(fd, buf, DARSHAN_EXE_LEN+1);
278
    if(ret != DARSHAN_EXE_LEN+1)
279
    {
280
        fprintf(stderr, "Error: failed to read exe string from darshan log file.\n");
281 282
        return(-1);
    }
283

284 285
    /* mount info is stored after the exe string, so truncate there */
    newline = strchr(buf, '\n');
286
    if(newline)
287
        *newline = '\0';
288 289 290 291

    return (0);
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/* darshan_log_putexe()
 *
 * wrties the application exe name to darshan log file
 *
 * returns 0 on success, -1 on failure 
 */
int darshan_log_putexe(darshan_fd fd, char *buf)
{
    int tmp_off = sizeof(struct darshan_header) + sizeof(struct darshan_job);
    int len;
    int ret;

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

    len = strlen(buf);

    ret = darshan_log_write(fd, buf, len);
    if(ret != len)
    {
        fprintf(stderr, "Error: failed to write exe string to darshan log file.\n");
        return(-1);
    }

    return(0);
}

323 324
/* darshan_log_getmounts()
 * 
325
 * retrieves mount table information from the log. Note that mnt_pts and
326
 * fs_types are arrays that will be allocated by the function and must be
327 328 329
 * freed by the caller. count will indicate the size of the arrays
 *
 * returns 0 on success, -1 on failure
330
 */
331
int darshan_log_getmounts(darshan_fd fd, char*** mnt_pts,
332 333
    char*** fs_types, int* count)
{
334
    int tmp_off = sizeof(struct darshan_header) + sizeof(struct darshan_job);
335 336 337
    int ret;
    char *pos;
    int array_index = 0;
338
    char buf[DARSHAN_EXE_LEN+1];
339

340 341
    ret = darshan_log_seek(fd, tmp_off);
    if(ret < 0)
342
    {
343 344 345
        fprintf(stderr, "Error: unable to seek in darshan log file.\n");
        return(-1);
    }
346

347 348
    /* read the trailing mount data from the darshan log */
    ret = darshan_log_read(fd, buf, DARSHAN_EXE_LEN+1);
349
    if(ret != DARSHAN_EXE_LEN+1)
350
    {
351
        fprintf(stderr, "Error: failed to read mount info from darshan log file.\n");
352
        return(-1);
353
    }
354

355
    /* count entries */
356
    *count = 0;
357
    pos = buf;
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    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)
     */
379
    while((pos = strrchr(buf, '\n')) != NULL)
380 381
    {
        /* overestimate string lengths */
382
        (*mnt_pts)[array_index] = malloc(DARSHAN_EXE_LEN);
383
        assert((*mnt_pts)[array_index]);
384
        (*fs_types)[array_index] = malloc(DARSHAN_EXE_LEN);
385 386
        assert((*fs_types)[array_index]);

387 388 389
        ret = sscanf(++pos, "%s\t%s", (*fs_types)[array_index],
            (*mnt_pts)[array_index]);
        if(ret != 2)
390
        {
391
            fprintf(stderr, "Error: poorly formatted mount table in darshan log file.\n");
392 393 394 395 396 397 398
            return(-1);
        }
        pos--;
        *pos = '\0';
        array_index++;
    }

399 400 401
    return(0);
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/* 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)
{
    int i;
    char line[1024];
    int ret;

    /* write each mount entry to file */
    for(i=count-1; i>=0; i--)
    {
        sprintf(line, "\n%s\t%s", fs_types[i], mnt_pts[i]);
        ret = darshan_log_write(fd, line, strlen(line));
        if (ret != strlen(line))
        {
            fprintf(stderr, "Error: failed to write darshan log mount entry.\n");
            return(-1);
        }
    }

    /* seek ahead to end of exe region, will be zero filled */
    ret = darshan_log_seek(fd, DARSHAN_JOB_RECORD_SIZE);
    if (ret < 0)
    {
        fprintf(stderr, "Error: unable to seek forward in darshan log file.\n");
        return(-1);
    }

    return(0);
}

440
/* darshan_log_gethash()
441
 *
442
 * read the hash of records from the darshan log file
443 444 445
 *
 * returns 0 on success, -1 on failure
 */
446
int darshan_log_gethash(darshan_fd fd, struct darshan_record_ref **hash)
447
{
448 449
    char *hash_buf;
    int hash_buf_sz = fd->rec_map.len;
450
    char *buf_ptr;
451 452 453 454
    darshan_record_id *rec_id_ptr;
    uint32_t *path_len_ptr;
    char *path_ptr;
    struct darshan_record_ref *ref;
455 456
    int ret;

457
    ret = darshan_log_seek(fd, fd->rec_map.off);
458
    if(ret < 0)
459 460
    {
        fprintf(stderr, "Error: unable to seek in darshan log file.\n");
461
        return(-1);
462
    }
463

464 465 466 467
    hash_buf = malloc(hash_buf_sz);
    if(!hash_buf)
        return(-1);

468
    /* read the record hash from the log file */
469
    ret = darshan_log_read(fd, hash_buf, fd->rec_map.len);
470
    if(ret != fd->rec_map.len)
471
    {
472
        fprintf(stderr, "Error: failed to read record hash from darshan log file.\n");
473 474 475
        return(-1);
    }

476
    buf_ptr = hash_buf;
477
    while(buf_ptr < (hash_buf + hash_buf_sz))
478
    {
479
        /* get pointers for each field of this darshan record */
480
        /* NOTE: darshan record hash serialization method: 
481 482
         *          ... darshan_record_id | (uint32_t) path_len | path ...
         */
483 484 485 486 487 488 489 490
        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;
        buf_ptr += *path_len_ptr;

        if(fd->swap_flag)
491
        {
492 493 494
            /* we need to sort out endianness issues before deserializing */
            DARSHAN_BSWAP64(rec_id_ptr);
            DARSHAN_BSWAP32(path_len_ptr);
495 496
        }

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
        HASH_FIND(hlink, *hash, rec_id_ptr, sizeof(darshan_record_id), ref);
        if(!ref)
        {
            ref = malloc(sizeof(*ref));
            if(!ref)
            {
                return(-1);
            }
            ref->rec.name = malloc(*path_len_ptr + 1);
            if(!ref->rec.name)
            {
                free(ref);
                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';
516

517 518 519
            /* add this record to the hash */
            HASH_ADD(hlink, *hash, rec.id, sizeof(darshan_record_id), ref);
        }
520 521
    }

522 523 524
    return(0);
}

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
/* darshan_log_puthash()
 *
 * writes the hash table of records to the darshan log file
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_puthash(darshan_fd fd, struct darshan_record_ref *hash)
{
    size_t hash_buf_sz;
    char *hash_buf;
    char *hash_buf_off;
    struct darshan_record_ref *ref, *tmp;
    uint32_t name_len;
    size_t record_sz;
    int ret;

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

    /* allocate a buffer to store 2 MiB worth of record data */
    /* NOTE: this buffer may be reallocated if estimate is too small */
    hash_buf_sz = 2 * 1024 * 1024;
    hash_buf = malloc(hash_buf_sz);
    if(!hash_buf)
    {
        return(-1);
    }

    /* serialize the record hash into a buffer for writing */
    hash_buf_off = hash_buf;
    HASH_ITER(hlink, hash, ref, tmp)
    {
        name_len = strlen(ref->rec.name);
        record_sz = sizeof(darshan_record_id) + sizeof(uint32_t) + name_len;
        /* make sure there is room in the buffer for this record */
        if((hash_buf_off + record_sz) > (hash_buf + hash_buf_sz))
        {
            char *tmp_buf;
            size_t old_buf_sz;

            /* if no room, reallocate the hash buffer at twice the current size */
            old_buf_sz = hash_buf_off - hash_buf;
            hash_buf_sz *= 2;
            tmp_buf = malloc(hash_buf_sz);
            if(!tmp_buf)
            {
                free(hash_buf);
                return(-1);
            }

            memcpy(tmp_buf, hash_buf, old_buf_sz);
            free(hash_buf);
            hash_buf = tmp_buf;
            hash_buf_off = hash_buf + old_buf_sz;
        }

        /* now serialize the record into the hash buffer.
         * NOTE: darshan record hash serialization method: 
         *          ... darshan_record_id | (uint32_t) path_len | path ...
         */
        *((darshan_record_id *)hash_buf_off) = ref->rec.id;
        hash_buf_off += sizeof(darshan_record_id);
        *((uint32_t *)hash_buf_off) = name_len;
        hash_buf_off += sizeof(uint32_t);
        memcpy(hash_buf_off, ref->rec.name, name_len);
        hash_buf_off += name_len;
    }

    /* collectively write out the record hash to the darshan log */
    hash_buf_sz = hash_buf_off - hash_buf;

    /* write the record hash to file */
    ret = darshan_log_write(fd, hash_buf, hash_buf_sz);
    if(ret != hash_buf_sz)
    {
        fprintf(stderr, "Error: failed to write record hash to darshan log file.\n");
        return(-1);
    }

    free(hash_buf);

    return(0);
}

/* darshan_log_getmod()
 *
 * reads a chunk of data for a corresponding darshan instrumentation module
 * NOTE: mod_buf_sz is the uncompressed size of data we wish to read for the given
 * module. chunks of data can be read sequentially for a given module by
 * repeatedly calling this function with the desired chunk sizes.
 *
 * returns 1 on successful read of data, 0 on end of module region, -1 on failure
 */
int darshan_log_getmod(darshan_fd fd, darshan_module_id mod_id,
    void *mod_buf, int mod_buf_sz)
624
{
625
    int mod_buf_end = fd->mod_map[mod_id].off + fd->mod_map[mod_id].len;
626
    int ret;
627

628 629
    if(!fd->mod_map[mod_id].len || fd->pos == mod_buf_end)
        return(0); /* no (more) data corresponding to this mod_id */
630

631 632 633 634 635 636
    /* only seek to start of module data if current log file position 
     * is not within the given mod_id's range. This allows one to
     * repeatedly call this function and get chunks of a module's
     * data piecemeal.
     */
    if((fd->pos < fd->mod_map[mod_id].off) || (fd->pos > mod_buf_end))
637
    {
638
        ret = darshan_log_seek(fd, fd->mod_map[mod_id].off);
639
        if(ret < 0)
640 641
        {
            fprintf(stderr, "Error: unable to seek in darshan log file.\n");
642
            return(-1);
643 644 645
        }
    }

646 647 648 649 650 651 652 653 654 655
    /* if the requested amount of data violates mapping info, error out */
    if(mod_buf_sz > (mod_buf_end - fd->pos))
    {
        fprintf(stderr, "Error: module data read violates stored mapping information.\n");
        return(-1);
    }

    /* read the module chunk from the log file */
    ret = darshan_log_read(fd, mod_buf, mod_buf_sz);
    if(ret != mod_buf_sz)
656
    {
657
        fprintf(stderr,
658
            "Error: failed to read module %s data from darshan log file.\n",
659
            darshan_module_names[mod_id]);
660 661 662
        return(-1);
    }

663
    return(1);
664 665
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
/* darshan_log_putmod()
 *
 * write a chunk of module data to the darshan log file
 *
 * returns 0 on success, -1 on failure
 */
int darshan_log_putmod(darshan_fd fd, darshan_module_id mod_id,
    void *mod_buf, int mod_buf_sz)
{
    int mod_buf_end = fd->mod_map[mod_id].off + fd->mod_map[mod_id].len;
    int ret;

    if(!fd->mod_map[mod_id].len)
    {
        fprintf(stderr, "Error: attempting to write data for empty module.\n");
        return(-1);
    }

    /* only seek to start of module data if current log file position 
     * is not within the given mod_id's range. This allows one to
     * repeatedly call this function and put chunks of a module's
     * data piecemeal.
     */
    if((fd->pos < fd->mod_map[mod_id].off) || (fd->pos > mod_buf_end))
    {
        ret = darshan_log_seek(fd, fd->mod_map[mod_id].off);
        if(ret < 0)
        {
            fprintf(stderr, "Error: unable to seek in darshan log file.\n");
            return(-1);
        }
    }

    /* if the given data violates stored mapping info, error out */
    if(mod_buf_sz > (mod_buf_end - fd->pos))
    {
        fprintf(stderr, "Error: module data write violates stored mapping information.\n");
        return(-1);
    }

    /* write the module chunk to the log file */
    ret = darshan_log_write(fd, mod_buf, mod_buf_sz);
    if(ret != mod_buf_sz)
    {
        fprintf(stderr,
            "Error: failed to write module %s data to darshan log file.\n",
            darshan_module_names[mod_id]);
        return(-1);
    }

    return(0);
}

719 720 721 722 723 724 725
/* darshan_log_close()
 *
 * close an open darshan file descriptor
 *
 * returns 0 on success, -1 on failure
 */
void darshan_log_close(darshan_fd fd)
726
{
727 728
    if(fd->gzf)
        gzclose(fd->gzf);
729

730
    free(fd);
731 732

    return;
733 734
}

735
/* **************************************************** */
736

737 738 739 740
/* return 0 on successful seek to offset, -1 on failure.
 */
static int darshan_log_seek(darshan_fd fd, off_t offset)
{
741 742
    z_off_t zoff = 0;
    z_off_t zoff_ret = 0;
743 744 745 746

    if(fd->pos == offset)
        return(0);

747
    if(fd->gzf)
748
    {
749 750 751 752 753 754 755 756
        zoff += offset;
        zoff_ret = gzseek(fd->gzf, zoff, SEEK_SET);
        if(zoff_ret == zoff)
        {
            fd->pos = offset;
            return(0);
        }
        return(-1);
757 758 759 760 761
    }

    return(-1);
}

762
/* return amount read on success, 0 on EOF, -1 on failure.
763
 */
764
static int darshan_log_read(darshan_fd fd, void* buf, int len)
765 766 767
{
    int ret;

768
    if(fd->gzf)
769
    {
770
        ret = gzread(fd->gzf, buf, len);
771 772 773 774 775 776 777 778
        if(ret > 0)
            fd->pos += ret;
        return(ret);
    }

    return(-1);
}

779
/* return amount written on success, -1 on failure.
780
 */
781
static int darshan_log_write(darshan_fd fd, void* buf, int len)
782 783 784
{
    int ret;

785
    if(fd->gzf)
786
    {
787
        ret = gzwrite(fd->gzf, buf, len);
788 789 790 791 792
        if(ret > 0)
            fd->pos += ret;
        return(ret);
    }

793
    return(-1);
794
}
795 796 797 798 799 800 801 802 803

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