darshan-common.c 8.35 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
/* track opaque record referencre using a hash link */
23 24 25 26 27 28 29 30 31 32 33 34
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;

35
    /* search the hash table for the given handle */
36 37 38 39 40 41 42
    HASH_FIND(hlink, ref_tracker_head, handle, handle_sz, ref_tracker);
    if(ref_tracker)
        return(ref_tracker->rec_ref_p);
    else
        return(NULL);
}

43
int darshan_add_record_ref(void **hash_head_p, void *handle, size_t handle_sz,
44 45 46 47
    void *rec_ref_p)
{
    struct darshan_record_ref_tracker *ref_tracker;
    struct darshan_record_ref_tracker *ref_tracker_head =
48
        *(struct darshan_record_ref_tracker **)hash_head_p;
49 50
    void *handle_p;

51
    /* allocate a reference tracker, with room to store the handle at the end */
52 53 54 55 56
    ref_tracker = malloc(sizeof(*ref_tracker) + handle_sz);
    if(!ref_tracker)
        return(0);
    memset(ref_tracker, 0, sizeof(*ref_tracker) + handle_sz);

57
    /* initialize the reference tracker and add it to the hash table */
58 59 60 61
    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);
62
    *hash_head_p = ref_tracker_head;
63 64 65
    return(1);
}

66
void *darshan_delete_record_ref(void **hash_head_p, void *handle, size_t handle_sz)
67 68 69
{
    struct darshan_record_ref_tracker *ref_tracker;
    struct darshan_record_ref_tracker *ref_tracker_head =
70
        *(struct darshan_record_ref_tracker **)hash_head_p;
71 72
    void *rec_ref_p;

73
    /* find the reference tracker for this handle */
74 75 76 77
    HASH_FIND(hlink, ref_tracker_head, handle, handle_sz, ref_tracker);
    if(!ref_tracker)
        return(NULL);

78
    /* if found, delete from hash table and return the record reference pointer */
79
    HASH_DELETE(hlink, ref_tracker_head, ref_tracker);
80
    *hash_head_p = ref_tracker_head;
81 82 83 84 85 86
    rec_ref_p = ref_tracker->rec_ref_p;
    free(ref_tracker);

    return(rec_ref_p);
}

87
void darshan_clear_record_refs(void **hash_head_p, int free_flag)
88 89 90
{
    struct darshan_record_ref_tracker *ref_tracker, *tmp;
    struct darshan_record_ref_tracker *ref_tracker_head =
91
        *(struct darshan_record_ref_tracker **)hash_head_p;
92

93
    /* iterate the hash table and remove/free all reference trackers */
94 95 96 97 98 99 100
    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);
    }
101
    *hash_head_p = ref_tracker_head;
102 103 104 105 106 107 108 109 110 111

    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;

112 113 114
    /* iterate the hash table, performing the given action for each reference
     * tracker's corresponding record reference pointer
     */
115 116 117 118 119 120 121 122
    HASH_ITER(hlink, ref_tracker_head, ref_tracker, tmp)
    {
        iter_action(ref_tracker->rec_ref_p);
    }

    return;
}

123 124 125 126 127 128
char* darshan_clean_file_path(const char* path)
{
    char* newpath = NULL;
    char* cwd = NULL;
    char* filter = NULL;

129 130 131 132 133
    /* NOTE: the last check in this if statement is for path strings that
     * begin with the '<' character.  We assume that these are special
     * reserved paths used by Darshan, like <STDIN>.
     */
    if(!path || strlen(path) < 1 || path[0] == '<')
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 176 177 178 179 180 181 182 183
        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);
}

184 185 186 187 188
/* compare function for sorting file records according to their 
 * darshan_base_record structure. Records are sorted first by
 * descending rank (to get all shared records, with rank set to -1, in
 * a contiguous region at the end of the record buffer) then
 * by ascending record identifiers (which are just unsigned integers).
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
 */
static int darshan_base_record_compare(const void* a_p, const void* b_p)
{
    const struct darshan_base_record *a = a_p;
    const struct darshan_base_record *b = b_p;

    if(a->rank < b->rank)
        return(1);
    if(a->rank > b->rank)
        return(-1);

    /* same rank, sort by ascending record ids */
    if(a->id > b->id)
        return(1);
    if(a->id < b->id)
        return(-1);

    return(0);
}

void darshan_record_sort(void *rec_buf, int rec_count, int rec_size)
{
    qsort(rec_buf, rec_count, rec_size, darshan_base_record_compare);
    return;
}

215 216 217 218 219 220 221 222 223 224 225 226
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);
}

227
void darshan_common_val_counter(void **common_val_root, int *common_val_count,
228
    int64_t val, int64_t *common_val_p, int64_t *common_cnt_p)
229 230
{
    struct darshan_common_val_counter* counter;
231
    struct darshan_common_val_counter* found = NULL;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    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++;
    }
248
    else if(*common_val_count < DARSHAN_COMMON_VAL_MAX_RUNTIME_COUNT)
249
    {
250
        /* we can add a new one as long as we haven't hit the limit */
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        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)++;
    }

270 271 272 273 274 275 276
    /* update common access counters as we go, as long as we haven't already
     * hit the limit in the number we are willing to track */
    if(found)
    {
        DARSHAN_COMMON_VAL_COUNTER_INC(common_val_p, common_cnt_p,
            found->val, found->freq, 1);
    }
277 278 279 280

    return;
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
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;
}

302 303 304 305 306 307 308 309
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */