darshan-core.c 6.8 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 "darshan-runtime-config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
17
#include <pthread.h>
18 19

#include <mpi.h>
20

21 22 23 24
#include "darshan-core.h"

extern char* __progname_full;

25 26 27
static void darshan_core_initialize(int *argc, char ***argv);
static void darshan_core_shutdown(void);

28
/* internal variables */
29 30
static struct darshan_core_job_runtime *darshan_global_job = NULL;
static pthread_mutex_t darshan_mutex = PTHREAD_MUTEX_INITIALIZER;
31

32 33 34
#define DARSHAN_LOCK() pthread_mutex_lock(&darshan_mutex)
#define DARSHAN_UNLOCK() pthread_mutex_unlock(&darshan_mutex)

35 36 37 38 39 40 41 42 43 44 45
/* intercept MPI initialize and finalize to initialize darshan */
int MPI_Init(int *argc, char ***argv)
{
    int ret;

    ret = DARSHAN_MPI_CALL(PMPI_Init)(argc, argv);
    if(ret != MPI_SUCCESS)
    {
        return(ret);
    }

46
    darshan_core_initialize(argc, argv);
47 48 49 50

    return(ret);
}

51
int MPI_Init_thread(int *argc, char ***argv, int required, int *provided)
52 53 54 55 56 57 58 59 60
{
    int ret;

    ret = DARSHAN_MPI_CALL(PMPI_Init_thread)(argc, argv, required, provided);
    if (ret != MPI_SUCCESS)
    {
        return(ret);
    }

61
    darshan_core_initialize(argc, argv);
62 63 64 65 66 67 68 69

    return(ret);
}

int MPI_Finalize(void)
{
    int ret;

70
    darshan_core_shutdown();
71 72 73 74 75

    ret = DARSHAN_MPI_CALL(PMPI_Finalize)();
    return(ret);
}

76
static void darshan_core_initialize(int *argc, char ***argv)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
{
    int i;
    int nprocs;
    int rank;
    int internal_timing_flag = 0;
    double init_start, init_time, init_max;
    char* truncate_string = "<TRUNCATED>";
    int truncate_offset;
    int chars_left = 0;

    DARSHAN_MPI_CALL(PMPI_Comm_size)(MPI_COMM_WORLD, &nprocs);
    DARSHAN_MPI_CALL(PMPI_Comm_rank)(MPI_COMM_WORLD, &rank);

    if(getenv("DARSHAN_INTERNAL_TIMING"))
        internal_timing_flag = 1;

    if (internal_timing_flag)
        init_start = DARSHAN_MPI_CALL(PMPI_Wtime)();

    /* setup darshan runtime if darshan is enabled and hasn't been initialized already */
    if (!getenv("DARSHAN_DISABLE") && !darshan_global_job)
    {
        /* allocate structure to track darshan_global_job information */
        darshan_global_job = malloc(sizeof(*darshan_global_job));
        if (darshan_global_job)
        {
            memset(darshan_global_job, 0, sizeof(*darshan_global_job));

            if (getenv("DARSHAN_DISABLE_TIMING"))
            {
                darshan_global_job->flags |= CP_FLAG_NOTIMING;
            }

            strcpy(darshan_global_job->log_job.version_string, CP_VERSION);
            darshan_global_job->log_job.magic_nr = CP_MAGIC_NR;
            darshan_global_job->log_job.uid = getuid();
            darshan_global_job->log_job.start_time = time(NULL);
            darshan_global_job->log_job.nprocs = nprocs;
            darshan_global_job->wtime_offset = DARSHAN_MPI_CALL(PMPI_Wtime)();
116
            darshan_global_job->mod_list_head = NULL;
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

            /* record exe and arguments */
            for(i=0; i<(*argc); i++)
            {
                chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
                strncat(darshan_global_job->exe, *(argv[i]), chars_left);
                if(i < ((*argc)-1))
                {
                    chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
                    strncat(darshan_global_job->exe, " ", chars_left);
                }
            }

            /* if we don't see any arguments, then use glibc symbol to get
             * program name at least (this happens in fortran)
             */
            if(argc == 0)
            {
                chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
                strncat(darshan_global_job->exe, __progname_full, chars_left);
                chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
                strncat(darshan_global_job->exe, " <unknown args>", chars_left);
            }

            if(chars_left == 0)
            {
                /* we ran out of room; mark that string was truncated */
                truncate_offset = CP_EXE_LEN - strlen(truncate_string);
                sprintf(&darshan_global_job->exe[truncate_offset], "%s",
                    truncate_string);
            }
        }
    }

    if(internal_timing_flag)
    {
        init_time = DARSHAN_MPI_CALL(PMPI_Wtime)() - init_start;
        DARSHAN_MPI_CALL(PMPI_Reduce)(&init_time, &init_max, 1,
            MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
        if(rank == 0)
        {
            printf("#darshan:<op>\t<nprocs>\t<time>\n");
            printf("darshan:init\t%d\t%f\n", nprocs, init_max);
        }
    }

    return;
}

166
static void darshan_core_shutdown()
167 168 169 170 171 172 173 174 175 176 177
{
    int internal_timing_flag = 0;

    if(getenv("DARSHAN_INTERNAL_TIMING"))
        internal_timing_flag = 1;

    printf("darshan SHUTDOWN\n");

    return;
}

178
/* ********************************************************* */
179 180 181 182 183 184

void darshan_core_register_module(
    char *name,
    struct darshan_module_funcs *funcs,
    int *runtime_mem_limit)
{
185 186 187
    struct darshan_core_module *tmp;
    struct darshan_core_module *new_mod;

188
    *runtime_mem_limit = 0;
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
    if (!darshan_global_job)
        return;

    DARSHAN_LOCK();
    tmp = darshan_global_job->mod_list_head;
    while(tmp)
    {
        /* silently return if this module is already registered */
        if (strcmp(tmp->name, name) == 0)
        {
            DARSHAN_UNLOCK();
            return;
        }
        tmp = tmp->next_mod;
    }

    /* allocate new module and add to the head of the linked list of darshan modules */
    new_mod = malloc(sizeof(*new_mod));
    if (!new_mod)
    {
        DARSHAN_UNLOCK();
        return;
    }

    memset(new_mod, 0, sizeof(*new_mod));
    strncpy(new_mod->name, name, DARSHAN_MOD_NAME_LEN);
    new_mod->mod_funcs = *funcs;
    new_mod->next_mod = darshan_global_job->mod_list_head;
    darshan_global_job->mod_list_head = new_mod;
    DARSHAN_UNLOCK();
219

220 221
    /* TODO: something smarter than just 2 MiB per module */
    *runtime_mem_limit = 2 * 1024 * 1024;
222 223 224 225 226 227 228 229 230 231

    return;
}

void darshan_core_lookup_id(
    void *name,
    int len,
    int printable_flag,
    darshan_file_id *id)
{
232
    darshan_file_id tmp_id;
233

234 235 236 237 238 239 240 241 242 243 244
    if (!darshan_global_job)
        return;

    /* TODO: what do you do with printable flag? */

    /* hash the input name to get a unique id for this record */
    tmp_id = darshan_hash(name, len, 0);
    
    /* TODO: how to store the filename to hash mapping? */

    *id = tmp_id;
245 246 247
    return;
}

248 249 250 251 252 253 254 255 256
double darshan_core_wtime()
{
    if(!darshan_global_job || darshan_global_job->flags & CP_FLAG_NOTIMING)
    {
        return(0);
    }

    return DARSHAN_MPI_CALL(PMPI_Wtime)();
}
257 258 259 260 261 262 263 264 265

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