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

6 7
#define _XOPEN_SOURCE 500

8 9 10 11 12 13
#include "darshan-runtime-config.h"

#include <stdio.h>
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
14 15 16 17 18
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <pthread.h>
19 20 21 22 23
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>

#include <mpi.h>
24

25
#include "darshan-core.h"
26
#include "utlist.h"
27 28 29

extern char* __progname_full;

30 31
static void darshan_core_initialize(int *argc, char ***argv);
static void darshan_core_shutdown(void);
32
static void darshan_core_cleanup(struct darshan_core_job_runtime* job);
33

34
/* internal variables */
35
static struct darshan_core_job_runtime *darshan_core_job = NULL;
36
static pthread_mutex_t darshan_mutex = PTHREAD_MUTEX_INITIALIZER;
37

38 39 40
#define DARSHAN_LOCK() pthread_mutex_lock(&darshan_mutex)
#define DARSHAN_UNLOCK() pthread_mutex_unlock(&darshan_mutex)

41 42 43 44 45 46 47 48
#define DARSHAN_MOD_REGISTER(__mod, __job) \
    LL_PREPEND(__job->mod_list_head, __mod)
#define DARSHAN_MOD_SEARCH(__mod, __tmp, __job) \
    LL_SEARCH(__job->mod_list_head, __mod, __tmp, mod_cmp)
#define DARSHAN_MOD_ITER(__mod, __tmp, __job) \
    LL_FOREACH_SAFE(__job->mod_list_head, __mod, __tmp)
#define DARSHAN_MOD_DELETE(__mod, __job) \
   LL_DELETE(__job->mod_list_head, __mod)
49

50 51 52 53 54 55 56 57 58 59 60
/* 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);
    }

61
    darshan_core_initialize(argc, argv);
62 63 64 65

    return(ret);
}

66
int MPI_Init_thread(int *argc, char ***argv, int required, int *provided)
67 68 69 70
{
    int ret;

    ret = DARSHAN_MPI_CALL(PMPI_Init_thread)(argc, argv, required, provided);
71
    if(ret != MPI_SUCCESS)
72 73 74 75
    {
        return(ret);
    }

76
    darshan_core_initialize(argc, argv);
77 78 79 80 81 82 83 84

    return(ret);
}

int MPI_Finalize(void)
{
    int ret;

85
    darshan_core_shutdown();
86 87 88 89 90

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

91 92
/* *********************************** */

93
static void darshan_core_initialize(int *argc, char ***argv)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
    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;

110
    if(internal_timing_flag)
111 112 113
        init_start = DARSHAN_MPI_CALL(PMPI_Wtime)();

    /* setup darshan runtime if darshan is enabled and hasn't been initialized already */
114
    if(!getenv("DARSHAN_DISABLE") && !darshan_core_job)
115
    {
116 117 118
        /* allocate structure to track darshan_core_job information */
        darshan_core_job = malloc(sizeof(*darshan_core_job));
        if(darshan_core_job)
119
        {
120
            memset(darshan_core_job, 0, sizeof(*darshan_core_job));
121

122
            if(getenv("DARSHAN_DISABLE_TIMING"))
123
            {
124
                darshan_core_job->flags |= CP_FLAG_NOTIMING;
125 126
            }

127 128 129 130 131 132
            strcpy(darshan_core_job->log_job.version_string, CP_VERSION);
            darshan_core_job->log_job.magic_nr = CP_MAGIC_NR;
            darshan_core_job->log_job.uid = getuid();
            darshan_core_job->log_job.start_time = time(NULL);
            darshan_core_job->log_job.nprocs = nprocs;
            darshan_core_job->wtime_offset = DARSHAN_MPI_CALL(PMPI_Wtime)();
133 134 135 136

            /* record exe and arguments */
            for(i=0; i<(*argc); i++)
            {
137 138
                chars_left = CP_EXE_LEN-strlen(darshan_core_job->exe);
                strncat(darshan_core_job->exe, *(argv[i]), chars_left);
139 140
                if(i < ((*argc)-1))
                {
141 142
                    chars_left = CP_EXE_LEN-strlen(darshan_core_job->exe);
                    strncat(darshan_core_job->exe, " ", chars_left);
143 144 145 146 147 148 149 150
                }
            }

            /* if we don't see any arguments, then use glibc symbol to get
             * program name at least (this happens in fortran)
             */
            if(argc == 0)
            {
151 152 153 154
                chars_left = CP_EXE_LEN-strlen(darshan_core_job->exe);
                strncat(darshan_core_job->exe, __progname_full, chars_left);
                chars_left = CP_EXE_LEN-strlen(darshan_core_job->exe);
                strncat(darshan_core_job->exe, " <unknown args>", chars_left);
155 156 157 158 159 160
            }

            if(chars_left == 0)
            {
                /* we ran out of room; mark that string was truncated */
                truncate_offset = CP_EXE_LEN - strlen(truncate_string);
161
                sprintf(&darshan_core_job->exe[truncate_offset], "%s",
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
                    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;
}

182
static void darshan_core_shutdown()
183
{
184 185 186
    int rank;
    char *logfile_name;
    struct darshan_core_job_runtime* final_job;
187
    struct darshan_core_module *mod, *tmp;
188
    int internal_timing_flag = 0;
189 190 191 192 193 194 195 196 197 198 199 200 201
    int jobid;
    char* jobid_str;
    char* envjobid;
    char* logpath;
    int ret;
    uint64_t hlevel;
    char hname[HOST_NAME_MAX];
    uint64_t logmod;
    char* logpath_override = NULL;
#ifdef __CP_LOG_ENV
    char env_check[256];
    char* env_tok;
#endif
202 203 204 205

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

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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    DARSHAN_LOCK();
    if(!darshan_core_job)
    {
        DARSHAN_UNLOCK();
        return;
    }
    /* disable further tracing while hanging onto the data so that we can
     * write it out
     */
    final_job = darshan_core_job;
    darshan_core_job = NULL;
    DARSHAN_UNLOCK();

    logfile_name = malloc(PATH_MAX);
    if(!logfile_name)
    {
        darshan_core_cleanup(final_job);
        return;
    }

    DARSHAN_MPI_CALL(PMPI_Comm_rank)(MPI_COMM_WORLD, &rank);

    /* construct log file name */
    if(rank == 0)
    {
        char cuser[L_cuserid] = {0};
        struct tm* my_tm;
        time_t start_time_tmp;

        /* Use CP_JOBID_OVERRIDE for the env var or CP_JOBID */
        envjobid = getenv(CP_JOBID_OVERRIDE);
        if (!envjobid)
        {
            envjobid = CP_JOBID;
        }

        /* Use CP_LOG_PATH_OVERRIDE for the value or __CP_LOG_PATH */
        logpath = getenv(CP_LOG_PATH_OVERRIDE);
        if (!logpath)
        {
#ifdef __CP_LOG_PATH
            logpath = __CP_LOG_PATH;
#endif
        }

        /* find a job id */
        jobid_str = getenv(envjobid);
        if(jobid_str)
        {
            /* in cobalt we can find it in env var */
            ret = sscanf(jobid_str, "%d", &jobid);
        }
        if(!jobid_str || ret != 1)
        {
            /* use pid as fall back */
            jobid = getpid();
        }

        /* break out time into something human readable */
        start_time_tmp = final_job->log_job.start_time;
        my_tm = localtime(&start_time_tmp);

        /* get the username for this job.  In order we will try each of the
         * following until one of them succeeds:
         *
         * - cuserid()
         * - getenv("LOGNAME")
         * - snprintf(..., geteuid());
         *
         * Note that we do not use getpwuid() because it generally will not
         * work in statically compiled binaries.
         */

#ifndef DARSHAN_DISABLE_CUSERID
        cuserid(cuser);
#endif

        /* if cuserid() didn't work, then check the environment */
        if (strcmp(cuser, "") == 0)
        {
            char* logname_string;
            logname_string = getenv("LOGNAME");
            if(logname_string)
            {
                strncpy(cuser, logname_string, (L_cuserid-1));
            }

        }

        /* if cuserid() and environment both fail, then fall back to uid */
        if (strcmp(cuser, "") == 0)
        {
            uid_t uid = geteuid();
            snprintf(cuser, sizeof(cuser), "%u", uid);
        }

        /* generate a random number to help differentiate the log */
        hlevel=DARSHAN_MPI_CALL(PMPI_Wtime)() * 1000000;
        (void) gethostname(hname, sizeof(hname));
        logmod = darshan_hash((void*)hname,strlen(hname),hlevel);

        /* see if darshan was configured using the --with-logpath-by-env
         * argument, which allows the user to specify an absolute path to
         * place logs via an env variable.
         */
#ifdef __CP_LOG_ENV
        /* just silently skip if the environment variable list is too big */
        if(strlen(__CP_LOG_ENV) < 256)
        {
            /* copy env variable list to a temporary buffer */
            strcpy(env_check, __CP_LOG_ENV);
            /* tokenize the comma-separated list */
            env_tok = strtok(env_check, ",");
            if(env_tok)
            {
                do
                {
                    /* check each env variable in order */
                    logpath_override = getenv(env_tok);
                    if(logpath_override)
                    {
                        /* stop as soon as we find a match */
                        break;
                    }
                }while((env_tok = strtok(NULL, ",")));
            }
        }
#endif

        if(logpath_override)
        {
            ret = snprintf(logfile_name, PATH_MAX,
                "%s/%s_%s_id%d_%d-%d-%d-%" PRIu64 ".darshan_partial",
                logpath_override,
                cuser, __progname, jobid,
                (my_tm->tm_mon+1),
                my_tm->tm_mday,
                (my_tm->tm_hour*60*60 + my_tm->tm_min*60 + my_tm->tm_sec),
                logmod);
            if(ret == (PATH_MAX-1))
            {
                /* file name was too big; squish it down */
                snprintf(logfile_name, PATH_MAX,
                    "%s/id%d.darshan_partial",
                    logpath_override, jobid);
            }
        }
        else if(logpath)
        {
            ret = snprintf(logfile_name, PATH_MAX,
                "%s/%d/%d/%d/%s_%s_id%d_%d-%d-%d-%" PRIu64 ".darshan_partial",
                logpath, (my_tm->tm_year+1900),
                (my_tm->tm_mon+1), my_tm->tm_mday,
                cuser, __progname, jobid,
                (my_tm->tm_mon+1),
                my_tm->tm_mday,
                (my_tm->tm_hour*60*60 + my_tm->tm_min*60 + my_tm->tm_sec),
                logmod);
            if(ret == (PATH_MAX-1))
            {
                /* file name was too big; squish it down */
                snprintf(logfile_name, PATH_MAX,
                    "%s/id%d.darshan_partial",
                    logpath, jobid);
            }
        }
        else
        {
            logfile_name[0] = '\0';
        }

        /* add jobid */
        final_job->log_job.jobid = (int64_t)jobid;
    }

    /* broadcast log file name */
    DARSHAN_MPI_CALL(PMPI_Bcast)(logfile_name, PATH_MAX, MPI_CHAR, 0,
        MPI_COMM_WORLD);

    if(strlen(logfile_name) == 0)
    {
        /* failed to generate log file name */
        darshan_core_cleanup(final_job);
        return;
    }

    final_job->log_job.end_time = time(NULL);

394
    /* TODO: coordinate shutdown accross all registered modules */
395 396 397 398 399 400 401



    free(logfile_name);
    darshan_core_cleanup(final_job);

    if (internal_timing_flag)
402
    {
403 404 405 406 407
        /* TODO: what do we want to time in new darshan version? */
    }
    
    return;
}
408

409 410 411 412 413 414 415
static void darshan_core_cleanup(struct darshan_core_job_runtime* job)
{
    struct darshan_core_module *mod, *tmp;

    DARSHAN_MOD_ITER(mod, tmp, job)
    {
        DARSHAN_MOD_DELETE(mod, job);
416 417 418
        free(mod);
    };

419
    free(job);
420 421 422 423

    return;
}

424 425 426 427 428
static int mod_cmp(struct darshan_core_module* a, struct darshan_core_module* b)
{
    return strcmp(a->name, b->name);
}

429
/* ********************************************************* */
430 431 432 433 434 435

void darshan_core_register_module(
    char *name,
    struct darshan_module_funcs *funcs,
    int *runtime_mem_limit)
{
436 437
    struct darshan_core_module tmp;
    struct darshan_core_module* mod;
438

439 440
    DARSHAN_LOCK();

441
    *runtime_mem_limit = 0;
442
    if(!darshan_core_job)
443 444
    {
        DARSHAN_UNLOCK();
445
        return;
446
    }
447 448 449

    /* see if this module is already registered */
    strncpy(tmp.name, name, DARSHAN_MOD_NAME_LEN);
450
    DARSHAN_MOD_SEARCH(mod, &tmp, darshan_core_job);
451
    if(mod)
452
    {
453 454 455 456 457 458
        /* if module is already registered, update module_funcs and return */
        /* NOTE: we do not recalculate memory limit here, just set to 0 */
        mod->mod_funcs = *funcs;

        DARSHAN_UNLOCK();
        return;
459 460
    }

461 462 463
    /* this module has not been registered yet, allocate and register it */
    mod = malloc(sizeof(*mod));
    if(!mod)
464 465 466 467
    {
        DARSHAN_UNLOCK();
        return;
    }
468
    memset(mod, 0, sizeof(*mod));
469

470 471
    strncpy(mod->name, name, DARSHAN_MOD_NAME_LEN);
    mod->mod_funcs = *funcs;
472
    DARSHAN_MOD_REGISTER(mod, darshan_core_job);
473

474 475
    /* TODO: something smarter than just 2 MiB per module */
    *runtime_mem_limit = 2 * 1024 * 1024;
476

477 478
    DARSHAN_UNLOCK();

479 480 481 482 483 484 485 486 487
    return;
}

void darshan_core_lookup_id(
    void *name,
    int len,
    int printable_flag,
    darshan_file_id *id)
{
488
    darshan_file_id tmp_id;
489

490
    if(!darshan_core_job)
491 492 493 494 495 496 497 498 499 500
        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;
501 502 503
    return;
}

504 505
double darshan_core_wtime()
{
506
    if(!darshan_core_job || darshan_core_job->flags & CP_FLAG_NOTIMING)
507 508 509 510 511 512
    {
        return(0);
    }

    return DARSHAN_MPI_CALL(PMPI_Wtime)();
}
513 514 515 516 517 518 519 520 521

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