darshan-logutils.c 30.9 KB
Newer Older
1 2 3 4 5
/*
 *  (C) 2009 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

6 7
#include <stdio.h>
#include <string.h>
8
#include <assert.h>
9
#include <stdlib.h>
10
#include "darshan-logutils.h"
11
#include "darshan-config.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

/* isn't there a clever c way to avoid this? */
char *darshan_names[] = {
    "CP_INDEP_OPENS",
    "CP_COLL_OPENS",               /* count of MPI collective opens */
    "CP_INDEP_READS",              /* count of independent MPI reads */
    "CP_INDEP_WRITES",             /* count of independent MPI writes */
    "CP_COLL_READS",               /* count of collective MPI reads */
    "CP_COLL_WRITES",              /* count of collective MPI writes */
    "CP_SPLIT_READS",              /* count of split collective MPI reads */
    "CP_SPLIT_WRITES",             /* count of split collective MPI writes */
    "CP_NB_READS",                 /* count of nonblocking MPI reads */
    "CP_NB_WRITES",                /* count of nonblocking MPI writes */
    "CP_SYNCS",                    /* count of MPI_File_sync */
    "CP_POSIX_READS",              /* count of posix reads */
    "CP_POSIX_WRITES",             /* count of posix writes */
    "CP_POSIX_OPENS",              /* count of posix opens */
    "CP_POSIX_SEEKS",              /* count of posix seeks */
    "CP_POSIX_STATS",              /* count of posix stat/lstat/fstats */
    "CP_POSIX_MMAPS",              /* count of posix mmaps */
    "CP_POSIX_FREADS",
    "CP_POSIX_FWRITES",
    "CP_POSIX_FOPENS",
    "CP_POSIX_FSEEKS",
    "CP_POSIX_FSYNCS",
    "CP_POSIX_FDSYNCS",
38 39 40
    "CP_INDEP_NC_OPENS",
    "CP_COLL_NC_OPENS",
    "CP_HDF5_OPENS",
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    "CP_COMBINER_NAMED",           /* count of each MPI datatype category */
    "CP_COMBINER_DUP",
    "CP_COMBINER_CONTIGUOUS",
    "CP_COMBINER_VECTOR",
    "CP_COMBINER_HVECTOR_INTEGER",
    "CP_COMBINER_HVECTOR",
    "CP_COMBINER_INDEXED",
    "CP_COMBINER_HINDEXED_INTEGER",
    "CP_COMBINER_HINDEXED",
    "CP_COMBINER_INDEXED_BLOCK",
    "CP_COMBINER_STRUCT_INTEGER",
    "CP_COMBINER_STRUCT",
    "CP_COMBINER_SUBARRAY",
    "CP_COMBINER_DARRAY",
    "CP_COMBINER_F90_REAL",
    "CP_COMBINER_F90_COMPLEX",
    "CP_COMBINER_F90_INTEGER",
    "CP_COMBINER_RESIZED",
    "CP_HINTS",                     /* count of MPI hints used */
    "CP_VIEWS",                     /* count of MPI set view calls */
    "CP_MODE",                      /* mode of file */
    "CP_BYTES_READ",                /* total bytes read */
    "CP_BYTES_WRITTEN",             /* total bytes written */
    "CP_MAX_BYTE_READ",             /* highest offset byte read */
    "CP_MAX_BYTE_WRITTEN",          /* highest offset byte written */
    "CP_CONSEC_READS",              /* count of consecutive reads */
    "CP_CONSEC_WRITES",             /* count of consecutive writes */
    "CP_SEQ_READS",                 /* count of sequential reads */
    "CP_SEQ_WRITES",                /* count of sequential writes */
    "CP_RW_SWITCHES",
    "CP_MEM_NOT_ALIGNED",           /* count of accesses not mem aligned */
    "CP_MEM_ALIGNMENT",             /* mem alignment in bytes */
    "CP_FILE_NOT_ALIGNED",          /* count of accesses not file aligned */
    "CP_FILE_ALIGNMENT",            /* file alignment in bytes */
75 76
    "CP_MAX_READ_TIME_SIZE",
    "CP_MAX_WRITE_TIME_SIZE",
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    "CP_SIZE_READ_0_100",           /* count of posix read size ranges */
    "CP_SIZE_READ_100_1K",
    "CP_SIZE_READ_1K_10K",
    "CP_SIZE_READ_10K_100K",
    "CP_SIZE_READ_100K_1M",
    "CP_SIZE_READ_1M_4M",
    "CP_SIZE_READ_4M_10M",
    "CP_SIZE_READ_10M_100M",
    "CP_SIZE_READ_100M_1G",
    "CP_SIZE_READ_1G_PLUS",
    "CP_SIZE_WRITE_0_100",          /* count of posix write size ranges */
    "CP_SIZE_WRITE_100_1K",
    "CP_SIZE_WRITE_1K_10K",
    "CP_SIZE_WRITE_10K_100K",
    "CP_SIZE_WRITE_100K_1M",
    "CP_SIZE_WRITE_1M_4M",
    "CP_SIZE_WRITE_4M_10M",
    "CP_SIZE_WRITE_10M_100M",
    "CP_SIZE_WRITE_100M_1G",
    "CP_SIZE_WRITE_1G_PLUS",
    "CP_SIZE_READ_AGG_0_100",       /* count of MPI read size ranges */
    "CP_SIZE_READ_AGG_100_1K",
    "CP_SIZE_READ_AGG_1K_10K",
    "CP_SIZE_READ_AGG_10K_100K",
    "CP_SIZE_READ_AGG_100K_1M",
    "CP_SIZE_READ_AGG_1M_4M",
    "CP_SIZE_READ_AGG_4M_10M",
    "CP_SIZE_READ_AGG_10M_100M",
    "CP_SIZE_READ_AGG_100M_1G",
    "CP_SIZE_READ_AGG_1G_PLUS",
    "CP_SIZE_WRITE_AGG_0_100",      /* count of MPI write size ranges */
    "CP_SIZE_WRITE_AGG_100_1K",
    "CP_SIZE_WRITE_AGG_1K_10K",
    "CP_SIZE_WRITE_AGG_10K_100K",
    "CP_SIZE_WRITE_AGG_100K_1M",
    "CP_SIZE_WRITE_AGG_1M_4M",
    "CP_SIZE_WRITE_AGG_4M_10M",
    "CP_SIZE_WRITE_AGG_10M_100M",
    "CP_SIZE_WRITE_AGG_100M_1G",
    "CP_SIZE_WRITE_AGG_1G_PLUS",
    "CP_EXTENT_READ_0_100",          /* count of MPI read extent ranges */
    "CP_EXTENT_READ_100_1K",
    "CP_EXTENT_READ_1K_10K",
    "CP_EXTENT_READ_10K_100K",
    "CP_EXTENT_READ_100K_1M",
    "CP_EXTENT_READ_1M_4M",
    "CP_EXTENT_READ_4M_10M",
    "CP_EXTENT_READ_10M_100M",
    "CP_EXTENT_READ_100M_1G",
    "CP_EXTENT_READ_1G_PLUS",
    "CP_EXTENT_WRITE_0_100",         /* count of MPI write extent ranges */
    "CP_EXTENT_WRITE_100_1K",
    "CP_EXTENT_WRITE_1K_10K",
    "CP_EXTENT_WRITE_10K_100K",
    "CP_EXTENT_WRITE_100K_1M",
    "CP_EXTENT_WRITE_1M_4M",
    "CP_EXTENT_WRITE_4M_10M",
    "CP_EXTENT_WRITE_10M_100M",
    "CP_EXTENT_WRITE_100M_1G",
    "CP_EXTENT_WRITE_1G_PLUS",
    "CP_STRIDE1_STRIDE",             /* the four most frequently appearing strides */
    "CP_STRIDE2_STRIDE",
    "CP_STRIDE3_STRIDE",
    "CP_STRIDE4_STRIDE",
    "CP_STRIDE1_COUNT",              /* count of each of the most frequent strides */
    "CP_STRIDE2_COUNT",
    "CP_STRIDE3_COUNT",
    "CP_STRIDE4_COUNT",
    "CP_ACCESS1_ACCESS",
    "CP_ACCESS2_ACCESS",
    "CP_ACCESS3_ACCESS",
    "CP_ACCESS4_ACCESS",
    "CP_ACCESS1_COUNT",
    "CP_ACCESS2_COUNT",
    "CP_ACCESS3_COUNT",
    "CP_ACCESS4_COUNT",
153
    "CP_DEVICE",
154
    "CP_SIZE_AT_OPEN",
155 156 157 158
    "CP_FASTEST_RANK",
    "CP_FASTEST_RANK_BYTES",
    "CP_SLOWEST_RANK",
    "CP_SLOWEST_RANK_BYTES",
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    "CP_NUM_INDICES"
};

/* isn't there a clever c way to avoid this? */
char *darshan_f_names[] = {
    "CP_F_OPEN_TIMESTAMP",        /* timestamp of first open */
    "CP_F_READ_START_TIMESTAMP",  /* timestamp of first read */
    "CP_F_WRITE_START_TIMESTAMP", /* timestamp of first write */
    "CP_F_CLOSE_TIMESTAMP",       /* timestamp of last close */
    "CP_F_READ_END_TIMESTAMP",    /* timestamp of last read */
    "CP_F_WRITE_END_TIMESTAMP",   /* timestamp of last write */
    "CP_F_POSIX_READ_TIME",       /* cumulative posix read time */
    "CP_F_POSIX_WRITE_TIME",      /* cumulative posix write time */
    "CP_F_POSIX_META_TIME",       /* cumulative posix meta time */
    "CP_F_MPI_META_TIME",         /* cumulative mpi-io metadata time */
    "CP_F_MPI_READ_TIME",         /* cumulative mpi-io read time */
    "CP_F_MPI_WRITE_TIME",        /* cumulative mpi-io write time */
177 178
    "CP_F_MAX_READ_TIME",
    "CP_F_MAX_WRITE_TIME",
179 180 181 182
    "CP_F_FASTEST_RANK_TIME",
    "CP_F_SLOWEST_RANK_TIME",
    "CP_F_VARIANCE_RANK_TIME",
    "CP_F_VARIANCE_RANK_BYTES",
183 184
    "CP_F_NC_READ_TIME",	/* cumulative time in pnetcdf reads */
    "CP_F_NC_WRITE_TIME",	/* cumulative time in pnetcdf writes */
185
    "CP_F_NUM_INDICES"
186 187
};

188 189 190 191 192 193 194
/* function pointers so that we can switch functions depending on what file
 * version is detected
 */
int (*getjob_internal)(darshan_fd file, struct darshan_job *job);
int (*getfile_internal)(darshan_fd fd, 
    struct darshan_job *job, 
    struct darshan_file *file);
195
#define JOB_SIZE_124 28
196

197
/* internal routines for parsing different file versions */
198 199 200 201 202 203 204 205 206 207
static int getjob_internal_200(darshan_fd file, struct darshan_job *job);
static int getfile_internal_200(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file);
static int getjob_internal_124(darshan_fd file, struct darshan_job *job);
static int getfile_internal_124(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file);
static int getfile_internal_122(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file);
static int getfile_internal_121(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file);
208 209
static int getfile_internal_1x(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file, int n_counters, int n_fcounters);
210
static void shift_missing_1_24(struct darshan_file* file);
211 212
static void shift_missing_1_22(struct darshan_file* file);
static void shift_missing_1_21(struct darshan_file* file);
213

214
/* a rather crude API for accessing raw binary darshan files */
215
darshan_fd darshan_log_open(const char *name)
216
{
217 218 219 220 221 222 223 224 225 226 227 228 229
    darshan_fd tmp_fd = malloc(sizeof(*tmp_fd));
    if(!tmp_fd)
        return(NULL);

    memset(tmp_fd, 0, sizeof(*tmp_fd));

    tmp_fd->gzf = gzopen(name, "r");
    if(!tmp_fd->gzf)
    {
        free(tmp_fd);
        tmp_fd = NULL;
    }
    return tmp_fd;
230 231
}

232
/* darshan_log_getjob()
233 234 235
 *
 * returns 0 on success, -1 on failure
 */
236
int darshan_log_getjob(darshan_fd file, struct darshan_job *job)
237 238
{
    int ret;
239

240
    gzseek(file->gzf, 0, SEEK_SET);
241

242 243 244 245 246
    /* read version number first so we know how to digest the rest of the
     * file
     */
    ret = gzread(file->gzf, file->version, 10);
    if(ret < 10)
247
    {
248
        fprintf(stderr, "Error: invalid log file (failed to read version).\n");
249 250 251
        return(-1);
    }

252 253
    if(  strcmp(file->version, "2.00") == 0 || 
		    strcmp(file->version, "2.01") == 0)
254
    {
255 256
        getjob_internal = getjob_internal_200;
        getfile_internal = getfile_internal_200;
257
        file->job_struct_size = sizeof(*job);
258
    }
259
    else if(strcmp(file->version, "1.24") == 0)
260
    {
261 262
        getjob_internal = getjob_internal_124;
        getfile_internal = getfile_internal_124;
263
        file->job_struct_size = JOB_SIZE_124;
264
    }
265
    else if(strcmp(file->version, "1.23") == 0)
266
    {
267 268 269
        /* same as 1.24, except that mnt points may be incorrect */
        getjob_internal = getjob_internal_124;
        getfile_internal = getfile_internal_124;
270
        file->job_struct_size = JOB_SIZE_124;
271
    }
272
    else if(strcmp(file->version, "1.22") == 0)
273
    {
274 275
        getjob_internal = getjob_internal_124;
        getfile_internal = getfile_internal_122;
276
        file->job_struct_size = JOB_SIZE_124;
277
    }
278
    else if(strcmp(file->version, "1.21") == 0)
279
    {
280
        getjob_internal = getjob_internal_124;
281
        getfile_internal = getfile_internal_121;
282
        file->job_struct_size = JOB_SIZE_124;
283 284
    }
    else
285
    {
286 287 288
        fprintf(stderr, "Error: incompatible darshan file.\n");
        fprintf(stderr, "Error: expected version %s, but got %s\n", 
                CP_VERSION, file->version);
289
        return(-1);
290 291
    }

292 293 294
    ret = getjob_internal(file, job);
    return(ret);
}
295

296 297 298 299 300 301 302
/* darshan_log_getfile()
 *
 * return 1 if file record found, 0 on eof, and -1 on error
 */
int darshan_log_getfile(darshan_fd fd, struct darshan_job *job, struct darshan_file *file)
{
    int ret;
303

304 305 306
    ret = getfile_internal(fd, job, file);

    return(ret);
307 308
}

309 310 311 312 313 314
/* darshan_log_getmounts()
 * 
 * retrieves mount table information from the log.  Note that devs, mnt_pts,
 * and fs_types are arrays that will be allocated by the function and must
 * be freed by the caller.  count will indicate the size of the arrays
 */
315
int darshan_log_getmounts(darshan_fd fd, int64_t** devs, char*** mnt_pts, char***
316 317 318 319 320 321 322
    fs_types, int* count, int *flag)
{
    int ret;
    char* pos;
    int array_index = 0;
    char buf[CP_EXE_LEN+1];

323
    gzseek(fd->gzf, fd->job_struct_size, SEEK_SET);
324

325
    ret = gzread(fd->gzf, buf, (CP_EXE_LEN + 1));
326 327 328 329 330
    if (ret < (CP_EXE_LEN + 1))
    {
        perror("gzread");
        return(-1);
    }
331
    if (gzeof(fd->gzf))
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
        *flag = 1;
    else
        *flag = 0;

    /* count entries */
    *count = 0;
    pos = buf;
    while((pos = strchr(pos, '\n')) != NULL)
    {
        pos++;
        (*count)++;
    }

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

    /* allocate output arrays */
352
    *devs = malloc((*count)*sizeof(int64_t));
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    assert(*devs);
    *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)
     */
    while((pos = strrchr(buf, '\n')) != NULL)
    {
        /* overestimate string lengths */
        (*mnt_pts)[array_index] = malloc(CP_EXE_LEN);
        assert((*mnt_pts)[array_index]);
        (*fs_types)[array_index] = malloc(CP_EXE_LEN);
        assert((*fs_types)[array_index]);
        
370 371
#if SIZEOF_LONG_INT == 4
        ret = sscanf(++pos, "%lld\t%s\t%s", &(*devs)[array_index],
372
            (*fs_types)[array_index], (*mnt_pts)[array_index]);
373 374 375 376 377 378 379
#elif SIZEOF_LONG_INT == 8
        ret = sscanf(++pos, "%ld\t%s\t%s", &(*devs)[array_index],
            (*fs_types)[array_index], (*mnt_pts)[array_index]);
#else
#  error Unexpected sizeof(long int)
#endif

380 381 382 383 384 385 386 387 388 389 390 391 392 393
        if(ret != 3)
        {
            fprintf(stderr, "Error: poorly formatted mount table in log file.\n");
            return(-1);
        }
        pos--;
        *pos = '\0';
        array_index++;
    }
   
    return (0);
}


394
int darshan_log_getexe(darshan_fd fd, char *buf, int *flag)
395 396
{
    int ret;
397
    char* newline;
398

399
    gzseek(fd->gzf, fd->job_struct_size, SEEK_SET);
400

401
    ret = gzread(fd->gzf, buf, (CP_EXE_LEN + 1));
402 403 404 405 406
    if (ret < (CP_EXE_LEN + 1))
    {
        perror("gzread");
        return(-1);
    }
407
    if (gzeof(fd->gzf))
408 409 410 411
        *flag = 1;
    else
        *flag = 0;

412 413 414 415 416 417 418 419
    /* this call is only supposed to return the exe string, but starting in
     * log format 1.23 there could be a table of mount entry information
     * after the exe.  Look for newline character and truncate there.
     */
    newline = strchr(buf, '\n');
    if(newline)
        *newline = '\0';

420 421 422
    return (0);
}

423
void darshan_log_close(darshan_fd file)
424
{
425 426
    gzclose(file->gzf);
    free(file);
427 428
}

429 430 431 432 433 434
/* darshan_log_print_version_warnings()
 *
 * Print summary of any problems with the detected log format
 */
void darshan_log_print_version_warnings(struct darshan_job *job)
{
435 436 437 438 439
    if(strcmp(job->version_string, "2.01") == 0) 
    {
        /* curent version */
        return;
    }
440 441
    if(strcmp(job->version_string, "2.00") == 0)
    {
442 443 444
        printf("#WARNING: version 2.00 log format does not support the following parameters:\n");
	printf("#  CP_F_NC_READ_TIME\n");
	printf("#  CP_F_NC_WRITE_TIME\n");
445 446 447
        return;
    }
 
448
    if(strcmp(job->version_string, "1.24") == 0)
449
    {
450 451 452 453 454 455 456 457 458 459
        printf("# WARNING: version 1.24 log format does not support the following parameters:\n");
        printf("#   CP_FASTEST_RANK\n");
        printf("#   CP_FASTEST_RANK_BYTES\n");
        printf("#   CP_SLOWEST_RANK\n");
        printf("#   CP_SLOWEST_RANK_BYTES\n");
        printf("#   CP_F_FASTEST_RANK_TIME\n");
        printf("#   CP_F_SLOWEST_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_BYTES\n");
        printf("# It also does not store the job id in the log file.\n");
460 461
        return;
    }
462
    
463 464
    if(strcmp(job->version_string, "1.23") == 0)
    {
465 466 467 468 469 470 471 472 473 474
        printf("# WARNING: version 1.23 log format does not support the following parameters:\n");
        printf("#   CP_FASTEST_RANK\n");
        printf("#   CP_FASTEST_RANK_BYTES\n");
        printf("#   CP_SLOWEST_RANK\n");
        printf("#   CP_SLOWEST_RANK_BYTES\n");
        printf("#   CP_F_FASTEST_RANK_TIME\n");
        printf("#   CP_F_SLOWEST_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_BYTES\n");
        printf("# It also may have incorrect mount point mappings for files with rank > 0\n");
475
        printf("# It also does not store the job id in the log file.\n");
476
        return;
477 478
    }

479 480 481 482
    if(strcmp(job->version_string, "1.22") == 0)
    {
        printf("# WARNING: version 1.22 log format does not support the following parameters:\n");
        printf("#   CP_DEVICE\n");
483
        printf("#   CP_SIZE_AT_OPEN\n");
484 485 486 487 488 489 490 491
        printf("#   CP_FASTEST_RANK\n");
        printf("#   CP_FASTEST_RANK_BYTES\n");
        printf("#   CP_SLOWEST_RANK\n");
        printf("#   CP_SLOWEST_RANK_BYTES\n");
        printf("#   CP_F_FASTEST_RANK_TIME\n");
        printf("#   CP_F_SLOWEST_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_BYTES\n");
492
        printf("# It does not record mounted file systems, mount points, or fs types.\n");
493 494
        printf("# It attributes syncs to cumulative metadata time, rather than cumulative write time.\n");
        printf("# It also does not store the job id in the log file.\n");
495 496
        return;
    }
497 498 499 500 501 502 503 504 505

    if(strcmp(job->version_string, "1.21") == 0)
    {
        printf("# WARNING: version 1.21 log format does not support the following parameters:\n");
        printf("#   CP_INDEP_NC_OPENS\n");
        printf("#   CP_COLL_NC_OPENS\n");
        printf("#   CP_HDF5_OPENS\n");
        printf("#   CP_MAX_READ_TIME_SIZE\n");
        printf("#   CP_MAX_WRITE_TIME_SIZE\n");
506
        printf("#   CP_DEVICE\n");
507
        printf("#   CP_SIZE_AT_OPEN\n");
508 509
        printf("#   CP_F_MAX_READ_TIME\n");
        printf("#   CP_F_MAX_WRITE_TIME\n");
510 511 512 513 514 515 516 517
        printf("#   CP_FASTEST_RANK\n");
        printf("#   CP_FASTEST_RANK_BYTES\n");
        printf("#   CP_SLOWEST_RANK\n");
        printf("#   CP_SLOWEST_RANK_BYTES\n");
        printf("#   CP_F_FASTEST_RANK_TIME\n");
        printf("#   CP_F_SLOWEST_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_TIME\n");
        printf("#   CP_F_VARIANCE_RANK_BYTES\n");
518 519
        printf("# It does not record mounted file systems, mount points, or fs types.\n");
        printf("# It attributes syncs to cumulative metadata time, rather than cumulative write time.\n");
520
        printf("#\n");
521
        printf("# It also does not store the job id in the file.\n");
522 523 524 525 526 527 528 529
        return;
    }

    fprintf(stderr, "Error: version %s not supported by parser.\n",
        job->version_string);
    return;
}

530 531 532 533
/* shift_missing_1_21()
 *
 * translates indices to account for counters that weren't present in log
 * format 1.21
534
 */
535
/*******************************
536
 * version 1.21 to 2.00 differences 
537 538 539 540 541 542 543 544
 * - added:
 *   - CP_INDEP_NC_OPENS
 *   - CP_COLL_NC_OPENS
 *   - CP_HDF5_OPENS
 *   - CP_MAX_READ_TIME_SIZE
 *   - CP_MAX_WRITE_TIME_SIZE
 *   - CP_DEVICE
 *   - CP_SIZE_AT_OPEN
545 546 547 548
 *   - CP_FASTEST_RANK
 *   - CP_FASTEST_RANK_BYTES
 *   - CP_SLOWEST_RANK
 *   - CP_SLOWEST_RANK_BYTES
549 550
 *   - CP_F_MAX_READ_TIME
 *   - CP_F_MAX_WRITE_TIME
551 552 553 554
 *   - CP_F_FASTEST_RANK_TIME
 *   - CP_F_SLOWEST_RANK_TIME
 *   - CP_F_VARIANCE_RANK_TIME
 *   - CP_F_VARIANCE_RANK_BYTES
555
 * - changed params:
556 557 558
 *   - CP_FILE_RECORD_SIZE: 1184 to 1328
 *   - CP_NUM_INDICES: 133 to 144
 *   - CP_F_NUM_INDICES: 12 to 18
559
 */
560 561 562 563 564 565 566 567 568
static void shift_missing_1_21(struct darshan_file* file)
{
    int c_index = 0;
    int missing_counters[] = {
        CP_INDEP_NC_OPENS,
        CP_COLL_NC_OPENS,
        CP_HDF5_OPENS,
        CP_MAX_READ_TIME_SIZE,
        CP_MAX_WRITE_TIME_SIZE,
569
        CP_DEVICE,
570
        CP_SIZE_AT_OPEN,
571 572 573 574
        CP_FASTEST_RANK,
        CP_FASTEST_RANK_BYTES,
        CP_SLOWEST_RANK,
        CP_SLOWEST_RANK_BYTES,
575 576 577 578
        -1};
    int missing_f_counters[] = {
        CP_F_MAX_READ_TIME,
        CP_F_MAX_WRITE_TIME,
579 580 581 582
        CP_F_FASTEST_RANK_TIME,
        CP_F_SLOWEST_RANK_TIME,
        CP_F_VARIANCE_RANK_TIME,
        CP_F_VARIANCE_RANK_BYTES,
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
        -1};

    c_index = 0;
    while(missing_counters[c_index] != -1)
    {
        int missing_counter = missing_counters[c_index];
        c_index++;
        if(missing_counter < (CP_NUM_INDICES - 1))
        {
            /* shift down */
            memmove(&file->counters[missing_counter+1],
                &file->counters[missing_counter],
                (CP_NUM_INDICES-missing_counter-1)*sizeof(int64_t));
        }
        /* zero out missing counter */
        file->counters[missing_counter] = 0;
    }

    c_index = 0;
    while(missing_f_counters[c_index] != -1)
    {
        int missing_counter = missing_f_counters[c_index];
        c_index++;
        if(missing_counter < (CP_F_NUM_INDICES - 1))
        {
            /* shift down */
            memmove(&file->fcounters[missing_counter+1],
                &file->fcounters[missing_counter],
                (CP_F_NUM_INDICES-missing_counter-1)*sizeof(double));
        }
        /* zero out missing counter */
        file->fcounters[missing_counter] = 0;
    }

    return;
}
619 620 621 622 623 624

/* shift_missing_1_22()
 *
 * translates indices to account for counters that weren't present in log
 * format 1.22
 */
625
/*******************************
626
 * version 1.22 to 2.00 differences
627 628 629 630
 *
 * - added:
 *   - CP_DEVICE
 *   - CP_SIZE_AT_OPEN
631 632 633 634 635 636 637 638
 *   - CP_FASTEST_RANK
 *   - CP_FASTEST_RANK_BYTES
 *   - CP_SLOWEST_RANK
 *   - CP_SLOWEST_RANK_BYTES
 *   - CP_F_FASTEST_RANK_TIME
 *   - CP_F_SLOWEST_RANK_TIME
 *   - CP_F_VARIANCE_RANK_TIME
 *   - CP_F_VARIANCE_RANK_BYTES
639
 * - changed params:
640 641 642
 *   - CP_FILE_RECORD_SIZE: 1240 to 1328
 *   - CP_NUM_INDICES: 138 to 144
 *   - CP_F_NUM_INDICES: 14 to 18
643
 */
644 645 646 647 648
static void shift_missing_1_22(struct darshan_file* file)
{
    int c_index = 0;
    int missing_counters[] = {
        CP_DEVICE,
649
        CP_SIZE_AT_OPEN,
650 651 652 653 654 655 656 657 658 659
        CP_FASTEST_RANK,
        CP_FASTEST_RANK_BYTES,
        CP_SLOWEST_RANK,
        CP_SLOWEST_RANK_BYTES,
        -1};
    int missing_f_counters[] = {
        CP_F_FASTEST_RANK_TIME,
        CP_F_SLOWEST_RANK_TIME,
        CP_F_VARIANCE_RANK_TIME,
        CP_F_VARIANCE_RANK_BYTES,
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
        -1};

    c_index = 0;
    while(missing_counters[c_index] != -1)
    {
        int missing_counter = missing_counters[c_index];
        c_index++;
        if(missing_counter < (CP_NUM_INDICES - 1))
        {
            /* shift down */
            memmove(&file->counters[missing_counter+1],
                &file->counters[missing_counter],
                (CP_NUM_INDICES-missing_counter-1)*sizeof(int64_t));
        }
        /* zero out missing counter */
        file->counters[missing_counter] = 0;
    }

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    c_index = 0;
    while(missing_f_counters[c_index] != -1)
    {
        int missing_counter = missing_f_counters[c_index];
        c_index++;
        if(missing_counter < (CP_F_NUM_INDICES - 1))
        {
            /* shift down */
            memmove(&file->fcounters[missing_counter+1],
                &file->fcounters[missing_counter],
                (CP_F_NUM_INDICES-missing_counter-1)*sizeof(double));
        }
        /* zero out missing counter */
        file->fcounters[missing_counter] = 0;
    }

694 695
    return;
}
696

697 698 699 700 701 702 703 704 705
/* shift_missing_1_24()
 *
 * translates indices to account for counters that weren't present in log
 * format 1.24
 */
/*******************************
 * version 1.24 to 2.00 differences
 *
 * - added:
706 707 708 709
 *   - CP_FASTEST_RANK
 *   - CP_FASTEST_RANK_BYTES
 *   - CP_SLOWEST_RANK
 *   - CP_SLOWEST_RANK_BYTES
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
 *   - CP_F_FASTEST_RANK_TIME
 *   - CP_F_SLOWEST_RANK_TIME
 *   - CP_F_VARIANCE_RANK_TIME
 *   - CP_F_VARIANCE_RANK_BYTES
 * - changed params:
 *   - CP_FILE_RECORD_SIZE: ? to 1328
 *   - CP_NUM_INDICES: 140 to 144
 *   - CP_F_NUM_INDICES: 14 to 18
 */
static void shift_missing_1_24(struct darshan_file* file)
{
    int c_index = 0;
    int missing_counters[] = {
        CP_FASTEST_RANK,
        CP_FASTEST_RANK_BYTES,
        CP_SLOWEST_RANK,
        CP_SLOWEST_RANK_BYTES,
        -1};
    int missing_f_counters[] = {
        CP_F_FASTEST_RANK_TIME,
        CP_F_SLOWEST_RANK_TIME,
        CP_F_VARIANCE_RANK_TIME,
        CP_F_VARIANCE_RANK_BYTES,
        -1};

    c_index = 0;
    while(missing_counters[c_index] != -1)
    {
        int missing_counter = missing_counters[c_index];
        c_index++;
        if(missing_counter < (CP_NUM_INDICES - 1))
        {
            /* shift down */
            memmove(&file->counters[missing_counter+1],
                &file->counters[missing_counter],
                (CP_NUM_INDICES-missing_counter-1)*sizeof(int64_t));
        }
        /* zero out missing counter */
        file->counters[missing_counter] = 0;
    }

    c_index = 0;
    while(missing_f_counters[c_index] != -1)
    {
        int missing_counter = missing_f_counters[c_index];
        c_index++;
        if(missing_counter < (CP_F_NUM_INDICES - 1))
        {
            /* shift down */
            memmove(&file->fcounters[missing_counter+1],
                &file->fcounters[missing_counter],
                (CP_F_NUM_INDICES-missing_counter-1)*sizeof(double));
        }
        /* zero out missing counter */
        file->fcounters[missing_counter] = 0;
    }

    return;
}


771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static int getjob_internal_200(darshan_fd file, struct darshan_job *job)
{
    int ret;

    gzseek(file->gzf, 0, SEEK_SET);

    ret = gzread(file->gzf, job, sizeof(*job));
    if (ret < sizeof(*job))
    {
        if(gzeof(file->gzf))
        {
            fprintf(stderr, "Error: invalid log file (too short).\n");
        }
        perror("darshan_job_init");
        return(-1);
    }

    if(job->magic_nr == CP_MAGIC_NR)
    {
        /* no byte swapping needed, this file is in host format already */
        file->swap_flag = 0;
        return(0);
    }

    /* try byte swapping */
    DARSHAN_BSWAP64(&job->magic_nr);
    if(job->magic_nr == CP_MAGIC_NR)
    {
        file->swap_flag = 1;
        DARSHAN_BSWAP64(&job->uid);
        DARSHAN_BSWAP64(&job->start_time);
        DARSHAN_BSWAP64(&job->end_time);
        DARSHAN_BSWAP64(&job->nprocs);
804
        DARSHAN_BSWAP64(&job->jobid);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 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
        return(0);
    }

    /* otherwise this file is just broken */
    fprintf(stderr, "Error: bad magic number in darshan file.\n");
    return(-1);
}

static int getfile_internal_200(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file)
{
    int ret;
    const char* err_string;
    int i;
    
    if(gztell(fd->gzf) < CP_JOB_RECORD_SIZE)
        gzseek(fd->gzf, CP_JOB_RECORD_SIZE, SEEK_SET);

    /* reset file record, so that diff compares against a zero'd out record
     * if file is missing
     */
    memset(file, 0, sizeof(&file));

    ret = gzread(fd->gzf, file, sizeof(*file));
    if(ret == sizeof(*file))
    {
        /* got exactly one, correct size record */
        if(fd->swap_flag)
        {
            /* swap bytes if necessary */
            DARSHAN_BSWAP64(&file->hash);
            DARSHAN_BSWAP64(&file->rank);
            for(i=0; i<CP_NUM_INDICES; i++)
                DARSHAN_BSWAP64(&file->counters[i]);
            for(i=0; i<CP_F_NUM_INDICES; i++)
                DARSHAN_BSWAP64(&file->fcounters[i]);
        }
        return(1);
    }


    if(ret > 0)
    {
        /* got a short read */
        fprintf(stderr, "Error: invalid file record (too small)\n");
        return(-1);
    }

    if(ret == 0 && gzeof(fd->gzf))
    {
        /* hit end of file */
        return(0);
    }

    /* all other errors */
    err_string = gzerror(fd->gzf, &ret);
    fprintf(stderr, "Error: %s\n", err_string);
    return(-1);
}

865 866 867
/* If we see version 1.24, assume that it is stored in big endian 32 bit
 * format.  Convert up to current format.
 */
868 869
static int getjob_internal_124(darshan_fd fd, struct darshan_job *job)
{
870 871 872 873 874 875 876
    char* buffer;
    int ret;
    uint32_t uid;
    int32_t start_time;
    int32_t end_time;
    int32_t nprocs;

877 878 879 880 881 882
#ifdef WORDS_BIGENDIAN
    fd->swap_flag = 0;
#else
    fd->swap_flag = 1;
#endif

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    memset(job, 0, sizeof(*job));

    buffer = (char*)malloc(JOB_SIZE_124);
    if(!buffer)
    {
        return(-1);
    }

    gzseek(fd->gzf, 0, SEEK_SET);

    ret = gzread(fd->gzf, buffer, JOB_SIZE_124);
    if (ret < JOB_SIZE_124)
    {
        fprintf(stderr, "Error: invalid log file (could not read file record).\n");
        free(buffer);
        return(-1);
    }

    /* pull job header information out of specific bytes in case struct
     * padding is off
     */
    strncpy(job->version_string, buffer, 8);
    uid = *((uint32_t*)&buffer[12]);
    start_time = *((int32_t*)&buffer[16]);
    end_time = *((int32_t*)&buffer[20]);
    nprocs = *((int32_t*)&buffer[24]);

    free(buffer);

912 913 914 915 916 917 918 919
    if(fd->swap_flag)
    {
        /* byte swap */
        DARSHAN_BSWAP32(&uid);
        DARSHAN_BSWAP32(&start_time);
        DARSHAN_BSWAP32(&end_time);
        DARSHAN_BSWAP32(&nprocs);
    }
920 921 922 923 924

    job->uid += uid;
    job->start_time += start_time;
    job->end_time += end_time;
    job->nprocs += nprocs;
925
    job->jobid = 0; /* old log versions did not have this field */
926 927 928 929 930
    
    /* set magic number */
    job->magic_nr = CP_MAGIC_NR;

    return(0);
931 932 933 934
}

static int getfile_internal_124(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file)
935
{
936 937
    int ret;

938
    ret = getfile_internal_1x(fd, job, file, 140, 14);
939 940 941 942 943 944
    if(ret <= 0)
        return(ret);

    shift_missing_1_24(file);

    return(1);
945 946 947 948 949 950 951
}

static int getfile_internal_122(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file)
{
    int ret;

952 953
    ret = getfile_internal_1x(fd, job, file, 138, 14);
    if(ret <= 0)
954 955 956 957
        return(ret);

    shift_missing_1_22(file);

958
    return(1);
959 960 961 962 963 964 965
}

static int getfile_internal_121(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file)
{
    int ret;

966 967
    ret = getfile_internal_1x(fd, job, file, 133, 12);
    if(ret <= 0)
968 969 970 971
        return(ret);

    shift_missing_1_21(file);

972
    return(1);
973 974 975 976
}

static int getfile_internal_1x(darshan_fd fd, struct darshan_job *job, 
    struct darshan_file *file, int n_counters, int n_fcounters)
977
{
978 979 980 981 982 983 984 985 986
    char* buffer;
    int ret;
    const char* err_string;
    int i;
    uint64_t hash;
    int32_t rank;
    int64_t* counters;
    double* fcounters;
    char* name_suffix;
987
    int FILE_SIZE_1x = (32 + n_counters*8 + n_fcounters*8);
988

989 990 991 992 993 994 995 996 997
    memset(file, 0, sizeof(*file));

    /* set file pointer if this is the first file record; otherwise pick up
     * where we left off last time
     */
    if(gztell(fd->gzf) < CP_JOB_RECORD_SIZE)
        gzseek(fd->gzf, CP_JOB_RECORD_SIZE, SEEK_SET);

    /* space for file struct, int64 array, and double array */
998
    buffer = (char*)malloc(FILE_SIZE_1x);
999 1000 1001 1002 1003
    if(!buffer)
    {
        return(-1);
    }

1004
    ret = gzread(fd->gzf, buffer, FILE_SIZE_1x);
1005

1006
    if(ret > 0 && ret < FILE_SIZE_1x)
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    {
        /* got a short read */
        fprintf(stderr, "Error: invalid file record (too small)\n");
        free(buffer);
        return(-1);
    }
    else if(ret == 0 && gzeof(fd->gzf))
    {
        /* hit end of file */
        free(buffer);
        return(0);
    }
    else if(ret <= 0)
    {
        /* all other errors */
        err_string = gzerror(fd->gzf, &ret);
        fprintf(stderr, "Error: %s\n", err_string);
        free(buffer);
        return(-1);
    }

    /* got exactly one, correct size record */
    hash = *((int64_t*)&buffer[0]);
    rank = *((int32_t*)&buffer[8]);
    counters = ((int64_t*)&buffer[16]);
1032 1033
    fcounters = ((double*)&buffer[16 + n_counters*8]);
    name_suffix = &buffer[16 + n_counters*8 + n_fcounters*8];
1034 1035 1036 1037 1038 1039 1040


    if(fd->swap_flag)
    {
        /* swap bytes if necessary */
        DARSHAN_BSWAP64(&hash);
        DARSHAN_BSWAP32(&rank);
1041
        for(i=0; i<n_counters; i++)
1042
            DARSHAN_BSWAP64(&counters[i]);
1043
        for(i=0; i<n_fcounters; i++)
1044 1045 1046 1047 1048 1049
            DARSHAN_BSWAP64(&fcounters[i]);
    }

    /* assign into new format */
    file->hash = hash;
    file->rank += rank;
1050 1051
    memcpy(file->counters, counters, n_counters*8);
    memcpy(file->fcounters, fcounters, n_fcounters*8);
1052 1053 1054 1055
    memcpy(file->name_suffix, name_suffix, 12);

    free(buffer);
    return(1);
1056 1057 1058 1059
}