darshan-lustre.c 11.8 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 28

#include "uthash.h"

#include "darshan.h"
#include "darshan-dynamic.h"

29 30 31 32 33 34
struct lustre_record_runtime
{
    struct darshan_lustre_record *record;
    UT_hash_handle hlink;
};

35 36 37 38
/* we just use a simple array for storing records. the POSIX module
 * only calls into the Lustre module for new records, so we will never
 * have to search for an existing Lustre record (assuming the Lustre
 * data remains immutable as it is now).
39
 */
40 41 42
struct lustre_runtime
{
    struct darshan_lustre_record *record_array;
43
    struct lustre_record_runtime *record_runtime_array;
44 45
    int record_array_size;
    int record_array_ndx;
46
    struct lustre_record_runtime *record_hash;
47 48 49 50 51 52 53
};

static struct lustre_runtime *lustre_runtime = NULL;
static pthread_mutex_t lustre_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static int instrumentation_disabled = 0;
static int my_rank = -1;

54 55
static void lustre_runtime_initialize(void);

56 57 58 59
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);
60 61 62
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);
63 64 65 66

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

67
void darshan_instrument_lustre_file(const char* filepath, int fd)
68
{
69
    struct lustre_record_runtime *rec_rt;
70
    struct darshan_fs_info fs_info;
71
    darshan_record_id rec_id;
72
    int limit_flag;
73 74

    LUSTRE_LOCK();
75 76
    /* make sure the lustre module is already initialized */
    lustre_runtime_initialize();
77

78
    /* if the array is full, we just back out */
79
    limit_flag = (lustre_runtime->record_array_ndx >= lustre_runtime->record_array_size);
80 81

    /* register a Lustre file record with Darshan */
82
    fs_info.fs_type = -1;
83 84 85 86 87
    darshan_core_register_record(
        (void *)filepath,
        strlen(filepath),
        DARSHAN_LUSTRE_MOD,
        1,
88
        limit_flag,
89
        &rec_id,
90
        &fs_info);
91 92 93

    /* if record id is 0, darshan has no more memory for instrumenting */
    if(rec_id == 0)
94 95
    {
        LUSTRE_UNLOCK();
96
        return;
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
    }

    /* search the hash table for this file record, and initialize if not found */
    HASH_FIND(hlink, lustre_runtime->record_hash, &rec_id, sizeof(darshan_record_id), rec_rt );
    if ( !rec_rt ) {
        struct darshan_lustre_record *rec;
        struct lov_user_md *lum;
        size_t lumsize = sizeof(struct lov_user_md) +
            LOV_MAX_STRIPE_COUNT * sizeof(struct lov_user_ost_data);

        /* allocate a new lustre record and append it to the array */
        rec_rt = &(lustre_runtime->record_runtime_array[lustre_runtime->record_array_ndx]);
        rec_rt->record = &(lustre_runtime->record_array[lustre_runtime->record_array_ndx]);
        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;
        }
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        if ( (lum = calloc(1, lumsize)) != NULL ) {
            lum->lmm_magic = LOV_USER_MAGIC;
            /* don't care about the return code for ioctl */
            ioctl( fd, LL_IOC_LOV_GETSTRIPE, (void *)lum );
            rec->counters[LUSTRE_STRIPE_SIZE] = lum->lmm_stripe_size;
            rec->counters[LUSTRE_STRIPE_WIDTH] = lum->lmm_stripe_count;
            rec->counters[LUSTRE_STRIPE_OFFSET] = 0; /* this currently doesn't work; lum->lmm_objects[0].l_ost_idx isn't being populated */
            /* TODO: add explicit list of OSTs */
            free(lum);
        }
        else
        {
            rec->counters[LUSTRE_STRIPE_SIZE] = -1;
            rec->counters[LUSTRE_STRIPE_WIDTH] = -1;
            rec->counters[LUSTRE_STRIPE_OFFSET] = -1;
        }
        HASH_ADD(hlink, lustre_runtime->record_hash, record->rec_id, sizeof(darshan_record_id), rec_rt);
        lustre_runtime->record_array_ndx++;
146
    }
147 148

    LUSTRE_UNLOCK();
149 150 151
    return;
}

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 182
static void lustre_runtime_initialize()
{
    int mem_limit;
    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));

183 184 185 186 187 188 189
    /* allocate array of Lustre records according to the amount of memory
     * assigned by Darshan
     */
    lustre_runtime->record_array_size = mem_limit / sizeof(struct darshan_lustre_record);

    lustre_runtime->record_array = malloc(lustre_runtime->record_array_size *
                                          sizeof(struct darshan_lustre_record));
190
    if(!lustre_runtime->record_array)
191 192 193 194 195 196
    {
        lustre_runtime->record_array_size = 0;
        return;
    }
    memset(lustre_runtime->record_array, 0, lustre_runtime->record_array_size *
        sizeof(struct darshan_lustre_record));
197

198 199 200 201 202 203 204 205 206 207
    lustre_runtime->record_runtime_array = malloc(lustre_runtime->record_array_size *
                                          sizeof(struct lustre_record_runtime));
    if(!lustre_runtime->record_runtime_array)
    {
        lustre_runtime->record_array_size = 0;
        return;
    }
    memset(lustre_runtime->record_runtime_array, 0, lustre_runtime->record_array_size *
        sizeof(struct lustre_record_runtime));

208 209 210
    return;
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/**************************************************************************
 * 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)
{
234
    struct lustre_record_runtime *file;
235
    int i;
236 237
    struct darshan_lustre_record *red_send_buf = NULL;
    struct darshan_lustre_record *red_recv_buf = NULL;
238 239 240
    MPI_Datatype red_type;
    MPI_Op red_op;

241 242
    assert(lustre_runtime);

243 244 245
    /* 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.
246
     */
247 248 249 250 251
    if(shared_rec_count && !getenv("DARSHAN_DISABLE_SHARED_REDUCTION"))
    {
        /* necessary initialization of shared records */
        for(i = 0; i < shared_rec_count; i++)
        {
252
            HASH_FIND(hlink, lustre_runtime->record_hash, &shared_recs[i],
253 254 255
                sizeof(darshan_record_id), file);
            assert(file);

256
            file->record->rank = -1;
257 258 259 260 261 262
        }

        /* 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
         */
263 264
        qsort(lustre_runtime->record_array, lustre_runtime->record_array_ndx,
            sizeof(struct darshan_lustre_record), lustre_record_compare);
265 266 267

        /* make *send_buf point to the shared files at the end of sorted array */
        red_send_buf =
268
            &(lustre_runtime->record_array[lustre_runtime->record_array_ndx-shared_rec_count]);
269 270 271 272

        /* allocate memory for the reduction output on rank 0 */
        if(my_rank == 0)
        {
273
            red_recv_buf = malloc(shared_rec_count * sizeof(struct darshan_lustre_record));
274 275 276 277 278 279
            if(!red_recv_buf)
            {
                return;
            }
        }

280
        DARSHAN_MPI_CALL(PMPI_Type_contiguous)(sizeof(struct darshan_lustre_record),
281 282
            MPI_BYTE, &red_type);
        DARSHAN_MPI_CALL(PMPI_Type_commit)(&red_type);
283
        DARSHAN_MPI_CALL(PMPI_Op_create)(lustre_record_reduction_op, 1, &red_op);
284 285 286 287 288 289
        DARSHAN_MPI_CALL(PMPI_Reduce)(red_send_buf, red_recv_buf,
            shared_rec_count, red_type, red_op, 0, mod_comm);

        /* clean up reduction state */
        if(my_rank == 0)
        {
290 291 292
            int tmp_ndx = lustre_runtime->record_array_ndx - shared_rec_count;
            memcpy(&(lustre_runtime->record_array[tmp_ndx]), red_recv_buf,
                shared_rec_count * sizeof(struct darshan_lustre_record));
293 294 295 296
            free(red_recv_buf);
        }
        else
        {
297
            lustre_runtime->record_array_ndx -= shared_rec_count;
298 299 300 301 302
        }

        DARSHAN_MPI_CALL(PMPI_Type_free)(&red_type);
        DARSHAN_MPI_CALL(PMPI_Op_free)(&red_op);
    }
303

304 305 306
    *lustre_buf = (void *)(lustre_runtime->record_array);
    *lustre_buf_sz = lustre_runtime->record_array_ndx * sizeof(struct darshan_lustre_record);

307 308 309 310 311 312 313
    return;
}

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

314
    HASH_CLEAR(hlink, lustre_runtime->record_hash);
315
    free(lustre_runtime->record_array);
316
    free(lustre_runtime->record_runtime_array);
317
    free(lustre_runtime);
318 319 320 321 322
    lustre_runtime = NULL;

    return;
}

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
/* compare function for sorting file records by descending rank */
static int lustre_record_compare(const void* a_p, const void* b_p)
{
    const struct darshan_lustre_record* a = a_p;
    const struct darshan_lustre_record* b = b_p;

    if(a->rank < b->rank)
        return 1;
    if(a->rank > b->rank)
        return -1;

    return 0;
}

/* 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;
}

376 377 378 379 380 381 382 383
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */