mmap_cache.c

filter: Statementstmt 75.6% 439 / 580 Branchbran 66.0% 210 / 318 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.2% 649 / 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 8068 mmap_cache * mmc_new() {
50 8068 mmap_cache * cache = (mmap_cache *)calloc(1, sizeof(mmap_cache));
51  
52 8068 cache->p_cur = NOPAGE;
53  
54 8068 cache->c_num_pages = def_c_num_pages;
55 8068 cache->c_page_size = def_c_page_size;
56  
57 8068 cache->start_slots = def_start_slots;
58 8068 cache->expire_time = def_expire_time;
59  
60 8068 cache->share_file = _mmc_get_def_share_filename(cache);
61 8068 cache->permissions = 0640;
62 8068 cache->init_file = def_init_file;
63 8068 cache->test_file = def_test_file;
64  
65 8068 return cache;
66 }
67  
68 72614 int mmc_set_param(mmap_cache * cache, char * param, char * val) {
69 72614 ▶ if (!strcmp(param, "init_file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(param, "init_file")) { 8068 64546 72614
70 8068 cache->init_file = atoi(val);
71 64546 ▶ } else if (!strcmp(param, "test_file")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "test_file")) { 8068 56478 64546
72 8068 cache->test_file = atoi(val);
73 56478 ▶ } else if (!strcmp(param, "page_size")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "page_size")) { 8068 48410 56478
74 8068 cache->c_page_size = atoi(val);
75 48410 ▶ } else if (!strcmp(param, "num_pages")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "num_pages")) { 8068 40342 48410
76 8068 cache->c_num_pages = atoi(val);
77 40342 ▶ } else if (!strcmp(param, "expire_time")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "expire_time")) { 8068 32274 40342
78 8068 cache->expire_time = atoi(val);
79 32274 ▶ } else if (!strcmp(param, "share_file")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "share_file")) { 8068 24206 32274
80 8068 cache->share_file = val;
81 24206 ▶ } else if (!strcmp(param, "permissions")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "permissions")) { 2 24204 24206
82 2 cache->permissions = atoi(val);
83 24204 ▶ } else if (!strcmp(param, "start_slots")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "start_slots")) { 8068 16136 24204
84 8068 cache->start_slots = atoi(val);
85 16136 ▶ } else if (!strcmp(param, "catch_deadlocks")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "catch_deadlocks")) { 8068 8068 16136
86 8068 cache->catch_deadlocks = atoi(val);
87 8068 ▶ } else if (!strcmp(param, "enable_stats")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "enable_stats")) { 8068 0 8068
88 8068 cache->enable_stats = atoi(val);
89 } else {
90 0 return _mmc_set_error(cache, 0, "Bad set_param parameter: %s", param);
91 }
92  
93 72614 return 0;
94 }
95  
96 9314 int mmc_get_param(mmap_cache * cache, char * param) {
97 9314 ▶ if (!strcmp(param, "page_size")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(param, "page_size")) { 0 9314 9314
98 0 return (int)cache->c_page_size;
99 9314 ▶ } else if (!strcmp(param, "num_pages")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "num_pages")) { 0 9314 9314
100 0 return (int)cache->c_num_pages;
101 9314 ▶ } else if (!strcmp(param, "expire_time")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "expire_time")) { 0 9314 9314
102 0 return (int)cache->expire_time;
103 9314 ▶ } else if (!strcmp(param, "repaired_pages")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "repaired_pages")) { 9314 0 9314
104 9314 return (int)cache->repaired_pages;
105 0 ▶ } else if (!strcmp(param, "debug")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(param, "debug")) { 0 0 0
106 #ifdef DEBUG
107 return 1;
108 #else
109 0 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 8068 int mmc_init(mmap_cache * cache) {
124 8068 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 8068 ▶ if (!cache->share_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cache->share_file) { 0 8068 8068
130 0 return _mmc_set_error(cache, 0, "No share file specified");
131 }
132  
133 /* Basic cache params */
134 8068 c_num_pages = cache->c_num_pages;
135 ASSERT(c_num_pages >= 1 && c_num_pages <= 1000);
136  
137 8068 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 8068 cache->c_size = c_size = (MU64)c_num_pages * c_page_size;
143  
144 8068 ▶ 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 8068 8068
145  
146 /* Map file into memory */
147 8068 ▶ if ( mmc_map_memory(cache) == -1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( mmc_map_memory(cache) == -1) return -1; 0 8068 8068
148  
149 /* Initialise pages if new file */
150 8068 ▶ if (do_init) {
Branch: 1 branch
BranchTrueFalseTotal
if (do_init) { 8068 0 8068
151 147150 ▶ for (i = 0; i < cache->c_num_pages; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cache->c_num_pages; i++) { 139082 8068 147150
152 139082 MU64 p_offset = (MU64)i * cache->c_page_size;
153 139082 mmc_lock_page(cache, p_offset);
154 139082 _mmc_init_page(cache, i);
155 139082 mmc_unlock_page(cache, p_offset);
156 }
157  
158 /* Unmap and re-map to stop gtop telling us our memory usage is up */
159 8068 ▶ if ( mmc_unmap_memory(cache) == -1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( mmc_unmap_memory(cache) == -1) return -1; 0 8068 8068
160 8068 ▶ if ( mmc_map_memory(cache) == -1) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( mmc_map_memory(cache) == -1) return -1; 0 8068 8068
161 }
162  
163 /* Test pages in file if asked */
164 8068 ▶ if (cache->test_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->test_file) { 0 8068 8068
165 0 ▶ for (i = 0; i < cache->c_num_pages; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cache->c_num_pages; i++) { 0 0 0
166 0 int bad_page = 0;
167 0 MU64 p_offset = (MU64)i * cache->c_page_size;
168  
169 /* Need to lock page, which tests header structure */
170 0 ▶ if (mmc_lock(cache, i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mmc_lock(cache, i)) { 0 0 0
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 0 ▶ if (!_mmc_test_page(cache))
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_test_page(cache)) 0 0 0
178 0 bad_page = 1;
179 }
180  
181 /* A bad page, initialise it */
182 0 ▶ if (bad_page) {
Branch: 1 branch
BranchTrueFalseTotal
if (bad_page) { 0 0 0
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 0 mmc_unlock_page(cache, p_offset);
191 0 cache->p_cur = NOPAGE;
192 }
193 }
194  
195 8068 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 8068 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 8068 ▶ if (cache->p_cur != NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur != NOPAGE) { 0 8068 8068
217 0 mmc_unlock(cache);
218 }
219  
220 /* Close file */
221 8068 ▶ if (cache->fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->fh) { 8068 0 8068
222 8068 mmc_close_fh(cache);
223 }
224  
225 /* Unmap memory */
226 8068 ▶ if (cache->mm_var) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->mm_var) { 8068 0 8068
227 8068 res = mmc_unmap_memory(cache);
228 8068 ▶ if (res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == -1) { 0 8068 8068
229 0 return _mmc_set_error(cache, errno, "Mmap of shared file %s failed", cache->share_file);
230 }
231 }
232  
233 8068 free(cache);
234  
235 8068 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 539172 int mmc_page_repaired(mmap_cache * cache) {
246 539172 return cache->page_repaired;
247 }
248  
249 1 char * mmc_error(mmap_cache * cache) {
250 1 ▶ if (cache->last_error)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->last_error) 1 0 1
251 1 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 281094 int mmc_lock(mmap_cache * cache, MU32 p_cur) {
265 MU64 p_offset;
266 void * p_ptr;
267 281094 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 281094 ▶ if (p_cur >= cache->c_num_pages)
Branch: 1 branch
BranchTrueFalseTotal
if (p_cur >= cache->c_num_pages) 0 281094 281094
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 281094 ▶ if (cache->p_cur != NOPAGE)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur != NOPAGE) 1 281093 281094
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 281093 p_offset = (MU64)p_cur * cache->c_page_size;
280 281093 p_ptr = PTR_ADD(cache->mm_var, p_offset);
281  
282 281093 res = mmc_lock_page(cache, p_offset);
283 281093 ▶ if (res) return res;
Branch: 1 branch
BranchTrueFalseTotal
if (res) return res; 0 281093 281093
284  
285 281093 cache->page_repaired = 0;
286 281093 cache->p_dirty = 0;
287 281093 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 281093 ▶ if (P_Magic(p_ptr) == P_MAGIC_DIRTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (P_Magic(p_ptr) == P_MAGIC_DIRTY) { 0 281093 281093
293 0 _mmc_repair_page(cache, p_cur, "left dirty by a killed writer");
294  
295 281093 ▶ } else if (P_Magic(p_ptr) != P_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (P_Magic(p_ptr) != P_MAGIC) { 0 281093 281093
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 281093 cache->p_num_slots = P_NumSlots(p_ptr);
302 281093 cache->p_free_slots = P_FreeSlots(p_ptr);
303 281093 cache->p_old_slots = P_OldSlots(p_ptr);
304 281093 cache->p_free_data = P_FreeData(p_ptr);
305 281093 cache->p_free_bytes = P_FreeBytes(p_ptr);
306 281093 cache->p_n_reads = P_NReads(p_ptr);
307 281093 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 281093 ▶ if (cache->p_num_slots < cache->start_slots ||
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_num_slots < cache->start_slots || 281093 0 281093
318 281093 ▶ 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 281093 281093
319 0 res = _mmc_set_error(cache, 0, "cache num_slots mismatch");
320 281093 ▶ 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 281093 281093
321 0 res = _mmc_set_error(cache, 0, "cache free slots mismatch");
322 281093 ▶ 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 281093 281093
323 0 res = _mmc_set_error(cache, 0, "cache old slots mismatch");
324 281093 ▶ 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 || 281093 0 281093
325 281093 ▶ cache->p_free_data > cache->c_page_size ||
Branch: 1 branch
BranchTrueFalseTotal
cache->p_free_data > cache->c_page_size || 281093 0 281093
326 281093 ▶ 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 281093 281093
327 0 res = _mmc_set_error(cache, 0, "cache free data mismatch");
328 281093 ▶ if (res) {
Branch: 1 branch
BranchTrueFalseTotal
if (res) { 0 281093 281093
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 281093 cache->p_cur = p_cur;
348 281093 cache->p_offset = p_offset;
349 281093 cache->p_base = p_ptr;
350 281093 cache->p_base_slots = PTR_ADD(p_ptr, P_HEADERSIZE);
351  
352 ASSERT(_mmc_test_page(cache));
353  
354 281093 return 0;
355 }
356  
357 /*
358 * mmc_unlock(
359 * cache_mmap * cache
360 * )
361 *
362 * Unlock any currently locked page
363 *
364 */
365 281093 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 281093 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 281093 ▶ if (cache->p_corrupt) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_corrupt) { 0 281093 281093
376 0 _mmc_repair_page(cache, cache->p_cur, cache->last_error);
377 0 cache->p_changed = 0;
378 0 cache->p_corrupt = 0;
379  
380 /* If changed, save page header changes back */
381 281093 ▶ } else if (cache->p_changed) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cache->p_changed) { 110956 170137 281093
382 110956 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 110956 _mmc_mark_dirty(cache);
387  
388 /* Save any changed information back to page */
389 110956 P_NumSlots(p_ptr) = cache->p_num_slots;
390 110956 P_FreeSlots(p_ptr) = cache->p_free_slots;
391 110956 P_OldSlots(p_ptr) = cache->p_old_slots;
392 110956 P_FreeData(p_ptr) = cache->p_free_data;
393 110956 P_FreeBytes(p_ptr) = cache->p_free_bytes;
394 110956 P_NReads(p_ptr) = cache->p_n_reads;
395 110956 P_NReadHits(p_ptr) = cache->p_n_read_hits;
396  
397 110956 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 281093 ▶ if (cache->p_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_dirty) { 110956 170137 281093
405 110956 P_SetMagic(cache->p_base, P_MAGIC);
406 110956 cache->p_dirty = 0;
407 }
408  
409 281093 mmc_unlock_page(cache, cache->p_offset);
410  
411 281093 cache->p_cur = NOPAGE;
412  
413 281093 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 103572 int mmc_is_locked(mmap_cache * cache) {
425  
426 103572 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 288611 int mmc_hash(
441 mmap_cache *cache,
442 void *key_ptr, int key_len,
443 MU32 *hash_page, MU32 *hash_slot
444 ) {
445 288611 MU32 h = 0x92f7e3b1;
446 288611 unsigned char * uc_key_ptr = (unsigned char *)key_ptr;
447 288611 unsigned char * uc_key_ptr_end = uc_key_ptr + key_len;
448  
449 2954942 ▶ while (uc_key_ptr != uc_key_ptr_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (uc_key_ptr != uc_key_ptr_end) { 2666331 288611 2954942
450 2666331 h = (h << 4) + (h >> 28) + *uc_key_ptr++;
451 }
452  
453 288611 *hash_page = h % cache->c_num_pages;
454 288611 *hash_slot = h / cache->c_num_pages;
455  
456 288611 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 186070 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 186070 ▶ if (cache->enable_stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->enable_stats) { 102 185968 186070
482 102 cache->p_changed = 1;
483 102 cache->p_n_reads++;
484 }
485  
486 /* Search slots for key */
487 186070 slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 0);
488  
489 /* Did we find a value? */
490 186070 ▶ if (!slot_ptr || *slot_ptr == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!slot_ptr || *slot_ptr == 0) { 186070 0 186070
if (!slot_ptr || *slot_ptr == 0) { 39461 146609 186070
491  
492 /* Return -1 if not */
493 39461 return -1;
494  
495 /* We found it! Check some other things... */
496 } else {
497  
498 146609 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
499 146609 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 146558 51 146609
500  
501 146609 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 146609 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 51 146558 146609
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 146590 ▶ if (S_Flags(base_det) & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_Flags(base_det) & FC_TOMBSTONE) { 5 146585 146590
517 5 return -1;
518 }
519  
520 /* Update hit time */
521 146585 S_LastAccess(base_det) = now;
522  
523 /* Copy values to pointers */
524 146585 *flags_p = S_Flags(base_det);
525 146585 *expire_on_p = expire_on;
526 146585 *val_len = S_ValLen(base_det);
527 146585 *val_ptr = S_ValPtr(base_det);
528  
529 /* Split off the modseq prefix if the value carries one */
530 146585 ▶ if (*flags_p & FC_HASMODSEQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*flags_p & FC_HASMODSEQ) { 10 146575 146585
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 146585 ▶ if (cache->enable_stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->enable_stats) { 101 146484 146585
538 101 cache->p_changed = 1;
539 101 cache->p_n_read_hits++;
540 }
541  
542 146585 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 103721 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 103721 int did_store = 0;
574 103721 int ms_len = (flags & FC_HASMODSEQ) ? FC_MODSEQ_LEN : 0;
575 103721 MU32 kvlen = KV_SlotLen(key_len, val_len + ms_len);
576  
577 /* Search for slot with given key */
578 103721 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 103721 ▶ if (!slot_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!slot_ptr) 0 103721 103721
582 0 return 0;
583  
584 /* Check the conditional-store rules against any existing live entry */
585 103721 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 17091 86630 103721
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 103711 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 103711 ▶ if (cache->p_free_bytes >= kvlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_free_bytes >= kvlen) { 103706 5 103711
624 MU32 * base_det;
625 MU32 now;
626  
627 103706 _mmc_mark_dirty(cache);
628  
629 /* If found, delete the existing slot before reusing it for the new value */
630 103706 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 17081 86625 103706
631 17081 _mmc_delete_slot(cache, slot_ptr);
632 ASSERT(*slot_ptr == 1);
633 }
634  
635 ASSERT(*slot_ptr <= 1);
636  
637 103706 base_det = PTR_ADD(cache->p_base, cache->p_free_data);
638 103706 ▶ now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
now = time_override ? time_override : (MU32)time(0); 103671 35 103706
639  
640 /* Calculate expiry time */
641 103706 ▶ if (expire_on == (MU32)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (expire_on == (MU32)-1) 103674 32 103706
642 103674 ▶ 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 103667 103674
643  
644 /* Store info into slot */
645 103706 S_LastAccess(base_det) = now;
646 103706 S_ExpireOn(base_det) = expire_on;
647 103706 S_SlotHash(base_det) = hash_slot;
648 103706 S_Flags(base_det) = flags;
649 103706 S_KeyLen(base_det) = (MU32)key_len;
650 103706 S_ValLen(base_det) = (MU32)(val_len + ms_len);
651  
652 /* Copy key/value to data section, modseq prefix first if present */
653 103706 memcpy(S_KeyPtr(base_det), key_ptr, key_len);
654 103706 ▶ if (ms_len)
Branch: 1 branch
BranchTrueFalseTotal
if (ms_len) 20 103686 103706
655 20 memcpy(S_ValPtr(base_det), &modseq, ms_len);
656 103706 memcpy(PTR_ADD(S_ValPtr(base_det), ms_len), val_ptr, val_len);
657  
658 /* Update used slots/free data info */
659 103706 cache->p_free_slots--;
660 103706 ▶ if (*slot_ptr == 1) { cache->p_old_slots--; }
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr == 1) { cache->p_old_slots--; } 23066 80640 103706
661  
662 /* Save new data offset */
663 103706 *slot_ptr = cache->p_free_data;
664  
665 /* Update free space */
666 103706 cache->p_free_bytes -= kvlen;
667 103706 cache->p_free_data += kvlen;
668  
669 /* Ensure changes are saved back */
670 103706 cache->p_changed = 1;
671  
672 103706 did_store = 1;
673 }
674  
675 103711 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) { 5166 6230 11396
697  
698 /* Return 0 if not deleted */
699 5166 return 0;
700  
701 /* We found it, delete it */
702 } else {
703  
704 /* Store flags in output pointer */
705 6230 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
706 6230 *flags = S_Flags(base_det);
707  
708 6230 _mmc_delete_slot(cache, slot_ptr);
709 6230 return 1;
710 }
711  
712 }
713  
714 558558 int last_access_cmp(const void * a, const void * b) {
715 558558 MU32 av = S_LastAccess(*(MU32 **)a);
716 558558 MU32 bv = S_LastAccess(*(MU32 **)b);
717 558558 ▶ if (av < bv) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (av < bv) return -1; 4051 554507 558558
718 554507 ▶ if (av > bv) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (av > bv) return 1; 124722 429785 554507
719 429785 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 104459 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 104459 ▶ if (len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 0) { 103721 738 104459
753 /* Length of key/value data when stored */
754 103721 MU32 kvlen = KV_SlotLen(len, 0);
755 103721 ROUNDLEN(kvlen);
756  
757 103721 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 103721 ▶ if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen)
Branch: 2 branches
BranchTrueFalseTotal
if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen) 103629 92 103721
if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen) 95394 8235 103629
761 95394 return 0;
762 }
763  
764 {
765 9065 MU32 num_slots = cache->p_num_slots;
766  
767 9065 MU32 used_slots = num_slots - cache->p_free_slots;
768 9065 MU32 * slot_ptr = cache->p_base_slots;
769 9065 MU32 * slot_end = slot_ptr + num_slots;
770  
771 /* Store pointers to used slots */
772 9065 MU32 ** copy_base_det = (MU32 **)calloc(used_slots, sizeof(MU32 *));
773 9065 MU32 ** copy_base_det_end = copy_base_det + used_slots;
774 9065 MU32 ** copy_base_det_out = copy_base_det;
775 9065 MU32 ** copy_base_det_in = copy_base_det + used_slots;
776  
777 9065 MU32 page_data_size = cache->c_page_size - num_slots * 4 - P_HEADERSIZE;
778 9065 MU32 in_slots, data_thresh, used_data = 0;
779 9065 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 9062 3 9065
780  
781 /* Loop for each existing slot, and store in a list */
782 1183680 ▶ for (; slot_ptr != slot_end; slot_ptr++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; slot_ptr != slot_end; slot_ptr++) { 1174615 9065 1183680
783 1174615 MU32 data_offset = *slot_ptr;
784 1174615 MU32 * base_det = S_Ptr(cache->p_base, data_offset);
785 MU32 expire_on, kvlen;
786  
787 /* Ignore if if free slot */
788 1174615 ▶ if (data_offset <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset <= 1) { 1034290 140325 1174615
789 1034290 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 140325 ▶ if (!_mmc_check_entry(cache, data_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, data_offset)) { 0 140325 140325
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 140325 ▶ if (mode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == 1) { 11708 128617 140325
803 11708 *copy_base_det_out++ = base_det;
804 11708 continue;
805 }
806  
807 /* Definitely out if expired, and not dirty */
808 128617 expire_on = S_ExpireOn(base_det);
809 128617 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 8 128609 128617
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 128611 kvlen = S_SlotLen(base_det);
816 128611 ROUNDLEN(kvlen);
817 ASSERT(kvlen <= page_data_size);
818 128611 used_data += kvlen;
819 ASSERT(used_data <= page_data_size);
820  
821 /* Potentially in */
822 128611 *--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 9065 slots_pct = (double)(copy_base_det_end - copy_base_det_out) / num_slots;
831 9065 ▶ 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 8860 9065
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 9065 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 9065 ▶ if (mode == 0 || mode == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (mode == 0 || mode == 1) { 9062 3 9065
if (mode == 0 || mode == 1) { 735 8327 9062
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 8327 in_slots = copy_base_det_end - copy_base_det_in;
848 8327 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 8327 data_thresh = (MU32)(0.6 * page_data_size);
852  
853 58867 ▶ 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) { 58859 8 58867
while (copy_base_det_in != copy_base_det_end && used_data >= data_thresh) { 50540 8319 58859
854 50540 MU32 * slot_ptr = *copy_base_det_in;
855 50540 MU32 kvlen = S_SlotLen(slot_ptr);
856 50540 ROUNDLEN(kvlen);
857 ASSERT(kvlen <= page_data_size);
858 50540 used_data -= kvlen;
859  
860 ASSERT(used_data >= 0);
861  
862 50540 copy_base_det_out = ++copy_base_det_in;
863 }
864 ASSERT(used_data < page_data_size);
865  
866 8327 *to_expunge = copy_base_det;
867 8327 *new_num_slots = num_slots;
868 8327 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 9065 int mmc_do_expunge(
881 mmap_cache * cache,
882 int num_expunge, MU32 new_num_slots, MU32 ** to_expunge
883 ) {
884 9065 MU32 * base_slots = cache->p_base_slots;
885  
886 9065 MU32 ** to_keep = to_expunge + num_expunge;
887 9065 MU32 ** to_keep_end = to_expunge + (cache->p_num_slots - cache->p_free_slots);
888 9065 MU32 new_used_slots = (to_keep_end - to_keep);
889  
890 /* Build new slots data and KV data */
891 9065 MU32 slot_data_size = new_num_slots * 4;
892 9065 MU32 * new_slot_data = (MU32 *)calloc(1, slot_data_size);
893  
894 9065 MU32 page_data_size = cache->c_page_size - new_num_slots * 4 - P_HEADERSIZE;
895  
896 9065 void * new_kv_data = calloc(1, page_data_size);
897 9065 MU32 new_offset = 0;
898  
899 /* Sanity check underlying fd is still the same file */
900 9065 ▶ if (!mmc_check_fh(cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mmc_check_fh(cache)) { 0 9065 9065
901 0 free(new_kv_data);
902 0 free(new_slot_data);
903 0 free(to_expunge);
904 0 return 0;
905 }
906  
907 9065 _mmc_mark_dirty(cache);
908  
909 /* Start all new slots empty */
910 9065 memset(new_slot_data, 0, slot_data_size);
911  
912 /* Copy entries to keep to new slot entires and data sections */
913 87136 ▶ for (;to_keep < to_keep_end; to_keep++) {
Branch: 1 branch
BranchTrueFalseTotal
for (;to_keep < to_keep_end; to_keep++) { 78071 9065 87136
914 78071 MU32 * old_base_det = *to_keep;
915 MU32 * new_slot_ptr;
916 MU32 kvlen;
917  
918 /* Hash key to find starting slot */
919 78071 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 78071 new_slot_ptr = new_slot_data + slot;
933 87263 ▶ while (*new_slot_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
while (*new_slot_ptr) { 9192 78071 87263
934 9192 ▶ if (++slot >= new_num_slots) { slot = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (++slot >= new_num_slots) { slot = 0; } 6 9186 9192
935 9192 new_slot_ptr = new_slot_data + slot;
936 }
937  
938 /* Copy slot and KV data */
939 78071 kvlen = S_SlotLen(old_base_det);
940 78071 memcpy(PTR_ADD(new_kv_data, new_offset), old_base_det, kvlen);
941  
942 /* Store slot data and mark as used */
943 78071 *new_slot_ptr = new_offset + new_num_slots * 4 + P_HEADERSIZE;
944  
945 78071 ROUNDLEN(kvlen);
946 78071 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 9065 memcpy(base_slots, new_slot_data, slot_data_size);
957 9065 memcpy(base_slots + new_num_slots, new_kv_data, new_offset);
958  
959 9065 cache->p_num_slots = new_num_slots;
960 9065 cache->p_free_slots = new_num_slots - new_used_slots;
961 9065 cache->p_old_slots = 0;
962 9065 cache->p_free_data = new_offset + new_num_slots * 4 + P_HEADERSIZE;
963 9065 cache->p_free_bytes = page_data_size - new_offset;
964  
965 /* Make sure changes are saved back to mmap'ed file */
966 9065 cache->p_changed = 1;
967  
968 /* Free allocated memory */
969 9065 free(new_kv_data);
970 9065 free(new_slot_data);
971 9065 free(to_expunge);
972  
973 ASSERT(_mmc_test_page(cache));
974  
975 9065 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 623 mmap_cache_it * mmc_iterate_new(mmap_cache * cache) {
1012 623 mmap_cache_it * it = (mmap_cache_it *)calloc(1, sizeof(mmap_cache_it));
1013 623 it->cache = cache;
1014 623 it->p_cur = NOPAGE;
1015  
1016 623 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 7080022 MU32 * mmc_iterate_next(mmap_cache_it * it) {
1029 7080022 mmap_cache * cache = it->cache;
1030 7080022 MU32 * slot_ptr = it->slot_ptr;
1031 MU32 * base_det;
1032 MU32 expire_on;
1033 7080022 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 7080004 18 7080022
1034  
1035 /* Go until we find a slot or exit */
1036 while (1) {
1037  
1038 /* End of page ... */
1039 59159423 ▶ if (slot_ptr == it->slot_ptr_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_ptr == it->slot_ptr_end) { 12130 59147293 59159423
1040  
1041 12130 ▶ if (it->p_cur == NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->p_cur == NOPAGE) { 623 11507 12130
1042 623 it->p_cur = 0;
1043  
1044 /* Unlock current page if any */
1045 } else {
1046 11507 mmc_unlock(it->cache);
1047  
1048 /* Move to the next page, return 0 if no more pages */
1049 11507 ▶ if (++it->p_cur == cache->c_num_pages) {
Branch: 1 branch
BranchTrueFalseTotal
if (++it->p_cur == cache->c_num_pages) { 623 10884 11507
1050 623 it->p_cur = NOPAGE;
1051 623 it->slot_ptr = 0;
1052 623 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 11507 ▶ if (mmc_lock(it->cache, it->p_cur) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mmc_lock(it->cache, it->p_cur) != 0) { 0 11507 11507
1061 0 it->p_cur = NOPAGE;
1062 0 it->slot_ptr = 0;
1063 0 return 0;
1064 }
1065  
1066 /* Setup new pointers */
1067 11507 slot_ptr = cache->p_base_slots;
1068 11507 it->slot_ptr_end = slot_ptr + cache->p_num_slots;
1069  
1070 /* Check again */
1071 11507 continue;
1072 }
1073  
1074 /* Slot not used */
1075 59147293 ▶ if (*slot_ptr <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr <= 1) { 52067886 7079407 59147293
1076 52067886 slot_ptr++;
1077 52067886 continue;
1078 }
1079  
1080 /* Can't walk a page with bad offsets; skip the rest of it (it is
1081 * reinitialised at unlock) */
1082 7079407 ▶ if (!_mmc_check_entry(cache, *slot_ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, *slot_ptr)) { 0 7079407 7079407
1083 0 slot_ptr = it->slot_ptr_end;
1084 0 continue;
1085 }
1086  
1087 /* Get pointer to details for this entry */
1088 7079407 base_det = S_Ptr(cache->p_base, *slot_ptr);
1089  
1090 /* Slot expired */
1091 7079407 expire_on = S_ExpireOn(base_det);
1092 7079407 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 23 7079384 7079407
if (expire_on && now >= expire_on) { 8 15 23
1093 8 slot_ptr++;
1094 8 continue;
1095 }
1096  
1097 7079399 break;
1098 }
1099  
1100 /* Move to the next slot for next iteration */
1101 7079399 it->slot_ptr = ++slot_ptr;
1102  
1103 /* Return that we found the next item */
1104 7079399 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 623 void mmc_iterate_close(mmap_cache_it * it) {
1114 /* Unlock page if locked */
1115 623 ▶ if (it->p_cur != NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->p_cur != NOPAGE) { 0 623 623
1116 0 mmc_unlock(it->cache);
1117 }
1118  
1119 /* Free memory */
1120 623 free(it);
1121 623 }
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 7120805 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 7120805 cache = cache;
1147  
1148 7120805 *key_ptr = S_KeyPtr(base_det);
1149 7120805 *key_len = S_KeyLen(base_det);
1150  
1151 7120805 *val_ptr = S_ValPtr(base_det);
1152 7120805 *val_len = S_ValLen(base_det);
1153  
1154 7120805 *last_access = S_LastAccess(base_det);
1155 7120805 *expire_on = S_ExpireOn(base_det);
1156 7120805 *flags = S_Flags(base_det);
1157  
1158 7120805 ▶ if (*flags & FC_HASMODSEQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*flags & FC_HASMODSEQ) { 4 7120801 7120805
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 7120805 }
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 23311 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 23311 _mmc_mark_dirty(cache);
1181  
1182 /* Set offset to 1 */
1183 23311 *slot_ptr = 1;
1184  
1185 /* Increase slot free counters */
1186 23311 cache->p_free_slots++;
1187 23311 cache->p_old_slots++;
1188  
1189 /* Ensure changes are saved back */
1190 23311 cache->p_changed = 1;
1191 23311 }
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 301187 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 301187 MU32 * slot_ptr = cache->p_base_slots + (hash_slot % cache->p_num_slots);
1212 301187 MU32 * first_deleted = (MU32 *)0;
1213  
1214 /* Total slots and pointer to end of slot data to do wrapping */
1215 301187 slots_left = cache->p_num_slots;
1216 301187 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 572993 ▶ while (slots_left--) {
Branch: 1 branch
BranchTrueFalseTotal
while (slots_left--) { 572993 0 572993
1222 572993 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 572993 ▶ if (data_offset == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 0) { 131257 441736 572993
1231 /* Return pointer to last checked slot */
1232 131257 break;
1233 }
1234 441736 ▶ if (data_offset == 1 && mode == 1 && 0 == first_deleted) {
Branch: 3 branches
BranchTrueFalseTotal
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 66197 375539 441736
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 8304 57893 66197
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 441736 ▶ if (data_offset == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 1) { 375539 66197 441736
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 375539 ▶ if (!_mmc_check_entry(cache, data_offset))
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, data_offset)) 0 375539 375539
1251 0 return (MU32 *)0;
1252  
1253 /* Offset is from start of data area */
1254 375539 base_det = S_Ptr(cache->p_base, data_offset);
1255  
1256 /* Two longs are key len and data len */
1257 375539 fkey_len = S_KeyLen(base_det);
1258  
1259 /* Key matches? */
1260 375539 ▶ 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)) { 286078 89461 375539
if (fkey_len == (MU32)key_len && !memcmp(key_ptr, S_KeyPtr(base_det), key_len)) { 169930 116148 286078
1261  
1262 /* Yep, found it! */
1263 169930 return slot_ptr;
1264 }
1265 }
1266  
1267 /* Linear probe and wrap at end of slot data... */
1268 271806 ▶ 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; } 372 271434 271806
1269 ASSERT(slot_ptr >= cache->p_base_slots && slot_ptr < slots_end);
1270 }
1271 /* No slot found */
1272 131257 ▶ if (++slots_left == 0) slot_ptr = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (++slots_left == 0) slot_ptr = 0; 0 131257 131257
1273  
1274 131257 ▶ if (1 == mode && 0 != first_deleted)
Branch: 2 branches
BranchTrueFalseTotal
if (1 == mode && 0 != first_deleted) 86630 44627 131257
if (1 == mode && 0 != first_deleted) 5985 80645 86630
1275 5985 return first_deleted;
1276 else
1277 125272 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 139082 void _mmc_init_page(mmap_cache * cache, MU32 p_cur) {
1289 /* Setup page details */
1290 139082 MU64 p_offset = (MU64)p_cur * cache->c_page_size;
1291 139082 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 139082 memset(p_ptr, 0, cache->c_page_size);
1296 139082 P_SetMagic(p_ptr, P_MAGIC_DIRTY);
1297  
1298 /* Setup header */
1299 139082 P_NumSlots(p_ptr) = cache->start_slots;
1300 139082 P_FreeSlots(p_ptr) = cache->start_slots;
1301 139082 P_OldSlots(p_ptr) = 0;
1302 139082 P_FreeData(p_ptr) = P_HEADERSIZE + cache->start_slots * 4;
1303 139082 P_FreeBytes(p_ptr) = cache->c_page_size - P_FreeData(p_ptr);
1304 139082 P_NReads(p_ptr) = 0;
1305 139082 P_NReadHits(p_ptr) = 0;
1306  
1307 139082 P_SetMagic(p_ptr, P_MAGIC);
1308 139082 }
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 0 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 0 ▶ strncpy(reason, why ? why : "unknown", sizeof(reason) - 1);
Branch: 1 branch
BranchTrueFalseTotal
strncpy(reason, why ? why : "unknown", sizeof(reason) - 1); 0 0 0
1324 0 reason[sizeof(reason) - 1] = 0;
1325  
1326 0 _mmc_init_page(cache, p_cur);
1327 0 cache->p_dirty = 0;
1328 0 cache->repaired_pages++;
1329 0 cache->page_repaired = 1;
1330 0 _mmc_set_error(cache, 0, "page %u of %s reinitialised: %s", p_cur, cache->share_file, reason);
1331 0 }
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 247038 void _mmc_mark_dirty(mmap_cache * cache) {
1341 ASSERT(cache->p_cur != NOPAGE);
1342 247038 ▶ if (cache->p_dirty)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_dirty) 136082 110956 247038
1343 136082 return;
1344 110956 P_SetMagic(cache->p_base, P_MAGIC_DIRTY);
1345 110956 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 7595271 int _mmc_check_entry(mmap_cache * cache, MU32 data_offset) {
1359 7595271 MU32 slots_end = P_HEADERSIZE + cache->p_num_slots * 4;
1360 MU32 * base_det;
1361 MU64 kvlen;
1362  
1363 7595271 ▶ if (data_offset < slots_end || (data_offset & 3) ||
Branch: 2 branches
BranchTrueFalseTotal
if (data_offset < slots_end || (data_offset & 3) || 7595271 0 7595271
if (data_offset < slots_end || (data_offset & 3) || 7595271 0 7595271
1364 7595271 ▶ (MU64)data_offset + sizeof(MU32) * 6 > cache->c_page_size) {
Branch: 1 branch
BranchTrueFalseTotal
(MU64)data_offset + sizeof(MU32) * 6 > cache->c_page_size) { 0 7595271 7595271
1365 0 cache->p_corrupt = 1;
1366 0 _mmc_set_error(cache, 0, "slot data offset %u outside page", data_offset);
1367 0 return 0;
1368 }
1369  
1370 7595271 base_det = S_Ptr(cache->p_base, data_offset);
1371 7595271 kvlen = sizeof(MU32) * 6 + (MU64)S_KeyLen(base_det) + (MU64)S_ValLen(base_det);
1372 7595271 ▶ if ((MU64)data_offset + kvlen > cache->c_page_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ((MU64)data_offset + kvlen > cache->c_page_size) { 0 7595271 7595271
1373 0 cache->p_corrupt = 1;
1374 0 _mmc_set_error(cache, 0, "entry at offset %u runs past end of page", data_offset);
1375 0 return 0;
1376 }
1377  
1378 7595271 return 1;
1379 }
1380  
1381 /*
1382 * int _mmc_test_page(mmap_cache * cache)
1383 *
1384 * Test integrity of current page
1385 *
1386 */
1387 0 int _mmc_test_page(mmap_cache * cache) {
1388 0 MU32 * slot_ptr = cache->p_base_slots;
1389 0 MU32 count_free = 0, count_old = 0, max_data_offset = 0;
1390 0 MU32 data_size = cache->c_page_size;
1391  
1392 ASSERT(cache->p_cur != NOPAGE);
1393 0 ▶ if (cache->p_cur == NOPAGE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur == NOPAGE) return 0; 0 0 0
1394  
1395 0 ▶ 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++) { 0 0 0
1396 0 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 0 ▶ if (!(data_offset == 0 || data_offset == 1 ||
Branch: 2 branches
BranchTrueFalseTotal
if (!(data_offset == 0 || data_offset == 1 || 0 0 0
if (!(data_offset == 0 || data_offset == 1 || 0 0 0
1402 0 ▶ (data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 &&
Branch: 1 branch
BranchTrueFalseTotal
(data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 && 0 0 0
1403 0 ▶ data_offset < cache->c_page_size))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
data_offset < cache->c_page_size))) return 0; 0 0 0
1404  
1405 0 ▶ if (data_offset == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 1) { 0 0 0
1406 0 count_old++;
1407 }
1408 0 ▶ if (data_offset <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset <= 1) { 0 0 0
1409 0 count_free++;
1410 0 continue;
1411 }
1412  
1413 0 ▶ if (data_offset > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset > 1) { 0 0 0
1414 0 MU32 * base_det = S_Ptr(cache->p_base, data_offset);
1415  
1416 0 MU32 last_access = S_LastAccess(base_det);
1417 0 MU32 expire_on = S_ExpireOn(base_det);
1418 0 MU32 key_len = S_KeyLen(base_det);
1419 0 MU32 val_len = S_ValLen(base_det);
1420 0 MU32 kvlen = S_SlotLen(base_det);
1421 0 ROUNDLEN(kvlen);
1422  
1423 ASSERT(last_access > 1000000000);
1424 0 ▶ if (!(last_access > 1000000000)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(last_access > 1000000000)) return 0; 0 0 0
1425 ASSERT(expire_on == 0 || (expire_on > 1000000000));
1426 0 ▶ if (!(expire_on == 0 || (expire_on > 1000000000))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(expire_on == 0 || (expire_on > 1000000000))) return 0; 0 0 0
if (!(expire_on == 0 || (expire_on > 1000000000))) return 0; 0 0 0
1427  
1428 ASSERT(key_len >= 0 && key_len < data_size);
1429 0 ▶ 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 0 0
1430 ASSERT(val_len >= 0 && val_len < data_size);
1431 0 ▶ 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 0 0
1432 ASSERT(kvlen >= 4*4 && kvlen < data_size);
1433 0 ▶ if (!(kvlen >= 4*4 && kvlen < data_size)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(kvlen >= 4*4 && kvlen < data_size)) return 0; 0 0 0
if (!(kvlen >= 4*4 && kvlen < data_size)) return 0; 0 0 0
1434  
1435 /* Keep track of largest end of data position */
1436 0 ▶ if (data_offset + kvlen > max_data_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset + kvlen > max_data_offset) { 0 0 0
1437 0 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 0 mmc_hash(cache, S_KeyPtr(base_det), (int)key_len,
1446 &hash_page, &hash_slot);
1447 ASSERT(hash_slot == S_SlotHash(base_det));
1448 0 ▶ if (!(hash_slot == S_SlotHash(base_det))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(hash_slot == S_SlotHash(base_det))) return 0; 0 0 0
1449  
1450 0 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 0 ▶ if (!(find_slot_ptr == slot_ptr)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(find_slot_ptr == slot_ptr)) return 0; 0 0 0
1454 }
1455  
1456 }
1457  
1458 }
1459  
1460 ASSERT(count_free == cache->p_free_slots);
1461 0 ▶ if (!(count_free == cache->p_free_slots)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(count_free == cache->p_free_slots)) return 0; 0 0 0
1462 ASSERT(count_old == cache->p_old_slots);
1463 0 ▶ if (!(count_old == cache->p_old_slots)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(count_old == cache->p_old_slots)) return 0; 0 0 0
1464 ASSERT(cache->p_free_data >= max_data_offset);
1465 0 ▶ 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 0 0
1466  
1467 0 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