-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmmap_cache.c
More file actions
1550 lines (1285 loc) · 42.9 KB
/
Copy pathmmap_cache.c
File metadata and controls
1550 lines (1285 loc) · 42.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* AUTHOR
*
* Rob Mueller <cpan@robm.fastmail.fm>
*
* COPYRIGHT AND LICENSE
*
* Copyright (C) 2003 by FastMail IP Partners
*
* This library is free software; you can redistribute it and/or modify
* it under the same terms as Perl itself.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <stdarg.h>
#include "mmap_cache.h"
#include "mmap_cache_internals.h"
/* Global time_override */
MU32 time_override = 0;
void mmc_set_time_override(MU32 set_time) {
time_override = set_time;
}
/* Default values for a new cache */
char * def_share_file = "/tmp/sharefile";
MU32 def_init_file = 0;
MU32 def_test_file = 0;
MU32 def_expire_time = 0;
MU32 def_c_num_pages = 89;
MU32 def_c_page_size = 65536;
MU32 def_start_slots = 89;
/*
* mmap_cache * mmc_new()
*
* Create a new cache object filled with default values. Values may
* be changed and once ready, you should call mmc_init() to actually
* open the cache file and mmap it.
*
*/
mmap_cache * mmc_new() {
mmap_cache * cache = (mmap_cache *)calloc(1, sizeof(mmap_cache));
cache->p_cur = NOPAGE;
cache->c_num_pages = def_c_num_pages;
cache->c_page_size = def_c_page_size;
cache->start_slots = def_start_slots;
cache->expire_time = def_expire_time;
cache->share_file = _mmc_get_def_share_filename(cache);
cache->permissions = 0640;
cache->init_file = def_init_file;
cache->test_file = def_test_file;
return cache;
}
int mmc_set_param(mmap_cache * cache, char * param, char * val) {
if (!strcmp(param, "init_file")) {
cache->init_file = atoi(val);
} else if (!strcmp(param, "test_file")) {
cache->test_file = atoi(val);
} else if (!strcmp(param, "page_size")) {
cache->c_page_size = atoi(val);
} else if (!strcmp(param, "num_pages")) {
cache->c_num_pages = atoi(val);
} else if (!strcmp(param, "expire_time")) {
cache->expire_time = atoi(val);
} else if (!strcmp(param, "share_file")) {
cache->share_file = val;
} else if (!strcmp(param, "permissions")) {
cache->permissions = atoi(val);
} else if (!strcmp(param, "start_slots")) {
cache->start_slots = atoi(val);
} else if (!strcmp(param, "catch_deadlocks")) {
cache->catch_deadlocks = atoi(val);
} else if (!strcmp(param, "enable_stats")) {
cache->enable_stats = atoi(val);
} else {
return _mmc_set_error(cache, 0, "Bad set_param parameter: %s", param);
}
return 0;
}
int mmc_get_param(mmap_cache * cache, char * param) {
if (!strcmp(param, "page_size")) {
return (int)cache->c_page_size;
} else if (!strcmp(param, "num_pages")) {
return (int)cache->c_num_pages;
} else if (!strcmp(param, "expire_time")) {
return (int)cache->expire_time;
} else if (!strcmp(param, "repaired_pages")) {
return (int)cache->repaired_pages;
} else if (!strcmp(param, "debug")) {
#ifdef DEBUG
return 1;
#else
return 0;
#endif
} else {
return _mmc_set_error(cache, 0, "Bad set_param parameter: %s", param);
}
}
/*
* int mmc_init(mmap_cache * cache)
*
* Initialise the cache object, opening the share file and mmap'ing any
* memory.
*
*/
int mmc_init(mmap_cache * cache) {
int i, do_init = cache->init_file;
MU32 c_num_pages, c_page_size;
MU64 c_size;
/* Need a share file */
if (!cache->share_file) {
return _mmc_set_error(cache, 0, "No share file specified");
}
/* Basic cache params */
c_num_pages = cache->c_num_pages;
ASSERT(c_num_pages >= 1 && c_num_pages <= 1000);
c_page_size = cache->c_page_size;
ASSERT(c_page_size >= 1024 && c_page_size <= 1024*1024*1024);
ASSERT(cache->start_slots >= 10 && cache->start_slots <= 500);
cache->c_size = c_size = (MU64)c_num_pages * c_page_size;
if ( mmc_open_cache_file(cache, &do_init) == -1) return -1;
/* Map file into memory */
if ( mmc_map_memory(cache) == -1) return -1;
/* Initialise pages if new file */
if (do_init) {
for (i = 0; i < cache->c_num_pages; i++) {
MU64 p_offset = (MU64)i * cache->c_page_size;
mmc_lock_page(cache, p_offset);
_mmc_init_page(cache, i);
mmc_unlock_page(cache, p_offset);
}
/* Unmap and re-map to stop gtop telling us our memory usage is up */
if ( mmc_unmap_memory(cache) == -1) return -1;
if ( mmc_map_memory(cache) == -1) return -1;
}
/* Test pages in file if asked */
if (cache->test_file) {
for (i = 0; i < cache->c_num_pages; i++) {
int bad_page = 0;
MU64 p_offset = (MU64)i * cache->c_page_size;
/* Need to lock page, which tests header structure */
if (mmc_lock(cache, i)) {
/* If that failed, assume bad header, so manually lock */
mmc_lock_page(cache, p_offset);
bad_page = 1;
/* If lock succeeded, test page structure */
} else {
if (!_mmc_test_page(cache))
bad_page = 1;
}
/* A bad page, initialise it */
if (bad_page) {
_mmc_init_page(cache, i);
/* Rerun test on this page, potential infinite
loop if init_page is broken, but then things
are really broken anyway */
i--;
}
mmc_unlock_page(cache, p_offset);
cache->p_cur = NOPAGE;
}
}
return 0;
}
/*
* int mmc_close(mmap_cache * cache)
*
* Close the given cache, unmmap'ing any memory and closing file
* descriptors.
*
*/
int mmc_close(mmap_cache *cache) {
int res;
/* Shouldn't call if not init'ed */
ASSERT(cache->fh);
ASSERT(cache->mm_var);
/* Shouldn't call if page still locked */
ASSERT(cache->p_cur == NOPAGE);
/* Unlock any locked page */
if (cache->p_cur != NOPAGE) {
mmc_unlock(cache);
}
/* Close file */
if (cache->fh) {
mmc_close_fh(cache);
}
/* Unmap memory */
if (cache->mm_var) {
res = mmc_unmap_memory(cache);
if (res == -1) {
return _mmc_set_error(cache, errno, "Mmap of shared file %s failed", cache->share_file);
}
}
free(cache);
return 0;
}
/*
* int mmc_page_repaired(mmap_cache * cache)
*
* True if the most recent mmc_lock or mmc_unlock reinitialised a page.
* mmc_error() then describes why. Cleared by the next mmc_lock.
*
*/
int mmc_page_repaired(mmap_cache * cache) {
return cache->page_repaired;
}
/*
* int mmc_peek(mmap_cache * cache, MU64 offset, MU32 * val)
* int mmc_poke(mmap_cache * cache, MU64 offset, MU32 val)
*
* Read or write one MU32 at a byte offset into the mapping, with no
* locking or checking beyond the mapping's bounds. For tests, which use
* them to damage a page deliberately; going through the mapping rather
* than the file keeps them independent of whether the platform keeps
* mmap and file I/O coherent (OpenBSD doesn't without msync).
*
*/
int mmc_peek(mmap_cache * cache, MU64 offset, MU32 * val) {
if ((offset & 3) || offset + sizeof(MU32) > cache->c_size)
return _mmc_set_error(cache, 0, "peek offset %llu outside mapping", offset);
*val = *(MU32 *)PTR_ADD(cache->mm_var, offset);
return 0;
}
int mmc_poke(mmap_cache * cache, MU64 offset, MU32 val) {
if ((offset & 3) || offset + sizeof(MU32) > cache->c_size)
return _mmc_set_error(cache, 0, "poke offset %llu outside mapping", offset);
*(MU32 *)PTR_ADD(cache->mm_var, offset) = val;
return 0;
}
char * mmc_error(mmap_cache * cache) {
if (cache->last_error)
return cache->last_error;
return "Unknown error";
}
/*
* mmc_lock(
* cache_mmap * cache, MU32 p_cur
* )
*
* Lock the given page number using fcntl locking. Setup
* cache->p_* fields with correct values for the given page
*
*/
int mmc_lock(mmap_cache * cache, MU32 p_cur) {
MU64 p_offset;
void * p_ptr;
int res = 0;
/* Argument sanity check. Valid pages are 0 .. c_num_pages-1; NOPAGE is
* already excluded by the >= comparison since c_num_pages << NOPAGE. */
if (p_cur >= cache->c_num_pages)
return _mmc_set_error(cache, 0, "page %u is NOPAGE or larger than number of pages", p_cur);
/* Check not already locked */
if (cache->p_cur != NOPAGE)
return _mmc_set_error(cache, 0, "page %u is already locked, can't lock multiple pages", cache->p_cur);
/* Setup page details */
p_offset = (MU64)p_cur * cache->c_page_size;
p_ptr = PTR_ADD(cache->mm_var, p_offset);
res = mmc_lock_page(cache, p_offset);
if (res) return res;
cache->page_repaired = 0;
cache->p_dirty = 0;
cache->p_corrupt = 0;
/* A dirty marker means whoever last held this page died (or was
* killed) part way through changing it; the lock has passed to us,
* but the page's structure can't be trusted, so start it afresh */
if (P_Magic(p_ptr) == P_MAGIC_DIRTY) {
_mmc_repair_page(cache, p_cur, "left dirty by a killed writer");
} else if (P_Magic(p_ptr) != P_MAGIC) {
mmc_unlock_page(cache, p_offset);
return _mmc_set_error(cache, 0, "magic page start marker not found. p_cur is %u, offset is %llu", p_cur, p_offset);
}
/* Copy to cache structure */
cache->p_num_slots = P_NumSlots(p_ptr);
cache->p_free_slots = P_FreeSlots(p_ptr);
cache->p_old_slots = P_OldSlots(p_ptr);
cache->p_free_data = P_FreeData(p_ptr);
cache->p_free_bytes = P_FreeBytes(p_ptr);
cache->p_n_reads = P_NReads(p_ptr);
cache->p_n_read_hits = P_NReadHits(p_ptr);
/* Reality check. Pages start with start_slots and only ever grow via
* expunge, so num_slots should never be below the configured
* start_slots, and the slot table, then the data, must fit in the
* page. A header that fails these was corrupted by something that
* predates the dirty marker (an older module version killed
* mid-update, say); the page is unusable as it is, so it is
* reinitialised like a dirty one rather than failing every access
* to it until someone recreates the file. */
if (cache->p_num_slots < cache->start_slots ||
P_HEADERSIZE + (MU64)cache->p_num_slots * 4 > cache->c_page_size)
res = _mmc_set_error(cache, 0, "cache num_slots mismatch");
else if (cache->p_free_slots > cache->p_num_slots)
res = _mmc_set_error(cache, 0, "cache free slots mismatch");
else if (cache->p_old_slots > cache->p_free_slots)
res = _mmc_set_error(cache, 0, "cache old slots mismatch");
else if (cache->p_free_data < P_HEADERSIZE + cache->p_num_slots * 4 ||
cache->p_free_data > cache->c_page_size ||
cache->p_free_data + cache->p_free_bytes != cache->c_page_size)
res = _mmc_set_error(cache, 0, "cache free data mismatch");
if (res) {
_mmc_repair_page(cache, p_cur, cache->last_error);
cache->p_num_slots = P_NumSlots(p_ptr);
cache->p_free_slots = P_FreeSlots(p_ptr);
cache->p_old_slots = P_OldSlots(p_ptr);
cache->p_free_data = P_FreeData(p_ptr);
cache->p_free_bytes = P_FreeBytes(p_ptr);
cache->p_n_reads = P_NReads(p_ptr);
cache->p_n_read_hits = P_NReadHits(p_ptr);
}
/* Check page header */
ASSERT(P_Magic(p_ptr) == P_MAGIC);
ASSERT(P_NumSlots(p_ptr) >= cache->start_slots && P_NumSlots(p_ptr) < cache->c_page_size);
ASSERT(P_FreeSlots(p_ptr) <= P_NumSlots(p_ptr));
ASSERT(P_OldSlots(p_ptr) <= P_FreeSlots(p_ptr));
ASSERT(P_FreeData(p_ptr) + P_FreeBytes(p_ptr) == cache->c_page_size);
/* Setup page pointers */
cache->p_cur = p_cur;
cache->p_offset = p_offset;
cache->p_base = p_ptr;
cache->p_base_slots = PTR_ADD(p_ptr, P_HEADERSIZE);
ASSERT(_mmc_test_page(cache));
return 0;
}
/*
* mmc_unlock(
* cache_mmap * cache
* )
*
* Unlock any currently locked page
*
*/
int mmc_unlock(mmap_cache * cache) {
ASSERT(cache->p_cur != NOPAGE);
/* A repair at lock time has been reported by now */
cache->page_repaired = 0;
/* A structural check failed during this lock. The page can't be
* saved back; start it afresh instead (the operation that found the
* problem has already reported a miss or a failed store) */
if (cache->p_corrupt) {
_mmc_repair_page(cache, cache->p_cur, cache->last_error);
cache->p_changed = 0;
cache->p_corrupt = 0;
/* If changed, save page header changes back */
} else if (cache->p_changed) {
void * p_ptr = cache->p_base;
/* The header fields are updated one at a time, so cover them with
* the marker like any other structural change */
_mmc_mark_dirty(cache);
/* Save any changed information back to page */
P_NumSlots(p_ptr) = cache->p_num_slots;
P_FreeSlots(p_ptr) = cache->p_free_slots;
P_OldSlots(p_ptr) = cache->p_old_slots;
P_FreeData(p_ptr) = cache->p_free_data;
P_FreeBytes(p_ptr) = cache->p_free_bytes;
P_NReads(p_ptr) = cache->p_n_reads;
P_NReadHits(p_ptr) = cache->p_n_read_hits;
cache->p_changed = 0;
}
/* Test before unlocking */
ASSERT(_mmc_test_page(cache));
/* Structure is complete again; clear the marker before letting go */
if (cache->p_dirty) {
P_SetMagic(cache->p_base, P_MAGIC);
cache->p_dirty = 0;
}
mmc_unlock_page(cache, cache->p_offset);
cache->p_cur = NOPAGE;
return 0;
}
/*
* mmc_is_locked(
* cache_mmap * cache
* )
*
* Return true if page is locked
*
*/
int mmc_is_locked(mmap_cache * cache) {
return cache->p_cur != NOPAGE ? 1 : 0;
}
/*
* int mmc_hash(
* cache_mmap * cache,
* void *key_ptr, int key_len,
* MU32 *hash_page, MU32 *hash_slot
* )
*
* Hashes the given key, and returns hash value, hash page and hash
* slot part
*
*/
int mmc_hash(
mmap_cache *cache,
void *key_ptr, int key_len,
MU32 *hash_page, MU32 *hash_slot
) {
MU32 h = 0x92f7e3b1;
unsigned char * uc_key_ptr = (unsigned char *)key_ptr;
unsigned char * uc_key_ptr_end = uc_key_ptr + key_len;
while (uc_key_ptr != uc_key_ptr_end) {
h = (h << 4) + (h >> 28) + *uc_key_ptr++;
}
*hash_page = h % cache->c_num_pages;
*hash_slot = h / cache->c_num_pages;
return 0;
}
/*
* int mmc_read(
* cache_mmap * cache, MU32 hash_slot,
* void *key_ptr, int key_len,
* void **val_ptr, int *val_len,
* MU32 *expire_on, MU32 *flags, MU64 *modseq
* )
*
* Read key from current page. Tombstones read as not found. If the
* value carries a modseq, it's returned via *modseq and val_ptr/val_len
* cover just the value bytes (check FC_HASMODSEQ in *flags)
*
*/
int mmc_read(
mmap_cache *cache, MU32 hash_slot,
void *key_ptr, int key_len,
void **val_ptr, int *val_len,
MU32 *expire_on_p, MU32 *flags_p, MU64 *modseq_p
) {
MU32 * slot_ptr;
/* Increase read count for page */
if (cache->enable_stats) {
cache->p_changed = 1;
cache->p_n_reads++;
}
/* Search slots for key */
slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 0);
/* Did we find a value? */
if (!slot_ptr || *slot_ptr == 0) {
/* Return -1 if not */
return -1;
/* We found it! Check some other things... */
} else {
MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
MU32 now = time_override ? time_override : (MU32)time(0);
MU32 expire_on = S_ExpireOn(base_det);
/* Sanity check hash matches */
ASSERT(S_SlotHash(base_det) == hash_slot);
/* Value expired? */
if (expire_on && now >= expire_on) {
/* Return not found, but leave slot. Might need writeback */
return -1;
}
/* A tombstone is a miss (don't bump hit time - if it LRUs out
* early that just degrades to plain delete semantics) */
if (S_Flags(base_det) & FC_TOMBSTONE) {
return -1;
}
/* Update hit time */
S_LastAccess(base_det) = now;
/* Copy values to pointers */
*flags_p = S_Flags(base_det);
*expire_on_p = expire_on;
*val_len = S_ValLen(base_det);
*val_ptr = S_ValPtr(base_det);
/* Split off the modseq prefix if the value carries one */
if (*flags_p & FC_HASMODSEQ) {
memcpy(modseq_p, *val_ptr, FC_MODSEQ_LEN);
*val_ptr = PTR_ADD(*val_ptr, FC_MODSEQ_LEN);
*val_len -= FC_MODSEQ_LEN;
}
/* Increase read hit count */
if (cache->enable_stats) {
cache->p_changed = 1;
cache->p_n_read_hits++;
}
return 0;
}
}
/*
* int mmc_write(
* cache_mmap * cache, MU32 hash_slot,
* void *key_ptr, int key_len,
* void *val_ptr, int val_len,
* MU32 expire_on, MU32 flags, MU64 modseq
* )
*
* Write key to current page. Returns 1 if stored, 0 if there was no
* space, -1 if the store was refused by the conditional-store rules
* (see below). modseq is only used if flags has FC_HASMODSEQ set
* (always set it with FC_TOMBSTONE).
*
* Conditional stores: an unexpired tombstone refuses any store without
* a modseq, and any store with an older modseq. A live entry with a
* modseq refuses stores with an older modseq. Tombstoning never lowers
* an existing tombstone's modseq, and tombstoning over a live entry
* whose modseq is already newer keeps the entry; both are success
* no-ops (the cache already reflects something at least as new).
*
*/
int mmc_write(
mmap_cache *cache, MU32 hash_slot,
void *key_ptr, int key_len,
void *val_ptr, int val_len,
MU32 expire_on, MU32 flags, MU64 modseq
) {
int did_store = 0;
int ms_len = (flags & FC_HASMODSEQ) ? FC_MODSEQ_LEN : 0;
MU32 kvlen = KV_SlotLen(key_len, val_len + ms_len);
/* Search for slot with given key */
MU32 * slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 1);
/* If all slots full, definitely can't store */
if (!slot_ptr)
return 0;
/* Check the conditional-store rules against any existing live entry */
if (*slot_ptr > 1) {
MU32 * old_det = S_Ptr(cache->p_base, *slot_ptr);
MU32 old_flags = S_Flags(old_det);
MU32 old_expire = S_ExpireOn(old_det);
MU32 now = time_override ? time_override : (MU32)time(0);
if ((old_flags & FC_HASMODSEQ) && !(old_expire && now >= old_expire)) {
MU64 old_modseq;
memcpy(&old_modseq, S_ValPtr(old_det), FC_MODSEQ_LEN);
if (old_flags & FC_TOMBSTONE) {
if (flags & FC_TOMBSTONE) {
/* Re-tombstone: never lower the modseq */
if (modseq <= old_modseq)
return 1;
} else if (!(flags & FC_HASMODSEQ) || modseq < old_modseq) {
/* Value is older than the invalidating change (or can't say) */
return -1;
}
} else if (flags & FC_TOMBSTONE) {
/* Stale invalidation: the live entry is already newer */
if (modseq <= old_modseq)
return 1;
} else if ((flags & FC_HASMODSEQ) && modseq < old_modseq) {
/* Never regress a live value to an older one */
return -1;
}
}
}
ROUNDLEN(kvlen);
ASSERT(cache->p_cur != NOPAGE);
/* If there's space, store the key/value in the data section.
* Important: we must not delete an existing slot for this key unless we
* actually have space for the replacement; otherwise a failed set() would
* silently destroy the previous value. */
if (cache->p_free_bytes >= kvlen) {
MU32 * base_det;
MU32 now;
_mmc_mark_dirty(cache);
/* If found, delete the existing slot before reusing it for the new value */
if (*slot_ptr > 1) {
_mmc_delete_slot(cache, slot_ptr);
ASSERT(*slot_ptr == 1);
}
ASSERT(*slot_ptr <= 1);
base_det = PTR_ADD(cache->p_base, cache->p_free_data);
now = time_override ? time_override : (MU32)time(0);
/* Calculate expiry time */
if (expire_on == (MU32)-1)
expire_on = cache->expire_time ? now + cache->expire_time : 0;
/* Store info into slot */
S_LastAccess(base_det) = now;
S_ExpireOn(base_det) = expire_on;
S_SlotHash(base_det) = hash_slot;
S_Flags(base_det) = flags;
S_KeyLen(base_det) = (MU32)key_len;
S_ValLen(base_det) = (MU32)(val_len + ms_len);
/* Copy key/value to data section, modseq prefix first if present */
memcpy(S_KeyPtr(base_det), key_ptr, key_len);
if (ms_len)
memcpy(S_ValPtr(base_det), &modseq, ms_len);
memcpy(PTR_ADD(S_ValPtr(base_det), ms_len), val_ptr, val_len);
/* Update used slots/free data info */
cache->p_free_slots--;
if (*slot_ptr == 1) { cache->p_old_slots--; }
/* Save new data offset */
*slot_ptr = cache->p_free_data;
/* Update free space */
cache->p_free_bytes -= kvlen;
cache->p_free_data += kvlen;
/* Ensure changes are saved back */
cache->p_changed = 1;
did_store = 1;
}
return did_store;
}
/*
* int mmc_delete(
* cache_mmap * cache, MU32 hash_slot,
* void *key_ptr, int key_len
* )
*
* Delete key from current page
*
*/
int mmc_delete(
mmap_cache *cache, MU32 hash_slot,
void *key_ptr, int key_len,
MU32 * flags
) {
/* Search slots for key */
MU32 * slot_ptr = _mmc_find_slot(cache, hash_slot, key_ptr, key_len, 2);
/* Did we find a value? */
if (!slot_ptr || *slot_ptr == 0) {
/* Return 0 if not deleted */
return 0;
/* We found it, delete it */
} else {
/* Store flags in output pointer */
MU32 * base_det = S_Ptr(cache->p_base, *slot_ptr);
*flags = S_Flags(base_det);
_mmc_delete_slot(cache, slot_ptr);
return 1;
}
}
int last_access_cmp(const void * a, const void * b) {
MU32 av = S_LastAccess(*(MU32 **)a);
MU32 bv = S_LastAccess(*(MU32 **)b);
if (av < bv) return -1;
if (av > bv) return 1;
return 0;
}
/*
* int mmc_calc_expunge(
* cache_mmap * cache, int mode, int len, MU32 * new_num_slots, MU32 *** to_expunge
* )
*
* Calculate entries to expunge from current page.
*
* If len >= 0
* If space available for len bytes & >30% slots free, nothing is expunged
* If len < 0 or not above
* If mode == 0, only expired items are expunged
* If mode == 1, all entries are expunged
* If mode == 2, entries are expunged till 40% free space is created
*
* If expunged is non-null pointer, result is filled with
* a list of slots to expunge
*
* Return value is number of items to expunge
*
*/
int mmc_calc_expunge(
mmap_cache * cache,
int mode, int len,
MU32 * new_num_slots, MU32 *** to_expunge
) {
double slots_pct;
ASSERT(cache->p_cur != NOPAGE);
/* If len >= 0, and space available for len bytes, nothing is expunged */
if (len >= 0) {
/* Length of key/value data when stored */
MU32 kvlen = KV_SlotLen(len, 0);
ROUNDLEN(kvlen);
slots_pct = (double)(cache->p_free_slots - cache->p_old_slots) / cache->p_num_slots;
/* Nothing to do if hash table more than 30% free slots and enough free space */
if (slots_pct > 0.3 && cache->p_free_bytes >= kvlen)
return 0;
}
{
MU32 num_slots = cache->p_num_slots;
MU32 used_slots = num_slots - cache->p_free_slots;
MU32 * slot_ptr = cache->p_base_slots;
MU32 * slot_end = slot_ptr + num_slots;
/* Store pointers to used slots */
MU32 ** copy_base_det = (MU32 **)calloc(used_slots, sizeof(MU32 *));
MU32 ** copy_base_det_end = copy_base_det + used_slots;
MU32 ** copy_base_det_out = copy_base_det;
MU32 ** copy_base_det_in = copy_base_det + used_slots;
MU32 page_data_size = cache->c_page_size - num_slots * 4 - P_HEADERSIZE;
MU32 in_slots, data_thresh, used_data = 0;
MU32 now = time_override ? time_override : (MU32)time(0);
/* Loop for each existing slot, and store in a list */
for (; slot_ptr != slot_end; slot_ptr++) {
MU32 data_offset = *slot_ptr;
MU32 * base_det = S_Ptr(cache->p_base, data_offset);
MU32 expire_on, kvlen;
/* Ignore if if free slot */
if (data_offset <= 1) {
continue;
}
/* Nothing on a page we can't walk safely is worth keeping or
* writing back; it is reinitialised at unlock */
if (!_mmc_check_entry(cache, data_offset)) {
free(copy_base_det);
*to_expunge = 0;
*new_num_slots = num_slots;
return 0;
}
/* Definitely out if mode == 1 which means expunge all */
if (mode == 1) {
*copy_base_det_out++ = base_det;
continue;
}
/* Definitely out if expired, and not dirty */
expire_on = S_ExpireOn(base_det);
if (expire_on && now >= expire_on) {
*copy_base_det_out++ = base_det;
continue;
}
/* Track used space */
kvlen = S_SlotLen(base_det);
ROUNDLEN(kvlen);
ASSERT(kvlen <= page_data_size);
used_data += kvlen;
ASSERT(used_data <= page_data_size);
/* Potentially in */
*--copy_base_det_in = base_det;
}
/* Check that definitely in and out slots add up to used slots */
ASSERT(copy_base_det_in == copy_base_det_out);
ASSERT(mode != 1 || copy_base_det_out == copy_base_det_end);
/* Increase slot count if free count is low and there's space to increase */
slots_pct = (double)(copy_base_det_end - copy_base_det_out) / num_slots;
if (slots_pct > 0.3 && (page_data_size - used_data > (num_slots + 1) * 4 || mode == 2)) {
num_slots = (num_slots * 2) + 1;
}
page_data_size = cache->c_page_size - num_slots * 4 - P_HEADERSIZE;
/* If mode == 0 or 1, we've just worked out ones to keep and
* which to dispose of, so return results */
if (mode == 0 || mode == 1) {
*to_expunge = copy_base_det;
*new_num_slots = num_slots;
return (copy_base_det_out - copy_base_det);
}
/* mode == 2, sort by last access, and remove till enough free space */
/* Sort those potentially in by last access */
in_slots = copy_base_det_end - copy_base_det_in;
qsort((void *)copy_base_det_in, in_slots, sizeof(MU32 *), &last_access_cmp);
/* Throw out old slots till we have 40% free data space */
data_thresh = (MU32)(0.6 * page_data_size);
while (copy_base_det_in != copy_base_det_end && used_data >= data_thresh) {
MU32 * slot_ptr = *copy_base_det_in;
MU32 kvlen = S_SlotLen(slot_ptr);
ROUNDLEN(kvlen);
ASSERT(kvlen <= page_data_size);
used_data -= kvlen;
ASSERT(used_data >= 0);
copy_base_det_out = ++copy_base_det_in;
}
ASSERT(used_data < page_data_size);
*to_expunge = copy_base_det;
*new_num_slots = num_slots;
return (copy_base_det_out - copy_base_det);
}
}
/*
* int mmc_do_expunge(
* cache_mmap * cache, int num_expunge, MU32 new_num_slots, MU32 ** to_expunge
* )
*
* Expunge given entries from current page.
*
*/
int mmc_do_expunge(
mmap_cache * cache,
int num_expunge, MU32 new_num_slots, MU32 ** to_expunge
) {
MU32 * base_slots = cache->p_base_slots;
MU32 ** to_keep = to_expunge + num_expunge;
MU32 ** to_keep_end = to_expunge + (cache->p_num_slots - cache->p_free_slots);
MU32 new_used_slots = (to_keep_end - to_keep);
/* Build new slots data and KV data */
MU32 slot_data_size = new_num_slots * 4;
MU32 * new_slot_data = (MU32 *)calloc(1, slot_data_size);
MU32 page_data_size = cache->c_page_size - new_num_slots * 4 - P_HEADERSIZE;
void * new_kv_data = calloc(1, page_data_size);
MU32 new_offset = 0;
/* Sanity check underlying fd is still the same file */
if (!mmc_check_fh(cache)) {
free(new_kv_data);
free(new_slot_data);
free(to_expunge);
return 0;
}
_mmc_mark_dirty(cache);
/* Start all new slots empty */
memset(new_slot_data, 0, slot_data_size);
/* Copy entries to keep to new slot entires and data sections */
for (;to_keep < to_keep_end; to_keep++) {
MU32 * old_base_det = *to_keep;
MU32 * new_slot_ptr;
MU32 kvlen;
/* Hash key to find starting slot */
MU32 slot = S_SlotHash(old_base_det) % new_num_slots;
#ifdef DEBUG
/* Check hash actually matches stored value */
{
MU32 hash_page_dummy, hash_slot;
mmc_hash(cache, S_KeyPtr(old_base_det), S_KeyLen(old_base_det), &hash_page_dummy, &hash_slot);
ASSERT(hash_slot == S_SlotHash(old_base_det));
}
#endif
/* Find free slot */
new_slot_ptr = new_slot_data + slot;
while (*new_slot_ptr) {
if (++slot >= new_num_slots) { slot = 0; }
new_slot_ptr = new_slot_data + slot;
}
/* Copy slot and KV data */
kvlen = S_SlotLen(old_base_det);
memcpy(PTR_ADD(new_kv_data, new_offset), old_base_det, kvlen);
/* Store slot data and mark as used */
*new_slot_ptr = new_offset + new_num_slots * 4 + P_HEADERSIZE;
ROUNDLEN(kvlen);
new_offset += kvlen;
}
ASSERT(new_offset <= page_data_size);
/* printf("page=%d\n", cache->p_cur);
printf("old_slots=%d, new_slots=%d\n", old_num_slots, new_num_slots);
printf("old_used_slots=%d, new_used_slots=%d\n", old_used_slots, new_used_slots);*/
/* Store back into mmap'ed file space */
memcpy(base_slots, new_slot_data, slot_data_size);
memcpy(base_slots + new_num_slots, new_kv_data, new_offset);
cache->p_num_slots = new_num_slots;
cache->p_free_slots = new_num_slots - new_used_slots;
cache->p_old_slots = 0;
cache->p_free_data = new_offset + new_num_slots * 4 + P_HEADERSIZE;
cache->p_free_bytes = page_data_size - new_offset;
/* Make sure changes are saved back to mmap'ed file */
cache->p_changed = 1;
/* Free allocated memory */
free(new_kv_data);
free(new_slot_data);
free(to_expunge);
ASSERT(_mmc_test_page(cache));
return 1;