mmap_cache.c

filter: Statementstmt 87.5% 508 / 580 Branchbran 79.2% 252 / 318 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.6% 760 / 898 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« FastMmap.xs ↑ Summary
1  
2 /*
3 * AUTHOR
4 *
5 * Rob Mueller <cpan@robm.fastmail.fm>
6 *
7 * COPYRIGHT AND LICENSE
8 *
9 * Copyright (C) 2003 by FastMail IP Partners
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the same terms as Perl itself.
13 *
14 */
15  
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <time.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include "mmap_cache.h"
23 #include "mmap_cache_internals.h"
24  
25 /* Global time_override */
26 MU32 time_override = 0;
27  
28 14 void mmc_set_time_override(MU32 set_time) {
29 14 time_override = set_time;
30 14 }
31  
32 /* Default values for a new cache */
33 char * def_share_file = "/tmp/sharefile";
34 MU32 def_init_file = 0;
35 MU32 def_test_file = 0;
36 MU32 def_expire_time = 0;
37 MU32 def_c_num_pages = 89;
38 MU32 def_c_page_size = 65536;
39 MU32 def_start_slots = 89;
40  
41 /*
42 * mmap_cache * mmc_new()
43 *
44 * Create a new cache object filled with default values. Values may
45 * be changed and once ready, you should call mmc_init() to actually
46 * open the cache file and mmap it.
47 *
48 */
49 8071 mmap_cache * mmc_new() {
50 8071 mmap_cache * cache = (mmap_cache *)calloc(1, sizeof(mmap_cache));
51  
52 8071 cache->p_cur = NOPAGE;
53  
54 8071 cache->c_num_pages = def_c_num_pages;
55 8071 cache->c_page_size = def_c_page_size;
56  
57 8071 cache->start_slots = def_start_slots;
58 8071 cache->expire_time = def_expire_time;
59  
60 8071 cache->share_file = _mmc_get_def_share_filename(cache);
61 8071 cache->permissions = 0640;
62 8071 cache->init_file = def_init_file;
63 8071 cache->test_file = def_test_file;
64  
65 8071 return cache;
66 }
67  
68 72632 int mmc_set_param(mmap_cache * cache, char * param, char * val) {
69 72632 ▶ if (!strcmp(param, "init_file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(param, "init_file")) { 8070 64562 72632
70 8070 cache->init_file = atoi(val);
71 64562 ▶ } else if (!strcmp(param, "test_file")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "test_file")) { 8070 56492 64562
72 8070 cache->test_file = atoi(val);
73 56492 ▶ } else if (!strcmp(param, "page_size")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "page_size")) { 8070 48422 56492
74 8070 cache->c_page_size = atoi(val);
75 48422 ▶ } else if (!strcmp(param, "num_pages")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "num_pages")) { 8070 40352 48422
76 8070 cache->c_num_pages = atoi(val);
77 40352 ▶ } else if (!strcmp(param, "expire_time")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "expire_time")) { 8070 32282 40352
78 8070 cache->expire_time = atoi(val);
79 32282 ▶ } else if (!strcmp(param, "share_file")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "share_file")) { 8070 24212 32282
80 8070 cache->share_file = val;
81 24212 ▶ } else if (!strcmp(param, "permissions")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "permissions")) { 2 24210 24212
82 2 cache->permissions = atoi(val);
83 24210 ▶ } else if (!strcmp(param, "start_slots")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "start_slots")) { 8070 16140 24210
84 8070 cache->start_slots = atoi(val);
85 16140 ▶ } else if (!strcmp(param, "catch_deadlocks")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "catch_deadlocks")) { 8070 8070 16140
86 8070 cache->catch_deadlocks = atoi(val);
87 8070 ▶ } else if (!strcmp(param, "enable_stats")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "enable_stats")) { 8070 0 8070
88 8070 cache->enable_stats = atoi(val);
89 } else {
90 0 return _mmc_set_error(cache, 0, "Bad set_param parameter: %s", param);
91 }
92  
93 72632 return 0;
94 }
95  
96 9325 int mmc_get_param(mmap_cache * cache, char * param) {
97 9325 ▶ if (!strcmp(param, "page_size")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(param, "page_size")) { 0 9325 9325
98 0 return (int)cache->c_page_size;
99 9325 ▶ } else if (!strcmp(param, "num_pages")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "num_pages")) { 0 9325 9325
100 0 return (int)cache->c_num_pages;
101 9325 ▶ } else if (!strcmp(param, "expire_time")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "expire_time")) { 0 9325 9325
102 0 return (int)cache->expire_time;
103 9325 ▶ } else if (!strcmp(param, "repaired_pages")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "repaired_pages")) { 9324 1 9325
104 9324 return (int)cache->repaired_pages;
105 1 ▶ } else if (!strcmp(param, "debug")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "debug")) { 1 0 1
106 #ifdef DEBUG
107 return 1;
108 #else
109 1 return 0;
110 #endif
111 } else {
112 0 return _mmc_set_error(cache, 0, "Bad set_param parameter: %s", param);
113 }
114 }
115  
116 /*
117 * int mmc_init(mmap_cache * cache)
118 *
119 * Initialise the cache object, opening the share file and mmap'ing any
120 * memory.
121 *
122 */
123 8070 int mmc_init(mmap_cache * cache) {
124 8070 int i, do_init = cache->init_file;
125 MU32 c_num_pages, c_page_size;
126 MU64 c_size;
127  
128 /* Need a share file */
129 8070 ▶ if (!cache->share_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cache->share_file) { 0 8070 8070
130 0 return _mmc_set_error(cache, 0, "No share file specified");
131 }
132  
133 /* Basic cache params */
134 8070 c_num_pages = cache->c_num_pages;
135 ASSERT(c_num_pages >= 1 && c_num_pages <= 1000);
136  
137 8070 c_page_size = cache->c_page_size;
138 ASSERT(c_page_size >= 1024 && c_page_size <= 1024*1024*1024);
139  
140 ASSERT(cache->start_slots >= 10 && cache->start_slots <= 500);
141  
142 8070 cache->c_size = c_size = (MU64)c_num_pages * c_page_size;
143  
144 8070 ▶ if ( mmc_open_cache_file(cache, &do_init) == -1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( mmc_open_cache_file(cache, &do_init) == -1) return -1; 0 8070 8070
145  
146 /* Map file into memory */
147 8070 ▶ if ( mmc_map_memory(cache) == -1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( mmc_map_memory(cache) == -1) return -1; 0 8070 8070
148  
149 /* Initialise pages if new file */
150 8070 ▶ if (do_init) {
Branch: 1 branch
BranchTrueFalseTotal
if (do_init) { 8069 1 8070
151 147158 ▶ for (i = 0; i < cache->c_num_pages; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cache->c_num_pages; i++) { 139089 8069 147158
152 139089 MU64 p_offset = (MU64)i * cache->c_page_size;
153 139089 mmc_lock_page(cache, p_offset);
154 139089 _mmc_init_page(cache, i);
155 139089 mmc_unlock_page(cache, p_offset);
156 }
157  
158 /* Unmap and re-map to stop gtop telling us our memory usage is up */
159 8069 ▶ if ( mmc_unmap_memory(cache) == -1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( mmc_unmap_memory(cache) == -1) return -1; 0 8069 8069
160 8069 ▶ if ( mmc_map_memory(cache) == -1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( mmc_map_memory(cache) == -1) return -1; 0 8069 8069
161 }
162  
163 /* Test pages in file if asked */
164 8070 ▶ if (cache->test_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->test_file) { 1 8069 8070
165 8 ▶ for (i = 0; i < cache->c_num_pages; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cache->c_num_pages; i++) { 7 1 8
166 7 int bad_page = 0;
167 7 MU64 p_offset = (MU64)i * cache->c_page_size;
168  
169 /* Need to lock page, which tests header structure */
170 7 ▶ if (mmc_lock(cache, i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mmc_lock(cache, i)) { 0 7 7
171 /* If that failed, assume bad header, so manually lock */
172 0 mmc_lock_page(cache, p_offset);
173 0 bad_page = 1;
174  
175 /* If lock succeeded, test page structure */
176 } else {
177 7 ▶ if (!_mmc_test_page(cache))
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_test_page(cache)) 0 7 7
178 0 bad_page = 1;
179 }
180  
181 /* A bad page, initialise it */
182 7 ▶ if (bad_page) {
Branch: 1 branch
BranchTrueFalseTotal
if (bad_page) { 0 7 7
183 0 _mmc_init_page(cache, i);
184 /* Rerun test on this page, potential infinite
185 loop if init_page is broken, but then things
186 are really broken anyway */
187 0 i--;
188 }
189  
190 7 mmc_unlock_page(cache, p_offset);
191 7 cache->p_cur = NOPAGE;
192 }
193 }
194  
195 8070 return 0;
196 }
197  
198 /*
199 * int mmc_close(mmap_cache * cache)
200 *
201 * Close the given cache, unmmap'ing any memory and closing file
202 * descriptors.
203 *
204 */
205 8070 int mmc_close(mmap_cache *cache) {
206 int res;
207  
208 /* Shouldn't call if not init'ed */
209 ASSERT(cache->fh);
210 ASSERT(cache->mm_var);
211  
212 /* Shouldn't call if page still locked */
213 ASSERT(cache->p_cur == NOPAGE);
214  
215 /* Unlock any locked page */
216 8070 ▶ if (cache->p_cur != NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur != NOPAGE) { 0 8070 8070
217 0 mmc_unlock(cache);
218 }
219  
220 /* Close file */
221 8070 ▶ if (cache->fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->fh) { 8070 0 8070
222 8070 mmc_close_fh(cache);
223 }
224  
225 /* Unmap memory */
226 8070 ▶ if (cache->mm_var) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->mm_var) { 8070 0 8070
227 8070 res = mmc_unmap_memory(cache);
228 8070 ▶ if (res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == -1) { 0 8070 8070
229 0 return _mmc_set_error(cache, errno, "Mmap of shared file %s failed", cache->share_file);
230 }
231 }
232  
233 8070 free(cache);
234  
235 8070 return 0;
236 }
237  
238 /*
239 * int mmc_page_repaired(mmap_cache * cache)
240 *
241 * True if the most recent mmc_lock or mmc_unlock reinitialised a page.
242 * mmc_error() then describes why. Cleared by the next mmc_lock.
243 *
244 */
245 539596 int mmc_page_repaired(mmap_cache * cache) {
246 539596 return cache->page_repaired;
247 }
248  
249 6 char * mmc_error(mmap_cache * cache) {
250 6 ▶ if (cache->last_error)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->last_error) 6 0 6
251 6 return cache->last_error;
252 0 return "Unknown error";
253 }
254  
255 /*
256 * mmc_lock(
257 * cache_mmap * cache, MU32 p_cur
258 * )
259 *
260 * Lock the given page number using fcntl locking. Setup
261 * cache->p_* fields with correct values for the given page
262 *
263 */
264 281320 int mmc_lock(mmap_cache * cache, MU32 p_cur) {
265 MU64 p_offset;
266 void * p_ptr;
267 281320 int res = 0;
268  
269 /* Argument sanity check. Valid pages are 0 .. c_num_pages-1; NOPAGE is
270 * already excluded by the >= comparison since c_num_pages << NOPAGE. */
271 281320 ▶ if (p_cur >= cache->c_num_pages)
Branch: 1 branch
BranchTrueFalseTotal
if (p_cur >= cache->c_num_pages) 0 281320 281320
272 0 return _mmc_set_error(cache, 0, "page %u is NOPAGE or larger than number of pages", p_cur);
273  
274 /* Check not already locked */
275 281320 ▶ if (cache->p_cur != NOPAGE)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur != NOPAGE) 1 281319 281320
276 1 return _mmc_set_error(cache, 0, "page %u is already locked, can't lock multiple pages", cache->p_cur);
277  
278 /* Setup page details */
279 281319 p_offset = (MU64)p_cur * cache->c_page_size;
280 281319 p_ptr = PTR_ADD(cache->mm_var, p_offset);
281  
282 281319 res = mmc_lock_page(cache, p_offset);
283 281319 ▶ if (res) return res;
Branch: 1 branch
BranchTrueFalseTotal
if (res) return res; 0 281319 281319
284  
285 281319 cache->page_repaired = 0;
286 281319 cache->p_dirty = 0;
287 281319 cache->p_corrupt = 0;
288  
289 /* A dirty marker means whoever last held this page died (or was
290 * killed) part way through changing it; the lock has passed to us,
291 * but the page's structure can't be trusted, so start it afresh */
292 281319 ▶ if (P_Magic(p_ptr) == P_MAGIC_DIRTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (P_Magic(p_ptr) == P_MAGIC_DIRTY) { 2 281317 281319
293 2 _mmc_repair_page(cache, p_cur, "left dirty by a killed writer");
294  
295 281317 ▶ } else if (P_Magic(p_ptr) != P_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (P_Magic(p_ptr) != P_MAGIC) { 0 281317 281317
296 0 mmc_unlock_page(cache, p_offset);
297 0 return _mmc_set_error(cache, 0, "magic page start marker not found. p_cur is %u, offset is %llu", p_cur, p_offset);
298 }
299  
300 /* Copy to cache structure */
301 281319 cache->p_num_slots = P_NumSlots(p_ptr);
302 281319 cache->p_free_slots = P_FreeSlots(p_ptr);
303 281319 cache->p_old_slots = P_OldSlots(p_ptr);
304 281319 cache->p_free_data = P_FreeData(p_ptr);
305 281319 cache->p_free_bytes = P_FreeBytes(p_ptr);
306 281319 cache->p_n_reads = P_NReads(p_ptr);
307 281319 cache->p_n_read_hits = P_NReadHits(p_ptr);
308  
309 /* Reality check. Pages start with start_slots and only ever grow via
310 * expunge, so num_slots should never be below the configured
311 * start_slots, and the slot table, then the data, must fit in the
312 * page. A header that fails these was corrupted by something that
313 * predates the dirty marker (an older module version killed
314 * mid-update, say); the page is unusable as it is, so it is
315 * reinitialised like a dirty one rather than failing every access
316 * to it until someone recreates the file. */
317 281319 ▶ if (cache->p_num_slots < cache->start_slots ||
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_num_slots < cache->start_slots || 281319 0 281319
318 281319 ▶ P_HEADERSIZE + (MU64)cache->p_num_slots * 4 > cache->c_page_size)
Branch: 1 branch
BranchTrueFalseTotal
P_HEADERSIZE + (MU64)cache->p_num_slots * 4 > cache->c_page_size) 0 281319 281319
319 0 res = _mmc_set_error(cache, 0, "cache num_slots mismatch");
320 281319 ▶ else if (cache->p_free_slots > cache->p_num_slots)
Branch: 1 branch
BranchTrueFalseTotal
else if (cache->p_free_slots > cache->p_num_slots) 0 281319 281319
321 0 res = _mmc_set_error(cache, 0, "cache free slots mismatch");
322 281319 ▶ else if (cache->p_old_slots > cache->p_free_slots)
Branch: 1 branch
BranchTrueFalseTotal
else if (cache->p_old_slots > cache->p_free_slots) 0 281319 281319
323 0 res = _mmc_set_error(cache, 0, "cache old slots mismatch");
324 281319 ▶ else if (cache->p_free_data < P_HEADERSIZE + cache->p_num_slots * 4 ||
Branch: 1 branch
BranchTrueFalseTotal
else if (cache->p_free_data < P_HEADERSIZE + cache->p_num_slots * 4 || 281319 0 281319
325 281319 ▶ cache->p_free_data > cache->c_page_size ||
Branch: 1 branch
BranchTrueFalseTotal
cache->p_free_data > cache->c_page_size || 281319 0 281319
326 281319 ▶ cache->p_free_data + cache->p_free_bytes != cache->c_page_size)
Branch: 1 branch
BranchTrueFalseTotal
cache->p_free_data + cache->p_free_bytes != cache->c_page_size) 0 281319 281319
327 0 res = _mmc_set_error(cache, 0, "cache free data mismatch");
328 281319 ▶ if (res) {
Branch: 1 branch
BranchTrueFalseTotal
if (res) { 0 281319 281319
329 0 _mmc_repair_page(cache, p_cur, cache->last_error);
330 0 cache->p_num_slots = P_NumSlots(p_ptr);
331 0 cache->p_free_slots = P_FreeSlots(p_ptr);
332 0 cache->p_old_slots = P_OldSlots(p_ptr);
333 0 cache->p_free_data = P_FreeData(p_ptr);
334 0 cache->p_free_bytes = P_FreeBytes(p_ptr);
335 0 cache->p_n_reads = P_NReads(p_ptr);
336 0 cache->p_n_read_hits = P_NReadHits(p_ptr);
337 }
338  
339 /* Check page header */
340 ASSERT(P_Magic(p_ptr) == P_MAGIC);
341 ASSERT(P_NumSlots(p_ptr) >= cache->start_slots && P_NumSlots(p_ptr) < cache->c_page_size);
342 ASSERT(P_FreeSlots(p_ptr) <= P_NumSlots(p_ptr));
343 ASSERT(P_OldSlots(p_ptr) <= P_FreeSlots(p_ptr));
344 ASSERT(P_FreeData(p_ptr) + P_FreeBytes(p_ptr) == cache->c_page_size);
345  
346 /* Setup page pointers */
347 281319 cache->p_cur = p_cur;
348 281319 cache->p_offset = p_offset;
349 281319 cache->p_base = p_ptr;
350 281319 cache->p_base_slots = PTR_ADD(p_ptr, P_HEADERSIZE);
351  
352 ASSERT(_mmc_test_page(cache));
353  
354 281319 return 0;
355 }
356  
357 /*
358 * mmc_unlock(
359 * cache_mmap * cache
360 * )
361 *
362 * Unlock any currently locked page
363 *
364 */
365 281312 int mmc_unlock(mmap_cache * cache) {
366  
367 ASSERT(cache->p_cur != NOPAGE);
368  
369 /* A repair at lock time has been reported by now */
370 281312 cache->page_repaired = 0;
371  
372 /* A structural check failed during this lock. The page can't be
373 * saved back; start it afresh instead (the operation that found the
374 * problem has already reported a miss or a failed store) */
375 281312 ▶ if (cache->p_corrupt) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_corrupt) { 3 281309 281312
376 3 _mmc_repair_page(cache, cache->p_cur, cache->last_error);
377 3 cache->p_changed = 0;
378 3 cache->p_corrupt = 0;
379  
380 /* If changed, save page header changes back */
381 281309 ▶ } else if (cache->p_changed) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cache->p_changed) { 111142 170167 281309
382 111142 void * p_ptr = cache->p_base;
383  
384 /* The header fields are updated one at a time, so cover them with
385 * the marker like any other structural change */
386 111142 _mmc_mark_dirty(cache);
387  
388 /* Save any changed information back to page */
389 111142 P_NumSlots(p_ptr) = cache->p_num_slots;
390 111142 P_FreeSlots(p_ptr) = cache->p_free_slots;
391 111142 P_OldSlots(p_ptr) = cache->p_old_slots;
392 111142 P_FreeData(p_ptr) = cache->p_free_data;
393 111142 P_FreeBytes(p_ptr) = cache->p_free_bytes;
394 111142 P_NReads(p_ptr) = cache->p_n_reads;
395 111142 P_NReadHits(p_ptr) = cache->p_n_read_hits;
396  
397 111142 cache->p_changed = 0;
398 }
399  
400 /* Test before unlocking */
401 ASSERT(_mmc_test_page(cache));
402  
403 /* Structure is complete again; clear the marker before letting go */
404 281312 ▶ if (cache->p_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_dirty) { 111142 170170 281312
405 111142 P_SetMagic(cache->p_base, P_MAGIC);
406 111142 cache->p_dirty = 0;
407 }
408  
409 281312 mmc_unlock_page(cache, cache->p_offset);
410  
411 281312 cache->p_cur = NOPAGE;
412  
413 281312 return 0;
414 }
415  
416 /*
417 * mmc_is_locked(
418 * cache_mmap * cache
419 * )
420 *
421 * Return true if page is locked
422 *
423 */
424 103787 int mmc_is_locked(mmap_cache * cache) {
425  
426 103787 return cache->p_cur != NOPAGE ? 1 : 0;
427 }
428  
429 /*
430 * int mmc_hash(
431 * cache_mmap * cache,
432 * void *key_ptr, int key_len,
433 * MU32 *hash_page, MU32 *hash_slot
434 * )
435 *
436 * Hashes the given key, and returns hash value, hash page and hash
437 * slot part
438 *
439 */
440 289113 int mmc_hash(
441 mmap_cache *cache,
442 void *key_ptr, int key_len,
443 MU32 *hash_page, MU32 *hash_slot
444 ) {
445 289113 MU32 h = 0x92f7e3b1;
446 289113 unsigned char * uc_key_ptr = (unsigned char *)key_ptr;
447 289113 unsigned char * uc_key_ptr_end = uc_key_ptr + key_len;
448  
449 2958191 ▶ while (uc_key_ptr != uc_key_ptr_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (uc_key_ptr != uc_key_ptr_end) { 2669078 289113 2958191
450 2669078 h = (h << 4) + (h >> 28) + *uc_key_ptr++;
451 }
452  
453 289113 *hash_page = h % cache->c_num_pages;
454 289113 *hash_slot = h / cache->c_num_pages;
455  
456 289113 return 0;
457 }
458  
459 /*
460 * int mmc_read(
461 * cache_mmap * cache, MU32 hash_slot,
462 * void *key_ptr, int key_len,
463 * void **val_ptr, int *val_len,
464 * MU32 *expire_on, MU32 *flags, MU64 *modseq
465 * )
466 *
467 * Read key from current page. Tombstones read as not found. If the
468 * value carries a modseq, it's returned via *modseq and val_ptr/val_len
469 * cover just the value bytes (check FC_HASMODSEQ in *flags)
470 *
471 */
472 186067 int mmc_read(
473 mmap_cache *cache, MU32 hash_slot,
474 void *key_ptr, int key_len,
475 void **val_ptr, int *val_len,
476 MU32 *expire_on_p, MU32 *flags_p, MU64 *modseq_p
477 ) {
478 MU32 * slot_ptr;
479  
480 /* Increase read count for page */
481 186067 ▶ if (cache->enable_stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->enable_stats) { 102 185965 186067
482 102 cache->p_changed = 1;
483 102 cache->p_n_reads++;
484 }
485  
486 /* Search slots for key */
487 186067 slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 0);
488  
489 /* Did we find a value? */
490 186067 ▶ if (!slot_ptr || *slot_ptr == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!slot_ptr || *slot_ptr == 0) { 186065 2 186067
if (!slot_ptr || *slot_ptr == 0) { 37031 149034 186065
491  
492 /* Return -1 if not */
493 37033 return -1;
494  
495 /* We found it! Check some other things... */
496 } else {
497  
498 149034 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
499 149034 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 148983 51 149034
500  
501 149034 MU32 expire_on = S_ExpireOn(base_det);
502  
503 /* Sanity check hash matches */
504 ASSERT(S_SlotHash(base_det) == hash_slot);
505  
506 /* Value expired? */
507 149034 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 51 148983 149034
if (expire_on && now >= expire_on) { 19 32 51
508  
509 /* Return not found, but leave slot. Might need writeback */
510  
511 19 return -1;
512 }
513  
514 /* A tombstone is a miss (don't bump hit time - if it LRUs out
515 * early that just degrades to plain delete semantics) */
516 149015 ▶ if (S_Flags(base_det) & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_Flags(base_det) & FC_TOMBSTONE) { 5 149010 149015
517 5 return -1;
518 }
519  
520 /* Update hit time */
521 149010 S_LastAccess(base_det) = now;
522  
523 /* Copy values to pointers */
524 149010 *flags_p = S_Flags(base_det);
525 149010 *expire_on_p = expire_on;
526 149010 *val_len = S_ValLen(base_det);
527 149010 *val_ptr = S_ValPtr(base_det);
528  
529 /* Split off the modseq prefix if the value carries one */
530 149010 ▶ if (*flags_p & FC_HASMODSEQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*flags_p & FC_HASMODSEQ) { 10 149000 149010
531 10 memcpy(modseq_p, *val_ptr, FC_MODSEQ_LEN);
532 10 *val_ptr = PTR_ADD(*val_ptr, FC_MODSEQ_LEN);
533 10 *val_len -= FC_MODSEQ_LEN;
534 }
535  
536 /* Increase read hit count */
537 149010 ▶ if (cache->enable_stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->enable_stats) { 101 148909 149010
538 101 cache->p_changed = 1;
539 101 cache->p_n_read_hits++;
540 }
541  
542 149010 return 0;
543 }
544 }
545  
546 /*
547 * int mmc_write(
548 * cache_mmap * cache, MU32 hash_slot,
549 * void *key_ptr, int key_len,
550 * void *val_ptr, int val_len,
551 * MU32 expire_on, MU32 flags, MU64 modseq
552 * )
553 *
554 * Write key to current page. Returns 1 if stored, 0 if there was no
555 * space, -1 if the store was refused by the conditional-store rules
556 * (see below). modseq is only used if flags has FC_HASMODSEQ set
557 * (always set it with FC_TOMBSTONE).
558 *
559 * Conditional stores: an unexpired tombstone refuses any store without
560 * a modseq, and any store with an older modseq. A live entry with a
561 * modseq refuses stores with an older modseq. Tombstoning never lowers
562 * an existing tombstone's modseq, and tombstoning over a live entry
563 * whose modseq is already newer keeps the entry; both are success
564 * no-ops (the cache already reflects something at least as new).
565 *
566 */
567 103918 int mmc_write(
568 mmap_cache *cache, MU32 hash_slot,
569 void *key_ptr, int key_len,
570 void *val_ptr, int val_len,
571 MU32 expire_on, MU32 flags, MU64 modseq
572 ) {
573 103918 int did_store = 0;
574 103918 int ms_len = (flags & FC_HASMODSEQ) ? FC_MODSEQ_LEN : 0;
575 103918 MU32 kvlen = KV_SlotLen(key_len, val_len + ms_len);
576  
577 /* Search for slot with given key */
578 103918 MU32 * slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 1);
579  
580 /* If all slots full, definitely can't store */
581 103918 ▶ if (!slot_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!slot_ptr) 0 103918 103918
582 0 return 0;
583  
584 /* Check the conditional-store rules against any existing live entry */
585 103918 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 17091 86827 103918
586 17091 MU32 * old_det = S_Ptr(cache->p_base, *slot_ptr);
587 17091 MU32 old_flags = S_Flags(old_det);
588 17091 MU32 old_expire = S_ExpireOn(old_det);
589 17091 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 17071 20 17091
590  
591 17091 ▶ if ((old_flags & FC_HASMODSEQ) && !(old_expire && now >= old_expire)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((old_flags & FC_HASMODSEQ) && !(old_expire && now >= old_expire)) { 22 17069 17091
if ((old_flags & FC_HASMODSEQ) && !(old_expire && now >= old_expire)) { 12 10 22
if ((old_flags & FC_HASMODSEQ) && !(old_expire && now >= old_expire)) { 11 1 12
592 MU64 old_modseq;
593 21 memcpy(&old_modseq, S_ValPtr(old_det), FC_MODSEQ_LEN);
594  
595 21 ▶ if (old_flags & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_flags & FC_TOMBSTONE) { 11 10 21
596 11 ▶ if (flags & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & FC_TOMBSTONE) { 1 10 11
597 /* Re-tombstone: never lower the modseq */
598 1 ▶ if (modseq <= old_modseq)
Branch: 1 branch
BranchTrueFalseTotal
if (modseq <= old_modseq) 1 0 1
599 10 return 1;
600 10 ▶ } else if (!(flags & FC_HASMODSEQ) || modseq < old_modseq) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (!(flags & FC_HASMODSEQ) || modseq < old_modseq) { 6 4 10
} else if (!(flags & FC_HASMODSEQ) || modseq < old_modseq) { 3 3 6
601 /* Value is older than the invalidating change (or can't say) */
602 7 return -1;
603 }
604 10 ▶ } else if (flags & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (flags & FC_TOMBSTONE) { 5 5 10
605 /* Stale invalidation: the live entry is already newer */
606 5 ▶ if (modseq <= old_modseq)
Branch: 1 branch
BranchTrueFalseTotal
if (modseq <= old_modseq) 0 5 5
607 0 return 1;
608 5 ▶ } else if ((flags & FC_HASMODSEQ) && modseq < old_modseq) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ((flags & FC_HASMODSEQ) && modseq < old_modseq) { 4 1 5
} else if ((flags & FC_HASMODSEQ) && modseq < old_modseq) { 2 2 4
609 /* Never regress a live value to an older one */
610 2 return -1;
611 }
612 }
613 }
614  
615 103908 ROUNDLEN(kvlen);
616  
617 ASSERT(cache->p_cur != NOPAGE);
618  
619 /* If there's space, store the key/value in the data section.
620 * Important: we must not delete an existing slot for this key unless we
621 * actually have space for the replacement; otherwise a failed set() would
622 * silently destroy the previous value. */
623 103908 ▶ if (cache->p_free_bytes >= kvlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_free_bytes >= kvlen) { 103903 5 103908
624 MU32 * base_det;
625 MU32 now;
626  
627 103903 _mmc_mark_dirty(cache);
628  
629 /* If found, delete the existing slot before reusing it for the new value */
630 103903 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 17081 86822 103903
631 17081 _mmc_delete_slot(cache, slot_ptr);
632 ASSERT(*slot_ptr == 1);
633 }
634  
635 ASSERT(*slot_ptr <= 1);
636  
637 103903 base_det = PTR_ADD(cache->p_base, cache->p_free_data);
638 103903 ▶ now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
now = time_override ? time_override : (MU32)time(0); 103868 35 103903
639  
640 /* Calculate expiry time */
641 103903 ▶ if (expire_on == (MU32)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (expire_on == (MU32)-1) 103871 32 103903
642 103871 ▶ expire_on = cache->expire_time ? now + cache->expire_time : 0;
Branch: 1 branch
BranchTrueFalseTotal
expire_on = cache->expire_time ? now + cache->expire_time : 0; 7 103864 103871
643  
644 /* Store info into slot */
645 103903 S_LastAccess(base_det) = now;
646 103903 S_ExpireOn(base_det) = expire_on;
647 103903 S_SlotHash(base_det) = hash_slot;
648 103903 S_Flags(base_det) = flags;
649 103903 S_KeyLen(base_det) = (MU32)key_len;
650 103903 S_ValLen(base_det) = (MU32)(val_len + ms_len);
651  
652 /* Copy key/value to data section, modseq prefix first if present */
653 103903 memcpy(S_KeyPtr(base_det), key_ptr, key_len);
654 103903 ▶ if (ms_len)
Branch: 1 branch
BranchTrueFalseTotal
if (ms_len) 20 103883 103903
655 20 memcpy(S_ValPtr(base_det), &modseq, ms_len);
656 103903 memcpy(PTR_ADD(S_ValPtr(base_det), ms_len), val_ptr, val_len);
657  
658 /* Update used slots/free data info */
659 103903 cache->p_free_slots--;
660 103903 ▶ if (*slot_ptr == 1) { cache->p_old_slots--; }
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr == 1) { cache->p_old_slots--; } 23066 80837 103903
661  
662 /* Save new data offset */
663 103903 *slot_ptr = cache->p_free_data;
664  
665 /* Update free space */
666 103903 cache->p_free_bytes -= kvlen;
667 103903 cache->p_free_data += kvlen;
668  
669 /* Ensure changes are saved back */
670 103903 cache->p_changed = 1;
671  
672 103903 did_store = 1;
673 }
674  
675 103908 return did_store;
676 }
677  
678 /*
679 * int mmc_delete(
680 * cache_mmap * cache, MU32 hash_slot,
681 * void *key_ptr, int key_len
682 * )
683 *
684 * Delete key from current page
685 *
686 */
687 11396 int mmc_delete(
688 mmap_cache *cache, MU32 hash_slot,
689 void *key_ptr, int key_len,
690 MU32 * flags
691 ) {
692 /* Search slots for key */
693 11396 MU32 * slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 2);
694  
695 /* Did we find a value? */
696 11396 ▶ if (!slot_ptr || *slot_ptr == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!slot_ptr || *slot_ptr == 0) { 11396 0 11396
if (!slot_ptr || *slot_ptr == 0) { 5177 6219 11396
697  
698 /* Return 0 if not deleted */
699 5177 return 0;
700  
701 /* We found it, delete it */
702 } else {
703  
704 /* Store flags in output pointer */
705 6219 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
706 6219 *flags = S_Flags(base_det);
707  
708 6219 _mmc_delete_slot(cache, slot_ptr);
709 6219 return 1;
710 }
711  
712 }
713  
714 515456 int last_access_cmp(const void * a, const void * b) {
715 515456 MU32 av = S_LastAccess(*(MU32 **)a);
716 515456 MU32 bv = S_LastAccess(*(MU32 **)b);
717 515456 ▶ if (av < bv) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (av < bv) return -1; 7736 507720 515456
718 507720 ▶ if (av > bv) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (av > bv) return 1; 88837 418883 507720
719 418883 return 0;
720 }
721  
722 /*
723 * int mmc_calc_expunge(
724 * cache_mmap * cache, int mode, int len, MU32 * new_num_slots, MU32 *** to_expunge
725 * )
726 *
727 * Calculate entries to expunge from current page.
728 *
729 * If len >= 0
730 * If space available for len bytes & >30% slots free, nothing is expunged
731 * If len < 0 or not above
732 * If mode == 0, only expired items are expunged
733 * If mode == 1, all entries are expunged
734 * If mode == 2, entries are expunged till 40% free space is created
735 *
736 * If expunged is non-null pointer, result is filled with
737 * a list of slots to expunge
738 *
739 * Return value is number of items to expunge
740 *
741 */
742 104656 int mmc_calc_expunge(
743 mmap_cache * cache,
744 int mode, int len,
745 MU32 * new_num_slots, MU32 *** to_expunge
746 ) {
747 double slots_pct;
748  
749 ASSERT(cache->p_cur != NOPAGE);
750  
751 /* If len >= 0, and space available for len bytes, nothing is expunged */
752 104656 ▶ if (len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 0) { 103918 738 104656
753 /* Length of key/value data when stored */
754 103918 MU32 kvlen = KV_SlotLen(len, 0);
755 103918 ROUNDLEN(kvlen);
756  
757 103918 slots_pct = (double)(cache->p_free_slots - cache->p_old_slots) / cache->p_num_slots;
758  
759 /* Nothing to do if hash table more than 30% free slots and enough free space */
760 103918 ▶ if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen)
Branch: 2 branches
BranchTrueFalseTotal
if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen) 103826 92 103918
if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen) 95606 8220 103826
761 95606 return 0;
762 }
763  
764 {
765 9050 MU32 num_slots = cache->p_num_slots;
766  
767 9050 MU32 used_slots = num_slots - cache->p_free_slots;
768 9050 MU32 * slot_ptr = cache->p_base_slots;
769 9050 MU32 * slot_end = slot_ptr + num_slots;
770  
771 /* Store pointers to used slots */
772 9050 MU32 ** copy_base_det = (MU32 **)calloc(used_slots, sizeof(MU32 *));
773 9050 MU32 ** copy_base_det_end = copy_base_det + used_slots;
774 9050 MU32 ** copy_base_det_out = copy_base_det;
775 9050 MU32 ** copy_base_det_in = copy_base_det + used_slots;
776  
777 9050 MU32 page_data_size = cache->c_page_size - num_slots * 4 - P_HEADERSIZE;
778 9050 MU32 in_slots, data_thresh, used_data = 0;
779 9050 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 9047 3 9050
780  
781 /* Loop for each existing slot, and store in a list */
782 1170990 ▶ for (; slot_ptr != slot_end; slot_ptr++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; slot_ptr != slot_end; slot_ptr++) { 1161940 9050 1170990
783 1161940 MU32 data_offset = *slot_ptr;
784 1161940 MU32 * base_det = S_Ptr(cache->p_base, data_offset);
785 MU32 expire_on, kvlen;
786  
787 /* Ignore if if free slot */
788 1161940 ▶ if (data_offset <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset <= 1) { 1022123 139817 1161940
789 1022123 continue;
790 }
791  
792 /* Nothing on a page we can't walk safely is worth keeping or
793 * writing back; it is reinitialised at unlock */
794 139817 ▶ if (!_mmc_check_entry(cache, data_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, data_offset)) { 0 139817 139817
795 0 free(copy_base_det);
796 0 *to_expunge = 0;
797 0 *new_num_slots = num_slots;
798 0 return 0;
799 }
800  
801 /* Definitely out if mode == 1 which means expunge all */
802 139817 ▶ if (mode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == 1) { 12728 127089 139817
803 12728 *copy_base_det_out++ = base_det;
804 12728 continue;
805 }
806  
807 /* Definitely out if expired, and not dirty */
808 127089 expire_on = S_ExpireOn(base_det);
809 127089 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 8 127081 127089
if (expire_on && now >= expire_on) { 6 2 8
810 6 *copy_base_det_out++ = base_det;
811 6 continue;
812 }
813  
814 /* Track used space */
815 127083 kvlen = S_SlotLen(base_det);
816 127083 ROUNDLEN(kvlen);
817 ASSERT(kvlen <= page_data_size);
818 127083 used_data += kvlen;
819 ASSERT(used_data <= page_data_size);
820  
821 /* Potentially in */
822 127083 *--copy_base_det_in = base_det;
823 }
824  
825 /* Check that definitely in and out slots add up to used slots */
826 ASSERT(copy_base_det_in == copy_base_det_out);
827 ASSERT(mode != 1 || copy_base_det_out == copy_base_det_end);
828  
829 /* Increase slot count if free count is low and there's space to increase */
830 9050 slots_pct = (double)(copy_base_det_end - copy_base_det_out) / num_slots;
831 9050 ▶ if (slots_pct > 0.3 && (page_data_size - used_data > (num_slots + 1) * 4 || mode == 2)) {
Branch: 3 branches
BranchTrueFalseTotal
if (slots_pct > 0.3 && (page_data_size - used_data > (num_slots + 1) * 4 || mode == 2)) { 205 8845 9050
if (slots_pct > 0.3 && (page_data_size - used_data > (num_slots + 1) * 4 || mode == 2)) { 129 76 205
if (slots_pct > 0.3 && (page_data_size - used_data > (num_slots + 1) * 4 || mode == 2)) { 129 0 129
832 205 num_slots = (num_slots * 2) + 1;
833 }
834 9050 page_data_size = cache->c_page_size - num_slots * 4 - P_HEADERSIZE;
835  
836 /* If mode == 0 or 1, we've just worked out ones to keep and
837 * which to dispose of, so return results */
838 9050 ▶ if (mode == 0 || mode == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (mode == 0 || mode == 1) { 9047 3 9050
if (mode == 0 || mode == 1) { 735 8312 9047
839 738 *to_expunge = copy_base_det;
840 738 *new_num_slots = num_slots;
841 738 return (copy_base_det_out - copy_base_det);
842 }
843  
844 /* mode == 2, sort by last access, and remove till enough free space */
845  
846 /* Sort those potentially in by last access */
847 8312 in_slots = copy_base_det_end - copy_base_det_in;
848 8312 qsort((void *)copy_base_det_in, in_slots, sizeof(MU32 *), &last_access_cmp);
849  
850 /* Throw out old slots till we have 40% free data space */
851 8312 data_thresh = (MU32)(0.6 * page_data_size);
852  
853 57829 ▶ while (copy_base_det_in != copy_base_det_end && used_data >= data_thresh) {
Branch: 2 branches
BranchTrueFalseTotal
while (copy_base_det_in != copy_base_det_end && used_data >= data_thresh) { 57821 8 57829
while (copy_base_det_in != copy_base_det_end && used_data >= data_thresh) { 49517 8304 57821
854 49517 MU32 * slot_ptr = *copy_base_det_in;
855 49517 MU32 kvlen = S_SlotLen(slot_ptr);
856 49517 ROUNDLEN(kvlen);
857 ASSERT(kvlen <= page_data_size);
858 49517 used_data -= kvlen;
859  
860 ASSERT(used_data >= 0);
861  
862 49517 copy_base_det_out = ++copy_base_det_in;
863 }
864 ASSERT(used_data < page_data_size);
865  
866 8312 *to_expunge = copy_base_det;
867 8312 *new_num_slots = num_slots;
868 8312 return (copy_base_det_out - copy_base_det);
869 }
870 }
871  
872 /*
873 * int mmc_do_expunge(
874 * cache_mmap * cache, int num_expunge, MU32 new_num_slots, MU32 ** to_expunge
875 * )
876 *
877 * Expunge given entries from current page.
878 *
879 */
880 9050 int mmc_do_expunge(
881 mmap_cache * cache,
882 int num_expunge, MU32 new_num_slots, MU32 ** to_expunge
883 ) {
884 9050 MU32 * base_slots = cache->p_base_slots;
885  
886 9050 MU32 ** to_keep = to_expunge + num_expunge;
887 9050 MU32 ** to_keep_end = to_expunge + (cache->p_num_slots - cache->p_free_slots);
888 9050 MU32 new_used_slots = (to_keep_end - to_keep);
889  
890 /* Build new slots data and KV data */
891 9050 MU32 slot_data_size = new_num_slots * 4;
892 9050 MU32 * new_slot_data = (MU32 *)calloc(1, slot_data_size);
893  
894 9050 MU32 page_data_size = cache->c_page_size - new_num_slots * 4 - P_HEADERSIZE;
895  
896 9050 void * new_kv_data = calloc(1, page_data_size);
897 9050 MU32 new_offset = 0;
898  
899 /* Sanity check underlying fd is still the same file */
900 9050 ▶ if (!mmc_check_fh(cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mmc_check_fh(cache)) { 0 9050 9050
901 0 free(new_kv_data);
902 0 free(new_slot_data);
903 0 free(to_expunge);
904 0 return 0;
905 }
906  
907 9050 _mmc_mark_dirty(cache);
908  
909 /* Start all new slots empty */
910 9050 memset(new_slot_data, 0, slot_data_size);
911  
912 /* Copy entries to keep to new slot entires and data sections */
913 86616 ▶ for (;to_keep < to_keep_end; to_keep++) {
Branch: 1 branch
BranchTrueFalseTotal
for (;to_keep < to_keep_end; to_keep++) { 77566 9050 86616
914 77566 MU32 * old_base_det = *to_keep;
915 MU32 * new_slot_ptr;
916 MU32 kvlen;
917  
918 /* Hash key to find starting slot */
919 77566 MU32 slot = S_SlotHash(old_base_det) % new_num_slots;
920  
921 #ifdef DEBUG
922 /* Check hash actually matches stored value */
923 {
924 MU32 hash_page_dummy, hash_slot;
925 mmc_hash(cache, S_KeyPtr(old_base_det), S_KeyLen(old_base_det), &hash_page_dummy, &hash_slot);
926  
927 ASSERT(hash_slot == S_SlotHash(old_base_det));
928 }
929 #endif
930  
931 /* Find free slot */
932 77566 new_slot_ptr = new_slot_data + slot;
933 86608 ▶ while (*new_slot_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
while (*new_slot_ptr) { 9042 77566 86608
934 9042 ▶ if (++slot >= new_num_slots) { slot = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (++slot >= new_num_slots) { slot = 0; } 14 9028 9042
935 9042 new_slot_ptr = new_slot_data + slot;
936 }
937  
938 /* Copy slot and KV data */
939 77566 kvlen = S_SlotLen(old_base_det);
940 77566 memcpy(PTR_ADD(new_kv_data, new_offset), old_base_det, kvlen);
941  
942 /* Store slot data and mark as used */
943 77566 *new_slot_ptr = new_offset + new_num_slots * 4 + P_HEADERSIZE;
944  
945 77566 ROUNDLEN(kvlen);
946 77566 new_offset += kvlen;
947 }
948  
949 ASSERT(new_offset <= page_data_size);
950  
951 /* printf("page=%d\n", cache->p_cur);
952 printf("old_slots=%d, new_slots=%d\n", old_num_slots, new_num_slots);
953 printf("old_used_slots=%d, new_used_slots=%d\n", old_used_slots, new_used_slots);*/
954  
955 /* Store back into mmap'ed file space */
956 9050 memcpy(base_slots, new_slot_data, slot_data_size);
957 9050 memcpy(base_slots + new_num_slots, new_kv_data, new_offset);
958  
959 9050 cache->p_num_slots = new_num_slots;
960 9050 cache->p_free_slots = new_num_slots - new_used_slots;
961 9050 cache->p_old_slots = 0;
962 9050 cache->p_free_data = new_offset + new_num_slots * 4 + P_HEADERSIZE;
963 9050 cache->p_free_bytes = page_data_size - new_offset;
964  
965 /* Make sure changes are saved back to mmap'ed file */
966 9050 cache->p_changed = 1;
967  
968 /* Free allocated memory */
969 9050 free(new_kv_data);
970 9050 free(new_slot_data);
971 9050 free(to_expunge);
972  
973 ASSERT(_mmc_test_page(cache));
974  
975 9050 return 1;
976 }
977  
978 /*
979 * void mmc_get_page_details(mmap_cache * cache, MU32 * n_reads, MU32 * n_read_hits)
980 *
981 * Return details about the current locked page. Currently just
982 * number of reads and number of reads that hit
983 *
984 */
985 267 void mmc_get_page_details(mmap_cache * cache, MU32 * n_reads, MU32 * n_read_hits) {
986 267 *n_reads = cache->p_n_reads;
987 267 *n_read_hits = cache->p_n_read_hits;
988 267 return;
989 }
990  
991 /*
992 * void mmc_reset_page_details(mmap_cache * cache)
993 *
994 * Reset any page details (currently just read hits)
995 *
996 */
997 178 void mmc_reset_page_details(mmap_cache * cache) {
998 178 cache->p_n_reads = 0;
999 178 cache->p_n_read_hits = 0;
1000 178 cache->p_changed = 1;
1001 178 return;
1002 }
1003  
1004 /*
1005 * mmap_cache_it * mmc_iterate_new(mmap_cache * cache)
1006 *
1007 * Setup a new iterator to iterate over stored items
1008 * in the cache
1009 *
1010 */
1011 624 mmap_cache_it * mmc_iterate_new(mmap_cache * cache) {
1012 624 mmap_cache_it * it = (mmap_cache_it *)calloc(1, sizeof(mmap_cache_it));
1013 624 it->cache = cache;
1014 624 it->p_cur = NOPAGE;
1015  
1016 624 return it;
1017 }
1018  
1019 /*
1020 * MU32 * mmc_iterate_next(mmap_cache_it * it)
1021 *
1022 * Move iterator to next item in the cache and return
1023 * pointer to details (0 if there is no next).
1024 *
1025 * You can retrieve details with mmc_get_details(...)
1026 *
1027 */
1028 7077137 MU32 * mmc_iterate_next(mmap_cache_it * it) {
1029 7077137 mmap_cache * cache = it->cache;
1030 7077137 MU32 * slot_ptr = it->slot_ptr;
1031 MU32 * base_det;
1032 MU32 expire_on;
1033 7077137 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 7077119 18 7077137
1034  
1035 /* Go until we find a slot or exit */
1036 while (1) {
1037  
1038 /* End of page ... */
1039 59159966 ▶ if (slot_ptr == it->slot_ptr_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_ptr == it->slot_ptr_end) { 12138 59147828 59159966
1040  
1041 12138 ▶ if (it->p_cur == NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->p_cur == NOPAGE) { 624 11514 12138
1042 624 it->p_cur = 0;
1043  
1044 /* Unlock current page if any */
1045 } else {
1046 11514 mmc_unlock(it->cache);
1047  
1048 /* Move to the next page, return 0 if no more pages */
1049 11514 ▶ if (++it->p_cur == cache->c_num_pages) {
Branch: 1 branch
BranchTrueFalseTotal
if (++it->p_cur == cache->c_num_pages) { 624 10890 11514
1050 624 it->p_cur = NOPAGE;
1051 624 it->slot_ptr = 0;
1052 624 return 0;
1053 }
1054 }
1055  
1056 /* Lock the new page number. If the lock fails (bad header, sanity
1057 * check failed, etc.) cache->p_base_slots is not updated to the new
1058 * page, so walking it would re-iterate stale memory. End iteration
1059 * cleanly instead; the error is left in cache->last_error. */
1060 11514 ▶ if (mmc_lock(it->cache, it->p_cur) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mmc_lock(it->cache, it->p_cur) != 0) { 0 11514 11514
1061 0 it->p_cur = NOPAGE;
1062 0 it->slot_ptr = 0;
1063 0 return 0;
1064 }
1065  
1066 /* Setup new pointers */
1067 11514 slot_ptr = cache->p_base_slots;
1068 11514 it->slot_ptr_end = slot_ptr + cache->p_num_slots;
1069  
1070 /* Check again */
1071 11514 continue;
1072 }
1073  
1074 /* Slot not used */
1075 59147828 ▶ if (*slot_ptr <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr <= 1) { 52071306 7076522 59147828
1076 52071306 slot_ptr++;
1077 52071306 continue;
1078 }
1079  
1080 /* Can't walk a page with bad offsets; skip the rest of it (it is
1081 * reinitialised at unlock) */
1082 7076522 ▶ if (!_mmc_check_entry(cache, *slot_ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, *slot_ptr)) { 1 7076521 7076522
1083 1 slot_ptr = it->slot_ptr_end;
1084 1 continue;
1085 }
1086  
1087 /* Get pointer to details for this entry */
1088 7076521 base_det = S_Ptr(cache->p_base, *slot_ptr);
1089  
1090 /* Slot expired */
1091 7076521 expire_on = S_ExpireOn(base_det);
1092 7076521 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 23 7076498 7076521
if (expire_on && now >= expire_on) { 8 15 23
1093 8 slot_ptr++;
1094 8 continue;
1095 }
1096  
1097 7076513 break;
1098 }
1099  
1100 /* Move to the next slot for next iteration */
1101 7076513 it->slot_ptr = ++slot_ptr;
1102  
1103 /* Return that we found the next item */
1104 7076513 return base_det;
1105 }
1106  
1107 /*
1108 * void mmc_iterate_close(mmap_cache_it * it)
1109 *
1110 * Finish and dispose of iterator memory
1111 *
1112 */
1113 624 void mmc_iterate_close(mmap_cache_it * it) {
1114 /* Unlock page if locked */
1115 624 ▶ if (it->p_cur != NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->p_cur != NOPAGE) { 0 624 624
1116 0 mmc_unlock(it->cache);
1117 }
1118  
1119 /* Free memory */
1120 624 free(it);
1121 624 }
1122  
1123 /*
1124 * void mmc_get_details(
1125 * mmap_cache * cache,
1126 * MU32 * base_det,
1127 * void ** key_ptr, int * key_len,
1128 * void ** val_ptr, int * val_len,
1129 * MU32 * last_access, MU32 * expire_on, MU32 * flags, MU64 * modseq
1130 * )
1131 *
1132 * Given a base_det pointer to entries details
1133 * (as returned by mmc_iterate_next(...) and
1134 * mmc_calc_expunge(...)) return details of that
1135 * entry in the cache. Like mmc_read, a modseq prefix is split off
1136 * into *modseq and val_ptr/val_len cover just the value bytes
1137 *
1138 */
1139 7116875 void mmc_get_details(
1140 mmap_cache * cache,
1141 MU32 * base_det,
1142 void ** key_ptr, int * key_len,
1143 void ** val_ptr, int * val_len,
1144 MU32 * last_access, MU32 * expire_on, MU32 * flags, MU64 * modseq
1145 ) {
1146 7116875 cache = cache;
1147  
1148 7116875 *key_ptr = S_KeyPtr(base_det);
1149 7116875 *key_len = S_KeyLen(base_det);
1150  
1151 7116875 *val_ptr = S_ValPtr(base_det);
1152 7116875 *val_len = S_ValLen(base_det);
1153  
1154 7116875 *last_access = S_LastAccess(base_det);
1155 7116875 *expire_on = S_ExpireOn(base_det);
1156 7116875 *flags = S_Flags(base_det);
1157  
1158 7116875 ▶ if (*flags & FC_HASMODSEQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*flags & FC_HASMODSEQ) { 4 7116871 7116875
1159 4 memcpy(modseq, *val_ptr, FC_MODSEQ_LEN);
1160 4 *val_ptr = PTR_ADD(*val_ptr, FC_MODSEQ_LEN);
1161 4 *val_len -= FC_MODSEQ_LEN;
1162 }
1163 7116875 }
1164  
1165  
1166 /*
1167 * _mmc_delete_slot(
1168 * mmap_cache * cache, MU32 * slot_ptr
1169 * )
1170 *
1171 * Delete details from the given slot
1172 *
1173 */
1174 23300 void _mmc_delete_slot(
1175 mmap_cache * cache, MU32 * slot_ptr
1176 ) {
1177 ASSERT(*slot_ptr > 1);
1178 ASSERT(cache->p_cur != NOPAGE);
1179  
1180 23300 _mmc_mark_dirty(cache);
1181  
1182 /* Set offset to 1 */
1183 23300 *slot_ptr = 1;
1184  
1185 /* Increase slot free counters */
1186 23300 cache->p_free_slots++;
1187 23300 cache->p_old_slots++;
1188  
1189 /* Ensure changes are saved back */
1190 23300 cache->p_changed = 1;
1191 23300 }
1192  
1193 /*
1194 * MU32 * _mmc_find_slot(
1195 * mmap_cache * cache, MU32 hash_slot,
1196 * void *key_ptr, int key_len,
1197 * int mode
1198 * )
1199 *
1200 * Search current page for a particular 'key'. Use 'hash_slot' to
1201 * calculate starting slot. Return pointer to slot.
1202 *
1203 */
1204 301469 MU32 * _mmc_find_slot(
1205 mmap_cache * cache, MU32 hash_slot,
1206 void *key_ptr, int key_len,
1207 int mode
1208 ) {
1209 MU32 slots_left, * slots_end;
1210 /* Modulo hash_slot to find starting slot */
1211 301469 MU32 * slot_ptr = cache->p_base_slots + (hash_slot % cache->p_num_slots);
1212 301469 MU32 * first_deleted = (MU32 *)0;
1213  
1214 /* Total slots and pointer to end of slot data to do wrapping */
1215 301469 slots_left = cache->p_num_slots;
1216 301469 slots_end = cache->p_base_slots + slots_left;
1217  
1218 ASSERT(cache->p_cur != NOPAGE);
1219  
1220 /* Loop with integer probing till we find or don't */
1221 573738 ▶ while (slots_left--) {
Branch: 1 branch
BranchTrueFalseTotal
while (slots_left--) { 573738 0 573738
1222 573738 MU32 data_offset = *slot_ptr;
1223 ASSERT(data_offset == 0 || data_offset == 1 ||
1224 ((data_offset >= P_HEADERSIZE + cache->p_num_slots*4) &&
1225 (data_offset < cache->c_page_size) &&
1226 ((data_offset & 3) == 0)));
1227  
1228 /* data_offset == 0 means empty slot, and no more beyond */
1229 /* data_offset == 1 means deleted slot, we can reuse if writing */
1230 573738 ▶ if (data_offset == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 0) { 129035 444703 573738
1231 /* Return pointer to last checked slot */
1232 129035 break;
1233 }
1234 444703 ▶ if (data_offset == 1 && mode == 1 && 0 == first_deleted) {
Branch: 3 branches
BranchTrueFalseTotal
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 65765 378938 444703
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 8304 57461 65765
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 6039 2265 8304
1235 /* Save pointer to first usable slot; if we don't find the key later,
1236 we'll fall back to returning this.
1237 */
1238 6039 first_deleted = slot_ptr;
1239 }
1240 /* deleted slot, keep looking */
1241 444703 ▶ if (data_offset == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 1) { 378938 65765 444703
1242  
1243 } else {
1244 MU32 * base_det;
1245 MU32 fkey_len;
1246  
1247 /* A bad offset or length here would send us reading outside
1248 * the page. Treat it as not found; the page is reinitialised
1249 * when it's unlocked */
1250 378938 ▶ if (!_mmc_check_entry(cache, data_offset))
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, data_offset)) 2 378936 378938
1251 2 return (MU32 *)0;
1252  
1253 /* Offset is from start of data area */
1254 378936 base_det = S_Ptr(cache->p_base, data_offset);
1255  
1256 /* Two longs are key len and data len */
1257 378936 fkey_len = S_KeyLen(base_det);
1258  
1259 /* Key matches? */
1260 378936 ▶ if (fkey_len == (MU32)key_len && !memcmp(key_ptr, S_KeyPtr(base_det), key_len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fkey_len == (MU32)key_len && !memcmp(key_ptr, S_KeyPtr(base_det), key_len)) { 289446 89490 378936
if (fkey_len == (MU32)key_len && !memcmp(key_ptr, S_KeyPtr(base_det), key_len)) { 172432 117014 289446
1261  
1262 /* Yep, found it! */
1263 172432 return slot_ptr;
1264 }
1265 }
1266  
1267 /* Linear probe and wrap at end of slot data... */
1268 272269 ▶ if (++slot_ptr == slots_end) { slot_ptr = cache->p_base_slots; }
Branch: 1 branch
BranchTrueFalseTotal
if (++slot_ptr == slots_end) { slot_ptr = cache->p_base_slots; } 422 271847 272269
1269 ASSERT(slot_ptr >= cache->p_base_slots && slot_ptr < slots_end);
1270 }
1271 /* No slot found */
1272 129035 ▶ if (++slots_left == 0) slot_ptr = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (++slots_left == 0) slot_ptr = 0; 0 129035 129035
1273  
1274 129035 ▶ if (1 == mode && 0 != first_deleted)
Branch: 2 branches
BranchTrueFalseTotal
if (1 == mode && 0 != first_deleted) 86827 42208 129035
if (1 == mode && 0 != first_deleted) 5985 80842 86827
1275 5985 return first_deleted;
1276 else
1277 123050 return slot_ptr;
1278 }
1279  
1280 /*
1281 * void _mmc_init_page(mmap_cache * cache, int page)
1282 *
1283 * Initialise the given page as empty. It's expected
1284 * that you've already locked the page before doing
1285 * this
1286 *
1287 */
1288 139094 void _mmc_init_page(mmap_cache * cache, MU32 p_cur) {
1289 /* Setup page details */
1290 139094 MU64 p_offset = (MU64)p_cur * cache->c_page_size;
1291 139094 void * p_ptr = PTR_ADD(cache->mm_var, p_offset);
1292  
1293 /* Initialise to all 0's, then mark dirty until the header is complete
1294 * so a death in here leaves a page the next locker knows to redo */
1295 139094 memset(p_ptr, 0, cache->c_page_size);
1296 139094 P_SetMagic(p_ptr, P_MAGIC_DIRTY);
1297  
1298 /* Setup header */
1299 139094 P_NumSlots(p_ptr) = cache->start_slots;
1300 139094 P_FreeSlots(p_ptr) = cache->start_slots;
1301 139094 P_OldSlots(p_ptr) = 0;
1302 139094 P_FreeData(p_ptr) = P_HEADERSIZE + cache->start_slots * 4;
1303 139094 P_FreeBytes(p_ptr) = cache->c_page_size - P_FreeData(p_ptr);
1304 139094 P_NReads(p_ptr) = 0;
1305 139094 P_NReadHits(p_ptr) = 0;
1306  
1307 139094 P_SetMagic(p_ptr, P_MAGIC);
1308 139094 }
1309  
1310 /*
1311 * void _mmc_repair_page(mmap_cache * cache, MU32 p_cur, char * why)
1312 *
1313 * Reinitialise a page found in a state we can't use (dirty marker left
1314 * by a killed writer, or a failed structure check) and record that we
1315 * did, so the caller can report it. Expects the page to be locked.
1316 *
1317 */
1318 5 void _mmc_repair_page(mmap_cache * cache, MU32 p_cur, char * why) {
1319 char reason[256];
1320  
1321 /* why may point at last_error, which _mmc_set_error is about to
1322 * replace */
1323 5 ▶ strncpy(reason, why ? why : "unknown", sizeof(reason) - 1);
Branch: 1 branch
BranchTrueFalseTotal
strncpy(reason, why ? why : "unknown", sizeof(reason) - 1); 5 0 5
1324 5 reason[sizeof(reason) - 1] = 0;
1325  
1326 5 _mmc_init_page(cache, p_cur);
1327 5 cache->p_dirty = 0;
1328 5 cache->repaired_pages++;
1329 5 cache->page_repaired = 1;
1330 5 _mmc_set_error(cache, 0, "page %u of %s reinitialised: %s", p_cur, cache->share_file, reason);
1331 5 }
1332  
1333 /*
1334 * void _mmc_mark_dirty(mmap_cache * cache)
1335 *
1336 * Flag the current locked page as mid-update, before its structure is
1337 * changed. Cleared again by mmc_unlock.
1338 *
1339 */
1340 247395 void _mmc_mark_dirty(mmap_cache * cache) {
1341 ASSERT(cache->p_cur != NOPAGE);
1342 247395 ▶ if (cache->p_dirty)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_dirty) 136253 111142 247395
1343 136253 return;
1344 111142 P_SetMagic(cache->p_base, P_MAGIC_DIRTY);
1345 111142 cache->p_dirty = 1;
1346 }
1347  
1348 /*
1349 * int _mmc_check_entry(mmap_cache * cache, MU32 data_offset)
1350 *
1351 * Check a used slot's data offset and the entry header it points at
1352 * lie within the current page, so following them can't read outside
1353 * it. Returns 1 if so. If not, the page is flagged corrupt (mmc_unlock
1354 * reinitialises it) and 0 is returned; the caller should give up on
1355 * the page.
1356 *
1357 */
1358 7595277 int _mmc_check_entry(mmap_cache * cache, MU32 data_offset) {
1359 7595277 MU32 slots_end = P_HEADERSIZE + cache->p_num_slots * 4;
1360 MU32 * base_det;
1361 MU64 kvlen;
1362  
1363 7595277 ▶ if (data_offset < slots_end || (data_offset & 3) ||
Branch: 2 branches
BranchTrueFalseTotal
if (data_offset < slots_end || (data_offset & 3) || 7595277 0 7595277
if (data_offset < slots_end || (data_offset & 3) || 7595277 0 7595277
1364 7595277 ▶ (MU64)data_offset + sizeof(MU32) * 6 > cache->c_page_size) {
Branch: 1 branch
BranchTrueFalseTotal
(MU64)data_offset + sizeof(MU32) * 6 > cache->c_page_size) { 2 7595275 7595277
1365 2 cache->p_corrupt = 1;
1366 2 _mmc_set_error(cache, 0, "slot data offset %u outside page", data_offset);
1367 2 return 0;
1368 }
1369  
1370 7595275 base_det = S_Ptr(cache->p_base, data_offset);
1371 7595275 kvlen = sizeof(MU32) * 6 + (MU64)S_KeyLen(base_det) + (MU64)S_ValLen(base_det);
1372 7595275 ▶ if ((MU64)data_offset + kvlen > cache->c_page_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ((MU64)data_offset + kvlen > cache->c_page_size) { 1 7595274 7595275
1373 1 cache->p_corrupt = 1;
1374 1 _mmc_set_error(cache, 0, "entry at offset %u runs past end of page", data_offset);
1375 1 return 0;
1376 }
1377  
1378 7595274 return 1;
1379 }
1380  
1381 /*
1382 * int _mmc_test_page(mmap_cache * cache)
1383 *
1384 * Test integrity of current page
1385 *
1386 */
1387 7 int _mmc_test_page(mmap_cache * cache) {
1388 7 MU32 * slot_ptr = cache->p_base_slots;
1389 7 MU32 count_free = 0, count_old = 0, max_data_offset = 0;
1390 7 MU32 data_size = cache->c_page_size;
1391  
1392 ASSERT(cache->p_cur != NOPAGE);
1393 7 ▶ if (cache->p_cur == NOPAGE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur == NOPAGE) return 0; 0 7 7
1394  
1395 630 ▶ for (; slot_ptr < cache->p_base_slots + cache->p_num_slots; slot_ptr++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; slot_ptr < cache->p_base_slots + cache->p_num_slots; slot_ptr++) { 623 7 630
1396 623 MU32 data_offset = *slot_ptr;
1397  
1398 ASSERT(data_offset == 0 || data_offset == 1 ||
1399 (data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 &&
1400 data_offset < cache->c_page_size));
1401 623 ▶ if (!(data_offset == 0 || data_offset == 1 ||
Branch: 2 branches
BranchTrueFalseTotal
if (!(data_offset == 0 || data_offset == 1 || 88 535 623
if (!(data_offset == 0 || data_offset == 1 || 88 0 88
1402 88 ▶ (data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 &&
Branch: 1 branch
BranchTrueFalseTotal
(data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 && 88 0 88
1403 88 ▶ data_offset < cache->c_page_size))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
data_offset < cache->c_page_size))) return 0; 0 88 88
1404  
1405 623 ▶ if (data_offset == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 1) { 0 623 623
1406 0 count_old++;
1407 }
1408 623 ▶ if (data_offset <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset <= 1) { 535 88 623
1409 535 count_free++;
1410 535 continue;
1411 }
1412  
1413 88 ▶ if (data_offset > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset > 1) { 88 0 88
1414 88 MU32 * base_det = S_Ptr(cache->p_base, data_offset);
1415  
1416 88 MU32 last_access = S_LastAccess(base_det);
1417 88 MU32 expire_on = S_ExpireOn(base_det);
1418 88 MU32 key_len = S_KeyLen(base_det);
1419 88 MU32 val_len = S_ValLen(base_det);
1420 88 MU32 kvlen = S_SlotLen(base_det);
1421 88 ROUNDLEN(kvlen);
1422  
1423 ASSERT(last_access > 1000000000);
1424 88 ▶ if (!(last_access > 1000000000)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(last_access > 1000000000)) return 0; 0 88 88
1425 ASSERT(expire_on == 0 || (expire_on > 1000000000));
1426 88 ▶ if (!(expire_on == 0 || (expire_on > 1000000000))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(expire_on == 0 || (expire_on > 1000000000))) return 0; 0 88 88
if (!(expire_on == 0 || (expire_on > 1000000000))) return 0; 0 0 0
1427  
1428 ASSERT(key_len >= 0 && key_len < data_size);
1429 88 ▶ if (!(key_len >= 0 && key_len < data_size)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(key_len >= 0 && key_len < data_size)) return 0; 0 88 88
1430 ASSERT(val_len >= 0 && val_len < data_size);
1431 88 ▶ if (!(val_len >= 0 && val_len < data_size)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(val_len >= 0 && val_len < data_size)) return 0; 0 88 88
1432 ASSERT(kvlen >= 4*4 && kvlen < data_size);
1433 88 ▶ if (!(kvlen >= 4*4 && kvlen < data_size)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(kvlen >= 4*4 && kvlen < data_size)) return 0; 88 0 88
if (!(kvlen >= 4*4 && kvlen < data_size)) return 0; 0 88 88
1434  
1435 /* Keep track of largest end of data position */
1436 88 ▶ if (data_offset + kvlen > max_data_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset + kvlen > max_data_offset) { 29 59 88
1437 29 max_data_offset = data_offset + kvlen;
1438 }
1439  
1440 /* Check if key lookup finds same thing */
1441 {
1442 MU32 hash_page, hash_slot, * find_slot_ptr;
1443  
1444 /* Hash it */
1445 88 mmc_hash(cache, S_KeyPtr(base_det), (int)key_len,
1446 &hash_page, &hash_slot);
1447 ASSERT(hash_slot == S_SlotHash(base_det));
1448 88 ▶ if (!(hash_slot == S_SlotHash(base_det))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(hash_slot == S_SlotHash(base_det))) return 0; 0 88 88
1449  
1450 88 find_slot_ptr = _mmc_find_slot(cache, hash_slot, S_KeyPtr(base_det), key_len, 0);
1451  
1452 ASSERT(find_slot_ptr == slot_ptr);
1453 88 ▶ if (!(find_slot_ptr == slot_ptr)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(find_slot_ptr == slot_ptr)) return 0; 0 88 88
1454 }
1455  
1456 }
1457  
1458 }
1459  
1460 ASSERT(count_free == cache->p_free_slots);
1461 7 ▶ if (!(count_free == cache->p_free_slots)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(count_free == cache->p_free_slots)) return 0; 0 7 7
1462 ASSERT(count_old == cache->p_old_slots);
1463 7 ▶ if (!(count_old == cache->p_old_slots)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(count_old == cache->p_old_slots)) return 0; 0 7 7
1464 ASSERT(cache->p_free_data >= max_data_offset);
1465 7 ▶ if (!(cache->p_free_data >= max_data_offset)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(cache->p_free_data >= max_data_offset)) return 0; 0 7 7
1466  
1467 7 return 1;
1468 }
1469  
1470 /*
1471 * int _mmc_dump_page(mmap_cache * cache)
1472 *
1473 * Dump text version of current page to STDOUT
1474 *
1475 */
1476 0 int _mmc_dump_page(mmap_cache * cache) {
1477 MU32 slot;
1478  
1479 ASSERT(cache->p_cur != NOPAGE);
1480  
1481 0 printf("PageNum: %d\n", cache->p_cur);
1482 0 printf("\n");
1483 0 printf("PageSize: %d\n", cache->c_page_size);
1484 0 printf("BasePage: %p\n", cache->p_base);
1485 0 printf("BaseSlots: %p\n", cache->p_base_slots);
1486 0 printf("\n");
1487 0 printf("NumSlots: %d\n", cache->p_num_slots);
1488 0 printf("FreeSlots: %d\n", cache->p_free_slots);
1489 0 printf("OldSlots: %d\n", cache->p_old_slots);
1490 0 printf("FreeData: %d\n", cache->p_free_data);
1491 0 printf("FreeBytes: %d\n", cache->p_free_bytes);
1492  
1493 0 ▶ for (slot = 0; slot < cache->p_num_slots; slot++) {
Branch: 1 branch
BranchTrueFalseTotal
for (slot = 0; slot < cache->p_num_slots; slot++) { 0 0 0
1494 0 MU32 * slot_ptr = cache->p_base_slots + slot;
1495  
1496 0 printf("Slot: %d; OF=%d; ", slot, *slot_ptr);
1497  
1498 0 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 0 0 0
1499 0 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
1500 0 MU32 key_len = S_KeyLen(base_det);
1501 0 MU32 val_len = S_ValLen(base_det);
1502 char key[256], val[256];
1503  
1504 0 printf("LA=%d, ET=%d, HS=%d, FL=%d\n",
1505 0 S_LastAccess(base_det), S_ExpireOn(base_det),
1506 0 S_SlotHash(base_det), S_Flags(base_det));
1507  
1508 /* Get data */
1509 0 memcpy(key, S_KeyPtr(base_det), key_len > 256 ? 256 : key_len);
1510 0 key[key_len] = 0;
1511 0 memcpy(val, S_ValPtr(base_det), val_len > 256 ? 256 : val_len);
1512 0 val[val_len] = 0;
1513  
1514 0 printf(" K=%s, V=%s\n", key, val);
1515  
1516 }
1517  
1518 }
1519  
1520 0 return 0;
1521 }
1522  
1523  
1524  
1525  
« FastMmap.xs ↑ Summary