| 1 |
/* |
| 2 |
* Copyright 2014 Samy Al Bahra. |
| 3 |
* Copyright 2014 Backtrace I/O, Inc. |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyrights |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyrights |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 |
* SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <ck_hs.h> |
| 29 |
|
| 30 |
#include <assert.h> |
| 31 |
#include <ck_malloc.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <inttypes.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <time.h> |
| 38 |
|
| 39 |
#include "../../common.h" |
| 40 |
#include "../../../src/ck_ht_hash.h" |
| 41 |
|
| 42 |
static ck_hs_t hs; |
| 43 |
static char **keys; |
| 44 |
static size_t keys_length = 0; |
| 45 |
static size_t keys_capacity = 128; |
| 46 |
static unsigned long global_seed; |
| 47 |
|
| 48 |
static void * |
| 49 |
hs_malloc(size_t r) |
| 50 |
{ |
| 51 |
|
| 52 |
return malloc(r); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
hs_free(void *p, size_t b, bool r) |
| 57 |
{ |
| 58 |
|
| 59 |
(void)b; |
| 60 |
(void)r; |
| 61 |
|
| 62 |
free(p); |
| 63 |
|
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
static struct ck_malloc my_allocator = { |
| 68 |
.malloc = hs_malloc, |
| 69 |
.free = hs_free |
| 70 |
}; |
| 71 |
|
| 72 |
static unsigned long |
| 73 |
hs_hash(const void *object, unsigned long seed) |
| 74 |
{ |
| 75 |
const char *c = object; |
| 76 |
unsigned long h; |
| 77 |
|
| 78 |
h = (unsigned long)MurmurHash64A(c, strlen(c), seed); |
| 79 |
return h; |
| 80 |
} |
| 81 |
|
| 82 |
static bool |
| 83 |
hs_compare(const void *previous, const void *compare) |
| 84 |
{ |
| 85 |
|
| 86 |
return strcmp(previous, compare) == 0; |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
set_destroy(void) |
| 91 |
{ |
| 92 |
|
| 93 |
ck_hs_destroy(&hs); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
set_init(unsigned int size, unsigned int mode) |
| 99 |
{ |
| 100 |
|
| 101 |
if (ck_hs_init(&hs, CK_HS_MODE_OBJECT | CK_HS_MODE_SPMC | mode, hs_hash, hs_compare, |
| 102 |
&my_allocator, size, global_seed) == false) { |
| 103 |
perror("ck_hs_init"); |
| 104 |
exit(EXIT_FAILURE); |
| 105 |
} |
| 106 |
|
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
static size_t |
| 111 |
set_count(void) |
| 112 |
{ |
| 113 |
|
| 114 |
return ck_hs_count(&hs); |
| 115 |
} |
| 116 |
|
| 117 |
static bool |
| 118 |
set_reset(void) |
| 119 |
{ |
| 120 |
|
| 121 |
return ck_hs_reset(&hs); |
| 122 |
} |
| 123 |
|
| 124 |
static void * |
| 125 |
test_apply(void *key, void *closure) |
| 126 |
{ |
| 127 |
|
| 128 |
(void)key; |
| 129 |
|
| 130 |
return closure; |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
run_test(const char *file, size_t r, unsigned int size, unsigned int mode) |
| 135 |
{ |
| 136 |
FILE *fp; |
| 137 |
char buffer[512]; |
| 138 |
size_t i, j; |
| 139 |
unsigned int d = 0; |
| 140 |
uint64_t s, e, a, gp, agp; |
| 141 |
struct ck_hs_stat st; |
| 142 |
char **t; |
| 143 |
|
| 144 |
keys = malloc(sizeof(char *) * keys_capacity); |
| 145 |
assert(keys != NULL); |
| 146 |
|
| 147 |
fp = fopen(file, "r"); |
| 148 |
assert(fp != NULL); |
| 149 |
|
| 150 |
while (fgets(buffer, sizeof(buffer), fp) != NULL) { |
| 151 |
buffer[strlen(buffer) - 1] = '\0'; |
| 152 |
keys[keys_length++] = strdup(buffer); |
| 153 |
assert(keys[keys_length - 1] != NULL); |
| 154 |
|
| 155 |
if (keys_length == keys_capacity) { |
| 156 |
t = realloc(keys, sizeof(char *) * (keys_capacity *= 2)); |
| 157 |
assert(t != NULL); |
| 158 |
keys = t; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
t = realloc(keys, sizeof(char *) * keys_length); |
| 163 |
assert(t != NULL); |
| 164 |
keys = t; |
| 165 |
|
| 166 |
set_init(size, mode); |
| 167 |
for (i = 0; i < keys_length; i++) { |
| 168 |
unsigned long h = CK_HS_HASH(&hs, hs_hash, keys[i]); |
| 169 |
|
| 170 |
if (ck_hs_get(&hs, h, keys[i]) == false) { |
| 171 |
if (ck_hs_put(&hs, h, keys[i]) == false) |
| 172 |
ck_error("ERROR: Failed get to put workload.\n"); |
| 173 |
} else { |
| 174 |
d++; |
| 175 |
} |
| 176 |
} |
| 177 |
ck_hs_stat(&hs, &st); |
| 178 |
|
| 179 |
fprintf(stderr, "# %zu entries stored, %u duplicates, %u probe.\n", |
| 180 |
set_count(), d, st.probe_maximum); |
| 181 |
|
| 182 |
a = 0; |
| 183 |
for (j = 0; j < r; j++) { |
| 184 |
if (set_reset() == false) |
| 185 |
ck_error("ERROR: Failed to reset hash table.\n"); |
| 186 |
|
| 187 |
s = rdtsc(); |
| 188 |
for (i = 0; i < keys_length; i++) { |
| 189 |
unsigned long h = CK_HS_HASH(&hs, hs_hash, keys[i]); |
| 190 |
|
| 191 |
if (ck_hs_get(&hs, h, keys[i]) == false && |
| 192 |
ck_hs_put(&hs, h, keys[i]) == false) { |
| 193 |
ck_error("ERROR: Failed get to put workload.\n"); |
| 194 |
} |
| 195 |
} |
| 196 |
e = rdtsc(); |
| 197 |
a += e - s; |
| 198 |
} |
| 199 |
gp = a / (r * keys_length); |
| 200 |
|
| 201 |
a = 0; |
| 202 |
for (j = 0; j < r; j++) { |
| 203 |
if (set_reset() == false) |
| 204 |
ck_error("ERROR: Failed to reset hash table.\n"); |
| 205 |
|
| 206 |
s = rdtsc(); |
| 207 |
for (i = 0; i < keys_length; i++) { |
| 208 |
unsigned long h = CK_HS_HASH(&hs, hs_hash, keys[i]); |
| 209 |
|
| 210 |
if (ck_hs_apply(&hs, h, keys[i], test_apply, (void *)keys[i]) == false) |
| 211 |
ck_error("ERROR: Failed in apply workload.\n"); |
| 212 |
} |
| 213 |
e = rdtsc(); |
| 214 |
a += e - s; |
| 215 |
} |
| 216 |
agp = a / (r * keys_length); |
| 217 |
|
| 218 |
fclose(fp); |
| 219 |
|
| 220 |
for (i = 0; i < keys_length; i++) { |
| 221 |
free(keys[i]); |
| 222 |
} |
| 223 |
|
| 224 |
printf("Get to put: %" PRIu64 " ticks\n", gp); |
| 225 |
printf(" Apply: %" PRIu64 " ticks\n", agp); |
| 226 |
|
| 227 |
free(keys); |
| 228 |
keys_length = 0; |
| 229 |
set_destroy(); |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
int |
| 234 |
main(int argc, char *argv[]) |
| 235 |
{ |
| 236 |
unsigned int r, size; |
| 237 |
|
| 238 |
common_srand48((long int)time(NULL)); |
| 239 |
if (argc < 2) { |
| 240 |
ck_error("Usage: ck_hs <dictionary> [<repetitions> <initial size>]\n"); |
| 241 |
} |
| 242 |
|
| 243 |
r = 16; |
| 244 |
if (argc >= 3) |
| 245 |
r = atoi(argv[2]); |
| 246 |
|
| 247 |
size = 8; |
| 248 |
if (argc >= 4) |
| 249 |
size = atoi(argv[3]); |
| 250 |
|
| 251 |
global_seed = common_lrand48(); |
| 252 |
run_test(argv[1], r, size, 0); |
| 253 |
|
| 254 |
printf("\n==============================================\n" |
| 255 |
"Delete mode\n" |
| 256 |
"==============================================\n"); |
| 257 |
run_test(argv[1], r, size, CK_HS_MODE_DELETE); |
| 258 |
return 0; |
| 259 |
} |
| 260 |
|