darshan-stdio-logutils.c 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#define _GNU_SOURCE
#include "darshan-util-config.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "darshan-logutils.h"

/* integer counter name strings for the STDIO module */
#define X(a) #a,
char *stdio_counter_names[] = {
    STDIO_COUNTERS
};

/* floating point counter name strings for the STDIO module */
char *stdio_f_counter_names[] = {
    STDIO_F_COUNTERS
};
#undef X

34 35
#define DARSHAN_STDIO_FILE_SIZE_1 240

36
/* prototypes for each of the STDIO module's logutil functions */
37
static int darshan_log_get_stdio_record(darshan_fd fd, void** stdio_buf_p);
38
static int darshan_log_put_stdio_record(darshan_fd fd, void* stdio_buf);
39
static void darshan_log_print_stdio_record(void *file_rec,
40
    char *file_name, char *mnt_pt, char *fs_type);
41
static void darshan_log_print_stdio_description(int ver);
42 43
static void darshan_log_print_stdio_record_diff(void *file_rec1, char *file_name1,
    void *file_rec2, char *file_name2);
44
static void darshan_log_agg_stdio_records(void *rec, void *agg_rec, int init_flag);
45 46 47 48 49 50 51 52 53 54 55

/* structure storing each function needed for implementing the darshan
 * logutil interface. these functions are used for reading, writing, and
 * printing module data in a consistent manner.
 */
struct darshan_mod_logutil_funcs stdio_logutils =
{
    .log_get_record = &darshan_log_get_stdio_record,
    .log_put_record = &darshan_log_put_stdio_record,
    .log_print_record = &darshan_log_print_stdio_record,
    .log_print_description = &darshan_log_print_stdio_description,
56 57
    .log_print_diff = &darshan_log_print_stdio_record_diff,
    .log_agg_records = &darshan_log_agg_stdio_records
58 59 60
};

/* retrieve a STDIO record from log file descriptor 'fd', storing the
61 62
 * data in the buffer address pointed to by 'stdio_buf_p'. Return 1 on
 * successful record read, 0 on no more data, and -1 on error.
63
 */
64
static int darshan_log_get_stdio_record(darshan_fd fd, void** stdio_buf_p)
65
{
66
    struct darshan_stdio_file *file = *((struct darshan_stdio_file **)stdio_buf_p);
67
    int rec_len;
68 69 70
    int i;
    int ret;

71 72 73 74 75 76 77 78 79 80
    if(fd->mod_map[DARSHAN_STDIO_MOD].len == 0)
        return(0);

    if(*stdio_buf_p == NULL)
    {
        file = malloc(sizeof(*file));
        if(!file)
            return(-1);
    }

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
    if(fd->mod_ver[DARSHAN_STDIO_MOD] == DARSHAN_STDIO_VER)
    {
        /* log format is in current version, so we don't need to do any
         * translation of counters while reading
         */
        rec_len = sizeof(struct darshan_stdio_file);
        ret = darshan_log_get_mod(fd, DARSHAN_STDIO_MOD, file, rec_len);
    }
    else
    {
        char scratch[1024] = {0};
        char *src_p, *dest_p;
        int len;

        if(fd->mod_ver[DARSHAN_STDIO_MOD] == 1)
        {
            rec_len = DARSHAN_STDIO_FILE_SIZE_1;
            ret = darshan_log_get_mod(fd, DARSHAN_STDIO_MOD, scratch, rec_len);
            if(ret != rec_len)
                goto exit;

            /* upconvert version 1 to version 2 in-place */
            dest_p = scratch + sizeof(struct darshan_base_record) +
                (2 * sizeof(int64_t));
            src_p = dest_p - sizeof(int64_t);
            len = rec_len - (src_p - scratch);
            memmove(dest_p, src_p, len);
            /* set FDOPENS to -1 */
            *((int64_t *)src_p) = -1;
        }

        memcpy(file, scratch, sizeof(struct darshan_posix_file));
    }
114

115
exit:
116 117
    if(*stdio_buf_p == NULL)
    {
118
        if(ret == rec_len)
119 120 121 122 123
            *stdio_buf_p = file;
        else
            free(file);
    }

124 125
    if(ret < 0)
        return(-1);
126
    else if(ret < rec_len)
127 128 129 130 131 132
        return(0);
    else
    {
        /* if the read was successful, do any necessary byte-swapping */
        if(fd->swap_flag)
        {
133 134
            DARSHAN_BSWAP64(&file->base_rec.id);
            DARSHAN_BSWAP64(&file->base_rec.rank);
135
            for(i=0; i<STDIO_NUM_INDICES; i++)
136 137 138 139 140 141 142
            {
                /* skip counters we explicitly set since they don't
                 * need to be byte swapped
                 */
                if((fd->mod_ver[DARSHAN_STDIO_MOD] == 1) &&
                    (i == STDIO_FDOPENS))
                    continue;
143
                DARSHAN_BSWAP64(&file->counters[i]);
144
            }
145
            for(i=0; i<STDIO_F_NUM_INDICES; i++)
146
                DARSHAN_BSWAP64(&file->fcounters[i]);
147 148 149 150 151 152 153 154 155
        }

        return(1);
    }
}

/* write the STDIO record stored in 'stdio_buf' to log file descriptor 'fd'.
 * Return 0 on success, -1 on failure
 */
156
static int darshan_log_put_stdio_record(darshan_fd fd, void* stdio_buf)
157
{
158
    struct darshan_stdio_file *rec = (struct darshan_stdio_file *)stdio_buf;
159 160 161
    int ret;

    /* append STDIO record to darshan log file */
162
    ret = darshan_log_put_mod(fd, DARSHAN_STDIO_MOD, rec,
163
        sizeof(struct darshan_stdio_file), DARSHAN_STDIO_VER);
164 165 166 167 168 169 170 171
    if(ret < 0)
        return(-1);

    return(0);
}

/* print all I/O data record statistics for the given STDIO record */
static void darshan_log_print_stdio_record(void *file_rec, char *file_name,
172
    char *mnt_pt, char *fs_type)
173 174
{
    int i;
175 176
    struct darshan_stdio_file *stdio_rec =
        (struct darshan_stdio_file *)file_rec;
177 178 179 180 181

    /* print each of the integer and floating point counters for the STDIO module */
    for(i=0; i<STDIO_NUM_INDICES; i++)
    {
        /* macro defined in darshan-logutils.h */
182
        DARSHAN_D_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
183
            stdio_rec->base_rec.rank, stdio_rec->base_rec.id, stdio_counter_names[i],
184 185 186 187 188 189 190
            stdio_rec->counters[i], file_name, mnt_pt, fs_type);
    }

    for(i=0; i<STDIO_F_NUM_INDICES; i++)
    {
        /* macro defined in darshan-logutils.h */
        DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
191
            stdio_rec->base_rec.rank, stdio_rec->base_rec.id, stdio_f_counter_names[i],
192 193 194 195 196 197 198
            stdio_rec->fcounters[i], file_name, mnt_pt, fs_type);
    }

    return;
}

/* print out a description of the STDIO module record fields */
199
static void darshan_log_print_stdio_description(int ver)
200 201
{
    printf("\n# description of STDIO counters:\n");
202
    printf("#   STDIO_{OPENS|FDOPENS|WRITES|READS|SEEKS|FLUSHES} are types of operations.\n");
203 204
    printf("#   STDIO_BYTES_*: total bytes read and written.\n");
    printf("#   STDIO_MAX_BYTE_*: highest offset byte read and written.\n");
205 206
    printf("#   STDIO_*_RANK: rank of the processes that were the fastest and slowest at I/O (for shared files).\n");
    printf("#   STDIO_*_RANK_BYTES: bytes transferred by the fastest and slowest ranks (for shared files).\n");
207 208 209
    printf("#   STDIO_F_*_START_TIMESTAMP: timestamp of the first call to that type of function.\n");
    printf("#   STDIO_F_*_END_TIMESTAMP: timestamp of the completion of the last call to that type of function.\n");
    printf("#   STDIO_F_*_TIME: cumulative time spent in different types of functions.\n");
210 211 212
    printf("#   STDIO_F_*_RANK_TIME: fastest and slowest I/O time for a single rank (for shared files).\n");
    printf("#   STDIO_F_VARIANCE_RANK_*: variance of total I/O time and bytes moved for all ranks (for shared files).\n");

213 214 215 216 217 218
    if(ver == 1)
    {
        printf("\n# WARNING: STDIO module log format version 1 has the following limitations:\n");
        printf("# - No support for properly instrumenting fdopen operations (STDIO_FDOPENS)\n");
    }

219 220 221 222 223 224
    if(ver >= 2)
    {
        printf("\n# WARNING: STDIO_OPENS counter includes STDIO_FDOPENS count\n");
        printf("\n# WARNING: STDIO counters related to file offsets may be incorrect if a file is simultaneously accessed by both STDIO and POSIX (e.g., using fdopen())\n");
        printf("# \t- Affected counters include: MAX_BYTE_{READ|WRITTEN}\n");
    }
225

226 227 228 229 230 231
    return;
}

static void darshan_log_print_stdio_record_diff(void *file_rec1, char *file_name1,
    void *file_rec2, char *file_name2)
{
232 233
    struct darshan_stdio_file *file1 = (struct darshan_stdio_file *)file_rec1;
    struct darshan_stdio_file *file2 = (struct darshan_stdio_file *)file_rec2;
234 235 236 237 238 239 240 241 242
    int i;

    /* NOTE: we assume that both input records are the same module format version */

    for(i=0; i<STDIO_NUM_INDICES; i++)
    {
        if(!file2)
        {
            printf("- ");
243
            DARSHAN_D_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
244
                file1->base_rec.rank, file1->base_rec.id, stdio_counter_names[i],
245 246 247 248 249 250
                file1->counters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
251
            DARSHAN_D_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
252
                file2->base_rec.rank, file2->base_rec.id, stdio_counter_names[i],
253 254 255 256 257
                file2->counters[i], file_name2, "", "");
        }
        else if(file1->counters[i] != file2->counters[i])
        {
            printf("- ");
258
            DARSHAN_D_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
259
                file1->base_rec.rank, file1->base_rec.id, stdio_counter_names[i],
260 261
                file1->counters[i], file_name1, "", "");
            printf("+ ");
262
            DARSHAN_D_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
263
                file2->base_rec.rank, file2->base_rec.id, stdio_counter_names[i],
264 265 266 267 268 269 270 271 272 273
                file2->counters[i], file_name2, "", "");
        }
    }

    for(i=0; i<STDIO_F_NUM_INDICES; i++)
    {
        if(!file2)
        {
            printf("- ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
274
                file1->base_rec.rank, file1->base_rec.id, stdio_f_counter_names[i],
275 276 277 278 279 280 281
                file1->fcounters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
282
                file2->base_rec.rank, file2->base_rec.id, stdio_f_counter_names[i],
283 284 285 286 287 288
                file2->fcounters[i], file_name2, "", "");
        }
        else if(file1->fcounters[i] != file2->fcounters[i])
        {
            printf("- ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
289
                file1->base_rec.rank, file1->base_rec.id, stdio_f_counter_names[i],
290 291 292
                file1->fcounters[i], file_name1, "", "");
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
293
                file2->base_rec.rank, file2->base_rec.id, stdio_f_counter_names[i],
294 295 296 297 298 299 300
                file2->fcounters[i], file_name2, "", "");
        }
    }

    return;
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
/* simple helper struct for determining time & byte variances */
struct var_t
{
    double n;
    double M;
    double S;
};

static void darshan_log_agg_stdio_records(void *rec, void *agg_rec, int init_flag)
{
    struct darshan_stdio_file *stdio_rec = (struct darshan_stdio_file *)rec;
    struct darshan_stdio_file *agg_stdio_rec = (struct darshan_stdio_file *)agg_rec;
    int i;
    double old_M;
    double stdio_time = stdio_rec->fcounters[STDIO_F_READ_TIME] +
        stdio_rec->fcounters[STDIO_F_WRITE_TIME] +
        stdio_rec->fcounters[STDIO_F_META_TIME];
    double stdio_bytes = (double)stdio_rec->counters[STDIO_BYTES_READ] +
        stdio_rec->counters[STDIO_BYTES_WRITTEN];
    struct var_t *var_time_p = (struct var_t *)
        ((char *)rec + sizeof(struct darshan_stdio_file));
    struct var_t *var_bytes_p = (struct var_t *)
        ((char *)var_time_p + sizeof(struct var_t));

    for(i = 0; i < STDIO_NUM_INDICES; i++)
    {
        switch(i)
        {
            case STDIO_OPENS:
330
            case STDIO_FDOPENS:
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
            case STDIO_READS:
            case STDIO_WRITES:
            case STDIO_SEEKS:
            case STDIO_FLUSHES:
            case STDIO_BYTES_WRITTEN:
            case STDIO_BYTES_READ:
                /* sum */
                agg_stdio_rec->counters[i] += stdio_rec->counters[i];
                break;
            case STDIO_MAX_BYTE_READ:
            case STDIO_MAX_BYTE_WRITTEN:
                /* max */
                if(stdio_rec->counters[i] > agg_stdio_rec->counters[i])
                {
                    agg_stdio_rec->counters[i] = stdio_rec->counters[i];
                }
                break;
            case STDIO_FASTEST_RANK:
            case STDIO_FASTEST_RANK_BYTES:
            case STDIO_SLOWEST_RANK:
            case STDIO_SLOWEST_RANK_BYTES:
                /* these are set with the FP counters */
                break;
            default:
                agg_stdio_rec->counters[i] = -1;
                break;
        }
    }

    for(i = 0; i < STDIO_F_NUM_INDICES; i++)
    {
        switch(i)
        {
            case STDIO_F_META_TIME:
            case STDIO_F_WRITE_TIME:
            case STDIO_F_READ_TIME:
                /* sum */
                agg_stdio_rec->fcounters[i] += stdio_rec->fcounters[i];
                break;
            case STDIO_F_OPEN_START_TIMESTAMP:
            case STDIO_F_CLOSE_START_TIMESTAMP:
            case STDIO_F_WRITE_START_TIMESTAMP:
            case STDIO_F_READ_START_TIMESTAMP:
                /* minimum non-zero */
                if((stdio_rec->fcounters[i] > 0)  &&
                    ((agg_stdio_rec->fcounters[i] == 0) ||
                    (stdio_rec->fcounters[i] < agg_stdio_rec->fcounters[i])))
                {
                    agg_stdio_rec->fcounters[i] = stdio_rec->fcounters[i];
                }
                break;
            case STDIO_F_OPEN_END_TIMESTAMP:
            case STDIO_F_CLOSE_END_TIMESTAMP:
            case STDIO_F_WRITE_END_TIMESTAMP:
            case STDIO_F_READ_END_TIMESTAMP:
                /* maximum */
                if(stdio_rec->fcounters[i] > agg_stdio_rec->fcounters[i])
                {
                    agg_stdio_rec->fcounters[i] = stdio_rec->fcounters[i];
                }
                break;
            case STDIO_F_FASTEST_RANK_TIME:
                if(init_flag)
                {
                    /* set fastest rank counters according to root rank. these counters
                     * will be determined as the aggregation progresses.
                     */
                    agg_stdio_rec->counters[STDIO_FASTEST_RANK] = stdio_rec->base_rec.rank;
                    agg_stdio_rec->counters[STDIO_FASTEST_RANK_BYTES] = stdio_bytes;
                    agg_stdio_rec->fcounters[STDIO_F_FASTEST_RANK_TIME] = stdio_time;
                }

                if(stdio_time < agg_stdio_rec->fcounters[STDIO_F_FASTEST_RANK_TIME])
                {
                    agg_stdio_rec->counters[STDIO_FASTEST_RANK] = stdio_rec->base_rec.rank;
                    agg_stdio_rec->counters[STDIO_FASTEST_RANK_BYTES] = stdio_bytes;
                    agg_stdio_rec->fcounters[STDIO_F_FASTEST_RANK_TIME] = stdio_time;
                }
                break;
            case STDIO_F_SLOWEST_RANK_TIME:
                if(init_flag)
                {
                    /* set slowest rank counters according to root rank. these counters
                     * will be determined as the aggregation progresses.
                     */
                    agg_stdio_rec->counters[STDIO_SLOWEST_RANK] = stdio_rec->base_rec.rank;
                    agg_stdio_rec->counters[STDIO_SLOWEST_RANK_BYTES] = stdio_bytes;
                    agg_stdio_rec->fcounters[STDIO_F_SLOWEST_RANK_TIME] = stdio_time;
                }

                if(stdio_time > agg_stdio_rec->fcounters[STDIO_F_SLOWEST_RANK_TIME])
                {
                    agg_stdio_rec->counters[STDIO_SLOWEST_RANK] = stdio_rec->base_rec.rank;
                    agg_stdio_rec->counters[STDIO_SLOWEST_RANK_BYTES] = stdio_bytes;
                    agg_stdio_rec->fcounters[STDIO_F_SLOWEST_RANK_TIME] = stdio_time;
                }
                break;
            case STDIO_F_VARIANCE_RANK_TIME:
                if(init_flag)
                {
                    var_time_p->n = 1;
                    var_time_p->M = stdio_time;
                    var_time_p->S = 0;
                }
                else
                {
                    old_M = var_time_p->M;

                    var_time_p->n++;
                    var_time_p->M += (stdio_time - var_time_p->M) / var_time_p->n;
                    var_time_p->S += (stdio_time - var_time_p->M) * (stdio_time - old_M);

                    agg_stdio_rec->fcounters[STDIO_F_VARIANCE_RANK_TIME] =
                        var_time_p->S / var_time_p->n;
                }
                break;
            case STDIO_F_VARIANCE_RANK_BYTES:
                if(init_flag)
                {
                    var_bytes_p->n = 1;
                    var_bytes_p->M = stdio_bytes;
                    var_bytes_p->S = 0;
                }
                else
                {
                    old_M = var_bytes_p->M;

                    var_bytes_p->n++;
                    var_bytes_p->M += (stdio_bytes - var_bytes_p->M) / var_bytes_p->n;
                    var_bytes_p->S += (stdio_bytes - var_bytes_p->M) * (stdio_bytes - old_M);

                    agg_stdio_rec->fcounters[STDIO_F_VARIANCE_RANK_BYTES] =
                        var_bytes_p->S / var_bytes_p->n;
                }
                break;
            default:
                agg_stdio_rec->fcounters[i] = -1;
                break;
        }
    }

    return;
}
474 475 476 477 478 479 480 481 482

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