darshan-lustre.c 15.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#define _XOPEN_SOURCE 500
#define _GNU_SOURCE

#include "darshan-runtime-config.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
19 20 21
#include <sys/ioctl.h>

/* XXX stick this into autoconf .h */
22
#include <lustre/lustre_user.h>
23 24 25 26 27

#include "uthash.h"

#include "darshan.h"
#include "darshan-dynamic.h"
28
#include "darshan-lustre.h"
29

30
struct lustre_runtime *lustre_runtime = NULL;
31 32 33 34
static pthread_mutex_t lustre_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static int instrumentation_disabled = 0;
static int my_rank = -1;

35 36
static void lustre_runtime_initialize(void);

37 38 39 40
static void lustre_begin_shutdown(void);
static void lustre_get_output_data(MPI_Comm mod_comm, darshan_record_id *shared_recs,
    int shared_rec_count, void **lustre_buf, int *lustre_buf_sz);
static void lustre_shutdown(void);
41 42 43
static int lustre_record_compare(const void* a_p, const void* b_p);
static void lustre_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype);
44 45 46 47

#define LUSTRE_LOCK() pthread_mutex_lock(&lustre_runtime_mutex)
#define LUSTRE_UNLOCK() pthread_mutex_unlock(&lustre_runtime_mutex)

48
void darshan_instrument_lustre_file(const char* filepath, int fd)
49
{
50
    struct lustre_record_runtime *rec_rt;
51
    struct darshan_lustre_record *rec;
52
    struct darshan_fs_info fs_info;
53
    darshan_record_id rec_id;
54
    int limit_flag;
55 56 57 58 59
    int i;
    struct lov_user_md *lum;
    size_t lumsize = sizeof(struct lov_user_md) +
        LOV_MAX_STRIPE_COUNT * sizeof(struct lov_user_ost_data);
    size_t rec_size;
60
    char *newname = NULL;
61 62

    LUSTRE_LOCK();
63 64 65 66 67 68
    if(instrumentation_disabled)
    {
        LUSTRE_UNLOCK();
        return;
    }

69 70
    /* make sure the lustre module is already initialized */
    lustre_runtime_initialize();
71 72 73 74 75
    if(!lustre_runtime)
    {
        LUSTRE_UNLOCK();
        return;
    }
76

77 78
    /* if we can't issue ioctl, we have no counter data at all */
    if ( (lum = calloc(1, lumsize)) == NULL )
79 80
    {
        LUSTRE_UNLOCK();
81
        return;
82
    }
83 84 85 86 87 88 89 90 91

    /* find out the OST count of this file so we can allocate memory */
    lum->lmm_magic = LOV_USER_MAGIC;
    lum->lmm_stripe_count = LOV_MAX_STRIPE_COUNT;

    /* -1 means ioctl failed, likely because file isn't on Lustre */
    if ( ioctl( fd, LL_IOC_LOV_GETSTRIPE, (void *)lum ) == -1 )
    {
        free(lum);
92
        LUSTRE_UNLOCK();
93 94 95 96 97
        return;
    }

    rec_size = LUSTRE_RECORD_SIZE( lum->lmm_stripe_count );

98 99 100 101 102
    /* get fully qualified name for record */
    newname = darshan_clean_file_path(filepath);
    if(!newname)
        newname = (char*)filepath;

103 104 105 106 107 108
    {
        /* broken out for clarity */
        void *end_of_new_record = (char*)lustre_runtime->next_free_record + rec_size;
        void *end_of_rec_buffer = (char*)lustre_runtime->record_buffer + lustre_runtime->record_buffer_max;
        limit_flag = ( end_of_new_record > end_of_rec_buffer );
    }
109 110

    /* register a Lustre file record with Darshan */
111
    fs_info.fs_type = -1;
112
    darshan_core_register_record(
113 114
        (void *)newname,
        strlen(newname),
115 116
        DARSHAN_LUSTRE_MOD,
        1,
117
        limit_flag,
118
        &rec_id,
119
        &fs_info);
120 121 122

    /* if record id is 0, darshan has no more memory for instrumenting */
    if(rec_id == 0)
123
    {
124
        free(lum);
125
        LUSTRE_UNLOCK();
126
        return;
127 128 129
    }

    /* search the hash table for this file record, and initialize if not found */
130
    HASH_FIND(hlink, lustre_runtime->record_runtime_hash, &rec_id, sizeof(darshan_record_id), rec_rt );
131 132
    if ( !rec_rt ) {
        /* allocate a new lustre record and append it to the array */
133 134 135 136 137
        rec_rt = &(lustre_runtime->record_runtime_array[lustre_runtime->record_count]);
        rec_rt->record = lustre_runtime->next_free_record;
        rec_rt->record_size = rec_size;
        lustre_runtime->next_free_record = (char*)(lustre_runtime->next_free_record) + rec_size;
        lustre_runtime->record_buffer_used += rec_size;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        rec = rec_rt->record;
        rec->rec_id = rec_id;
        rec->rank = my_rank;

        /* implicit assumption here that none of these counters will change
         * after the first time a file is opened.  This may not always be
         * true in the future */
        if ( fs_info.fs_type != -1 ) 
        {
            rec->counters[LUSTRE_OSTS] = fs_info.ost_count;
            rec->counters[LUSTRE_MDTS] = fs_info.mdt_count;
        }
        else
        {
            rec->counters[LUSTRE_OSTS] = -1;
            rec->counters[LUSTRE_MDTS] = -1;
        }
155

156 157 158 159 160 161 162 163 164 165
        rec->counters[LUSTRE_STRIPE_SIZE] = lum->lmm_stripe_size;
        rec->counters[LUSTRE_STRIPE_WIDTH] = lum->lmm_stripe_count;
        rec->counters[LUSTRE_STRIPE_OFFSET] = lum->lmm_stripe_offset;
        for ( i = 0; i < lum->lmm_stripe_count; i++ )
            rec->ost_ids[i] = lum->lmm_objects[i].l_ost_idx;
        free(lum);

        HASH_ADD(hlink, lustre_runtime->record_runtime_hash, record->rec_id, sizeof(darshan_record_id), rec_rt);

        lustre_runtime->record_count++;
166
    }
167 168

    LUSTRE_UNLOCK();
169 170 171
    return;
}

172 173 174
static void lustre_runtime_initialize()
{
    int mem_limit;
175
    int max_records;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    struct darshan_module_funcs lustre_mod_fns =
    {
        .begin_shutdown = &lustre_begin_shutdown,
        .get_output_data = &lustre_get_output_data,
        .shutdown = &lustre_shutdown
    };

    /* don't do anything if already initialized or instrumenation is disabled */
    if(lustre_runtime || instrumentation_disabled)
        return;

    /* register the lustre module with darshan-core */
    darshan_core_register_module(
        DARSHAN_LUSTRE_MOD,
        &lustre_mod_fns,
        &my_rank,
        &mem_limit,
        NULL);

    /* return if no memory assigned by darshan core */
    if(mem_limit == 0)
        return;

    lustre_runtime = malloc(sizeof(*lustre_runtime));
    if(!lustre_runtime)
        return;
    memset(lustre_runtime, 0, sizeof(*lustre_runtime));

204 205 206
    /* allocate the full size of the memory limit we are given */
    lustre_runtime->record_buffer= malloc(mem_limit);
    if(!lustre_runtime->record_buffer)
207
    {
208
        lustre_runtime->record_buffer_max = 0;
209 210
        return;
    }
211 212 213 214 215 216 217 218 219 220 221 222 223
    lustre_runtime->record_buffer_max = mem_limit;
    lustre_runtime->next_free_record = lustre_runtime->record_buffer;
    memset(lustre_runtime->record_buffer, 0, lustre_runtime->record_buffer_max);

    /* Allocate array of Lustre runtime data.  We calculate the maximum possible
     * number of records that will fit into mem_limit by assuming that each
     * record has the minimum possible OST count, then allocate that many 
     * runtime records.  record_buffer will always run out of memory before
     * we overflow record_runtime_array.
     */
    max_records = mem_limit / sizeof(struct darshan_lustre_record);
    lustre_runtime->record_runtime_array =
        malloc( max_records * sizeof(struct lustre_record_runtime));
224 225
    if(!lustre_runtime->record_runtime_array)
    {
226 227
        lustre_runtime->record_buffer_max = 0;
        free( lustre_runtime->record_buffer );
228 229
        return;
    }
230 231
    memset(lustre_runtime->record_runtime_array, 0,
        max_records * sizeof(struct lustre_record_runtime));
232

233 234 235
    return;
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**************************************************************************
 * Functions exported by Lustre module for coordinating with darshan-core *
 **************************************************************************/

static void lustre_begin_shutdown(void)
{
    assert(lustre_runtime);

    LUSTRE_LOCK();
    /* disable further instrumentation while Darshan shuts down */
    instrumentation_disabled = 1;
    LUSTRE_UNLOCK();

    return;
}

static void lustre_get_output_data(
    MPI_Comm mod_comm,
    darshan_record_id *shared_recs,
    int shared_rec_count,
    void **lustre_buf,
    int *lustre_buf_sz)
{
259
    struct lustre_record_runtime *file;
260 261
    int i;

262 263
    assert(lustre_runtime);

264 265 266
    /* if there are globally shared files, do a shared file reduction */
    /* NOTE: the shared file reduction is also skipped if the 
     * DARSHAN_DISABLE_SHARED_REDUCTION environment variable is set.
267
     */
268
    if (shared_rec_count && !getenv("DARSHAN_DISABLE_SHARED_REDUCTION"))
269 270 271 272
    {
        /* necessary initialization of shared records */
        for(i = 0; i < shared_rec_count; i++)
        {
273
            HASH_FIND(hlink, lustre_runtime->record_runtime_hash, &shared_recs[i],
274 275 276
                sizeof(darshan_record_id), file);
            assert(file);

277
            file->record->rank = -1;
278 279 280 281 282 283
        }

        /* sort the array of files descending by rank so that we get all of the 
         * shared files (marked by rank -1) in a contiguous portion at end 
         * of the array
         */
284
        sort_lustre_records();
285

286 287 288
        /* simply drop all shared records from non-root ranks by truncating
         * the record array and recalculating the size of the used buffer
         */
289
        if (my_rank != 0)
290
        {
291
            lustre_runtime->record_count -= shared_rec_count;
292 293 294 295 296
            lustre_runtime->record_buffer_used = 0;
            for ( i = 0; i < lustre_runtime->record_count; i++ )
                lustre_runtime->record_buffer_used += 
                    LUSTRE_RECORD_SIZE( (lustre_runtime->record_runtime_array[i]).record->counters[LUSTRE_STRIPE_WIDTH] );
        }
297
    }
298

299 300
    *lustre_buf = (void *)(lustre_runtime->record_buffer);
    *lustre_buf_sz = lustre_runtime->record_buffer_used;
301

302 303 304 305 306 307 308
    return;
}

static void lustre_shutdown(void)
{
    assert(lustre_runtime);

309
    HASH_CLEAR(hlink, lustre_runtime->record_runtime_hash);
310
    free(lustre_runtime->record_runtime_array);
311
    free(lustre_runtime->record_buffer);
312
    free(lustre_runtime);
313 314 315 316 317
    lustre_runtime = NULL;

    return;
}

318 319 320
/* compare function for sorting file records by descending rank */
static int lustre_record_compare(const void* a_p, const void* b_p)
{
321 322
    const struct lustre_record_runtime* a = a_p;
    const struct lustre_record_runtime* b = b_p;
323

324
    if (a->record->rank < b->record->rank)
325
        return 1;
326
    if (a->record->rank > b->record->rank)
327 328
        return -1;

329 330 331 332 333 334 335
    /* if ( a->record->rank == b->record->rank ) we MUST do a secondary
     * sort so that the order of qsort is fully deterministic and consistent
     * across all MPI ranks.  Without a secondary sort, the sort order can
     * be affected by rank-specific variations (e.g., the order in which
     * files are first opened).
     */

336 337 338
    return 0;
}

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
/*
 * Sort the record_runtimes and records by MPI rank to facilitate shared redux.
 * This requires craftiness and additional heap utilization because the records
 * (but not record_runtimes) have variable size.  Currently has to temporarily
 * duplicate the entire record_buffer; there is room for more memory-efficient
 * optimization if this becomes a scalability issue.
 */
int sort_lustre_records()
{
    int i;
    struct darshan_lustre_record *rec;
    struct lustre_record_runtime *rec_rt, *tmp_rec_rt;
    char  *new_buf, *p;

    /* Create a new buffer to store an entire replica of record_buffer.  Since
     * we know the exact size of record_buffer's useful data at this point, we
     * can allocate the exact amount we need instead of record_buffer_max */
    new_buf = malloc(lustre_runtime->record_buffer_used);
    p = new_buf;
    if ( !new_buf )
        return 1;

    /* qsort breaks the hash table, so delete it now to free its memory buffers
     * and prevent later confusion */
    HASH_ITER( hlink, lustre_runtime->record_runtime_hash, rec_rt, tmp_rec_rt )
        HASH_DELETE( hlink, lustre_runtime->record_runtime_hash, rec_rt );

    /* sort the runtime records, which is has fixed-length elements */
    qsort(
        lustre_runtime->record_runtime_array,
        lustre_runtime->record_count,
        sizeof(struct lustre_record_runtime),
        lustre_record_compare
    );

    /* rebuild the hash and array with the qsorted runtime records */
    for ( i = 0; i < lustre_runtime->record_count; i++ )
    {
        rec_rt = &(lustre_runtime->record_runtime_array[i]);
        HASH_ADD(hlink, lustre_runtime->record_runtime_hash, record->rec_id, sizeof(darshan_record_id), rec_rt );
    }

    /* create reordered record buffer, then copy it back in place */
    for ( i = 0; i < lustre_runtime->record_count; i++ )
    {
        rec_rt = &(lustre_runtime->record_runtime_array[i]);
        memcpy( p, rec_rt->record, rec_rt->record_size );
        /* fix record pointers within each runtime record too - pre-emptively
         * point them at where they will live in record_buffer after we memcpy
         * below */
        rec_rt->record = (struct darshan_lustre_record *)((char*)(lustre_runtime->record_buffer) + (p - new_buf));

        p += rec_rt->record_size;
    }
    memcpy( 
        lustre_runtime->record_buffer, 
        new_buf, 
        lustre_runtime->record_buffer_used );

    free(new_buf);
    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 440
/* this is just boilerplate reduction code that isn't currently used */
static void lustre_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype)
{
    struct darshan_lustre_record tmp_record;
    struct darshan_lustre_record *infile = infile_v;
    struct darshan_lustre_record *inoutfile = inoutfile_v;
    int i, j;

    assert(lustre_runtime);

    for( i=0; i<*len; i++ )
    {
        memset(&tmp_record, 0, sizeof(struct darshan_lustre_record));
        tmp_record.rec_id = infile->rec_id;
        tmp_record.rank = -1;

        /* preserve only rank 0's value */
        for( j = LUSTRE_OSTS; j < LUSTRE_NUM_INDICES; j++)
        {
            if ( my_rank == 0 ) 
            {
                tmp_record.counters[j] = infile->counters[j];
            }
            else
            {
                tmp_record.counters[j] = inoutfile->counters[j];
            }
        }

        /* update pointers */
        *inoutfile = tmp_record;
        inoutfile++;
        infile++;
    }

    return;
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
/*
 *  Dump the memory structure of our records and runtime records
 */
void print_lustre_runtime( void )
{
    int i, j;
    struct darshan_lustre_record *rec;

    /* print what we just loaded */
    for ( i = 0; i < lustre_runtime->record_count; i++ )
    {
        rec = (lustre_runtime->record_runtime_array[i]).record;
        printf( "File %2d\n", i );
        for ( j = 0; j < LUSTRE_NUM_INDICES; j++ )
        {
Glenn K. Lockwood's avatar
Glenn K. Lockwood committed
456
            printf( "  Counter %-2d: %10ld, addr %ld\n", 
457 458 459 460 461 462
                j, 
                rec->counters[j],
                (char*)(&(rec->counters[j])) - (char*)(lustre_runtime->record_buffer) );
        }
        for ( j = 0; j < rec->counters[LUSTRE_STRIPE_WIDTH]; j++ )
        {
Glenn K. Lockwood's avatar
Glenn K. Lockwood committed
463 464
            if ( j > 0 && j % 2 == 0 ) printf("\n");
            printf( "  Stripe  %-2d: %10ld, addr %-9d", 
465 466 467 468
                j, 
                rec->ost_ids[j],
                (char*)(&(rec->ost_ids[j])) - (char*)(lustre_runtime->record_buffer) );
        }
Glenn K. Lockwood's avatar
Glenn K. Lockwood committed
469
        printf( "\n" );
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    }
    return;
}

/*
 *  Dump the order in which records appear in memory
 */
void print_array( void )
{
    int i;
    struct lustre_record_runtime *rec_rt;
    printf("*** DUMPING RECORD LIST BY ARRAY SEQUENCE\n");
    for ( i = 0; i < lustre_runtime->record_count; i++ )
    {
        rec_rt = &(lustre_runtime->record_runtime_array[i]);
        printf( "*** record %d rank %d osts %d\n", 
            rec_rt->record->rec_id, 
            rec_rt->record->rank,
            rec_rt->record->counters[LUSTRE_STRIPE_WIDTH]);
    }
}
void print_hash( void )
{
    struct lustre_record_runtime *rec_rt, *tmp_rec_rt;
    printf("*** DUMPING RECORD LIST BY HASH SEQUENCE\n");
    HASH_ITER( hlink, lustre_runtime->record_runtime_hash, rec_rt, tmp_rec_rt )
    {
        printf( "*** record %d rank %d osts %d\n", 
            rec_rt->record->rec_id, 
            rec_rt->record->rank,
            rec_rt->record->counters[LUSTRE_STRIPE_WIDTH]);
    }
    return;
}




508 509 510 511 512 513 514 515
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */