darshan-common.c 7.4 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 "uthash.h"

20 21
#include "darshan.h"

22 23 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 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

struct darshan_record_ref_tracker
{
    void *rec_ref_p;
    UT_hash_handle hlink;
};

void *darshan_lookup_record_ref(void *hash_head, void *handle, size_t handle_sz)
{
    struct darshan_record_ref_tracker *ref_tracker;
    struct darshan_record_ref_tracker *ref_tracker_head =
        (struct darshan_record_ref_tracker *)hash_head;

    HASH_FIND(hlink, ref_tracker_head, handle, handle_sz, ref_tracker);
    if(ref_tracker)
        return(ref_tracker->rec_ref_p);
    else
        return(NULL);
}

int darshan_add_record_ref(void **hash_head, void *handle, size_t handle_sz,
    void *rec_ref_p)
{
    struct darshan_record_ref_tracker *ref_tracker;
    struct darshan_record_ref_tracker *ref_tracker_head =
        *(struct darshan_record_ref_tracker **)hash_head;
    void *handle_p;

    ref_tracker = malloc(sizeof(*ref_tracker) + handle_sz);
    if(!ref_tracker)
        return(0);
    memset(ref_tracker, 0, sizeof(*ref_tracker) + handle_sz);

    ref_tracker->rec_ref_p = rec_ref_p;
    handle_p = (char *)ref_tracker + sizeof(*ref_tracker);
    memcpy(handle_p, handle, handle_sz);
    HASH_ADD_KEYPTR(hlink, ref_tracker_head, handle_p, handle_sz, ref_tracker);
    *hash_head = ref_tracker_head;
    return(1);
}

void *darshan_delete_record_ref(void **hash_head, void *handle, size_t handle_sz)
{
    struct darshan_record_ref_tracker *ref_tracker;
    struct darshan_record_ref_tracker *ref_tracker_head =
        *(struct darshan_record_ref_tracker **)hash_head;
    void *rec_ref_p;

    HASH_FIND(hlink, ref_tracker_head, handle, handle_sz, ref_tracker);
    if(!ref_tracker)
        return(NULL);

    HASH_DELETE(hlink, ref_tracker_head, ref_tracker);
    *hash_head = ref_tracker_head;
    rec_ref_p = ref_tracker->rec_ref_p;
    free(ref_tracker);

    return(rec_ref_p);
}

void darshan_clear_record_refs(void **hash_head, int free_flag)
{
    struct darshan_record_ref_tracker *ref_tracker, *tmp;
    struct darshan_record_ref_tracker *ref_tracker_head =
        *(struct darshan_record_ref_tracker **)hash_head;

    HASH_ITER(hlink, ref_tracker_head, ref_tracker, tmp)
    {
        HASH_DELETE(hlink, ref_tracker_head, ref_tracker);
        if(free_flag)
            free(ref_tracker->rec_ref_p);
        free(ref_tracker);
    }
    *hash_head = ref_tracker_head;

    return;
}

void darshan_iter_record_refs(void *hash_head, void (*iter_action)(void *))
{
    struct darshan_record_ref_tracker *ref_tracker, *tmp;
    struct darshan_record_ref_tracker *ref_tracker_head =
        (struct darshan_record_ref_tracker *)hash_head;

    HASH_ITER(hlink, ref_tracker_head, ref_tracker, tmp)
    {
        iter_action(ref_tracker->rec_ref_p);
    }

    return;
}

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
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);
}

171 172 173 174
/* HACK: global variables for determining 4 most common values */
static int64_t* walker_val_p = NULL;
static int64_t* walker_cnt_p = NULL;

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
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, 0);
        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);
}

206
void darshan_common_val_counter(void **common_val_root, int *common_val_count,
207
    int64_t val, int64_t *common_val_p, int64_t *common_cnt_p)
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
    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++;
    }
227
    else if(*common_val_count < DARSHAN_COMMON_VAL_MAX_RUNTIME_COUNT)
228
    {
229
        /* we can add a new one as long as we haven't hit the limit */
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        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)++;
    }

249 250 251 252 253 254 255 256
#ifdef __DARSHAN_ENABLE_MMAP_LOGS
    /* if we are using darshan's mmap feature, update common access
     * counters as we go
     */
    DARSHAN_COMMON_VAL_COUNTER_INC(common_val_p, common_cnt_p,
        found->val, found->freq, 1);
#endif

257 258 259
    return;
}

260 261
void darshan_walk_common_vals(void *common_val_root, int64_t *val_p,
    int64_t *cnt_p)
262 263 264 265 266 267 268 269
{
    walker_val_p = val_p;
    walker_cnt_p = cnt_p;

    twalk(common_val_root, darshan_common_val_walker);
    return;
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
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;
}

291 292 293 294 295 296 297 298
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */