darshan-hdf5.c 5.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  (C) 2009 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include "mpi.h"
#include "darshan.h"
#include "darshan-config.h"

/* hope this doesn't change any time soon */
typedef int hid_t; 
typedef int herr_t;

17 18
#ifdef DARSHAN_PRELOAD

19 20 21 22
#define __USE_GNU
#include <dlfcn.h>
#include <stdlib.h>

23 24 25 26 27
#define DARSHAN_FORWARD_DECL(name,ret,args) \
  ret (*__real_ ## name)args = NULL;

#define DARSHAN_DECL(__name) __name

28 29 30 31 32 33 34 35 36 37
#define MAP_OR_FAIL(func) \
    if (!(__real_ ## func)) \
    { \
        __real_ ## func = dlsym(RTLD_NEXT, #func); \
        if(!(__real_ ## func)) { \
            fprintf(stderr, "Darshan failed to map symbol: %s\n", #func); \
            exit(1); \
        } \
    }

38 39 40 41 42 43 44
#else

#define DARSHAN_FORWARD_DECL(name,ret,args) \
  extern ret __real_ ## name args;

#define DARSHAN_DECL(__name) __wrap_ ## __name

45 46
#define MAP_OR_FAIL(func) 

47 48 49 50 51 52
#endif

DARSHAN_FORWARD_DECL(H5Fcreate, hid_t, (const char *filename, unsigned flags, hid_t create_plist, hid_t access_plist));
DARSHAN_FORWARD_DECL(H5Fopen, hid_t, (const char *filename, unsigned flags, hid_t access_plist));
DARSHAN_FORWARD_DECL(H5Fclose, herr_t, (hid_t file_id));

53 54
static struct darshan_file_runtime* darshan_file_by_hid(int hid);

55
hid_t DARSHAN_DECL(H5Fcreate)(const char *filename, unsigned flags,
56 57 58 59 60 61 62
    hid_t create_plist, hid_t access_plist)
{
    int ret;
    struct darshan_file_runtime* file;
    char* tmp;
    int hash_index;

63 64
    MAP_OR_FAIL(H5Fcreate);

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    ret = __real_H5Fcreate(filename, flags, create_plist, access_plist);
    if(ret >= 0)
    {  
        CP_LOCK();
        /* use ROMIO approach to strip prefix if present */
        /* strip off prefix if there is one, but only skip prefixes
         * if they are greater than length one to allow for windows
         * drive specifications (e.g. c:\...) 
         */
        tmp = strchr(filename, ':');
        if (tmp > filename + 1) {
            filename = tmp + 1;
        }

        file = darshan_file_by_name(filename);
        /* TODO: handle the case of multiple concurrent opens */
        if(file && (file->hid == -1))
        {
            file->hid = ret;
            CP_INC(file, CP_HDF5_OPENS, 1);
            hash_index = file->hid & CP_HASH_MASK;
            file->hid_prev = NULL;
            file->hid_next = darshan_global_job->hid_table[hash_index];
            if(file->hid_next) 
                file->hid_next->hid_prev = file;
            darshan_global_job->hid_table[hash_index] = file;
        }
        CP_UNLOCK();
    }

    return(ret);
}

98
hid_t DARSHAN_DECL(H5Fopen)(const char *filename, unsigned flags,
99 100 101 102 103 104 105
    hid_t access_plist)
{
    int ret;
    struct darshan_file_runtime* file;
    char* tmp;
    int hash_index;

106 107
    MAP_OR_FAIL(H5Fopen);

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    ret = __real_H5Fopen(filename, flags, access_plist);
    if(ret >= 0)
    {  
        CP_LOCK();
        /* use ROMIO approach to strip prefix if present */
        /* strip off prefix if there is one, but only skip prefixes
         * if they are greater than length one to allow for windows
         * drive specifications (e.g. c:\...) 
         */
        tmp = strchr(filename, ':');
        if (tmp > filename + 1) {
            filename = tmp + 1;
        }

        file = darshan_file_by_name(filename);
        /* TODO: handle the case of multiple concurrent opens */
        if(file && (file->hid == -1))
        {
            file->hid = ret;
            CP_INC(file, CP_HDF5_OPENS, 1);
            hash_index = file->hid & CP_HASH_MASK;
            file->hid_prev = NULL;
            file->hid_next = darshan_global_job->hid_table[hash_index];
            if(file->hid_next) 
                file->hid_next->hid_prev = file;
            darshan_global_job->hid_table[hash_index] = file;
        }

        CP_UNLOCK();
    }

    return(ret);

}

143
herr_t DARSHAN_DECL(H5Fclose)(hid_t file_id)
144 145 146 147 148 149
{
    struct darshan_file_runtime* file;
    int hash_index;
    int tmp_hid = file_id;
    int ret;

150 151
    MAP_OR_FAIL(H5Fclose);

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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
    ret = __real_H5Fclose(file_id);

    CP_LOCK();
    file = darshan_file_by_hid(file_id);
    if(file)
    {
        file->hid = -1;
        if(file->hid_prev == NULL)
        {
            /* head of hid hash table list */
            hash_index = tmp_hid & CP_HASH_MASK;
            darshan_global_job->hid_table[hash_index] = file->hid_next;
            if(file->hid_next)
                file->hid_next->hid_prev = NULL;
        }
        else
        {
            if(file->hid_prev)
                file->hid_prev->hid_next = file->hid_next;
            if(file->hid_next)
                file->hid_next->hid_prev = file->hid_prev;
        }
        file->hid_prev = NULL;
        file->hid_next = NULL;
        darshan_global_job->darshan_mru_file = file; /* in case we open it again */
    }
    CP_UNLOCK();

    return(ret);

}

static struct darshan_file_runtime* darshan_file_by_hid(int hid)
{
    int hash_index;
    struct darshan_file_runtime* tmp_file;

    if(!darshan_global_job)
    {
        return(NULL);
    }

    /* if we have already condensed the data, then just hand the first file
     * back
     */
    if(darshan_global_job->flags & CP_FLAG_CONDENSED)
    {
        return(&darshan_global_job->file_runtime_array[0]);
    }

    /* try mru first */
    if(darshan_global_job->darshan_mru_file && darshan_global_job->darshan_mru_file->hid == hid)
    {
        return(darshan_global_job->darshan_mru_file);
    }

    /* search hash table */
    hash_index = hid & CP_HASH_MASK;
    tmp_file = darshan_global_job->hid_table[hash_index];
    while(tmp_file)
    {
        if(tmp_file->hid == hid)
        {
            darshan_global_job->darshan_mru_file = tmp_file;
            return(tmp_file);
        }
        tmp_file = tmp_file->hid_next;
    }

    return(NULL);
}


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