Revision 7cf0125c xseg/xtypes/xcache_test.c
b/xseg/xtypes/xcache_test.c | ||
---|---|---|
8 | 8 |
|
9 | 9 |
unsigned long sum = 0; |
10 | 10 |
struct xlock lock; |
11 |
uint32_t lru = 0; |
|
11 | 12 |
|
12 | 13 |
struct ce{ |
13 | 14 |
unsigned long tid; |
... | ... | |
28 | 29 |
__sync_add_and_fetch(&sum, 1); |
29 | 30 |
} |
30 | 31 |
|
32 |
void put_test(void *c, void *e) |
|
33 |
{ |
|
34 |
struct ce *ce = e; |
|
35 |
if (ce->tid){ |
|
36 |
fprintf(stderr, "Invalid lru eviction\n"); |
|
37 |
fprintf(stderr, "test3: failed\n"); |
|
38 |
} else { |
|
39 |
fprintf(stderr, "test3: PASSED\n"); |
|
40 |
} |
|
41 |
} |
|
42 |
|
|
31 | 43 |
void *node_init(void *c) |
32 | 44 |
{ |
33 | 45 |
return malloc(sizeof(struct ce)); |
34 | 46 |
} |
35 | 47 |
|
48 |
|
|
36 | 49 |
int test1(unsigned long n) |
37 | 50 |
{ |
38 | 51 |
struct xcache cache; |
... | ... | |
47 | 60 |
unsigned long i; |
48 | 61 |
int r; |
49 | 62 |
|
50 |
xcache_init(&cache, n, &c_ops, NULL); |
|
63 |
xcache_init(&cache, n, &c_ops, lru, NULL);
|
|
51 | 64 |
|
52 | 65 |
for (i = 0; i < n; i++) { |
53 | 66 |
sprintf(name, "%lu", i); |
... | ... | |
104 | 117 |
} |
105 | 118 |
xcache_close(&cache); |
106 | 119 |
if (sum != 2*n){ |
107 |
fprintf(stderr, "%lu puts instead of %lu\n", sum, n); |
|
120 |
fprintf(stderr, "%lu puts instead of %lu\n", sum, 2*n);
|
|
108 | 121 |
return -1; |
109 | 122 |
} |
110 | 123 |
return 0; |
111 | 124 |
} |
112 | 125 |
|
113 | 126 |
|
127 |
int test3(unsigned long n) |
|
128 |
{ |
|
129 |
struct xcache cache; |
|
130 |
struct xcache_ops c_ops = { |
|
131 |
.on_init = init, |
|
132 |
.on_put = put_test, |
|
133 |
.on_node_init = node_init |
|
134 |
}; |
|
135 |
struct ce *ce; |
|
136 |
xcache_handler h; |
|
137 |
sum = 0; |
|
138 |
char name[XSEG_MAX_TARGETLEN]; |
|
139 |
unsigned long i; |
|
140 |
int r; |
|
141 |
|
|
142 |
xcache_init(&cache, n, &c_ops, lru, NULL); |
|
143 |
|
|
144 |
for (i = 0; i < n; i++) { |
|
145 |
sprintf(name, "%lu", i); |
|
146 |
h = xcache_lookup(&cache, name); |
|
147 |
if (h != NoEntry){ |
|
148 |
fprintf(stderr, "Cache return cache entry\n"); |
|
149 |
return -1; |
|
150 |
} |
|
151 |
|
|
152 |
h = xcache_alloc_init(&cache, name); |
|
153 |
if (h == NoEntry){ |
|
154 |
fprintf(stderr, "Could not allocate cache entry\n"); |
|
155 |
return -1; |
|
156 |
} |
|
157 |
ce = get_cache_entry(&cache, h); |
|
158 |
ce->tid = i; |
|
159 |
r = xcache_insert(&cache, h); |
|
160 |
if (r < 0){ |
|
161 |
xcache_put(&cache, h); |
|
162 |
fprintf(stderr, "Could not insert cache entry\n"); |
|
163 |
return -1; |
|
164 |
} |
|
165 |
xcache_put(&cache, h); |
|
166 |
} |
|
167 |
|
|
168 |
sprintf(name, "%lu", n); |
|
169 |
h = xcache_lookup(&cache, name); |
|
170 |
if (h != NoEntry){ |
|
171 |
fprintf(stderr, "Cache return cache entry\n"); |
|
172 |
return -1; |
|
173 |
} |
|
174 |
|
|
175 |
h = xcache_alloc_init(&cache, name); |
|
176 |
if (h == NoEntry){ |
|
177 |
fprintf(stderr, "Could not allocate cache entry\n"); |
|
178 |
return -1; |
|
179 |
} |
|
180 |
r = xcache_insert(&cache, h); |
|
181 |
if (r < 0){ |
|
182 |
xcache_put(&cache, h); |
|
183 |
fprintf(stderr, "Could not insert cache entry\n"); |
|
184 |
return -1; |
|
185 |
} |
|
186 |
xcache_put(&cache, h); |
|
187 |
|
|
188 |
//xcache_close(&cache); |
|
189 |
return 0; |
|
190 |
} |
|
191 |
|
|
114 | 192 |
struct thread_arg{ |
115 | 193 |
struct xcache *cache; |
116 | 194 |
unsigned long tid; |
... | ... | |
157 | 235 |
unsigned long i; |
158 | 236 |
int r; |
159 | 237 |
|
160 |
xcache_init(&cache, cache_size, &c_ops, NULL); |
|
238 |
xcache_init(&cache, cache_size, &c_ops, lru, NULL);
|
|
161 | 239 |
|
162 | 240 |
struct thread_arg *targs = malloc(sizeof(struct thread_arg)*nr_threads * n); |
163 | 241 |
pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads); |
... | ... | |
236 | 314 |
struct timeval start, end, tv; |
237 | 315 |
int r; |
238 | 316 |
int cache_size = atoi(argv[1]); |
239 |
int n = atoi(argv[2]); |
|
240 |
int t = atoi(argv[3]); |
|
317 |
int lru_type = atoi(argv[2]); |
|
318 |
int n = atoi(argv[3]); |
|
319 |
int t = atoi(argv[4]); |
|
320 |
|
|
321 |
lru = XCACHE_LRU_ARRAY; |
|
322 |
if (lru_type) |
|
323 |
lru = XCACHE_LRU_HEAP; |
|
241 | 324 |
|
242 | 325 |
fprintf(stderr, "Running test1\n"); |
243 | 326 |
gettimeofday(&start, NULL); |
... | ... | |
264 | 347 |
fprintf(stderr, "test2: PASSED\n"); |
265 | 348 |
} |
266 | 349 |
|
267 |
/* |
|
350 |
|
|
268 | 351 |
fprintf(stderr, "running test3\n"); |
269 | 352 |
gettimeofday(&start, NULL); |
270 |
r = test3(n, t);
|
|
353 |
r = test3(cache_size);
|
|
271 | 354 |
if (r < 0){ |
272 | 355 |
fprintf(stderr, "test3: failed\n"); |
273 | 356 |
return -1; |
274 | 357 |
} |
275 | 358 |
gettimeofday(&end, NULL); |
276 |
fprintf(stderr, "test3: PASSED\n"); |
|
277 | 359 |
timersub(&end, &start, &tv); |
278 | 360 |
fprintf(stderr, "Test time: %ds %dusec\n\n", (int)tv.tv_sec, (int)tv.tv_usec); |
279 |
*/ |
|
361 |
|
|
280 | 362 |
return 0; |
281 | 363 |
} |
Also available in: Unified diff