darshan-lustre.c 5.62 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
/*
 * 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>

#include "uthash.h"

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

25 26 27 28
/* 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).
29
 */
30 31 32 33 34 35 36 37 38 39 40 41
struct lustre_runtime
{
    struct darshan_lustre_record *record_array;
    int record_array_size;
    int record_array_ndx;
};

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;

42 43
static void lustre_runtime_initialize(void);

44 45 46 47 48 49 50 51
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);

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

52 53 54 55 56 57 58
/* TODO: is there any way we can further compact Lustre data to save space?
 * e.g., are all files in the same directory guaranteed same striping parameters?
 * if so, can we store stripe parameters on per-directory basis and the OST
 * list on a per-file basis? maybe the storage savings are small enough this isn't
 * worth it, but nice to keep in mind
 */

59 60
void darshan_instrument_lustre_file(char *filepath)
{
61
    struct darshan_lustre_record *rec;
62 63 64
    darshan_record_id rec_id;

    LUSTRE_LOCK();
65 66
    /* make sure the lustre module is already initialized */
    lustre_runtime_initialize();
67

68 69 70
    /* if the array is full, we just back out */
    if(lustre_runtime->record_array_ndx >= lustre_runtime->record_array_size)
        return;
71 72 73 74 75 76 77

    /* register a Lustre file record with Darshan */
    darshan_core_register_record(
        (void *)filepath,
        strlen(filepath),
        DARSHAN_LUSTRE_MOD,
        1,
78
        0,
79 80 81 82 83 84 85
        &rec_id,
        NULL);

    /* if record id is 0, darshan has no more memory for instrumenting */
    if(rec_id == 0)
        return;

86 87 88 89 90 91 92 93
    /* allocate a new lustre record and append it to the array */
    rec = &(lustre_runtime->record_array[lustre_runtime->record_array_ndx++]);
    rec->rec_id = rec_id;
    rec->rank = my_rank;

    /* TODO: gather lustre data, store in record hash */
    /* counters in lustre_ref->record->counters */
    rec->counters[LUSTRE_TEST_COUNTER] = 88;
94 95

    LUSTRE_UNLOCK();
96 97 98
    return;
}

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 127 128 129
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));

130 131 132 133 134 135 136
    /* 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));
137
    if(!lustre_runtime->record_array)
138 139 140 141 142 143
    {
        lustre_runtime->record_array_size = 0;
        return;
    }
    memset(lustre_runtime->record_array, 0, lustre_runtime->record_array_size *
        sizeof(struct darshan_lustre_record));
144 145 146 147

    return;
}

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
/**************************************************************************
 * 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)
{
    assert(lustre_runtime);

173 174 175 176 177
    /* TODO: determine lustre record shared across all processes,
     * and have only rank 0 write these records out. No shared 
     * reductions should be necessary as the Lustre data for a
     * given file should be the same on each process
     */
178

179 180 181
    *lustre_buf = (void *)(lustre_runtime->record_array);
    *lustre_buf_sz = lustre_runtime->record_array_ndx * sizeof(struct darshan_lustre_record);

182 183 184 185 186 187 188 189
    return;
}

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

    /* TODO: free data structures */
190 191
    free(lustre_runtime->record_array);
    free(lustre_runtime);
192 193 194 195 196 197 198 199 200 201 202 203 204
    lustre_runtime = NULL;

    return;
}

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