bb-latency-bench.c 6.1 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

#include "abt.h"
#include "abt-snoozer.h"
#include "bake-bulk.h"

20 21 22 23 24 25 26 27
static void bench_routine_write(bake_target_id_t bti, int iterations, double* measurement_array, int size);
static void bench_routine_read(bake_target_id_t bti, int iterations, double* measurement_array, int size);
static void bench_routine_noop(bake_target_id_t bti, int iterations, double* measurement_array);
static void bench_routine_print(const char* op, int size, int iterations, double* measurement_array);
static int measurement_cmp(const void* a, const void *b);

static double *measurement_array = NULL;
static bake_bulk_region_id_t rid;
Philip Carns's avatar
Philip Carns committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

int main(int argc, char **argv) 
{
    int ret;
    bake_target_id_t bti;
    int min_size, max_size, iterations, cur_size;
 
    if(argc != 5)
    {
        fprintf(stderr, "Usage: bb-latency-bench <server addr> <iterations> <min_sz> <max_sz>\n");
        fprintf(stderr, "  Example: ./bb-latency-bench tcp://localhost:1234 1000 4 32\n");
        return(-1);
    }       

    ret = sscanf(argv[2], "%d", &iterations);
    assert(ret == 1);

    ret = sscanf(argv[3], "%d", &min_size);
    assert(ret == 1);

    ret = sscanf(argv[4], "%d", &max_size);
    assert(ret == 1);

51 52 53
    measurement_array = malloc(sizeof(*measurement_array)*iterations);
    assert(measurement_array);

Philip Carns's avatar
Philip Carns committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    /* set up Argobots */
    ret = ABT_init(argc, argv);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_init()\n");
        return(-1);
    }
    ret = ABT_snoozer_xstream_self_set();
    if(ret != 0)
    {
        ABT_finalize();
        fprintf(stderr, "Error: ABT_snoozer_xstream_self_set()\n");
        return(-1);
    }

    ret = bake_probe_instance(argv[1], &bti);
    if(ret < 0)
    {
        ABT_finalize();
        fprintf(stderr, "Error: bake_probe_instance()\n");
        return(-1);
    }

77 78 79 80
    printf("# <op> <iterations> <size> <min> <q1> <med> <avg> <q3> <max>\n");

    bench_routine_noop(bti, iterations, measurement_array);
    bench_routine_print("noop", 0, iterations, measurement_array);
Philip Carns's avatar
Philip Carns committed
81
    for(cur_size=min_size; cur_size <= max_size; cur_size *= 2)
82 83 84 85 86 87
    {
        bench_routine_write(bti, iterations, measurement_array, cur_size);
        bench_routine_print("write", cur_size, iterations, measurement_array);
        bench_routine_read(bti, iterations, measurement_array, cur_size);
        bench_routine_print("read", cur_size, iterations, measurement_array);
    }
Philip Carns's avatar
Philip Carns committed
88 89 90 91 92
    
    bake_release_instance(bti);

    ABT_finalize();

93 94
    free(measurement_array);

Philip Carns's avatar
Philip Carns committed
95 96 97 98 99
    return(0);
}

static double Wtime(void)
{
100 101 102
    struct timespec tp;
    clock_gettime(CLOCK_MONOTONIC, &tp);
    return((double)tp.tv_sec + (double)(tp.tv_nsec) / (double)1000000000.0);
Philip Carns's avatar
Philip Carns committed
103 104
}

105
static void bench_routine_write(bake_target_id_t bti, int iterations, double *measurement_array, int size)
Philip Carns's avatar
Philip Carns committed
106 107
{
    int ret;
108
    double tm1, tm2;
Philip Carns's avatar
Philip Carns committed
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
    char *buffer;
    uint64_t region_offset = 0;
    int i;

    buffer = calloc(1, size);
    assert(buffer);

    /* create region */
    ret = bake_bulk_create(bti, size*iterations, &rid);
    assert(ret == 0);

    sleep(1);

    for(i=0; i<iterations; i++)
    {
        tm1 = Wtime();
        /* transfer data (writes) */
        ret = bake_bulk_write(
            bti,
            rid,
            region_offset,
            buffer,
            size);
        tm2 = Wtime();
        assert(ret == 0);
        region_offset += size;
135
        measurement_array[i] = tm2-tm1;
Philip Carns's avatar
Philip Carns committed
136 137
    }

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    /* persist */
    ret = bake_bulk_persist(bti, rid);
    assert(ret == 0);

    free(buffer);

    return;
}

static void bench_routine_read(bake_target_id_t bti, int iterations, double *measurement_array, int size)
{
    int ret;
    double tm1, tm2;
    char *buffer;
    uint64_t region_offset = 0;
    int i;

    buffer = calloc(1, size);
    assert(buffer);
Philip Carns's avatar
Philip Carns committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    sleep(1);

    for(i=0; i<iterations; i++)
    {
        tm1 = Wtime();
        /* transfer data (reads) */
        ret = bake_bulk_read(
            bti,
            rid,
            region_offset,
            buffer,
            size);
        tm2 = Wtime();
        assert(ret == 0);
        region_offset += size;
173
        measurement_array[i] = tm2-tm1;
Philip Carns's avatar
Philip Carns committed
174 175 176 177 178 179
    }

    free(buffer);

    return;
}
180

181
static void bench_routine_noop(bake_target_id_t bti, int iterations, double *measurement_array)
182 183
{
    int ret;
184
    double tm1, tm2;
185 186 187 188 189 190 191 192 193 194 195 196
    int i;

    sleep(1);

    for(i=0; i<iterations; i++)
    {
        tm1 = Wtime();
        /* noop */
        ret = bake_bulk_noop(bti);
        tm2 = Wtime();
        assert(ret == 0);

197
        measurement_array[i] = tm2-tm1;
198 199
    }

200 201 202 203 204 205 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
    return;
}

static int measurement_cmp(const void* a, const void *b)
{
    const double *d_a = a;
    const double *d_b = b;

    if(*d_a < *d_b)
        return(-1);
    else if(*d_a > *d_b)
        return(1);
    else
        return(0);
}

static void bench_routine_print(const char* op, int size, int iterations, double* measurement_array)
{
    double min, max, q1, q3, med, avg, sum;
    int bracket1, bracket2;
    int i;

    qsort(measurement_array, iterations, sizeof(double), measurement_cmp);

    min = measurement_array[0];
    max = measurement_array[iterations-1];

    sum = 0;
    for(i=0; i<iterations; i++)
    {
        sum += measurement_array[i];
    }
    avg = sum/(double)iterations;

    bracket1 = iterations/2;
    if(iterations%2)
        bracket2 = bracket1 + 1;
    else
        bracket2 = bracket1;
    med = (measurement_array[bracket1] + measurement_array[bracket2])/(double)2;

    bracket1 = iterations/4;
    if(iterations%4)
        bracket2 = bracket1 + 1;
    else
        bracket2 = bracket1;
    q1 = (measurement_array[bracket1] + measurement_array[bracket2])/(double)2;

    bracket1 *= 3;
    if(iterations%4)
        bracket2 = bracket1 + 1;
    else
        bracket2 = bracket1;
    q3 = (measurement_array[bracket1] + measurement_array[bracket2])/(double)2;

    printf("%s\t%d\t%d\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f\n", op, iterations, size, min, q1, med, avg, q3, max);
256 257 258

    return;
}