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

6
#define _GNU_SOURCE
7

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
/* TODO is __progname_full needed here */
extern char* __progname;
30

31
/* internal variables */
32
static struct darshan_core_job_runtime *darshan_core_job = NULL;
33
static pthread_mutex_t darshan_mutex = PTHREAD_MUTEX_INITIALIZER;
34 35 36 37 38
static int my_rank = -1;

static void darshan_core_initialize(int *argc, char ***argv);
static void darshan_core_shutdown(void);
static void darshan_core_cleanup(struct darshan_core_job_runtime* job);
39

40 41 42
#define DARSHAN_LOCK() pthread_mutex_lock(&darshan_mutex)
#define DARSHAN_UNLOCK() pthread_mutex_unlock(&darshan_mutex)

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

52
/* intercept MPI initialize and finalize to manage darshan core runtime */
53 54 55 56 57 58 59 60 61 62
int MPI_Init(int *argc, char ***argv)
{
    int ret;

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

63
    darshan_core_initialize(argc, argv);
64 65 66 67

    return(ret);
}

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

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

78
    darshan_core_initialize(argc, argv);
79 80 81 82 83 84 85 86

    return(ret);
}

int MPI_Finalize(void)
{
    int ret;

87
    darshan_core_shutdown();
88 89 90 91 92

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

93 94
/* *********************************** */

95
static void darshan_core_initialize(int *argc, char ***argv)
96 97 98 99 100 101 102 103 104 105
{
    int i;
    int nprocs;
    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);
106
    DARSHAN_MPI_CALL(PMPI_Comm_rank)(MPI_COMM_WORLD, &my_rank);
107 108 109 110

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

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

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

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

128 129 130 131 132 133
            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)();
134 135 136 137

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

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

            if(chars_left == 0)
            {
                /* we ran out of room; mark that string was truncated */
                truncate_offset = CP_EXE_LEN - strlen(truncate_string);
162
                sprintf(&darshan_core_job->exe[truncate_offset], "%s",
163 164 165 166 167 168 169 170 171 172
                    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);
173
        if(my_rank == 0)
174 175 176 177 178 179 180 181 182
        {
            printf("#darshan:<op>\t<nprocs>\t<time>\n");
            printf("darshan:init\t%d\t%f\n", nprocs, init_max);
        }
    }

    return;
}

183
static void darshan_core_shutdown()
184
{
185 186
    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
    int jobid;
    char* jobid_str;
    char* envjobid;
    char* logpath;
    int ret;
194 195
    int local_ret = 0;
    int all_ret = 0;
196 197 198 199 200 201 202 203
    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
204 205
    int64_t first_start_time;
    int64_t last_end_time;
206 207 208 209

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

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    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;
    }

    /* construct log file name */
231
    if(my_rank == 0)
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 394 395
    {
        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);

396 397 398 399 400 401 402 403 404 405
    /* reduce to report first start time and last end time across all ranks
     * at rank 0
     */
    DARSHAN_MPI_CALL(PMPI_Reduce)(&final_job->log_job.start_time, &first_start_time, 1, MPI_LONG_LONG, MPI_MIN, 0, MPI_COMM_WORLD);
    DARSHAN_MPI_CALL(PMPI_Reduce)(&final_job->log_job.end_time, &last_end_time, 1, MPI_LONG_LONG, MPI_MAX, 0, MPI_COMM_WORLD);
    if(my_rank == 0)
    {
        final_job->log_job.start_time = first_start_time;
        final_job->log_job.end_time = last_end_time;
    }
406

407 408 409
    /* TODO: coordinate shutdown accross all registered modules */
    DARSHAN_MOD_ITER(mod, tmp, final_job)
    {
410

411
    }
412 413 414 415 416

    free(logfile_name);
    darshan_core_cleanup(final_job);

    if (internal_timing_flag)
417
    {
418 419 420 421 422
        /* TODO: what do we want to time in new darshan version? */
    }
    
    return;
}
423

424 425 426 427 428 429 430
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);
431
        free(mod);
432
    }
433

434
    free(job);
435 436 437 438

    return;
}

439 440 441 442 443
static int mod_cmp(struct darshan_core_module* a, struct darshan_core_module* b)
{
    return strcmp(a->name, b->name);
}

444
/* ********************************************************* */
445 446 447 448 449 450

void darshan_core_register_module(
    char *name,
    struct darshan_module_funcs *funcs,
    int *runtime_mem_limit)
{
451 452
    struct darshan_core_module tmp;
    struct darshan_core_module* mod;
453

454 455
    DARSHAN_LOCK();

456
    *runtime_mem_limit = 0;
457
    if(!darshan_core_job)
458 459
    {
        DARSHAN_UNLOCK();
460
        return;
461
    }
462 463 464

    /* see if this module is already registered */
    strncpy(tmp.name, name, DARSHAN_MOD_NAME_LEN);
465
    DARSHAN_MOD_SEARCH(mod, &tmp, darshan_core_job);
466
    if(mod)
467
    {
468 469 470 471 472 473
        /* 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;
474 475
    }

476 477 478
    /* this module has not been registered yet, allocate and register it */
    mod = malloc(sizeof(*mod));
    if(!mod)
479 480 481 482
    {
        DARSHAN_UNLOCK();
        return;
    }
483
    memset(mod, 0, sizeof(*mod));
484

485 486
    strncpy(mod->name, name, DARSHAN_MOD_NAME_LEN);
    mod->mod_funcs = *funcs;
487
    DARSHAN_MOD_REGISTER(mod, darshan_core_job);
488

489 490
    /* TODO: something smarter than just 2 MiB per module */
    *runtime_mem_limit = 2 * 1024 * 1024;
491

492 493
    DARSHAN_UNLOCK();

494 495 496 497 498 499 500 501 502
    return;
}

void darshan_core_lookup_id(
    void *name,
    int len,
    int printable_flag,
    darshan_file_id *id)
{
503
    darshan_file_id tmp_id;
504

505
    if(!darshan_core_job)
506 507 508 509 510 511 512 513 514 515
        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;
516 517 518
    return;
}

519 520
double darshan_core_wtime()
{
521
    /* TODO since NOTIMING is the only flag (currently), maybe we just drop 'flags' */
522
    if(!darshan_core_job || darshan_core_job->flags & CP_FLAG_NOTIMING)
523 524 525 526 527 528
    {
        return(0);
    }

    return DARSHAN_MPI_CALL(PMPI_Wtime)();
}
529 530 531 532 533 534 535 536 537

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