-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmvrlu.c
1731 lines (1497 loc) · 44.4 KB
/
mvrlu.c
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
// SPDX-FileCopyrightText: Copyright (c) 2018-2019 Virginia Tech
// SPDX-License-Identifier: Apache-2.0
#ifndef __KERNEL__
#include "mvrlu.h"
#else
#include <linux/mvrlu.h>
#endif
#include "mvrlu_i.h"
#include "debug.h"
#include "port.h"
/*
* Global data structures
*/
static mvrlu_thread_list_t g_live_threads ____cacheline_aligned2;
static mvrlu_thread_list_t g_zombie_threads ____cacheline_aligned2;
static mvrlu_qp_thread_t g_qp_thread ____cacheline_aligned2;
#ifdef MVRLU_ENABLE_STATS
static mvrlu_stat_t g_stat ____cacheline_aligned2;
#endif
/*
* Forward declarations
*/
static void qp_update_qp_clk_for_reclaim(mvrlu_qp_thread_t *qp_thread,
mvrlu_thread_struct_t *thread);
static int wakeup_qp_thread_for_reclaim(void);
static void print_config(void);
/*
* Clock-related functions
*/
#ifndef MVRLU_ORDO_TIMESTAMPING
static volatile unsigned long
__g_wrt_clk[2 * CACHE_DEFAULT_PADDING] ____cacheline_aligned2;
#define g_wrt_clk __g_wrt_clk[CACHE_DEFAULT_PADDING]
#define gte_clock(__t1, __t2) ((__t1) >= (__t2))
#define lte_clock(__t1, __t2) ((__t1) <= (__t2))
#define get_clock() g_wrt_clk
#define get_clock_relaxed() get_clock()
#define init_clock() \
do { \
g_wrt_clk = 0; \
} while (0)
#define new_clock(__x) (g_wrt_clk + 1)
#define advance_clock() smp_faa(&g_wrt_clk, 1)
#define correct_qp_clk(qp_clk) qp_clk
#else /* MVRLU_ORDO_TIMESTAMPING */
#include "ordo_clock.h"
#define gte_clock(__t1, __t2) ordo_gt_clock(__t1, __t2)
#define lte_clock(__t1, __t2) ordo_lt_clock(__t1, __t2)
#define get_clock() ordo_get_clock()
#define get_clock_relaxed() ordo_get_clock_relaxed()
#define init_clock() ordo_clock_init()
#define new_clock(__local_clk) ordo_new_clock((__local_clk) + ordo_boundary())
#define advance_clock()
#define correct_qp_clk(qp_clk) qp_clk - ordo_boundary()
#endif /* MVRLU_ORDO_TIMESTAMPING */
/*
* Utility functions
*/
#define log_to_thread(__log) \
({ \
void *p = (void *)(__log); \
void *q; \
q = p - ((size_t) & ((mvrlu_thread_struct_t *)0)->log); \
(mvrlu_thread_struct_t *)q; \
})
#define list_to_thread(__list) \
({ \
void *p = (void *)(__list); \
void *q; \
q = p - ((size_t) & ((mvrlu_thread_struct_t *)0)->list); \
(mvrlu_thread_struct_t *)q; \
})
#define chs_to_thread(__chs) \
({ \
void *p = (void *)(__chs)->cpy_hdr.p_wrt_clk; \
void *q; \
q = p - ((size_t) & ((mvrlu_wrt_set_t *)0)->wrt_clk); \
((mvrlu_wrt_set_t *)q)->thread; \
})
static inline void assert_chs_type(const mvrlu_cpy_hdr_struct_t *chs)
{
mvrlu_assert(chs->obj_hdr.type == TYPE_WRT_SET ||
chs->obj_hdr.type == TYPE_COPY ||
chs->obj_hdr.type == TYPE_FREE ||
chs->obj_hdr.type == TYPE_BOGUS);
}
/*
* Statistics functions
*/
#ifdef MVRLU_ENABLE_STATS
#define stat_thread_inc(self, x) stat_inc(&(self)->stat, stat_##x)
#define stat_thread_acc(self, x, y) stat_acc(&(self)->stat, stat_##x, y)
#define stat_thread_max(self, x, y) stat_max(&(self)->stat, stat_##x, y)
#define stat_qp_inc(qp, x) stat_inc(&(qp)->stat, stat_##x)
#define stat_qp_acc(qp, x, y) stat_acc(&(qp)->stat, stat_##x, y)
#define stat_qp_max(qp, x, y) stat_max(&(qp)->stat, stat_##x, y)
#define stat_log_inc(log, x) stat_thread_inc(log_to_thread(log), x)
#define stat_log_acc(log, x, y) stat_thread_acc(log_to_thread(log), x, y)
#define stat_log_max(log, x, y) stat_thread_max(log_to_thread(log), x, y)
#define stat_thread_merge(self) stat_atomic_merge(&g_stat, &(self)->stat)
#define stat_qp_merge(qp) stat_atomic_merge(&g_stat, &(qp)->stat)
#else /* MVRLU_ENABLE_STATS */
#define stat_thread_inc(self, x)
#define stat_thread_acc(self, x, y)
#define stat_thread_max(self, x, y)
#define stat_qp_inc(qp, x)
#define stat_qp_acc(qp, x, y)
#define stat_qp_max(qp, x, y)
#define stat_log_inc(log, x)
#define stat_log_acc(log, x, y)
#define stat_log_max(log, x, y)
#define stat_thread_merge(self)
#define stat_qp_merge(qp)
#endif /* MVRLU_ENABLE_STATS */
static const char *stat_get_name(int s)
{
/*
* Check out following implementation tricks:
* - C preprocessor applications
* https://bit.ly/2H1sC5G
* - Stringification
* https://gcc.gnu.org/onlinedocs/gcc-4.1.2/cpp/Stringification.html
* - Designated Initializers in C
* https://www.geeksforgeeks.org/designated-initializers-c/
*/
#undef S
#define S(x) [stat_##x] = #x,
static const char *stat_string[stat_max__ + 1] = { STAT_NAMES };
mvrlu_assert(s >= 0 && s < stat_max__);
return stat_string[s];
}
static void stat_print_cnt(mvrlu_stat_t *stat)
{
int i;
for (i = 0; i < stat_max__; ++i) {
printf(" %30s = %lu\n", stat_get_name(i), stat->cnt[i]);
}
}
static void stat_reset(mvrlu_stat_t *stat)
{
int i;
for (i = 0; i < stat_max__; ++i) {
stat->cnt[i] = 0;
}
}
static void stat_atomic_merge(mvrlu_stat_t *tgt, mvrlu_stat_t *src)
{
int i;
for (i = 0; i < stat_max__; ++i) {
smp_faa(&tgt->cnt[i], src->cnt[i]);
}
}
static inline void stat_inc(mvrlu_stat_t *stat, int s)
{
stat->cnt[s]++;
}
static inline void stat_acc(mvrlu_stat_t *stat, int s, unsigned long v)
{
stat->cnt[s] += v;
}
static inline void stat_max(mvrlu_stat_t *stat, int s, unsigned long v)
{
if (v > stat->cnt[s])
stat->cnt[s] = v;
}
/*
* thread information
*/
static inline void init_mvrlu_list(mvrlu_list_t *list)
{
list->next = list;
list->prev = list;
}
static inline void mvrlu_list_add(mvrlu_list_t *new, mvrlu_list_t *head)
{
head->next->prev = new;
new->next = head->next;
new->prev = head;
head->next = new;
}
static inline void mvrlu_list_del(mvrlu_list_t *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}
static inline int mvrlu_list_empty(const mvrlu_list_t *head)
{
return head->next == head && head->prev == head;
}
static inline void mvrlu_list_rotate_left(mvrlu_list_t *head)
{
/* Rotate a list in counterclockwise direction:
*
* Before rotation:
* [T3]->{{H}}->[T0]->[T1]->[T2]
* /|\ |
* +------------------------+
*
* After rotation:
* [T3]->[T0]->{{H}}->[T1]->[T2]
* /|\ |
* +------------------------+
*/
if (!mvrlu_list_empty(head)) {
mvrlu_list_t *first;
first = head->next;
mvrlu_list_del(first);
mvrlu_list_add(first, head->prev);
}
}
#define thread_list_for_each_safe(tl, pos, n, thread) \
for (pos = (tl)->list.next, n = (pos)->next, \
thread = list_to_thread(pos); \
pos != &(tl)->list; \
pos = n, n = (pos)->next, thread = list_to_thread(pos))
static inline int thread_list_has_waiter(mvrlu_thread_list_t *tl)
{
return tl->thread_wait;
}
static inline void init_thread_list(mvrlu_thread_list_t *tl)
{
port_spin_init(&tl->lock);
tl->cur_tid = 0;
tl->num = 0;
init_mvrlu_list(&tl->list);
}
static inline void thread_list_destroy(mvrlu_thread_list_t *tl)
{
port_spin_destroy(&tl->lock);
}
static inline void thread_list_lock(mvrlu_thread_list_t *tl)
{
/* Lock acquisition with a normal priority */
port_spin_lock(&tl->lock);
}
static inline void thread_list_lock_force(mvrlu_thread_list_t *tl)
{
/* Lock acquisition with a high priority
* which turns on the thread_wait flag
* so a lengthy task can stop voluntarily
* stop and resume later. */
if (!port_spin_trylock(&tl->lock)) {
smp_cas(&tl->thread_wait, 0, 1);
port_spin_lock(&tl->lock);
}
}
static inline void thread_list_unlock(mvrlu_thread_list_t *tl)
{
if (tl->thread_wait)
smp_atomic_store(&tl->thread_wait, 0);
port_spin_unlock(&tl->lock);
}
static inline void thread_list_add(mvrlu_thread_list_t *tl,
mvrlu_thread_struct_t *self)
{
thread_list_lock_force(tl);
{
self->tid = tl->cur_tid++;
tl->num++;
mvrlu_list_add(&self->list, &tl->list);
}
thread_list_unlock(tl);
}
static inline void thread_list_del_unsafe(mvrlu_thread_list_t *tl,
mvrlu_thread_struct_t *self)
{
tl->num--;
mvrlu_list_del(&self->list);
self->list.prev = self->list.next = NULL;
}
static inline void thread_list_del(mvrlu_thread_list_t *tl,
mvrlu_thread_struct_t *self)
{
thread_list_lock_force(tl);
{
thread_list_del_unsafe(tl, self);
}
thread_list_unlock(tl);
}
static inline void thread_list_rotate_left_unsafe(mvrlu_thread_list_t *tl)
{
/* NOTE: A caller should hold a lock */
mvrlu_list_rotate_left(&tl->list);
}
/*
* Object access functions
*/
static inline mvrlu_act_hdr_struct_t *obj_to_ahs(void *obj)
{
mvrlu_act_hdr_struct_t *ahs = obj;
return &ahs[-1];
}
static inline mvrlu_act_hdr_struct_t *vobj_to_ahs(volatile void *vobj)
{
return obj_to_ahs((void *)vobj);
}
static inline mvrlu_cpy_hdr_struct_t *obj_to_chs(void *obj)
{
mvrlu_cpy_hdr_struct_t *chs = obj;
return &chs[-1];
}
static inline mvrlu_cpy_hdr_struct_t *vobj_to_chs(volatile void *vobj)
{
return obj_to_chs((void *)vobj);
}
static inline mvrlu_obj_hdr_t *obj_to_obj_hdr(void *obj)
{
mvrlu_obj_hdr_t *ohdr = obj;
return &ohdr[-1];
}
static inline mvrlu_obj_hdr_t *vobj_to_obj_hdr(volatile void *vobj)
{
return obj_to_obj_hdr((void *)vobj);
}
static inline int is_obj_actual(mvrlu_obj_hdr_t *obj_hdr)
{
#ifdef MVRLU_DISABLE_ADDR_ACTUAL_TYPE_CHECKING
/* Test object type based on its type information
* in the header. It may cause one cache miss. */
return obj_hdr->type == TYPE_ACTUAL;
#else
/* Test if an object is in the log region or not.
* If not, it is an actual object. We avoid one
* memory reference so we may avoid one cache miss. */
return !port_addr_in_log_region(obj_hdr);
#endif /* MVRLU_DISABLE_ADDR_ACTUAL_TYPE_CHECKING */
}
static inline void *get_act_obj(void *obj)
{
mvrlu_obj_hdr_t *obj_hdr = obj_to_obj_hdr(obj);
if (likely(is_obj_actual(obj_hdr)))
return obj;
return (void *)obj_to_chs(obj)->cpy_hdr.p_act;
}
static inline unsigned int align_uint_to_cacheline(unsigned int unum)
{
return (unum + ~MVRLU_CACHE_LINE_MASK) & MVRLU_CACHE_LINE_MASK;
}
static inline void *align_ptr_to_cacheline(void *p)
{
return (void *)(((unsigned long)p + ~MVRLU_CACHE_LINE_MASK) &
MVRLU_CACHE_LINE_MASK);
}
/*
* Object copy operations
*/
static inline unsigned int get_log_size(const mvrlu_cpy_hdr_struct_t *chs)
{
return chs->obj_hdr.obj_size + chs->obj_hdr.padding_size;
}
static inline unsigned long get_wrt_clk(const mvrlu_cpy_hdr_struct_t *chs)
{
unsigned long wrt_clk;
wrt_clk = chs->cpy_hdr.__wrt_clk;
if (unlikely(wrt_clk == MAX_VERSION)) {
smp_rmb();
wrt_clk = *chs->cpy_hdr.p_wrt_clk;
}
return wrt_clk;
}
static int try_lock_obj(mvrlu_act_hdr_struct_t *ahs, volatile void *p_old_copy,
volatile void *p_new_copy)
{
int ret;
if (ahs->act_hdr.p_lock != NULL || ahs->obj_hdr.p_copy != p_old_copy)
return 0;
ret = smp_cas(&ahs->act_hdr.p_lock, NULL, p_new_copy);
if (!ret)
return 0; /* smp_cas() failed */
if (unlikely(ahs->obj_hdr.p_copy != p_old_copy)) {
/* If it is ABA, unlock and return false */
smp_wmb();
ahs->act_hdr.p_lock = NULL;
return 0;
}
/* Finally succeeded. Updating p_copy of p_new_copy
* will be done upon commit. */
return 1;
}
static void try_detach_obj(mvrlu_cpy_hdr_struct_t *chs)
{
mvrlu_act_hdr_struct_t *ahs;
void *p_act, *p_copy;
/* If the object is the latest object after qp2, the
* p_copy needs to be set to NULL */
/* Copy to the actual object when it is the latest copy */
p_act = (void *)chs->cpy_hdr.p_act;
ahs = obj_to_ahs(p_act);
p_copy = (void *)chs->obj_hdr.obj;
if (ahs->obj_hdr.p_copy != p_copy)
return;
/* Set p_copy of the actual object to NULL */
if (smp_cas(&ahs->obj_hdr.p_copy, p_copy, NULL)) {
/* Succeed in detaching the object */
return;
}
}
static int try_writeback_obj(mvrlu_cpy_hdr_struct_t *chs)
{
mvrlu_act_hdr_struct_t *ahs;
void *p_act, *p_copy;
/* Copy to the actual object when it is the latest copy */
p_act = (void *)chs->cpy_hdr.p_act;
ahs = obj_to_ahs(p_act);
p_copy = (void *)chs->obj_hdr.obj;
if (ahs->obj_hdr.p_copy != p_copy)
return 0;
/* Write back the copy to the master */
memcpy(p_act, p_copy, chs->obj_hdr.obj_size);
smp_wmb_tso();
return 1;
}
static void free_obj(mvrlu_cpy_hdr_struct_t *chs)
{
#ifdef MVRLU_ENABLE_FREE_POISIONING
mvrlu_act_hdr_struct_t *ahs;
ahs = vobj_to_ahs(chs->cpy_hdr.p_act);
memset((void *)chs->cpy_hdr.p_act, MVRLU_FREE_POSION,
ahs->obj_hdr.obj_size);
memset((void *)ahs, MVRLU_FREE_POSION, sizeof(*ahs));
#endif
port_free(vobj_to_ahs(chs->cpy_hdr.p_act));
}
/*
* Log operations
*/
static inline unsigned long log_used(mvrlu_log_t *log)
{
unsigned long used = log->tail_cnt - log->head_cnt;
stat_log_max(log, max_log_used_bytes, used);
return used;
}
static inline unsigned int log_index(unsigned long cnt)
{
return cnt & ~MVRLU_LOG_MASK;
}
static inline void *log_at(mvrlu_log_t *log, unsigned long cnt)
{
return (void *)&log->buffer[log_index(cnt)];
}
static inline mvrlu_wrt_set_struct_t *log_at_wss(mvrlu_log_t *log,
unsigned long cnt)
{
mvrlu_wrt_set_struct_t *wss;
wss = (mvrlu_wrt_set_struct_t *)log_at(log, cnt);
mvrlu_assert(wss->chs.obj_hdr.type == TYPE_WRT_SET);
return wss;
}
static inline mvrlu_cpy_hdr_struct_t *log_at_chs(mvrlu_log_t *log,
unsigned long cnt)
{
mvrlu_cpy_hdr_struct_t *chs;
chs = (mvrlu_cpy_hdr_struct_t *)log_at(log, cnt);
mvrlu_assert(chs == align_ptr_to_cacheline(chs));
return chs;
}
static inline unsigned int add_extra_padding(mvrlu_log_t *log,
unsigned int log_size,
unsigned int extra_size, int bogus)
{
unsigned int padding = 0;
extra_size = extra_size + sizeof(mvrlu_cpy_hdr_struct_t);
extra_size = align_uint_to_cacheline(extra_size);
if (bogus == 0 && log_index(log->tail_cnt + log_size + extra_size) <
log_index(log->tail_cnt)) {
padding = MVRLU_LOG_SIZE - log_index(log->tail_cnt + log_size);
}
if (padding) {
mvrlu_assert(log_index(log->tail_cnt + log_size + padding) ==
0);
}
return padding;
}
static mvrlu_cpy_hdr_struct_t *log_alloc(mvrlu_log_t *log,
unsigned int obj_size, int *bogus)
{
mvrlu_cpy_hdr_struct_t *chs;
mvrlu_wrt_set_struct_t *wss;
unsigned int log_size;
unsigned int extra_pad;
/* Make log_size cacheline aligned */
log_size = obj_size + sizeof(mvrlu_cpy_hdr_struct_t);
log_size = align_uint_to_cacheline(log_size);
mvrlu_assert(log_size < MVRLU_LOG_SIZE);
/* If an allocation wraps around the end of a log,
* insert a bogus object to prevent such case in real
* object access.
*
* +-----------------------------------+
* | |bogus|
* +-----------------------------------+
* \ \
* \ +- 1) log->tail_cnt
* +- 2) log->tail_cnt + log_size
*/
if (log_index(log->tail_cnt + log_size) < log_index(log->tail_cnt)) {
unsigned int bogus_size;
chs = log_at(log, log->tail_cnt);
memset(chs, 0, sizeof(*chs));
bogus_size = MVRLU_LOG_SIZE - log_index(log->tail_cnt);
chs->obj_hdr.padding_size = bogus_size;
chs->obj_hdr.type = TYPE_BOGUS;
log->tail_cnt += bogus_size;
mvrlu_assert(log_index(log->tail_cnt) == 0);
*bogus = 1;
}
mvrlu_assert(log_index(log->tail_cnt) <
log_index(log->tail_cnt + log_size));
extra_pad = add_extra_padding(log, log_size, sizeof(*wss), *bogus);
log_size += extra_pad;
/*
* +--- mvrlu_cpy_hdr_struct_t ---+
* / \
* +---------------------------------------------+----
* | mvrlu_cpy_hdr_t | mvrlu_obj_hdr | copy obj | ...
* +---------------------------------------------+----
*/
chs = log_at(log, log->tail_cnt);
memset(chs, 0, sizeof(*chs));
chs->cpy_hdr.__wrt_clk = MAX_VERSION;
chs->obj_hdr.obj_size = obj_size;
chs->obj_hdr.padding_size = log_size - obj_size;
return chs;
}
static mvrlu_cpy_hdr_struct_t *log_append_begin(mvrlu_log_t *log,
volatile void *p_act,
unsigned int obj_size,
int *bogus)
{
mvrlu_cpy_hdr_struct_t *chs;
*bogus = 0;
/* Add a write set if it is not allocated */
if (unlikely(!log->cur_wrt_set)) {
mvrlu_wrt_set_t *ws;
mvrlu_assert(log_index(log->tail_cnt) <
log_index(log->tail_cnt + sizeof(*ws)));
chs = log_alloc(log, sizeof(*ws), bogus);
ws = (mvrlu_wrt_set_t *)chs->obj_hdr.obj;
chs->cpy_hdr.p_wrt_clk = &ws->wrt_clk;
chs->obj_hdr.type = TYPE_WRT_SET;
ws->wrt_clk = MAX_VERSION;
ws->num_objs = 0;
ws->start_tail_cnt = log->tail_cnt;
ws->thread = log_to_thread(log);
log->cur_wrt_set = ws;
log->tail_cnt += get_log_size(chs);
mvrlu_assert(*bogus == 0);
}
/* allocate an object */
chs = log_alloc(log, obj_size, bogus);
chs->cpy_hdr.p_wrt_clk = &log->cur_wrt_set->wrt_clk;
chs->cpy_hdr.p_act = p_act;
chs->obj_hdr.type = TYPE_COPY;
return chs;
}
static inline void log_append_abort(mvrlu_log_t *log,
mvrlu_cpy_hdr_struct_t *chs)
{
/* Do nothing since tail_cnt and num_objs are not updated yet.
* Let's keep this for readability of the code. */
}
static inline void log_append_end(mvrlu_log_t *log, mvrlu_cpy_hdr_struct_t *chs,
int bogus_allocated)
{
log->tail_cnt += get_log_size(chs);
log->cur_wrt_set->num_objs++;
if (bogus_allocated) {
log->cur_wrt_set->num_objs++;
}
}
static inline unsigned long ws_iter_begin(mvrlu_wrt_set_t *ws)
{
mvrlu_cpy_hdr_struct_t *chs;
chs = obj_to_chs(ws);
return ws->start_tail_cnt + get_log_size(chs);
}
static inline unsigned long ws_iter_next(unsigned long iter,
mvrlu_cpy_hdr_struct_t *chs)
{
return iter + get_log_size(chs);
}
static inline int fp_is_free(mvrlu_free_ptrs_t *free_ptrs,
mvrlu_act_hdr_struct_t *ahs)
{
void *p_act;
unsigned int i;
p_act = ahs->obj_hdr.obj;
for (i = 0; i < free_ptrs->num_ptrs; ++i) {
if (free_ptrs->ptrs[i] == p_act)
return 1;
}
return 0;
}
static inline void fp_reset(mvrlu_free_ptrs_t *free_ptrs)
{
free_ptrs->num_ptrs = 0;
}
#define ws_for_each(log, ws, obj_idx, log_cnt) \
for ((obj_idx) = 0, (log_cnt) = ws_iter_begin(ws); \
(obj_idx) < (ws)->num_objs; \
++(obj_idx), (log_cnt) = ws_iter_next(log_cnt, chs))
static void ws_move_lock_to_copy(mvrlu_log_t *log, mvrlu_free_ptrs_t *free_ptrs)
{
mvrlu_wrt_set_t *ws;
mvrlu_cpy_hdr_struct_t *chs;
unsigned long cnt;
unsigned long wrt_clk_next;
unsigned int num_free;
unsigned int i;
ws = log->cur_wrt_set;
num_free = free_ptrs->num_ptrs;
ws_for_each (log, ws, i, cnt) {
mvrlu_act_hdr_struct_t *ahs;
volatile void *p_old_copy, *p_old_copy2;
chs = log_at_chs(log, cnt);
assert_chs_type(chs);
if (unlikely(chs->obj_hdr.type == TYPE_BOGUS)) {
continue;
}
ahs = vobj_to_ahs(chs->cpy_hdr.p_act);
mvrlu_assert(chs->obj_hdr.type == TYPE_COPY);
mvrlu_assert(ahs->act_hdr.p_lock == chs->obj_hdr.obj);
/* If an object is free()-ed, change its type. */
if (unlikely(num_free > 0 && fp_is_free(free_ptrs, ahs))) {
chs->obj_hdr.type = TYPE_FREE;
--num_free;
/* Freed copy should not be accessible
* from the version chain. */
continue;
}
/* If the size of copied object is zero, that is
* for try_lock_const() so we do not insert it
* to the version list. */
if (!chs->obj_hdr.obj_size)
continue;
/* Move a locked object to the version chain
* of an actual object. */
p_old_copy = ahs->obj_hdr.p_copy;
while (1) {
/* Initialize p_copy and wrt_clk_next. */
chs->obj_hdr.p_copy = p_old_copy;
if (p_old_copy == NULL)
chs->cpy_hdr.wrt_clk_next = MIN_VERSION;
else {
wrt_clk_next =
get_wrt_clk(vobj_to_chs(p_old_copy));
chs->cpy_hdr.wrt_clk_next = wrt_clk_next;
}
mvrlu_assert(chs->cpy_hdr.wrt_clk_next != MAX_VERSION);
smp_wmb_tso();
/* Since p_copy of p_act can be set to NULL upon
* reclaim, we should update it using smp_cas(). */
if (smp_cas_v(&ahs->obj_hdr.p_copy, p_old_copy,
chs->obj_hdr.obj, p_old_copy2))
break;
/* smp_cas_v() failed. Retry.
* p_old_copy2 is updated by smp_cas_v(). */
p_old_copy = p_old_copy2;
}
mvrlu_assert(ahs->obj_hdr.p_copy == chs->obj_hdr.obj);
}
}
static void ws_unlock(mvrlu_log_t *log, unsigned long wrt_clk)
{
mvrlu_wrt_set_t *ws;
mvrlu_cpy_hdr_struct_t *chs;
unsigned long cnt;
unsigned int i;
ws = log->cur_wrt_set;
wrt_clk = ws->wrt_clk;
ws_for_each (log, ws, i, cnt) {
chs = log_at_chs(log, cnt);
assert_chs_type(chs);
if (unlikely(chs->obj_hdr.type == TYPE_BOGUS)) {
continue;
}
mvrlu_assert(chs->obj_hdr.type == TYPE_COPY ||
chs->obj_hdr.type == TYPE_FREE);
/* Mark version */
if (wrt_clk != MAX_VERSION) {
chs->cpy_hdr.__wrt_clk = wrt_clk;
smp_wmb_tso();
}
/* Unlock */
if (likely(chs->obj_hdr.type == TYPE_COPY)) {
mvrlu_act_hdr_struct_t *ahs;
ahs = vobj_to_ahs(chs->cpy_hdr.p_act);
mvrlu_assert(ahs->act_hdr.p_lock == chs->obj_hdr.obj);
ahs->act_hdr.p_lock = NULL;
}
}
}
static void log_commit(mvrlu_log_t *log, mvrlu_free_ptrs_t *free_ptrs,
unsigned long local_clk)
{
mvrlu_assert(log->cur_wrt_set);
mvrlu_assert(obj_to_chs(log->cur_wrt_set)->obj_hdr.type ==
TYPE_WRT_SET);
/* Move a committed object to its version chain */
ws_move_lock_to_copy(log, free_ptrs);
smp_wmb();
/* Make them public atomically */
smp_atomic_store(&log->cur_wrt_set->wrt_clk, new_clock(local_clk));
/* Advance global clock */
advance_clock();
/* Unlock objects with marking wrt_clk */
ws_unlock(log, log->cur_wrt_set->wrt_clk);
/* Clean up */
log->cur_wrt_set = NULL;
fp_reset(free_ptrs);
}
static void log_abort(mvrlu_log_t *log, mvrlu_free_ptrs_t *free_ptrs)
{
/* Unlock objects without marking wrt_clk */
ws_unlock(log, MAX_VERSION);
/* Reset the current write set */
log->tail_cnt = log->cur_wrt_set->start_tail_cnt;
log->cur_wrt_set = NULL;
fp_reset(free_ptrs);
}
static inline int try_lock(volatile unsigned int *lock)
{
if (*lock == 0 && smp_cas(lock, 0, 1))
return 1;
return 0;
}
static inline void unlock(volatile unsigned int *lock)
{
*lock = 0;
}
static void log_reclaim(mvrlu_log_t *log)
{
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* MV-RLU
* ======
*
* head'
* head qp2 qp1 tail
* \ / / /
* +---------+====================+------+------+
* | |..........|/////////| | |
* +---------+====================+------+------+
* ~~~~~~~~~~>
* reclaim
* ~~~~~~~~~~>
* writeback
*
* 1. (qp1, tail)
* - The master is still accessible.
* 2. (qp2, qp1]
* - Nobody accesses the master because all threads
* access the copy. Thus, we can safely write back
* the copy to the master.
* - Write back only if the copy is the latest.
* - While nobody accesses the master, the copy is
* still accessible. So we cannot free the master yet.
* 3. [head, qp2]
* - Nobobdy accesses this copy because a thread
* accesses either of the master or a newer copy.
* Thus, we can safely reclaim this copy.
* - Since nobody accesses either the master
* or the copy, we can free the master.
* 4. head' = qp2
* - head is updated to qp2 because log reclamation
* will resume from qp2.
*
*/
mvrlu_wrt_set_t *ws;
mvrlu_wrt_set_struct_t *wss;
mvrlu_cpy_hdr_struct_t *chs;
unsigned long cnt;
unsigned long qp_clk1;
unsigned long qp_clk2;
unsigned int i;
unsigned long start_cnt;
unsigned long tail_cnt;
unsigned int index;
int reclaim;
int try_writeback;
if (!log->need_reclaim)
return;
if (!try_lock(&log->reclaim_lock))
return;
qp_clk1 = log->qp_clk1;
qp_clk2 = log->qp_clk2;
start_cnt = log->head_cnt;
tail_cnt = log->tail_cnt;
while (start_cnt < tail_cnt) {
reclaim = 0;
try_writeback = 0;
index = log_index(start_cnt);
wss = log_at_wss(log, index);
ws = &(wss->wrt_set);
if (gte_clock(ws->wrt_clk, qp_clk1) && ws->wrt_clk != qp_clk1)
break;
else if (lte_clock(ws->wrt_clk, qp_clk2))
reclaim = 1;
else if (lte_clock(ws->wrt_clk, qp_clk1))
try_writeback = 1;
ws_for_each (log, ws, i, cnt) {
chs = log_at_chs(log, cnt);
assert_chs_type(chs);
switch (chs->obj_hdr.type) {
case TYPE_COPY:
if (try_writeback && try_writeback_obj(chs))
try_detach_obj(chs);
stat_log_inc(log, n_reclaim_copy);
break;
case TYPE_FREE:
if (reclaim) {
free_obj(chs);
stat_log_inc(log, n_reclaim_free);
}
break;
case TYPE_BOGUS:
break; /* Do nothing */
default:
mvrlu_assert(0 && "Never be here");
break;
}
mvrlu_assert(cnt <= log->tail_cnt);
}
start_cnt = cnt;
mvrlu_assert(start_cnt <= log->tail_cnt);
if (reclaim)
log->head_cnt = start_cnt;
stat_log_inc(log, n_reclaim_wrt_set);
}
stat_log_inc(log, n_reclaim);
log->need_reclaim = 0;
unlock(&log->reclaim_lock);
}
static void log_reclaim_force(mvrlu_log_t *log)
{
if (log->need_reclaim) {
log_reclaim(log);
return;
}
if (log->head_cnt != log->tail_cnt) {
int count = 0; /* TODO FIXME */
wakeup_qp_thread_for_reclaim();
do {
port_cpu_relax_and_yield();
smp_mb();
count++;
if (count == 1000) {
wakeup_qp_thread_for_reclaim();
count = 0;
}
} while (!log->need_reclaim);
log_reclaim(log);
}
}
/*
* Quiescent detection functions
*/
static void qp_init(mvrlu_qp_thread_t *qp_thread, unsigned long qp_clk)
{
mvrlu_thread_struct_t *thread;
mvrlu_list_t *pos, *n;
thread_list_lock(&g_live_threads);
{
thread_list_for_each_safe (&g_live_threads, pos, n, thread) {
thread->qp_info.run_cnt = thread->run_cnt;
thread->qp_info.need_wait =
thread->qp_info.run_cnt & 0x1;
}