darshan-stdio-logutils.c 15.4 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 34
/*
 * 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

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

/* 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,
54 55
    .log_print_diff = &darshan_log_print_stdio_record_diff,
    .log_agg_records = &darshan_log_agg_stdio_records
56 57 58
};

/* retrieve a STDIO record from log file descriptor 'fd', storing the
59 60
 * 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.
61
 */
62
static int darshan_log_get_stdio_record(darshan_fd fd, void** stdio_buf_p)
63
{
64
    struct darshan_stdio_file *file = *((struct darshan_stdio_file **)stdio_buf_p);
65 66 67
    int i;
    int ret;

68 69 70 71 72 73 74 75 76 77
    if(fd->mod_map[DARSHAN_STDIO_MOD].len == 0)
        return(0);

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

78
    /* read a STDIO module record from the darshan log file */
79
    ret = darshan_log_get_mod(fd, DARSHAN_STDIO_MOD, file,
80
        sizeof(struct darshan_stdio_file));
81 82 83 84 85 86 87 88 89

    if(*stdio_buf_p == NULL)
    {
        if(ret == sizeof(struct darshan_stdio_file))
            *stdio_buf_p = file;
        else
            free(file);
    }

90 91
    if(ret < 0)
        return(-1);
92
    else if(ret < sizeof(struct darshan_stdio_file))
93 94 95 96 97 98
        return(0);
    else
    {
        /* if the read was successful, do any necessary byte-swapping */
        if(fd->swap_flag)
        {
99 100
            DARSHAN_BSWAP64(&file->base_rec.id);
            DARSHAN_BSWAP64(&file->base_rec.rank);
101
            for(i=0; i<STDIO_NUM_INDICES; i++)
102
                DARSHAN_BSWAP64(&file->counters[i]);
103
            for(i=0; i<STDIO_F_NUM_INDICES; i++)
104
                DARSHAN_BSWAP64(&file->fcounters[i]);
105 106 107 108 109 110 111 112 113 114 115
        }

        return(1);
    }
}

/* write the STDIO record stored in 'stdio_buf' to log file descriptor 'fd'.
 * Return 0 on success, -1 on failure
 */
static int darshan_log_put_stdio_record(darshan_fd fd, void* stdio_buf, int ver)
{
116
    struct darshan_stdio_file *rec = (struct darshan_stdio_file *)stdio_buf;
117 118 119
    int ret;

    /* append STDIO record to darshan log file */
120
    ret = darshan_log_put_mod(fd, DARSHAN_STDIO_MOD, rec,
121
        sizeof(struct darshan_stdio_file), DARSHAN_STDIO_VER);
122 123 124 125 126 127 128 129 130 131 132
    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,
    char *mnt_pt, char *fs_type, int ver)
{
    int i;
133 134
    struct darshan_stdio_file *stdio_rec =
        (struct darshan_stdio_file *)file_rec;
135 136 137 138 139 140

    /* 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 */
        DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
141
            stdio_rec->base_rec.rank, stdio_rec->base_rec.id, stdio_counter_names[i],
142 143 144 145 146 147 148
            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],
149
            stdio_rec->base_rec.rank, stdio_rec->base_rec.id, stdio_f_counter_names[i],
150 151 152 153 154 155 156 157 158 159
            stdio_rec->fcounters[i], file_name, mnt_pt, fs_type);
    }

    return;
}

/* print out a description of the STDIO module record fields */
static void darshan_log_print_stdio_description()
{
    printf("\n# description of STDIO counters:\n");
Philip Carns's avatar
Philip Carns committed
160
    printf("#   STDIO_{OPENS|WRITES|READS|SEEKS|FLUSHES} are types of operations.\n");
161 162
    printf("#   STDIO_BYTES_*: total bytes read and written.\n");
    printf("#   STDIO_MAX_BYTE_*: highest offset byte read and written.\n");
163 164
    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");
165 166 167
    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");
168 169 170 171
    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");

    DARSHAN_PRINT_HEADER();
172 173 174 175 176 177 178

    return;
}

static void darshan_log_print_stdio_record_diff(void *file_rec1, char *file_name1,
    void *file_rec2, char *file_name2)
{
179 180
    struct darshan_stdio_file *file1 = (struct darshan_stdio_file *)file_rec1;
    struct darshan_stdio_file *file2 = (struct darshan_stdio_file *)file_rec2;
181 182 183 184 185 186 187 188 189 190
    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("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
191
                file1->base_rec.rank, file1->base_rec.id, stdio_counter_names[i],
192 193 194 195 196 197 198
                file1->counters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
199
                file2->base_rec.rank, file2->base_rec.id, stdio_counter_names[i],
200 201 202 203 204 205
                file2->counters[i], file_name2, "", "");
        }
        else if(file1->counters[i] != file2->counters[i])
        {
            printf("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
206
                file1->base_rec.rank, file1->base_rec.id, stdio_counter_names[i],
207 208 209
                file1->counters[i], file_name1, "", "");
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
210
                file2->base_rec.rank, file2->base_rec.id, stdio_counter_names[i],
211 212 213 214 215 216 217 218 219 220
                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],
221
                file1->base_rec.rank, file1->base_rec.id, stdio_f_counter_names[i],
222 223 224 225 226 227 228
                file1->fcounters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
229
                file2->base_rec.rank, file2->base_rec.id, stdio_f_counter_names[i],
230 231 232 233 234 235
                file2->fcounters[i], file_name2, "", "");
        }
        else if(file1->fcounters[i] != file2->fcounters[i])
        {
            printf("- ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
236
                file1->base_rec.rank, file1->base_rec.id, stdio_f_counter_names[i],
237 238 239
                file1->fcounters[i], file_name1, "", "");
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
240
                file2->base_rec.rank, file2->base_rec.id, stdio_f_counter_names[i],
241 242 243 244 245 246 247
                file2->fcounters[i], file_name2, "", "");
        }
    }

    return;
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 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 323 324 325 326 327 328 329 330 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
/* 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:
            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;
}
420 421 422 423 424 425 426 427 428

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