darshan-mdhim-logutils.c 13.8 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 * 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 MDHIM module */
#define X(a) #a,
char *mdhim_counter_names[] = {
    MDHIM_COUNTERS
};

/* floating point counter name strings for the MDHIM module */
char *mdhim_f_counter_names[] = {
    MDHIM_F_COUNTERS
};
#undef X

/* prototypes for each of the MDHIM module's logutil functions */
static int darshan_log_get_mdhim_record(darshan_fd fd, void** mdhim_buf_p);
static int darshan_log_put_mdhim_record(darshan_fd fd, void* mdhim_buf);
static void darshan_log_print_mdhim_record(void *file_rec,
    char *file_name, char *mnt_pt, char *fs_type);
static void darshan_log_print_mdhim_description(int ver);
static void darshan_log_print_mdhim_record_diff(void *file_rec1, char *file_name1,
    void *file_rec2, char *file_name2);
static void darshan_log_agg_mdhim_records(void *rec, void *agg_rec, int init_flag);

/* 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 mdhim_logutils =
{
    .log_get_record = &darshan_log_get_mdhim_record,
    .log_put_record = &darshan_log_put_mdhim_record,
    .log_print_record = &darshan_log_print_mdhim_record,
    .log_print_description = &darshan_log_print_mdhim_description,
    .log_print_diff = &darshan_log_print_mdhim_record_diff,
    .log_agg_records = &darshan_log_agg_mdhim_records
};

/* retrieve a MDHIM record from log file descriptor 'fd', storing the
 * data in the buffer address pointed to by 'mdhim_buf_p'. Return 1 on
 * successful record read, 0 on no more data, and -1 on error.
 */
static int darshan_log_get_mdhim_record(darshan_fd fd, void** mdhim_buf_p)
{
    struct darshan_mdhim_record *rec =
        *((struct darshan_mdhim_record **)mdhim_buf_p);
66
    struct darshan_mdhim_record tmp_rec;
67 68 69 70 71 72 73 74 75 76 77 78 79
    int i;
    int ret;

    if(fd->mod_map[DARSHAN_MDHIM_MOD].len == 0)
        return(0);

    if(*mdhim_buf_p == NULL)
    {
        rec = malloc(sizeof(*rec));
        if(!rec)
            return(-1);
    }

80 81 82
    /* read the fixed-sized portion of the bdMDHIM module record from the
     * darshan log file */
    ret = darshan_log_get_mod(fd, DARSHAN_MDHIM_MOD, &tmp_rec,
83
        sizeof(struct darshan_mdhim_record));
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    if (ret < 0)
        return (-1);
    else if (ret < sizeof(struct darshan_mdhim_record))
        return (0);
    /* swap bytes if necessary */
    if (fd->swap_flag)
    {
        /* reader-makes-right:  don't look at a field until it has
         * been swapped */
        DARSHAN_BSWAP64(&tmp_rec.base_rec.id);
        DARSHAN_BSWAP64(&tmp_rec.base_rec.rank);
        for (i=0; i< MDHIM_NUM_INDICES; i++)
            DARSHAN_BSWAP64(&tmp_rec.counters[i]);
        for (i=0; i< MDHIM_F_NUM_INDICES; i++)
            DARSHAN_BSWAP64(&tmp_rec.fcounters[i]);
        for (i=0; i< tmp_rec.counters[MDHIM_SERVERS]; i++)
            DARSHAN_BSWAP32(&tmp_rec.server_histogram[i]);
    }
102 103 104

    if(*mdhim_buf_p == NULL)
    {
105 106 107 108 109 110 111 112 113 114 115 116
        rec = malloc(MDHIM_RECORD_SIZE(tmp_rec.counters[MDHIM_SERVERS]));
        if (!rec)
            return (-1);
    }
    memcpy(rec, &tmp_rec, sizeof(struct darshan_mdhim_record));

    if (rec->counters[MDHIM_SERVERS] > 1) {
        ret = darshan_log_get_mod(fd, DARSHAN_MDHIM_MOD,
                &(rec->server_histogram[1]),
                (rec->counters[MDHIM_SERVERS] - 1)*sizeof(int32_t));
        if (ret < (rec->counters[MDHIM_SERVERS] -1)*sizeof(int32_t))
            ret = -1;
117
        else
118 119 120 121 122 123
        {
            ret = 1;
            if (fd->swap_flag)
                for(i=1; i< rec->counters[MDHIM_SERVERS]; i++)
                    DARSHAN_BSWAP32(&(rec->server_histogram[i]));
        }
124 125 126
    }
    else
    {
127 128 129 130 131 132 133 134
        ret = 1;
    }
    if (*mdhim_buf_p == NULL)
    {
        if (ret == 1)
            *mdhim_buf_p = rec;
        else
            free(rec);
135
    }
136
    return (ret);
137 138 139 140 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 175 176 177 178 179 180 181
}

/* write the MDHIM record stored in 'mdhim_buf' to log file descriptor 'fd'.
 * Return 0 on success, -1 on failure
 */
static int darshan_log_put_mdhim_record(darshan_fd fd, void* mdhim_buf)
{
    struct darshan_mdhim_record *rec = (struct darshan_mdhim_record *)mdhim_buf;
    int ret;

    /* append MDHIM record to darshan log file */
    ret = darshan_log_put_mod(fd, DARSHAN_MDHIM_MOD, rec,
        sizeof(struct darshan_mdhim_record), DARSHAN_MDHIM_VER);
    if(ret < 0)
        return(-1);

    return(0);
}

/* print all I/O data record statistics for the given MDHIM record */
static void darshan_log_print_mdhim_record(void *file_rec, char *file_name,
    char *mnt_pt, char *fs_type)
{
    int i;
    struct darshan_mdhim_record *mdhim_rec =
        (struct darshan_mdhim_record *)file_rec;

    /* print each of the integer and floating point counters for the MDHIM module */
    for(i=0; i<MDHIM_NUM_INDICES; i++)
    {
        /* macro defined in darshan-logutils.h */
        DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
            mdhim_rec->base_rec.rank, mdhim_rec->base_rec.id,
            mdhim_counter_names[i], mdhim_rec->counters[i],
            file_name, mnt_pt, fs_type);
    }

    for(i=0; i<MDHIM_F_NUM_INDICES; i++)
    {
        /* macro defined in darshan-logutils.h */
        DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
            mdhim_rec->base_rec.rank, mdhim_rec->base_rec.id,
            mdhim_f_counter_names[i], mdhim_rec->fcounters[i],
            file_name, mnt_pt, fs_type);
    }
182 183 184 185 186 187 188 189 190 191 192
    for (i=0; i< mdhim_rec->counters[MDHIM_SERVERS]; i++)
    {
        char strbuf[25];
        snprintf(strbuf, 25, "MDHIM_SERVER_%d", i);
        DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                mdhim_rec->base_rec.rank,
                mdhim_rec->base_rec.id,
                strbuf,
                (int64_t)mdhim_rec->server_histogram[i],
                file_name, mnt_pt, fs_type);
    }
193 194 195 196 197 198 199 200 201
    return;
}

/* print out a description of the MDHIM module record fields */
static void darshan_log_print_mdhim_description(int ver)
{
    printf("\n# description of MDHIM counters:\n");
    printf("#   MDHIM_PUTS: number of 'mdhim_put' function calls.\n");
    printf("#   MDHIM_GETS: number of 'mdhim_get' function calls.\n");
202
    printf("#   MDHIM_SERVERS: how many mdhim servers \n");
203 204
    printf("#   MDHIM_F_PUT_TIMESTAMP: timestamp of the first call to function 'mdhim_put'.\n");
    printf("#   MDHIM_F_GET_TIMESTAMP: timestamp of the first call to function 'mdhim_get'.\n");
205
    printf("#   MDHIM_SERVER_N: how many operations sent to this server\n");
206 207 208 209 210 211 212 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

    return;
}

/* print a diff of two MDHIM records (with the same record id) */
static void darshan_log_print_mdhim_record_diff(void *file_rec1, char *file_name1,
    void *file_rec2, char *file_name2)
{
    struct darshan_mdhim_record *file1 = (struct darshan_mdhim_record *)file_rec1;
    struct darshan_mdhim_record *file2 = (struct darshan_mdhim_record *)file_rec2;
    int i;

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

    for(i=0; i<MDHIM_NUM_INDICES; i++)
    {
        if(!file2)
        {
            printf("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file1->base_rec.rank, file1->base_rec.id, mdhim_counter_names[i],
                file1->counters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file2->base_rec.rank, file2->base_rec.id, mdhim_counter_names[i],
                file2->counters[i], file_name2, "", "");
        }
        else if(file1->counters[i] != file2->counters[i])
        {
            printf("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file1->base_rec.rank, file1->base_rec.id, mdhim_counter_names[i],
                file1->counters[i], file_name1, "", "");
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file2->base_rec.rank, file2->base_rec.id, mdhim_counter_names[i],
                file2->counters[i], file_name2, "", "");
        }
    }

    for(i=0; i<MDHIM_F_NUM_INDICES; i++)
    {
        if(!file2)
        {
            printf("- ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file1->base_rec.rank, file1->base_rec.id, mdhim_f_counter_names[i],
                file1->fcounters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file2->base_rec.rank, file2->base_rec.id, mdhim_f_counter_names[i],
                file2->fcounters[i], file_name2, "", "");
        }
        else if(file1->fcounters[i] != file2->fcounters[i])
        {
            printf("- ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file1->base_rec.rank, file1->base_rec.id, mdhim_f_counter_names[i],
                file1->fcounters[i], file_name1, "", "");
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                file2->base_rec.rank, file2->base_rec.id, mdhim_f_counter_names[i],
                file2->fcounters[i], file_name2, "", "");
        }
    }
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
    i=0;
    while (1)
    {
        char strbuf[25];
        snprintf(strbuf, 25, "MDHIM_SERVER_%d", i);
        if (!file2 || (i >= file2->counters[MDHIM_SERVERS]))
        {
            printf("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                    file1->base_rec.rank,
                    file1->base_rec.id,
                    strbuf,
                    (int64_t)file1->server_histogram[i],
                    file_name1, "", "");
        }
        else if (!file1 || (i >= file1->counters[MDHIM_SERVERS]))
        {
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                    file2->base_rec.rank,
                    file2->base_rec.id,
                    strbuf,
                    (int64_t)file2->server_histogram[i],
                    file_name2, "", "");
        }
        else if (file1->server_histogram[i] != file2->server_histogram[i])
        {
            printf("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                    file1->base_rec.rank,
                    file1->base_rec.id,
                    strbuf,
                    (int64_t)file1->server_histogram[i],
                    file_name1, "", "");
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_MDHIM_MOD],
                    file2->base_rec.rank,
                    file2->base_rec.id,
                    strbuf,
                    (int64_t)file2->server_histogram[i],
                    file_name2, "", "");
        }
        i++;
        if ( (!file1 || i >= file1->counters[MDHIM_SERVERS]) &&
            (!file2 || i >= file2->counters[MDHIM_SERVERS] ) )
            break;
    }
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    return;
}

/* aggregate the input MDHIM record 'rec'  into the output record 'agg_rec' */
static void darshan_log_agg_mdhim_records(void *rec, void *agg_rec, int init_flag)
{
    struct darshan_mdhim_record *mdhim_rec = (struct darshan_mdhim_record *)rec;
    struct darshan_mdhim_record *agg_mdhim_rec = (struct darshan_mdhim_record *)agg_rec;
    int i;

    for(i = 0; i < MDHIM_NUM_INDICES; i++)
    {
        switch(i)
        {
            case MDHIM_PUTS:
                /* sum */
                agg_mdhim_rec->counters[i] += mdhim_rec->counters[i];
                break;
            case MDHIM_GETS:
                /* sum */
                agg_mdhim_rec->counters[i] += mdhim_rec->counters[i];
                break;
348 349 350 351
            case MDHIM_SERVERS:
                /* all clients should have the same value for this, hence
                 * assignment instead of aggregating */
                agg_mdhim_rec->counters[i] = mdhim_rec->counters[i];
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
            default:
                /* if we don't know how to aggregate this counter, just set to -1 */
                agg_mdhim_rec->counters[i] = -1;
                break;
        }
    }

    for(i = 0; i < MDHIM_F_NUM_INDICES; i++)
    {
        switch(i)
        {
            case MDHIM_F_PUT_TIMESTAMP:
                /* min non-zero */
                if((mdhim_rec->fcounters[i] > 0)  &&
                    ((agg_mdhim_rec->fcounters[i] == 0) ||
                    (mdhim_rec->fcounters[i] < agg_mdhim_rec->fcounters[i])))
                {
                    agg_mdhim_rec->fcounters[i] = mdhim_rec->fcounters[i];
                }
                break;
            case MDHIM_F_GET_TIMESTAMP:
                /* min non-zero */
                if((mdhim_rec->fcounters[i] > 0)  &&
                    ((agg_mdhim_rec->fcounters[i] == 0) ||
                    (mdhim_rec->fcounters[i] < agg_mdhim_rec->fcounters[i])))
                {
                    agg_mdhim_rec->fcounters[i] = mdhim_rec->fcounters[i];
                }
                break;

            default:
                /* if we don't know how to aggregate this counter, just set to -1 */
                agg_mdhim_rec->fcounters[i] = -1;
                break;
        }
    }
388 389 390 391
    for (i=0; i< mdhim_rec->counters[MDHIM_SERVERS]; i++)
    {
        agg_mdhim_rec->server_histogram[i] += mdhim_rec->server_histogram[i];
    }
392 393 394 395 396 397 398 399 400 401 402 403

    return;
}

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