mmap_cache.c

filter: Statementstmt 87.4% 516 / 590 Branchbran 78.5% 256 / 326 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.2% 772 / 916 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 /*
250 * int mmc_peek(mmap_cache * cache, MU64 offset, MU32 * val)
251 * int mmc_poke(mmap_cache * cache, MU64 offset, MU32 val)
252 *
253 * Read or write one MU32 at a byte offset into the mapping, with no
254 * locking or checking beyond the mapping's bounds. For tests, which use
255 * them to damage a page deliberately; going through the mapping rather
256 * than the file keeps them independent of whether the platform keeps
257 * mmap and file I/O coherent (OpenBSD doesn't without msync).
258 *
259 */
260 306 int mmc_peek(mmap_cache * cache, MU64 offset, MU32 * val) {
261 306 ▶ if ((offset & 3) || offset + sizeof(MU32) > cache->c_size)
Branch: 2 branches
BranchTrueFalseTotal
if ((offset & 3) || offset + sizeof(MU32) > cache->c_size) 306 0 306
if ((offset & 3) || offset + sizeof(MU32) > cache->c_size) 0 306 306
262 0 return _mmc_set_error(cache, 0, "peek offset %llu outside mapping", offset);
263 306 *val = *(MU32 *)PTR_ADD(cache->mm_var, offset);
264 306 return 0;
265 }
266  
267 59 int mmc_poke(mmap_cache * cache, MU64 offset, MU32 val) {
268 59 ▶ if ((offset & 3) || offset + sizeof(MU32) > cache->c_size)
Branch: 2 branches
BranchTrueFalseTotal
if ((offset & 3) || offset + sizeof(MU32) > cache->c_size) 59 0 59
if ((offset & 3) || offset + sizeof(MU32) > cache->c_size) 0 59 59
269 0 return _mmc_set_error(cache, 0, "poke offset %llu outside mapping", offset);
270 59 *(MU32 *)PTR_ADD(cache->mm_var, offset) = val;
271 59 return 0;
272 }
273  
274 6 char * mmc_error(mmap_cache * cache) {
275 6 ▶ if (cache->last_error)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->last_error) 6 0 6
276 6 return cache->last_error;
277 0 return "Unknown error";
278 }
279  
280 /*
281 * mmc_lock(
282 * cache_mmap * cache, MU32 p_cur
283 * )
284 *
285 * Lock the given page number using fcntl locking. Setup
286 * cache->p_* fields with correct values for the given page
287 *
288 */
289 281320 int mmc_lock(mmap_cache * cache, MU32 p_cur) {
290 MU64 p_offset;
291 void * p_ptr;
292 281320 int res = 0;
293  
294 /* Argument sanity check. Valid pages are 0 .. c_num_pages-1; NOPAGE is
295 * already excluded by the >= comparison since c_num_pages << NOPAGE. */
296 281320 ▶ if (p_cur >= cache->c_num_pages)
Branch: 1 branch
BranchTrueFalseTotal
if (p_cur >= cache->c_num_pages) 0 281320 281320
297 0 return _mmc_set_error(cache, 0, "page %u is NOPAGE or larger than number of pages", p_cur);
298  
299 /* Check not already locked */
300 281320 ▶ if (cache->p_cur != NOPAGE)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur != NOPAGE) 1 281319 281320
301 1 return _mmc_set_error(cache, 0, "page %u is already locked, can't lock multiple pages", cache->p_cur);
302  
303 /* Setup page details */
304 281319 p_offset = (MU64)p_cur * cache->c_page_size;
305 281319 p_ptr = PTR_ADD(cache->mm_var, p_offset);
306  
307 281319 res = mmc_lock_page(cache, p_offset);
308 281319 ▶ if (res) return res;
Branch: 1 branch
BranchTrueFalseTotal
if (res) return res; 0 281319 281319
309  
310 281319 cache->page_repaired = 0;
311 281319 cache->p_dirty = 0;
312 281319 cache->p_corrupt = 0;
313  
314 /* A dirty marker means whoever last held this page died (or was
315 * killed) part way through changing it; the lock has passed to us,
316 * but the page's structure can't be trusted, so start it afresh */
317 281319 ▶ if (P_Magic(p_ptr) == P_MAGIC_DIRTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (P_Magic(p_ptr) == P_MAGIC_DIRTY) { 2 281317 281319
318 2 _mmc_repair_page(cache, p_cur, "left dirty by a killed writer");
319  
320 281317 ▶ } else if (P_Magic(p_ptr) != P_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (P_Magic(p_ptr) != P_MAGIC) { 0 281317 281317
321 0 mmc_unlock_page(cache, p_offset);
322 0 return _mmc_set_error(cache, 0, "magic page start marker not found. p_cur is %u, offset is %llu", p_cur, p_offset);
323 }
324  
325 /* Copy to cache structure */
326 281319 cache->p_num_slots = P_NumSlots(p_ptr);
327 281319 cache->p_free_slots = P_FreeSlots(p_ptr);
328 281319 cache->p_old_slots = P_OldSlots(p_ptr);
329 281319 cache->p_free_data = P_FreeData(p_ptr);
330 281319 cache->p_free_bytes = P_FreeBytes(p_ptr);
331 281319 cache->p_n_reads = P_NReads(p_ptr);
332 281319 cache->p_n_read_hits = P_NReadHits(p_ptr);
333  
334 /* Reality check. Pages start with start_slots and only ever grow via
335 * expunge, so num_slots should never be below the configured
336 * start_slots, and the slot table, then the data, must fit in the
337 * page. A header that fails these was corrupted by something that
338 * predates the dirty marker (an older module version killed
339 * mid-update, say); the page is unusable as it is, so it is
340 * reinitialised like a dirty one rather than failing every access
341 * to it until someone recreates the file. */
342 281319 ▶ if (cache->p_num_slots < cache->start_slots ||
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_num_slots < cache->start_slots || 281319 0 281319
343 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
344 0 res = _mmc_set_error(cache, 0, "cache num_slots mismatch");
345 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
346 0 res = _mmc_set_error(cache, 0, "cache free slots mismatch");
347 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
348 0 res = _mmc_set_error(cache, 0, "cache old slots mismatch");
349 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
350 281319 ▶ cache->p_free_data > cache->c_page_size ||
Branch: 1 branch
BranchTrueFalseTotal
cache->p_free_data > cache->c_page_size || 281319 0 281319
351 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
352 0 res = _mmc_set_error(cache, 0, "cache free data mismatch");
353 281319 ▶ if (res) {
Branch: 1 branch
BranchTrueFalseTotal
if (res) { 0 281319 281319
354 0 _mmc_repair_page(cache, p_cur, cache->last_error);
355 0 cache->p_num_slots = P_NumSlots(p_ptr);
356 0 cache->p_free_slots = P_FreeSlots(p_ptr);
357 0 cache->p_old_slots = P_OldSlots(p_ptr);
358 0 cache->p_free_data = P_FreeData(p_ptr);
359 0 cache->p_free_bytes = P_FreeBytes(p_ptr);
360 0 cache->p_n_reads = P_NReads(p_ptr);
361 0 cache->p_n_read_hits = P_NReadHits(p_ptr);
362 }
363  
364 /* Check page header */
365 ASSERT(P_Magic(p_ptr) == P_MAGIC);
366 ASSERT(P_NumSlots(p_ptr) >= cache->start_slots && P_NumSlots(p_ptr) < cache->c_page_size);
367 ASSERT(P_FreeSlots(p_ptr) <= P_NumSlots(p_ptr));
368 ASSERT(P_OldSlots(p_ptr) <= P_FreeSlots(p_ptr));
369 ASSERT(P_FreeData(p_ptr) + P_FreeBytes(p_ptr) == cache->c_page_size);
370  
371 /* Setup page pointers */
372 281319 cache->p_cur = p_cur;
373 281319 cache->p_offset = p_offset;
374 281319 cache->p_base = p_ptr;
375 281319 cache->p_base_slots = PTR_ADD(p_ptr, P_HEADERSIZE);
376  
377 ASSERT(_mmc_test_page(cache));
378  
379 281319 return 0;
380 }
381  
382 /*
383 * mmc_unlock(
384 * cache_mmap * cache
385 * )
386 *
387 * Unlock any currently locked page
388 *
389 */
390 281312 int mmc_unlock(mmap_cache * cache) {
391  
392 ASSERT(cache->p_cur != NOPAGE);
393  
394 /* A repair at lock time has been reported by now */
395 281312 cache->page_repaired = 0;
396  
397 /* A structural check failed during this lock. The page can't be
398 * saved back; start it afresh instead (the operation that found the
399 * problem has already reported a miss or a failed store) */
400 281312 ▶ if (cache->p_corrupt) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_corrupt) { 3 281309 281312
401 3 _mmc_repair_page(cache, cache->p_cur, cache->last_error);
402 3 cache->p_changed = 0;
403 3 cache->p_corrupt = 0;
404  
405 /* If changed, save page header changes back */
406 281309 ▶ } else if (cache->p_changed) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cache->p_changed) { 111300 170009 281309
407 111300 void * p_ptr = cache->p_base;
408  
409 /* The header fields are updated one at a time, so cover them with
410 * the marker like any other structural change */
411 111300 _mmc_mark_dirty(cache);
412  
413 /* Save any changed information back to page */
414 111300 P_NumSlots(p_ptr) = cache->p_num_slots;
415 111300 P_FreeSlots(p_ptr) = cache->p_free_slots;
416 111300 P_OldSlots(p_ptr) = cache->p_old_slots;
417 111300 P_FreeData(p_ptr) = cache->p_free_data;
418 111300 P_FreeBytes(p_ptr) = cache->p_free_bytes;
419 111300 P_NReads(p_ptr) = cache->p_n_reads;
420 111300 P_NReadHits(p_ptr) = cache->p_n_read_hits;
421  
422 111300 cache->p_changed = 0;
423 }
424  
425 /* Test before unlocking */
426 ASSERT(_mmc_test_page(cache));
427  
428 /* Structure is complete again; clear the marker before letting go */
429 281312 ▶ if (cache->p_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_dirty) { 111300 170012 281312
430 111300 P_SetMagic(cache->p_base, P_MAGIC);
431 111300 cache->p_dirty = 0;
432 }
433  
434 281312 mmc_unlock_page(cache, cache->p_offset);
435  
436 281312 cache->p_cur = NOPAGE;
437  
438 281312 return 0;
439 }
440  
441 /*
442 * mmc_is_locked(
443 * cache_mmap * cache
444 * )
445 *
446 * Return true if page is locked
447 *
448 */
449 103744 int mmc_is_locked(mmap_cache * cache) {
450  
451 103744 return cache->p_cur != NOPAGE ? 1 : 0;
452 }
453  
454 /*
455 * int mmc_hash(
456 * cache_mmap * cache,
457 * void *key_ptr, int key_len,
458 * MU32 *hash_page, MU32 *hash_slot
459 * )
460 *
461 * Hashes the given key, and returns hash value, hash page and hash
462 * slot part
463 *
464 */
465 289113 int mmc_hash(
466 mmap_cache *cache,
467 void *key_ptr, int key_len,
468 MU32 *hash_page, MU32 *hash_slot
469 ) {
470 289113 MU32 h = 0x92f7e3b1;
471 289113 unsigned char * uc_key_ptr = (unsigned char *)key_ptr;
472 289113 unsigned char * uc_key_ptr_end = uc_key_ptr + key_len;
473  
474 2958190 ▶ while (uc_key_ptr != uc_key_ptr_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (uc_key_ptr != uc_key_ptr_end) { 2669077 289113 2958190
475 2669077 h = (h << 4) + (h >> 28) + *uc_key_ptr++;
476 }
477  
478 289113 *hash_page = h % cache->c_num_pages;
479 289113 *hash_slot = h / cache->c_num_pages;
480  
481 289113 return 0;
482 }
483  
484 /*
485 * int mmc_read(
486 * cache_mmap * cache, MU32 hash_slot,
487 * void *key_ptr, int key_len,
488 * void **val_ptr, int *val_len,
489 * MU32 *expire_on, MU32 *flags, MU64 *modseq
490 * )
491 *
492 * Read key from current page. Tombstones read as not found. If the
493 * value carries a modseq, it's returned via *modseq and val_ptr/val_len
494 * cover just the value bytes (check FC_HASMODSEQ in *flags)
495 *
496 */
497 186110 int mmc_read(
498 mmap_cache *cache, MU32 hash_slot,
499 void *key_ptr, int key_len,
500 void **val_ptr, int *val_len,
501 MU32 *expire_on_p, MU32 *flags_p, MU64 *modseq_p
502 ) {
503 MU32 * slot_ptr;
504  
505 /* Increase read count for page */
506 186110 ▶ if (cache->enable_stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->enable_stats) { 102 186008 186110
507 102 cache->p_changed = 1;
508 102 cache->p_n_reads++;
509 }
510  
511 /* Search slots for key */
512 186110 slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 0);
513  
514 /* Did we find a value? */
515 186110 ▶ if (!slot_ptr || *slot_ptr == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!slot_ptr || *slot_ptr == 0) { 186108 2 186110
if (!slot_ptr || *slot_ptr == 0) { 36274 149834 186108
516  
517 /* Return -1 if not */
518 36276 return -1;
519  
520 /* We found it! Check some other things... */
521 } else {
522  
523 149834 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
524 149834 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 149783 51 149834
525  
526 149834 MU32 expire_on = S_ExpireOn(base_det);
527  
528 /* Sanity check hash matches */
529 ASSERT(S_SlotHash(base_det) == hash_slot);
530  
531 /* Value expired? */
532 149834 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 51 149783 149834
if (expire_on && now >= expire_on) { 19 32 51
533  
534 /* Return not found, but leave slot. Might need writeback */
535  
536 19 return -1;
537 }
538  
539 /* A tombstone is a miss (don't bump hit time - if it LRUs out
540 * early that just degrades to plain delete semantics) */
541 149815 ▶ if (S_Flags(base_det) & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_Flags(base_det) & FC_TOMBSTONE) { 5 149810 149815
542 5 return -1;
543 }
544  
545 /* Update hit time */
546 149810 S_LastAccess(base_det) = now;
547  
548 /* Copy values to pointers */
549 149810 *flags_p = S_Flags(base_det);
550 149810 *expire_on_p = expire_on;
551 149810 *val_len = S_ValLen(base_det);
552 149810 *val_ptr = S_ValPtr(base_det);
553  
554 /* Split off the modseq prefix if the value carries one */
555 149810 ▶ if (*flags_p & FC_HASMODSEQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*flags_p & FC_HASMODSEQ) { 10 149800 149810
556 10 memcpy(modseq_p, *val_ptr, FC_MODSEQ_LEN);
557 10 *val_ptr = PTR_ADD(*val_ptr, FC_MODSEQ_LEN);
558 10 *val_len -= FC_MODSEQ_LEN;
559 }
560  
561 /* Increase read hit count */
562 149810 ▶ if (cache->enable_stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->enable_stats) { 101 149709 149810
563 101 cache->p_changed = 1;
564 101 cache->p_n_read_hits++;
565 }
566  
567 149810 return 0;
568 }
569 }
570  
571 /*
572 * int mmc_write(
573 * cache_mmap * cache, MU32 hash_slot,
574 * void *key_ptr, int key_len,
575 * void *val_ptr, int val_len,
576 * MU32 expire_on, MU32 flags, MU64 modseq
577 * )
578 *
579 * Write key to current page. Returns 1 if stored, 0 if there was no
580 * space, -1 if the store was refused by the conditional-store rules
581 * (see below). modseq is only used if flags has FC_HASMODSEQ set
582 * (always set it with FC_TOMBSTONE).
583 *
584 * Conditional stores: an unexpired tombstone refuses any store without
585 * a modseq, and any store with an older modseq. A live entry with a
586 * modseq refuses stores with an older modseq. Tombstoning never lowers
587 * an existing tombstone's modseq, and tombstoning over a live entry
588 * whose modseq is already newer keeps the entry; both are success
589 * no-ops (the cache already reflects something at least as new).
590 *
591 */
592 104071 int mmc_write(
593 mmap_cache *cache, MU32 hash_slot,
594 void *key_ptr, int key_len,
595 void *val_ptr, int val_len,
596 MU32 expire_on, MU32 flags, MU64 modseq
597 ) {
598 104071 int did_store = 0;
599 104071 int ms_len = (flags & FC_HASMODSEQ) ? FC_MODSEQ_LEN : 0;
600 104071 MU32 kvlen = KV_SlotLen(key_len, val_len + ms_len);
601  
602 /* Search for slot with given key */
603 104071 MU32 * slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 1);
604  
605 /* If all slots full, definitely can't store */
606 104071 ▶ if (!slot_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!slot_ptr) 0 104071 104071
607 0 return 0;
608  
609 /* Check the conditional-store rules against any existing live entry */
610 104071 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 17092 86979 104071
611 17092 MU32 * old_det = S_Ptr(cache->p_base, *slot_ptr);
612 17092 MU32 old_flags = S_Flags(old_det);
613 17092 MU32 old_expire = S_ExpireOn(old_det);
614 17092 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 17072 20 17092
615  
616 17092 ▶ 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 17070 17092
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
617 MU64 old_modseq;
618 21 memcpy(&old_modseq, S_ValPtr(old_det), FC_MODSEQ_LEN);
619  
620 21 ▶ if (old_flags & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_flags & FC_TOMBSTONE) { 11 10 21
621 11 ▶ if (flags & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & FC_TOMBSTONE) { 1 10 11
622 /* Re-tombstone: never lower the modseq */
623 1 ▶ if (modseq <= old_modseq)
Branch: 1 branch
BranchTrueFalseTotal
if (modseq <= old_modseq) 1 0 1
624 10 return 1;
625 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
626 /* Value is older than the invalidating change (or can't say) */
627 7 return -1;
628 }
629 10 ▶ } else if (flags & FC_TOMBSTONE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (flags & FC_TOMBSTONE) { 5 5 10
630 /* Stale invalidation: the live entry is already newer */
631 5 ▶ if (modseq <= old_modseq)
Branch: 1 branch
BranchTrueFalseTotal
if (modseq <= old_modseq) 0 5 5
632 0 return 1;
633 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
634 /* Never regress a live value to an older one */
635 2 return -1;
636 }
637 }
638 }
639  
640 104061 ROUNDLEN(kvlen);
641  
642 ASSERT(cache->p_cur != NOPAGE);
643  
644 /* If there's space, store the key/value in the data section.
645 * Important: we must not delete an existing slot for this key unless we
646 * actually have space for the replacement; otherwise a failed set() would
647 * silently destroy the previous value. */
648 104061 ▶ if (cache->p_free_bytes >= kvlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_free_bytes >= kvlen) { 104056 5 104061
649 MU32 * base_det;
650 MU32 now;
651  
652 104056 _mmc_mark_dirty(cache);
653  
654 /* If found, delete the existing slot before reusing it for the new value */
655 104056 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 17082 86974 104056
656 17082 _mmc_delete_slot(cache, slot_ptr);
657 ASSERT(*slot_ptr == 1);
658 }
659  
660 ASSERT(*slot_ptr <= 1);
661  
662 104056 base_det = PTR_ADD(cache->p_base, cache->p_free_data);
663 104056 ▶ now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
now = time_override ? time_override : (MU32)time(0); 104021 35 104056
664  
665 /* Calculate expiry time */
666 104056 ▶ if (expire_on == (MU32)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (expire_on == (MU32)-1) 104024 32 104056
667 104024 ▶ 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 104017 104024
668  
669 /* Store info into slot */
670 104056 S_LastAccess(base_det) = now;
671 104056 S_ExpireOn(base_det) = expire_on;
672 104056 S_SlotHash(base_det) = hash_slot;
673 104056 S_Flags(base_det) = flags;
674 104056 S_KeyLen(base_det) = (MU32)key_len;
675 104056 S_ValLen(base_det) = (MU32)(val_len + ms_len);
676  
677 /* Copy key/value to data section, modseq prefix first if present */
678 104056 memcpy(S_KeyPtr(base_det), key_ptr, key_len);
679 104056 ▶ if (ms_len)
Branch: 1 branch
BranchTrueFalseTotal
if (ms_len) 20 104036 104056
680 20 memcpy(S_ValPtr(base_det), &modseq, ms_len);
681 104056 memcpy(PTR_ADD(S_ValPtr(base_det), ms_len), val_ptr, val_len);
682  
683 /* Update used slots/free data info */
684 104056 cache->p_free_slots--;
685 104056 ▶ if (*slot_ptr == 1) { cache->p_old_slots--; }
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr == 1) { cache->p_old_slots--; } 23066 80990 104056
686  
687 /* Save new data offset */
688 104056 *slot_ptr = cache->p_free_data;
689  
690 /* Update free space */
691 104056 cache->p_free_bytes -= kvlen;
692 104056 cache->p_free_data += kvlen;
693  
694 /* Ensure changes are saved back */
695 104056 cache->p_changed = 1;
696  
697 104056 did_store = 1;
698 }
699  
700 104061 return did_store;
701 }
702  
703 /*
704 * int mmc_delete(
705 * cache_mmap * cache, MU32 hash_slot,
706 * void *key_ptr, int key_len
707 * )
708 *
709 * Delete key from current page
710 *
711 */
712 11396 int mmc_delete(
713 mmap_cache *cache, MU32 hash_slot,
714 void *key_ptr, int key_len,
715 MU32 * flags
716 ) {
717 /* Search slots for key */
718 11396 MU32 * slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 2);
719  
720 /* Did we find a value? */
721 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) { 5172 6224 11396
722  
723 /* Return 0 if not deleted */
724 5172 return 0;
725  
726 /* We found it, delete it */
727 } else {
728  
729 /* Store flags in output pointer */
730 6224 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
731 6224 *flags = S_Flags(base_det);
732  
733 6224 _mmc_delete_slot(cache, slot_ptr);
734 6224 return 1;
735 }
736  
737 }
738  
739 536475 int last_access_cmp(const void * a, const void * b) {
740 536475 MU32 av = S_LastAccess(*(MU32 **)a);
741 536475 MU32 bv = S_LastAccess(*(MU32 **)b);
742 536475 ▶ if (av < bv) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (av < bv) return -1; 8244 528231 536475
743 528231 ▶ if (av > bv) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (av > bv) return 1; 100586 427645 528231
744 427645 return 0;
745 }
746  
747 /*
748 * int mmc_calc_expunge(
749 * cache_mmap * cache, int mode, int len, MU32 * new_num_slots, MU32 *** to_expunge
750 * )
751 *
752 * Calculate entries to expunge from current page.
753 *
754 * If len >= 0
755 * If space available for len bytes & >30% slots free, nothing is expunged
756 * If len < 0 or not above
757 * If mode == 0, only expired items are expunged
758 * If mode == 1, all entries are expunged
759 * If mode == 2, entries are expunged till 40% free space is created
760 *
761 * If expunged is non-null pointer, result is filled with
762 * a list of slots to expunge
763 *
764 * Return value is number of items to expunge
765 *
766 */
767 104809 int mmc_calc_expunge(
768 mmap_cache * cache,
769 int mode, int len,
770 MU32 * new_num_slots, MU32 *** to_expunge
771 ) {
772 double slots_pct;
773  
774 ASSERT(cache->p_cur != NOPAGE);
775  
776 /* If len >= 0, and space available for len bytes, nothing is expunged */
777 104809 ▶ if (len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 0) { 104071 738 104809
778 /* Length of key/value data when stored */
779 104071 MU32 kvlen = KV_SlotLen(len, 0);
780 104071 ROUNDLEN(kvlen);
781  
782 104071 slots_pct = (double)(cache->p_free_slots - cache->p_old_slots) / cache->p_num_slots;
783  
784 /* Nothing to do if hash table more than 30% free slots and enough free space */
785 104071 ▶ if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen)
Branch: 2 branches
BranchTrueFalseTotal
if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen) 103979 92 104071
if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen) 95718 8261 103979
786 95718 return 0;
787 }
788  
789 {
790 9091 MU32 num_slots = cache->p_num_slots;
791  
792 9091 MU32 used_slots = num_slots - cache->p_free_slots;
793 9091 MU32 * slot_ptr = cache->p_base_slots;
794 9091 MU32 * slot_end = slot_ptr + num_slots;
795  
796 /* Store pointers to used slots */
797 9091 MU32 ** copy_base_det = (MU32 **)calloc(used_slots, sizeof(MU32 *));
798 9091 MU32 ** copy_base_det_end = copy_base_det + used_slots;
799 9091 MU32 ** copy_base_det_out = copy_base_det;
800 9091 MU32 ** copy_base_det_in = copy_base_det + used_slots;
801  
802 9091 MU32 page_data_size = cache->c_page_size - num_slots * 4 - P_HEADERSIZE;
803 9091 MU32 in_slots, data_thresh, used_data = 0;
804 9091 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 9088 3 9091
805  
806 /* Loop for each existing slot, and store in a list */
807 1186020 ▶ for (; slot_ptr != slot_end; slot_ptr++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; slot_ptr != slot_end; slot_ptr++) { 1176929 9091 1186020
808 1176929 MU32 data_offset = *slot_ptr;
809 1176929 MU32 * base_det = S_Ptr(cache->p_base, data_offset);
810 MU32 expire_on, kvlen;
811  
812 /* Ignore if if free slot */
813 1176929 ▶ if (data_offset <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset <= 1) { 1035020 141909 1176929
814 1035020 continue;
815 }
816  
817 /* Nothing on a page we can't walk safely is worth keeping or
818 * writing back; it is reinitialised at unlock */
819 141909 ▶ if (!_mmc_check_entry(cache, data_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, data_offset)) { 0 141909 141909
820 0 free(copy_base_det);
821 0 *to_expunge = 0;
822 0 *new_num_slots = num_slots;
823 0 return 0;
824 }
825  
826 /* Definitely out if mode == 1 which means expunge all */
827 141909 ▶ if (mode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == 1) { 12524 129385 141909
828 12524 *copy_base_det_out++ = base_det;
829 12524 continue;
830 }
831  
832 /* Definitely out if expired, and not dirty */
833 129385 expire_on = S_ExpireOn(base_det);
834 129385 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 8 129377 129385
if (expire_on && now >= expire_on) { 6 2 8
835 6 *copy_base_det_out++ = base_det;
836 6 continue;
837 }
838  
839 /* Track used space */
840 129379 kvlen = S_SlotLen(base_det);
841 129379 ROUNDLEN(kvlen);
842 ASSERT(kvlen <= page_data_size);
843 129379 used_data += kvlen;
844 ASSERT(used_data <= page_data_size);
845  
846 /* Potentially in */
847 129379 *--copy_base_det_in = base_det;
848 }
849  
850 /* Check that definitely in and out slots add up to used slots */
851 ASSERT(copy_base_det_in == copy_base_det_out);
852 ASSERT(mode != 1 || copy_base_det_out == copy_base_det_end);
853  
854 /* Increase slot count if free count is low and there's space to increase */
855 9091 slots_pct = (double)(copy_base_det_end - copy_base_det_out) / num_slots;
856 9091 ▶ 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 8886 9091
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
857 205 num_slots = (num_slots * 2) + 1;
858 }
859 9091 page_data_size = cache->c_page_size - num_slots * 4 - P_HEADERSIZE;
860  
861 /* If mode == 0 or 1, we've just worked out ones to keep and
862 * which to dispose of, so return results */
863 9091 ▶ if (mode == 0 || mode == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (mode == 0 || mode == 1) { 9088 3 9091
if (mode == 0 || mode == 1) { 735 8353 9088
864 738 *to_expunge = copy_base_det;
865 738 *new_num_slots = num_slots;
866 738 return (copy_base_det_out - copy_base_det);
867 }
868  
869 /* mode == 2, sort by last access, and remove till enough free space */
870  
871 /* Sort those potentially in by last access */
872 8353 in_slots = copy_base_det_end - copy_base_det_in;
873 8353 qsort((void *)copy_base_det_in, in_slots, sizeof(MU32 *), &last_access_cmp);
874  
875 /* Throw out old slots till we have 40% free data space */
876 8353 data_thresh = (MU32)(0.6 * page_data_size);
877  
878 58603 ▶ 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) { 58595 8 58603
while (copy_base_det_in != copy_base_det_end && used_data >= data_thresh) { 50250 8345 58595
879 50250 MU32 * slot_ptr = *copy_base_det_in;
880 50250 MU32 kvlen = S_SlotLen(slot_ptr);
881 50250 ROUNDLEN(kvlen);
882 ASSERT(kvlen <= page_data_size);
883 50250 used_data -= kvlen;
884  
885 ASSERT(used_data >= 0);
886  
887 50250 copy_base_det_out = ++copy_base_det_in;
888 }
889 ASSERT(used_data < page_data_size);
890  
891 8353 *to_expunge = copy_base_det;
892 8353 *new_num_slots = num_slots;
893 8353 return (copy_base_det_out - copy_base_det);
894 }
895 }
896  
897 /*
898 * int mmc_do_expunge(
899 * cache_mmap * cache, int num_expunge, MU32 new_num_slots, MU32 ** to_expunge
900 * )
901 *
902 * Expunge given entries from current page.
903 *
904 */
905 9091 int mmc_do_expunge(
906 mmap_cache * cache,
907 int num_expunge, MU32 new_num_slots, MU32 ** to_expunge
908 ) {
909 9091 MU32 * base_slots = cache->p_base_slots;
910  
911 9091 MU32 ** to_keep = to_expunge + num_expunge;
912 9091 MU32 ** to_keep_end = to_expunge + (cache->p_num_slots - cache->p_free_slots);
913 9091 MU32 new_used_slots = (to_keep_end - to_keep);
914  
915 /* Build new slots data and KV data */
916 9091 MU32 slot_data_size = new_num_slots * 4;
917 9091 MU32 * new_slot_data = (MU32 *)calloc(1, slot_data_size);
918  
919 9091 MU32 page_data_size = cache->c_page_size - new_num_slots * 4 - P_HEADERSIZE;
920  
921 9091 void * new_kv_data = calloc(1, page_data_size);
922 9091 MU32 new_offset = 0;
923  
924 /* Sanity check underlying fd is still the same file */
925 9091 ▶ if (!mmc_check_fh(cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mmc_check_fh(cache)) { 0 9091 9091
926 0 free(new_kv_data);
927 0 free(new_slot_data);
928 0 free(to_expunge);
929 0 return 0;
930 }
931  
932 9091 _mmc_mark_dirty(cache);
933  
934 /* Start all new slots empty */
935 9091 memset(new_slot_data, 0, slot_data_size);
936  
937 /* Copy entries to keep to new slot entires and data sections */
938 88220 ▶ for (;to_keep < to_keep_end; to_keep++) {
Branch: 1 branch
BranchTrueFalseTotal
for (;to_keep < to_keep_end; to_keep++) { 79129 9091 88220
939 79129 MU32 * old_base_det = *to_keep;
940 MU32 * new_slot_ptr;
941 MU32 kvlen;
942  
943 /* Hash key to find starting slot */
944 79129 MU32 slot = S_SlotHash(old_base_det) % new_num_slots;
945  
946 #ifdef DEBUG
947 /* Check hash actually matches stored value */
948 {
949 MU32 hash_page_dummy, hash_slot;
950 mmc_hash(cache, S_KeyPtr(old_base_det), S_KeyLen(old_base_det), &hash_page_dummy, &hash_slot);
951  
952 ASSERT(hash_slot == S_SlotHash(old_base_det));
953 }
954 #endif
955  
956 /* Find free slot */
957 79129 new_slot_ptr = new_slot_data + slot;
958 87668 ▶ while (*new_slot_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
while (*new_slot_ptr) { 8539 79129 87668
959 8539 ▶ if (++slot >= new_num_slots) { slot = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (++slot >= new_num_slots) { slot = 0; } 6 8533 8539
960 8539 new_slot_ptr = new_slot_data + slot;
961 }
962  
963 /* Copy slot and KV data */
964 79129 kvlen = S_SlotLen(old_base_det);
965 79129 memcpy(PTR_ADD(new_kv_data, new_offset), old_base_det, kvlen);
966  
967 /* Store slot data and mark as used */
968 79129 *new_slot_ptr = new_offset + new_num_slots * 4 + P_HEADERSIZE;
969  
970 79129 ROUNDLEN(kvlen);
971 79129 new_offset += kvlen;
972 }
973  
974 ASSERT(new_offset <= page_data_size);
975  
976 /* printf("page=%d\n", cache->p_cur);
977 printf("old_slots=%d, new_slots=%d\n", old_num_slots, new_num_slots);
978 printf("old_used_slots=%d, new_used_slots=%d\n", old_used_slots, new_used_slots);*/
979  
980 /* Store back into mmap'ed file space */
981 9091 memcpy(base_slots, new_slot_data, slot_data_size);
982 9091 memcpy(base_slots + new_num_slots, new_kv_data, new_offset);
983  
984 9091 cache->p_num_slots = new_num_slots;
985 9091 cache->p_free_slots = new_num_slots - new_used_slots;
986 9091 cache->p_old_slots = 0;
987 9091 cache->p_free_data = new_offset + new_num_slots * 4 + P_HEADERSIZE;
988 9091 cache->p_free_bytes = page_data_size - new_offset;
989  
990 /* Make sure changes are saved back to mmap'ed file */
991 9091 cache->p_changed = 1;
992  
993 /* Free allocated memory */
994 9091 free(new_kv_data);
995 9091 free(new_slot_data);
996 9091 free(to_expunge);
997  
998 ASSERT(_mmc_test_page(cache));
999  
1000 9091 return 1;
1001 }
1002  
1003 /*
1004 * void mmc_get_page_details(mmap_cache * cache, MU32 * n_reads, MU32 * n_read_hits)
1005 *
1006 * Return details about the current locked page. Currently just
1007 * number of reads and number of reads that hit
1008 *
1009 */
1010 267 void mmc_get_page_details(mmap_cache * cache, MU32 * n_reads, MU32 * n_read_hits) {
1011 267 *n_reads = cache->p_n_reads;
1012 267 *n_read_hits = cache->p_n_read_hits;
1013 267 return;
1014 }
1015  
1016 /*
1017 * void mmc_reset_page_details(mmap_cache * cache)
1018 *
1019 * Reset any page details (currently just read hits)
1020 *
1021 */
1022 178 void mmc_reset_page_details(mmap_cache * cache) {
1023 178 cache->p_n_reads = 0;
1024 178 cache->p_n_read_hits = 0;
1025 178 cache->p_changed = 1;
1026 178 return;
1027 }
1028  
1029 /*
1030 * mmap_cache_it * mmc_iterate_new(mmap_cache * cache)
1031 *
1032 * Setup a new iterator to iterate over stored items
1033 * in the cache
1034 *
1035 */
1036 624 mmap_cache_it * mmc_iterate_new(mmap_cache * cache) {
1037 624 mmap_cache_it * it = (mmap_cache_it *)calloc(1, sizeof(mmap_cache_it));
1038 624 it->cache = cache;
1039 624 it->p_cur = NOPAGE;
1040  
1041 624 return it;
1042 }
1043  
1044 /*
1045 * MU32 * mmc_iterate_next(mmap_cache_it * it)
1046 *
1047 * Move iterator to next item in the cache and return
1048 * pointer to details (0 if there is no next).
1049 *
1050 * You can retrieve details with mmc_get_details(...)
1051 *
1052 */
1053 6873317 MU32 * mmc_iterate_next(mmap_cache_it * it) {
1054 6873317 mmap_cache * cache = it->cache;
1055 6873317 MU32 * slot_ptr = it->slot_ptr;
1056 MU32 * base_det;
1057 MU32 expire_on;
1058 6873317 ▶ MU32 now = time_override ? time_override : (MU32)time(0);
Branch: 1 branch
BranchTrueFalseTotal
MU32 now = time_override ? time_override : (MU32)time(0); 6873299 18 6873317
1059  
1060 /* Go until we find a slot or exit */
1061 while (1) {
1062  
1063 /* End of page ... */
1064 59159966 ▶ if (slot_ptr == it->slot_ptr_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot_ptr == it->slot_ptr_end) { 12138 59147828 59159966
1065  
1066 12138 ▶ if (it->p_cur == NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->p_cur == NOPAGE) { 624 11514 12138
1067 624 it->p_cur = 0;
1068  
1069 /* Unlock current page if any */
1070 } else {
1071 11514 mmc_unlock(it->cache);
1072  
1073 /* Move to the next page, return 0 if no more pages */
1074 11514 ▶ if (++it->p_cur == cache->c_num_pages) {
Branch: 1 branch
BranchTrueFalseTotal
if (++it->p_cur == cache->c_num_pages) { 624 10890 11514
1075 624 it->p_cur = NOPAGE;
1076 624 it->slot_ptr = 0;
1077 624 return 0;
1078 }
1079 }
1080  
1081 /* Lock the new page number. If the lock fails (bad header, sanity
1082 * check failed, etc.) cache->p_base_slots is not updated to the new
1083 * page, so walking it would re-iterate stale memory. End iteration
1084 * cleanly instead; the error is left in cache->last_error. */
1085 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
1086 0 it->p_cur = NOPAGE;
1087 0 it->slot_ptr = 0;
1088 0 return 0;
1089 }
1090  
1091 /* Setup new pointers */
1092 11514 slot_ptr = cache->p_base_slots;
1093 11514 it->slot_ptr_end = slot_ptr + cache->p_num_slots;
1094  
1095 /* Check again */
1096 11514 continue;
1097 }
1098  
1099 /* Slot not used */
1100 59147828 ▶ if (*slot_ptr <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr <= 1) { 52275126 6872702 59147828
1101 52275126 slot_ptr++;
1102 52275126 continue;
1103 }
1104  
1105 /* Can't walk a page with bad offsets; skip the rest of it (it is
1106 * reinitialised at unlock) */
1107 6872702 ▶ if (!_mmc_check_entry(cache, *slot_ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, *slot_ptr)) { 1 6872701 6872702
1108 1 slot_ptr = it->slot_ptr_end;
1109 1 continue;
1110 }
1111  
1112 /* Get pointer to details for this entry */
1113 6872701 base_det = S_Ptr(cache->p_base, *slot_ptr);
1114  
1115 /* Slot expired */
1116 6872701 expire_on = S_ExpireOn(base_det);
1117 6872701 ▶ if (expire_on && now >= expire_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (expire_on && now >= expire_on) { 23 6872678 6872701
if (expire_on && now >= expire_on) { 8 15 23
1118 8 slot_ptr++;
1119 8 continue;
1120 }
1121  
1122 6872693 break;
1123 }
1124  
1125 /* Move to the next slot for next iteration */
1126 6872693 it->slot_ptr = ++slot_ptr;
1127  
1128 /* Return that we found the next item */
1129 6872693 return base_det;
1130 }
1131  
1132 /*
1133 * void mmc_iterate_close(mmap_cache_it * it)
1134 *
1135 * Finish and dispose of iterator memory
1136 *
1137 */
1138 624 void mmc_iterate_close(mmap_cache_it * it) {
1139 /* Unlock page if locked */
1140 624 ▶ if (it->p_cur != NOPAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->p_cur != NOPAGE) { 0 624 624
1141 0 mmc_unlock(it->cache);
1142 }
1143  
1144 /* Free memory */
1145 624 free(it);
1146 624 }
1147  
1148 /*
1149 * void mmc_get_details(
1150 * mmap_cache * cache,
1151 * MU32 * base_det,
1152 * void ** key_ptr, int * key_len,
1153 * void ** val_ptr, int * val_len,
1154 * MU32 * last_access, MU32 * expire_on, MU32 * flags, MU64 * modseq
1155 * )
1156 *
1157 * Given a base_det pointer to entries details
1158 * (as returned by mmc_iterate_next(...) and
1159 * mmc_calc_expunge(...)) return details of that
1160 * entry in the cache. Like mmc_read, a modseq prefix is split off
1161 * into *modseq and val_ptr/val_len cover just the value bytes
1162 *
1163 */
1164 6913594 void mmc_get_details(
1165 mmap_cache * cache,
1166 MU32 * base_det,
1167 void ** key_ptr, int * key_len,
1168 void ** val_ptr, int * val_len,
1169 MU32 * last_access, MU32 * expire_on, MU32 * flags, MU64 * modseq
1170 ) {
1171 6913594 cache = cache;
1172  
1173 6913594 *key_ptr = S_KeyPtr(base_det);
1174 6913594 *key_len = S_KeyLen(base_det);
1175  
1176 6913594 *val_ptr = S_ValPtr(base_det);
1177 6913594 *val_len = S_ValLen(base_det);
1178  
1179 6913594 *last_access = S_LastAccess(base_det);
1180 6913594 *expire_on = S_ExpireOn(base_det);
1181 6913594 *flags = S_Flags(base_det);
1182  
1183 6913594 ▶ if (*flags & FC_HASMODSEQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*flags & FC_HASMODSEQ) { 4 6913590 6913594
1184 4 memcpy(modseq, *val_ptr, FC_MODSEQ_LEN);
1185 4 *val_ptr = PTR_ADD(*val_ptr, FC_MODSEQ_LEN);
1186 4 *val_len -= FC_MODSEQ_LEN;
1187 }
1188 6913594 }
1189  
1190  
1191 /*
1192 * _mmc_delete_slot(
1193 * mmap_cache * cache, MU32 * slot_ptr
1194 * )
1195 *
1196 * Delete details from the given slot
1197 *
1198 */
1199 23306 void _mmc_delete_slot(
1200 mmap_cache * cache, MU32 * slot_ptr
1201 ) {
1202 ASSERT(*slot_ptr > 1);
1203 ASSERT(cache->p_cur != NOPAGE);
1204  
1205 23306 _mmc_mark_dirty(cache);
1206  
1207 /* Set offset to 1 */
1208 23306 *slot_ptr = 1;
1209  
1210 /* Increase slot free counters */
1211 23306 cache->p_free_slots++;
1212 23306 cache->p_old_slots++;
1213  
1214 /* Ensure changes are saved back */
1215 23306 cache->p_changed = 1;
1216 23306 }
1217  
1218 /*
1219 * MU32 * _mmc_find_slot(
1220 * mmap_cache * cache, MU32 hash_slot,
1221 * void *key_ptr, int key_len,
1222 * int mode
1223 * )
1224 *
1225 * Search current page for a particular 'key'. Use 'hash_slot' to
1226 * calculate starting slot. Return pointer to slot.
1227 *
1228 */
1229 301664 MU32 * _mmc_find_slot(
1230 mmap_cache * cache, MU32 hash_slot,
1231 void *key_ptr, int key_len,
1232 int mode
1233 ) {
1234 MU32 slots_left, * slots_end;
1235 /* Modulo hash_slot to find starting slot */
1236 301664 MU32 * slot_ptr = cache->p_base_slots + (hash_slot % cache->p_num_slots);
1237 301664 MU32 * first_deleted = (MU32 *)0;
1238  
1239 /* Total slots and pointer to end of slot data to do wrapping */
1240 301664 slots_left = cache->p_num_slots;
1241 301664 slots_end = cache->p_base_slots + slots_left;
1242  
1243 ASSERT(cache->p_cur != NOPAGE);
1244  
1245 /* Loop with integer probing till we find or don't */
1246 573177 ▶ while (slots_left--) {
Branch: 1 branch
BranchTrueFalseTotal
while (slots_left--) { 573177 0 573177
1247 573177 MU32 data_offset = *slot_ptr;
1248 ASSERT(data_offset == 0 || data_offset == 1 ||
1249 ((data_offset >= P_HEADERSIZE + cache->p_num_slots*4) &&
1250 (data_offset < cache->c_page_size) &&
1251 ((data_offset & 3) == 0)));
1252  
1253 /* data_offset == 0 means empty slot, and no more beyond */
1254 /* data_offset == 1 means deleted slot, we can reuse if writing */
1255 573177 ▶ if (data_offset == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 0) { 128425 444752 573177
1256 /* Return pointer to last checked slot */
1257 128425 break;
1258 }
1259 444752 ▶ if (data_offset == 1 && mode == 1 && 0 == first_deleted) {
Branch: 3 branches
BranchTrueFalseTotal
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 65061 379691 444752
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 8303 56758 65061
if (data_offset == 1 && mode == 1 && 0 == first_deleted) { 6038 2265 8303
1260 /* Save pointer to first usable slot; if we don't find the key later,
1261 we'll fall back to returning this.
1262 */
1263 6038 first_deleted = slot_ptr;
1264 }
1265 /* deleted slot, keep looking */
1266 444752 ▶ if (data_offset == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 1) { 379691 65061 444752
1267  
1268 } else {
1269 MU32 * base_det;
1270 MU32 fkey_len;
1271  
1272 /* A bad offset or length here would send us reading outside
1273 * the page. Treat it as not found; the page is reinitialised
1274 * when it's unlocked */
1275 379691 ▶ if (!_mmc_check_entry(cache, data_offset))
Branch: 1 branch
BranchTrueFalseTotal
if (!_mmc_check_entry(cache, data_offset)) 2 379689 379691
1276 2 return (MU32 *)0;
1277  
1278 /* Offset is from start of data area */
1279 379689 base_det = S_Ptr(cache->p_base, data_offset);
1280  
1281 /* Two longs are key len and data len */
1282 379689 fkey_len = S_KeyLen(base_det);
1283  
1284 /* Key matches? */
1285 379689 ▶ 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)) { 289941 89748 379689
if (fkey_len == (MU32)key_len && !memcmp(key_ptr, S_KeyPtr(base_det), key_len)) { 173237 116704 289941
1286  
1287 /* Yep, found it! */
1288 173237 return slot_ptr;
1289 }
1290 }
1291  
1292 /* Linear probe and wrap at end of slot data... */
1293 271513 ▶ 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; } 371 271142 271513
1294 ASSERT(slot_ptr >= cache->p_base_slots && slot_ptr < slots_end);
1295 }
1296 /* No slot found */
1297 128425 ▶ if (++slots_left == 0) slot_ptr = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (++slots_left == 0) slot_ptr = 0; 0 128425 128425
1298  
1299 128425 ▶ if (1 == mode && 0 != first_deleted)
Branch: 2 branches
BranchTrueFalseTotal
if (1 == mode && 0 != first_deleted) 86979 41446 128425
if (1 == mode && 0 != first_deleted) 5984 80995 86979
1300 5984 return first_deleted;
1301 else
1302 122441 return slot_ptr;
1303 }
1304  
1305 /*
1306 * void _mmc_init_page(mmap_cache * cache, int page)
1307 *
1308 * Initialise the given page as empty. It's expected
1309 * that you've already locked the page before doing
1310 * this
1311 *
1312 */
1313 139094 void _mmc_init_page(mmap_cache * cache, MU32 p_cur) {
1314 /* Setup page details */
1315 139094 MU64 p_offset = (MU64)p_cur * cache->c_page_size;
1316 139094 void * p_ptr = PTR_ADD(cache->mm_var, p_offset);
1317  
1318 /* Initialise to all 0's, then mark dirty until the header is complete
1319 * so a death in here leaves a page the next locker knows to redo */
1320 139094 memset(p_ptr, 0, cache->c_page_size);
1321 139094 P_SetMagic(p_ptr, P_MAGIC_DIRTY);
1322  
1323 /* Setup header */
1324 139094 P_NumSlots(p_ptr) = cache->start_slots;
1325 139094 P_FreeSlots(p_ptr) = cache->start_slots;
1326 139094 P_OldSlots(p_ptr) = 0;
1327 139094 P_FreeData(p_ptr) = P_HEADERSIZE + cache->start_slots * 4;
1328 139094 P_FreeBytes(p_ptr) = cache->c_page_size - P_FreeData(p_ptr);
1329 139094 P_NReads(p_ptr) = 0;
1330 139094 P_NReadHits(p_ptr) = 0;
1331  
1332 139094 P_SetMagic(p_ptr, P_MAGIC);
1333 139094 }
1334  
1335 /*
1336 * void _mmc_repair_page(mmap_cache * cache, MU32 p_cur, char * why)
1337 *
1338 * Reinitialise a page found in a state we can't use (dirty marker left
1339 * by a killed writer, or a failed structure check) and record that we
1340 * did, so the caller can report it. Expects the page to be locked.
1341 *
1342 */
1343 5 void _mmc_repair_page(mmap_cache * cache, MU32 p_cur, char * why) {
1344 char reason[256];
1345  
1346 /* why may point at last_error, which _mmc_set_error is about to
1347 * replace */
1348 5 ▶ strncpy(reason, why ? why : "unknown", sizeof(reason) - 1);
Branch: 1 branch
BranchTrueFalseTotal
strncpy(reason, why ? why : "unknown", sizeof(reason) - 1); 5 0 5
1349 5 reason[sizeof(reason) - 1] = 0;
1350  
1351 5 _mmc_init_page(cache, p_cur);
1352 5 cache->p_dirty = 0;
1353 5 cache->repaired_pages++;
1354 5 cache->page_repaired = 1;
1355 5 _mmc_set_error(cache, 0, "page %u of %s reinitialised: %s", p_cur, cache->share_file, reason);
1356 5 }
1357  
1358 /*
1359 * void _mmc_mark_dirty(mmap_cache * cache)
1360 *
1361 * Flag the current locked page as mid-update, before its structure is
1362 * changed. Cleared again by mmc_unlock.
1363 *
1364 */
1365 247753 void _mmc_mark_dirty(mmap_cache * cache) {
1366 ASSERT(cache->p_cur != NOPAGE);
1367 247753 ▶ if (cache->p_dirty)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_dirty) 136453 111300 247753
1368 136453 return;
1369 111300 P_SetMagic(cache->p_base, P_MAGIC_DIRTY);
1370 111300 cache->p_dirty = 1;
1371 }
1372  
1373 /*
1374 * int _mmc_check_entry(mmap_cache * cache, MU32 data_offset)
1375 *
1376 * Check a used slot's data offset and the entry header it points at
1377 * lie within the current page, so following them can't read outside
1378 * it. Returns 1 if so. If not, the page is flagged corrupt (mmc_unlock
1379 * reinitialises it) and 0 is returned; the caller should give up on
1380 * the page.
1381 *
1382 */
1383 7394302 int _mmc_check_entry(mmap_cache * cache, MU32 data_offset) {
1384 7394302 MU32 slots_end = P_HEADERSIZE + cache->p_num_slots * 4;
1385 MU32 * base_det;
1386 MU64 kvlen;
1387  
1388 7394302 ▶ if (data_offset < slots_end || (data_offset & 3) ||
Branch: 2 branches
BranchTrueFalseTotal
if (data_offset < slots_end || (data_offset & 3) || 7394302 0 7394302
if (data_offset < slots_end || (data_offset & 3) || 7394302 0 7394302
1389 7394302 ▶ (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 7394300 7394302
1390 2 cache->p_corrupt = 1;
1391 2 _mmc_set_error(cache, 0, "slot data offset %u outside page", data_offset);
1392 2 return 0;
1393 }
1394  
1395 7394300 base_det = S_Ptr(cache->p_base, data_offset);
1396 7394300 kvlen = sizeof(MU32) * 6 + (MU64)S_KeyLen(base_det) + (MU64)S_ValLen(base_det);
1397 7394300 ▶ if ((MU64)data_offset + kvlen > cache->c_page_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ((MU64)data_offset + kvlen > cache->c_page_size) { 1 7394299 7394300
1398 1 cache->p_corrupt = 1;
1399 1 _mmc_set_error(cache, 0, "entry at offset %u runs past end of page", data_offset);
1400 1 return 0;
1401 }
1402  
1403 7394299 return 1;
1404 }
1405  
1406 /*
1407 * int _mmc_test_page(mmap_cache * cache)
1408 *
1409 * Test integrity of current page
1410 *
1411 */
1412 7 int _mmc_test_page(mmap_cache * cache) {
1413 7 MU32 * slot_ptr = cache->p_base_slots;
1414 7 MU32 count_free = 0, count_old = 0, max_data_offset = 0;
1415 7 MU32 data_size = cache->c_page_size;
1416  
1417 ASSERT(cache->p_cur != NOPAGE);
1418 7 ▶ if (cache->p_cur == NOPAGE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cache->p_cur == NOPAGE) return 0; 0 7 7
1419  
1420 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
1421 623 MU32 data_offset = *slot_ptr;
1422  
1423 ASSERT(data_offset == 0 || data_offset == 1 ||
1424 (data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 &&
1425 data_offset < cache->c_page_size));
1426 623 ▶ if (!(data_offset == 0 || data_offset == 1 ||
Branch: 2 branches
BranchTrueFalseTotal
if (!(data_offset == 0 || data_offset == 1 || 87 536 623
if (!(data_offset == 0 || data_offset == 1 || 87 0 87
1427 87 ▶ (data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 &&
Branch: 1 branch
BranchTrueFalseTotal
(data_offset >= P_HEADERSIZE + cache->p_num_slots * 4 && 87 0 87
1428 87 ▶ data_offset < cache->c_page_size))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
data_offset < cache->c_page_size))) return 0; 0 87 87
1429  
1430 623 ▶ if (data_offset == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset == 1) { 0 623 623
1431 0 count_old++;
1432 }
1433 623 ▶ if (data_offset <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset <= 1) { 536 87 623
1434 536 count_free++;
1435 536 continue;
1436 }
1437  
1438 87 ▶ if (data_offset > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset > 1) { 87 0 87
1439 87 MU32 * base_det = S_Ptr(cache->p_base, data_offset);
1440  
1441 87 MU32 last_access = S_LastAccess(base_det);
1442 87 MU32 expire_on = S_ExpireOn(base_det);
1443 87 MU32 key_len = S_KeyLen(base_det);
1444 87 MU32 val_len = S_ValLen(base_det);
1445 87 MU32 kvlen = S_SlotLen(base_det);
1446 87 ROUNDLEN(kvlen);
1447  
1448 ASSERT(last_access > 1000000000);
1449 87 ▶ if (!(last_access > 1000000000)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(last_access > 1000000000)) return 0; 0 87 87
1450 ASSERT(expire_on == 0 || (expire_on > 1000000000));
1451 87 ▶ if (!(expire_on == 0 || (expire_on > 1000000000))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(expire_on == 0 || (expire_on > 1000000000))) return 0; 0 87 87
if (!(expire_on == 0 || (expire_on > 1000000000))) return 0; 0 0 0
1452  
1453 ASSERT(key_len >= 0 && key_len < data_size);
1454 87 ▶ 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 87 87
1455 ASSERT(val_len >= 0 && val_len < data_size);
1456 87 ▶ 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 87 87
1457 ASSERT(kvlen >= 4*4 && kvlen < data_size);
1458 87 ▶ if (!(kvlen >= 4*4 && kvlen < data_size)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(kvlen >= 4*4 && kvlen < data_size)) return 0; 87 0 87
if (!(kvlen >= 4*4 && kvlen < data_size)) return 0; 0 87 87
1459  
1460 /* Keep track of largest end of data position */
1461 87 ▶ if (data_offset + kvlen > max_data_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_offset + kvlen > max_data_offset) { 31 56 87
1462 31 max_data_offset = data_offset + kvlen;
1463 }
1464  
1465 /* Check if key lookup finds same thing */
1466 {
1467 MU32 hash_page, hash_slot, * find_slot_ptr;
1468  
1469 /* Hash it */
1470 87 mmc_hash(cache, S_KeyPtr(base_det), (int)key_len,
1471 &hash_page, &hash_slot);
1472 ASSERT(hash_slot == S_SlotHash(base_det));
1473 87 ▶ if (!(hash_slot == S_SlotHash(base_det))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(hash_slot == S_SlotHash(base_det))) return 0; 0 87 87
1474  
1475 87 find_slot_ptr = _mmc_find_slot(cache, hash_slot, S_KeyPtr(base_det), key_len, 0);
1476  
1477 ASSERT(find_slot_ptr == slot_ptr);
1478 87 ▶ if (!(find_slot_ptr == slot_ptr)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(find_slot_ptr == slot_ptr)) return 0; 0 87 87
1479 }
1480  
1481 }
1482  
1483 }
1484  
1485 ASSERT(count_free == cache->p_free_slots);
1486 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
1487 ASSERT(count_old == cache->p_old_slots);
1488 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
1489 ASSERT(cache->p_free_data >= max_data_offset);
1490 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
1491  
1492 7 return 1;
1493 }
1494  
1495 /*
1496 * int _mmc_dump_page(mmap_cache * cache)
1497 *
1498 * Dump text version of current page to STDOUT
1499 *
1500 */
1501 0 int _mmc_dump_page(mmap_cache * cache) {
1502 MU32 slot;
1503  
1504 ASSERT(cache->p_cur != NOPAGE);
1505  
1506 0 printf("PageNum: %d\n", cache->p_cur);
1507 0 printf("\n");
1508 0 printf("PageSize: %d\n", cache->c_page_size);
1509 0 printf("BasePage: %p\n", cache->p_base);
1510 0 printf("BaseSlots: %p\n", cache->p_base_slots);
1511 0 printf("\n");
1512 0 printf("NumSlots: %d\n", cache->p_num_slots);
1513 0 printf("FreeSlots: %d\n", cache->p_free_slots);
1514 0 printf("OldSlots: %d\n", cache->p_old_slots);
1515 0 printf("FreeData: %d\n", cache->p_free_data);
1516 0 printf("FreeBytes: %d\n", cache->p_free_bytes);
1517  
1518 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
1519 0 MU32 * slot_ptr = cache->p_base_slots + slot;
1520  
1521 0 printf("Slot: %d; OF=%d; ", slot, *slot_ptr);
1522  
1523 0 ▶ if (*slot_ptr > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot_ptr > 1) { 0 0 0
1524 0 MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
1525 0 MU32 key_len = S_KeyLen(base_det);
1526 0 MU32 val_len = S_ValLen(base_det);
1527 char key[256], val[256];
1528  
1529 0 printf("LA=%d, ET=%d, HS=%d, FL=%d\n",
1530 0 S_LastAccess(base_det), S_ExpireOn(base_det),
1531 0 S_SlotHash(base_det), S_Flags(base_det));
1532  
1533 /* Get data */
1534 0 memcpy(key, S_KeyPtr(base_det), key_len > 256 ? 256 : key_len);
1535 0 key[key_len] = 0;
1536 0 memcpy(val, S_ValPtr(base_det), val_len > 256 ? 256 : val_len);
1537 0 val[val_len] = 0;
1538  
1539 0 printf(" K=%s, V=%s\n", key, val);
1540  
1541 }
1542  
1543 }
1544  
1545 0 return 0;
1546 }
1547  
1548  
1549  
1550  
« FastMmap.xs ↑ Summary