darshan-hdf5.c 4.44 KB
Newer Older
1 2 3 4 5
/*
 *  (C) 2009 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

6
#include "darshan-runtime-config.h"
7 8 9 10 11 12 13 14 15 16
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include "mpi.h"
#include "darshan.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
static struct darshan_file_runtime* darshan_file_by_hid(int hid);
54 55
static void darshan_file_close_hid(int hid);
static struct darshan_file_runtime* darshan_file_by_name_sethid(const char* name, int hid);
56

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

64 65
    MAP_OR_FAIL(H5Fcreate);

66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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;
        }

80 81
        file = darshan_file_by_name_sethid(filename, ret);
        if(file)
82
        {
83 84 85
            if(CP_F_VALUE(file, CP_F_OPEN_TIMESTAMP) == 0)
                CP_F_SET(file, CP_F_OPEN_TIMESTAMP,
                PMPI_Wtime());
86 87 88 89 90 91 92 93
            CP_INC(file, CP_HDF5_OPENS, 1);
        }
        CP_UNLOCK();
    }

    return(ret);
}

94
hid_t DARSHAN_DECL(H5Fopen)(const char *filename, unsigned flags,
95 96 97 98 99 100
    hid_t access_plist)
{
    int ret;
    struct darshan_file_runtime* file;
    char* tmp;

101 102
    MAP_OR_FAIL(H5Fopen);

103 104 105 106 107 108 109 110 111 112 113 114 115 116
    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;
        }

117 118
        file = darshan_file_by_name_sethid(filename, ret);
        if(file)
119
        {
120 121 122
            if(CP_F_VALUE(file, CP_F_OPEN_TIMESTAMP) == 0)
                CP_F_SET(file, CP_F_OPEN_TIMESTAMP,
                PMPI_Wtime());
123 124 125 126 127 128 129 130 131 132
            CP_INC(file, CP_HDF5_OPENS, 1);
        }

        CP_UNLOCK();
    }

    return(ret);

}

133
herr_t DARSHAN_DECL(H5Fclose)(hid_t file_id)
134 135 136 137
{
    struct darshan_file_runtime* file;
    int ret;

138 139
    MAP_OR_FAIL(H5Fclose);

140 141 142 143 144 145
    ret = __real_H5Fclose(file_id);

    CP_LOCK();
    file = darshan_file_by_hid(file_id);
    if(file)
    {
146
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, PMPI_Wtime());
147
        darshan_file_close_hid(file_id);
148 149 150 151 152 153 154
    }
    CP_UNLOCK();

    return(ret);

}

155
static struct darshan_file_runtime* darshan_file_by_name_sethid(const char* name, int hid)
156 157 158
{
    struct darshan_file_runtime* tmp_file;

159 160 161
    tmp_file = darshan_file_by_name_sethandle(name, &hid, sizeof(hid), DARSHAN_HID);
    return(tmp_file);
}
162

163 164 165 166 167
static void darshan_file_close_hid(int hid)
{
    darshan_file_closehandle(&hid, sizeof(hid), DARSHAN_HID);
    return;
}
168

169 170 171
static struct darshan_file_runtime* darshan_file_by_hid(int hid)
{
    struct darshan_file_runtime* tmp_file;
172

173 174 175
    tmp_file = darshan_file_by_handle(&hid, sizeof(hid), DARSHAN_HID);
    
    return(tmp_file);
176 177 178 179 180 181 182 183 184 185 186
}


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