darshan-common.c 4.68 KB
Newer Older
1
/*
Shane Snyder's avatar
Shane Snyder committed
2 3 4
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
5
 */
6 7
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE
8 9 10 11 12 13 14

#include "darshan-runtime-config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
15 16
#include <search.h>
#include <assert.h>
17 18 19

#include "darshan.h"

20 21 22 23
static int darshan_common_val_compare(const void* a_p, const void* b_p);
static void darshan_common_val_walker(const void* nodep, const VISIT which,
    const int depth);

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
char* darshan_clean_file_path(const char* path)
{
    char* newpath = NULL;
    char* cwd = NULL;
    char* filter = NULL;

    if(!path || strlen(path) < 1)
        return(NULL);

    if(path[0] == '/')
    {
        /* it is already an absolute path */
        newpath = malloc(strlen(path)+1);
        if(newpath)
        {
            strcpy(newpath, path);
        }
    }
    else
    {
        /* handle relative path */
        cwd = malloc(PATH_MAX);
        if(cwd)
        {
            if(getcwd(cwd, PATH_MAX))
            {
                newpath = malloc(strlen(path) + strlen(cwd) + 2);
                if(newpath)
                {
                    sprintf(newpath, "%s/%s", cwd, path);
                }
            }
            free(cwd);
        }
    }

    if(!newpath)
        return(NULL);

    /* filter out any double slashes */
    while((filter = strstr(newpath, "//")))
    {
        /* shift down one character */
        memmove(filter, &filter[1], (strlen(&filter[1]) + 1));
    }

    /* filter out any /./ instances */
    while((filter = strstr(newpath, "/./")))
    {
        /* shift down two characters */
        memmove(filter, &filter[2], (strlen(&filter[2]) + 1));
    }

    /* return result */
    return(newpath);
}

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 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/* HACK: global variables for determining 4 most common values */
static int64_t* walker_val_p = NULL;
static int64_t* walker_cnt_p = NULL;

void darshan_common_val_counter(void **common_val_root, int *common_val_count,
    int64_t val)
{
    struct darshan_common_val_counter* counter;
    struct darshan_common_val_counter* found;
    struct darshan_common_val_counter tmp_counter;
    void* tmp;

    /* don't count any values of 0 */
    if(val == 0)
        return;

    /* check to see if this val is already recorded */
    tmp_counter.val = val;
    tmp_counter.freq = 1;
    tmp = tfind(&tmp_counter, common_val_root, darshan_common_val_compare);
    if(tmp)
    {
        found = *(struct darshan_common_val_counter**)tmp;
        found->freq++;
        return;
    }

    /* we can add a new one as long as we haven't hit the limit */
    if(*common_val_count < DARSHAN_COMMON_VAL_MAX_RUNTIME_COUNT)
    {
        counter = malloc(sizeof(*counter));
        if(!counter)
        {
            return;
        }

        counter->val = val;
        counter->freq = 1;

        tmp = tsearch(counter, common_val_root, darshan_common_val_compare);
        found = *(struct darshan_common_val_counter**)tmp;
        /* if we get a new answer out here we are in trouble; this was
         * already checked with the tfind()
         */
        assert(found == counter);

        (*common_val_count)++;
    }

    return;
}

void darshan_walk_common_vals(void *common_val_root, int64_t* val_p,
    int64_t* cnt_p)
{
    walker_val_p = val_p;
    walker_cnt_p = cnt_p;

    twalk(common_val_root, darshan_common_val_walker);
    tdestroy(common_val_root, free);

    return;
}

static void darshan_common_val_walker(const void* nodep, const VISIT which,
    const int depth)
{
    struct darshan_common_val_counter* counter;

    switch (which)
    {
        case postorder:
        case leaf:
            counter = *(struct darshan_common_val_counter**)nodep;
            DARSHAN_COMMON_VAL_COUNTER_INC(walker_val_p, walker_cnt_p,
                counter->val, counter->freq);
        default:
            break;
    }

    return;
}

static int darshan_common_val_compare(const void* a_p, const void* b_p)
{
    const struct darshan_common_val_counter* a = a_p;
    const struct darshan_common_val_counter* b = b_p;

    if(a->val < b->val)
        return(-1);
    if(a->val > b->val)
        return(1);
    return(0);
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
void darshan_variance_reduce(void *invec, void *inoutvec, int *len,
    MPI_Datatype *dt)
{
    int i;
    struct darshan_variance_dt *X = invec;
    struct darshan_variance_dt *Y = inoutvec;
    struct darshan_variance_dt  Z;

    for (i=0; i<*len; i++,X++,Y++)
    {
        Z.n = X->n + Y->n;
        Z.T = X->T + Y->T;
        Z.S = X->S + Y->S + (X->n/(Y->n*Z.n)) *
           ((Y->n/X->n)*X->T - Y->T) * ((Y->n/X->n)*X->T - Y->T);

        *Y = Z;
    }

    return;
}

197 198 199 200 201 202 203 204
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */