-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartbuf_final.patch
4261 lines (4118 loc) · 120 KB
/
artbuf_final.patch
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
From 8c588da9ee2a53da898d49e86cc05d1d9fac9a59 Mon Sep 17 00:00:00 2001
From: "m.pantilimonov" <[email protected]>
Date: Sun, 25 Aug 2019 11:49:47 +0300
Subject: [PATCH 1/2] squashed art version without 'test_artree' and 'lib
artree'
---
.../pg_buffercache/pg_buffercache--1.2.sql | 35 +
contrib/pg_buffercache/pg_buffercache_pages.c | 233 ++
src/backend/storage/buffer/buf_table.c | 304 ++-
src/backend/storage/buffer/bufmgr.c | 295 ++-
src/backend/storage/buffer/freelist.c | 2 +
src/backend/storage/ipc/shmem.c | 36 +
src/backend/storage/smgr/smgr.c | 3 +
src/backend/utils/Makefile | 2 +-
src/backend/utils/tree/Makefile | 17 +
src/backend/utils/tree/shm_tree.c | 1909 +++++++++++++++++
src/include/storage/buf_internals.h | 18 +-
src/include/storage/shmem.h | 3 +
src/include/storage/smgr.h | 7 +
src/include/utils/shm_tree.h | 83 +
14 files changed, 2932 insertions(+), 15 deletions(-)
create mode 100644 src/backend/utils/tree/Makefile
create mode 100644 src/backend/utils/tree/shm_tree.c
create mode 100644 src/include/utils/shm_tree.h
diff --git a/contrib/pg_buffercache/pg_buffercache--1.2.sql b/contrib/pg_buffercache/pg_buffercache--1.2.sql
index 6ee5d8435b..c50e14261d 100644
--- a/contrib/pg_buffercache/pg_buffercache--1.2.sql
+++ b/contrib/pg_buffercache/pg_buffercache--1.2.sql
@@ -19,3 +19,38 @@ CREATE VIEW pg_buffercache AS
-- Don't want these to be available to public.
REVOKE ALL ON FUNCTION pg_buffercache_pages() FROM PUBLIC;
REVOKE ALL ON pg_buffercache FROM PUBLIC;
+
+CREATE FUNCTION pg_buffertree_common()
+RETURNS RECORD
+AS 'MODULE_PATHNAME', 'pg_buffertree_common'
+LANGUAGE C PARALLEL SAFE;
+
+-- Create a view for convenient access.
+CREATE VIEW pg_buffertree_common AS
+ SELECT P.* FROM pg_buffertree_common() AS P
+ (leaves_used int4, node4_used int4, node16_used int4,
+ node48_used int4, node256_used int4, subtrees_used int4,
+ leaves_total int4, node4_total int4, node16_total int4,
+ node48_total int4, node256_total int4, subtrees_total int4,
+ leaves_mem int4, node4_mem int4, node16_mem int4,
+ node48_mem int4, node256_mem int4, subtrees_mem int4);
+
+-- Don't want these to be available to public.
+REVOKE ALL ON FUNCTION pg_buffertree_common() FROM PUBLIC;
+REVOKE ALL ON pg_buffertree_common FROM PUBLIC;
+
+CREATE FUNCTION pg_buffertree_stats()
+RETURNS SETOF RECORD
+AS 'MODULE_PATHNAME', 'pg_buffertree_stats'
+LANGUAGE C PARALLEL SAFE;
+
+-- Create a view for convenient access.
+CREATE VIEW pg_buffertree AS
+ SELECT P.* FROM pg_buffertree_stats() AS P
+ (relfilenode oid, reltablespace oid, reldatabase oid,
+ relforknumber int2, nleaves int4, nelem4 int4, nelem16 int4,
+ nelem48 int4, nelem256 int4);
+
+-- Don't want these to be available to public.
+REVOKE ALL ON FUNCTION pg_buffertree_stats() FROM PUBLIC;
+REVOKE ALL ON pg_buffertree FROM PUBLIC;
diff --git a/contrib/pg_buffercache/pg_buffercache_pages.c b/contrib/pg_buffercache/pg_buffercache_pages.c
index b410aafa5a..b2f2ea9226 100644
--- a/contrib/pg_buffercache/pg_buffercache_pages.c
+++ b/contrib/pg_buffercache/pg_buffercache_pages.c
@@ -13,6 +13,7 @@
#include "funcapi.h"
#include "storage/buf_internals.h"
#include "storage/bufmgr.h"
+#include "utils/shm_tree.h"
#define NUM_BUFFERCACHE_PAGES_MIN_ELEM 8
@@ -237,3 +238,235 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
else
SRF_RETURN_DONE(funcctx);
}
+
+PG_FUNCTION_INFO_V1(pg_buffertree_common);
+
+Datum
+pg_buffertree_common(PG_FUNCTION_ARGS)
+{
+ TupleDesc tupdesc;
+ Datum values[18];
+ bool nulls[18];
+ long *stats;
+
+ /* Initialise NULL flags array */
+ MemSet(nulls, 0, sizeof(nulls));
+
+ /* Initialise attributes information in the tuple descriptor */
+ tupdesc = CreateTemplateTupleDesc(18, false);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 1, "leaves_used",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 2, "node4_used",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 3, "node16_used",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 4, "node48_used",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 5, "node256_used",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 6, "subtrees_used",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 7, "leaves_total",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 8, "node4_total",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 9, "node16_total",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 10, "node48_total",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 11, "node256_total",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 12, "subtrees_total",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 13, "leaves_mem",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 14, "node4_mem",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 15, "node16_mem",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 16, "node48_mem",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 17, "node256_mem",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 18, "subtrees_mem",
+ INT4OID, -1, 0);
+
+ BlessTupleDesc(tupdesc);
+
+ stats = BufTreeStats();
+
+ /* Fill values */
+ values[0] = Int32GetDatum(stats[0]);
+ values[1] = Int32GetDatum(stats[1]);
+ values[2] = Int32GetDatum(stats[2]);
+ values[3] = Int32GetDatum(stats[3]);
+ values[4] = Int32GetDatum(stats[4]);
+ values[5] = Int32GetDatum(stats[5]);
+ values[6] = Int32GetDatum(stats[6]);
+ values[7] = Int32GetDatum(stats[7]);
+ values[8] = Int32GetDatum(stats[8]);
+ values[9] = Int32GetDatum(stats[9]);
+ values[10] = Int32GetDatum(stats[10]);
+ values[11] = Int32GetDatum(stats[11]);
+ values[12] = Int32GetDatum(stats[12]);
+ values[13] = Int32GetDatum(stats[13]);
+ values[14] = Int32GetDatum(stats[14]);
+ values[15] = Int32GetDatum(stats[15]);
+ values[16] = Int32GetDatum(stats[16]);
+ values[17] = Int32GetDatum(stats[17]);
+
+ /* Returns the record as Datum */
+ PG_RETURN_DATUM(HeapTupleGetDatum(
+ heap_form_tuple(tupdesc, values, nulls)));
+}
+
+#define NUM_BUFFERTREE_STATS_ELEM 9
+
+/*
+ * Function context for data persisting over repeated calls.
+ */
+typedef struct
+{
+ TupleDesc tupdesc;
+ ARTREE_STATS *record;
+} BufferTreeStatsContext;
+
+typedef struct
+{
+ int record_idx;
+ ARTREE_STATS *record;
+} TreeIteratorState;
+
+static int
+StatsIteratorCallback(void *data, const uint8 *k, uint32_t k_len, void *val)
+{
+ TreeIteratorState *state = (TreeIteratorState *) data;
+ BufferTag *tag = (BufferTag *) k;
+ ARTREE *subtree = (ARTREE *) val;
+ ARTREE_STATS *stats = &state->record[state->record_idx++];
+
+ artree_fill_stats(subtree, stats);
+ stats->rnode = tag->rnode;
+ stats->forkNum = tag->forkNum;
+
+ return 0;
+}
+
+PG_FUNCTION_INFO_V1(pg_buffertree_stats);
+
+Datum
+pg_buffertree_stats(PG_FUNCTION_ARGS)
+{
+ FuncCallContext *funcctx;
+ Datum result;
+ MemoryContext oldcontext;
+ BufferTreeStatsContext *fctx; /* User function context. */
+ TupleDesc tupledesc;
+ TupleDesc expected_tupledesc;
+ HeapTuple tuple;
+
+ if (SRF_IS_FIRSTCALL())
+ {
+ ARTREE_STATS stats;
+ TreeIteratorState iter_state;
+ int max_calls;
+
+ funcctx = SRF_FIRSTCALL_INIT();
+
+ /* Switch context when allocating stuff to be used in later calls */
+ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
+
+ /* Create a user function context for cross-call persistence */
+ fctx = (BufferTreeStatsContext *) palloc(sizeof(BufferTreeStatsContext));
+
+ if (get_call_result_type(fcinfo, NULL, &expected_tupledesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+
+ if (expected_tupledesc->natts != NUM_BUFFERTREE_STATS_ELEM)
+ elog(ERROR, "incorrect number of output arguments");
+
+ /* Construct a tuple descriptor for the result rows. */
+ tupledesc = CreateTemplateTupleDesc(expected_tupledesc->natts, false);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 1, "relfilenode",
+ OIDOID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 2, "reltablespace",
+ OIDOID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 3, "reldatabase",
+ OIDOID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 4, "relforknumber",
+ INT2OID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 5, "nleaves",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 6, "nelem4",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 7, "nelem16",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 8, "nelem48",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupledesc, (AttrNumber) 9, "nelem256",
+ INT4OID, -1, 0);
+
+ fctx->tupdesc = BlessTupleDesc(tupledesc);
+
+ /* Lock main tree while we collect stats */
+ BufLockMainTree(LW_SHARED);
+ BufGetMainTreeStats(&stats);
+ max_calls = stats.nleaves + 1;
+
+ fctx->record = (ARTREE_STATS *)
+ MemoryContextAllocHuge(CurrentMemoryContext,
+ sizeof(ARTREE_STATS) * max_calls);
+
+ /* Set max calls and remember the user function context. */
+ funcctx->max_calls = max_calls;
+ funcctx->user_fctx = fctx;
+
+ /* Return to original context when allocating transient memory */
+ MemoryContextSwitchTo(oldcontext);
+
+ iter_state.record_idx = 0;
+ iter_state.record = fctx->record;
+ artree_iter(BufGetMainTree(), StatsIteratorCallback, &iter_state);
+
+ if (max_calls - 1 != iter_state.record_idx)
+ elog(ERROR, "incorrect stats in Buffer Shared Tree");
+
+ /* Save stats of the main buffer tree as last record */
+ fctx->record[iter_state.record_idx] = stats;
+
+ /* Don't forget to unlock once done */
+ BufUnLockMainTree();
+ }
+
+ funcctx = SRF_PERCALL_SETUP();
+
+ /* Get the saved state */
+ fctx = funcctx->user_fctx;
+
+ if (funcctx->call_cntr < funcctx->max_calls)
+ {
+ uint32 i = funcctx->call_cntr;
+ Datum values[NUM_BUFFERTREE_STATS_ELEM];
+ bool nulls[NUM_BUFFERTREE_STATS_ELEM];
+
+ MemSet((void *) nulls, 0, sizeof(nulls));
+
+ values[0] = ObjectIdGetDatum(fctx->record[i].rnode.relNode);
+ values[1] = ObjectIdGetDatum(fctx->record[i].rnode.spcNode);
+ values[2] = ObjectIdGetDatum(fctx->record[i].rnode.dbNode);
+ values[3] = ObjectIdGetDatum(fctx->record[i].forkNum);
+ values[4] = Int32GetDatum(fctx->record[i].nleaves);
+ values[5] = Int32GetDatum(fctx->record[i].nelem4);
+ values[6] = Int32GetDatum(fctx->record[i].nelem16);
+ values[7] = Int32GetDatum(fctx->record[i].nelem48);
+ values[8] = Int32GetDatum(fctx->record[i].nelem256);
+
+ /* Build and return the tuple. */
+ tuple = heap_form_tuple(fctx->tupdesc, values, nulls);
+ result = HeapTupleGetDatum(tuple);
+
+ SRF_RETURN_NEXT(funcctx, result);
+ }
+ else
+ SRF_RETURN_DONE(funcctx);
+}
diff --git a/src/backend/storage/buffer/buf_table.c b/src/backend/storage/buffer/buf_table.c
index 88155204fd..53b1c8bcbe 100644
--- a/src/backend/storage/buffer/buf_table.c
+++ b/src/backend/storage/buffer/buf_table.c
@@ -23,6 +23,7 @@
#include "storage/bufmgr.h"
#include "storage/buf_internals.h"
+#include "port/pg_bswap.h"
/* entry for buffer lookup hashtable */
@@ -34,6 +35,65 @@ typedef struct
static HTAB *SharedBufHash;
+static ARTREE *SharedBufTree;
+
+static FreeListARTree *SharedBlockSubtrees;
+
+/*
+ * quantity of leafs should depends on NBuffers and subtree's pool size...
+ * if we won't recycle empty subtrees, then, eventually, pool will be exhausted.
+ */
+#define NODESUBTREE_NELEM 10000
+
+#define PAYLOAD_MOD 0xF0000000
+
+// #define VALIDATE_ART 1
+
+#define USE_ART 1
+
+// #define USE_HASH 1
+
+#ifdef WORDS_BIGENDIAN
+#define SETUP_BLOCK_KEY(tagPtr) (tagPtr->blockNum)
+#else
+#define SETUP_BLOCK_KEY(tagPtr) (pg_bswap32(tagPtr->blockNum))
+#endif
+
+ARTREE *
+BufGetMainTree()
+{
+ return SharedBufTree;
+}
+
+void BufLockMainTree(LWLockMode mode)
+{
+ LWLockAcquire(artree_getlock(SharedBufTree), mode);
+}
+
+void BufUnLockMainTree()
+{
+ LWLockRelease(artree_getlock(SharedBufTree));
+}
+
+void BufTryLockTree(ARTREE *artp, LWLockMode mode)
+{
+ if (artp) LWLockAcquire(artree_getlock(artp), mode);
+}
+
+void BufTryUnLockTree(ARTREE *artp)
+{
+ if (artp) LWLockRelease(artree_getlock(artp));
+}
+
+void BufGetMainTreeStats(ARTREE_STATS *stats)
+{
+ /* caller should synchronize access */
+ artree_fill_stats(BufGetMainTree(), stats);
+ stats->rnode.dbNode = 0;
+ stats->rnode.relNode = 0;
+ stats->rnode.spcNode = 0;
+ stats->forkNum = 0;
+}
/*
* Estimate space needed for mapping hashtable
@@ -42,7 +102,32 @@ static HTAB *SharedBufHash;
Size
BufTableShmemSize(int size)
{
- return hash_estimate_size(size, sizeof(BufferLookupEnt));
+#ifdef USE_HASH
+ Size hashsize = hash_estimate_size(size, sizeof(BufferLookupEnt));
+#else
+ Size hashsize = 0;
+#endif
+
+ return hashsize;
+}
+
+/*
+ * Estimate space needed for mapping tree
+ */
+Size
+BufTreeShmemSize(int size)
+{
+#ifdef USE_ART
+ Size treesize = artree_estimate_size(NODESUBTREE_NELEM,
+ sizeof(BufferTag) - sizeof(BlockNumber),
+ size,
+ sizeof(BlockNumber),
+ 0);
+#else
+ Size treesize = 0;
+#endif
+
+ return treesize;
}
/*
@@ -53,9 +138,12 @@ void
InitBufTable(int size)
{
HASHCTL info;
+ ARTREECTL tinfo;
+ bool found;
/* assume no locking is needed yet */
+#ifdef USE_HASH
/* BufferTag maps to Buffer */
info.keysize = sizeof(BufferTag);
info.entrysize = sizeof(BufferLookupEnt);
@@ -65,6 +153,24 @@ InitBufTable(int size)
size, size,
&info,
HASH_ELEM | HASH_BLOBS | HASH_PARTITION);
+#endif
+#ifdef USE_ART
+ tinfo.keysize = sizeof(BufferTag) - sizeof(BlockNumber);
+ /* in case of buffer tree we can save id inside leaf pointer */
+ tinfo.entrysize = 0;
+ SharedBufTree = ShmemInitTree("Shared Buffer Lookup Tree",
+ NODESUBTREE_NELEM,
+ NBuffers,
+ &tinfo,
+ ARTREE_ELEM);
+
+ SharedBlockSubtrees = ShmemInitStruct("Shared Buffer Block Trees",
+ artree_subtreelist_size(NODESUBTREE_NELEM),
+ &found);
+
+ artree_build_subtreelist(SharedBlockSubtrees, SharedBufTree,
+ NODESUBTREE_NELEM, NBuffers);
+#endif
}
/*
@@ -89,21 +195,62 @@ BufTableHashCode(BufferTag *tagPtr)
* Caller must hold at least share lock on BufMappingLock for tag's partition
*/
int
-BufTableLookup(BufferTag *tagPtr, uint32 hashcode)
+BufTableLookup(ARTREE *subtree, BufferTag *tagPtr, uint32 hashcode)
{
BufferLookupEnt *result;
+#ifdef USE_ART
+ uintptr_t shmresult = 0;
+ if (subtree)
+ {
+ uint32 block_key = SETUP_BLOCK_KEY(tagPtr);
+ shmresult = (uintptr_t) artree_search(
+ subtree, (const uint8 *) &block_key);
+ }
+#endif
+#ifdef USE_HASH
result = (BufferLookupEnt *)
hash_search_with_hash_value(SharedBufHash,
(void *) tagPtr,
hashcode,
HASH_FIND,
NULL);
+#endif
+#ifdef VALIDATE_ART
+ if (!shmresult)
+ {
+ if (result != NULL)
+ {
+ elog(WARNING, "lookup:artree didn't find, hash did.");
+ }
+ }
+ else
+ {
+ if (result != NULL)
+ {
+ if (result->id != (shmresult & ~PAYLOAD_MOD)) {
+ elog(WARNING, "lookup:artree mismatch. hash=%d tree=%zu",
+ result->id, shmresult & ~PAYLOAD_MOD);
+ }
+ }
+ else
+ {
+ elog(WARNING, "lookup:artree found tag, hash didn't.");
+ }
+ }
+#endif
+#ifdef USE_ART
+ if (!shmresult)
+ return -1;
+ return shmresult & ~PAYLOAD_MOD;
+#endif
+#ifdef USE_HASH
if (!result)
return -1;
return result->id;
+#endif
}
/*
@@ -117,7 +264,7 @@ BufTableLookup(BufferTag *tagPtr, uint32 hashcode)
* Caller must hold exclusive lock on BufMappingLock for tag's partition
*/
int
-BufTableInsert(BufferTag *tagPtr, uint32 hashcode, int buf_id)
+BufTableInsert(ARTREE *subtree, BufferTag *tagPtr, uint32 hashcode, int buf_id)
{
BufferLookupEnt *result;
bool found;
@@ -125,19 +272,63 @@ BufTableInsert(BufferTag *tagPtr, uint32 hashcode, int buf_id)
Assert(buf_id >= 0); /* -1 is reserved for not-in-table */
Assert(tagPtr->blockNum != P_NEW); /* invalid tag */
+#ifdef USE_ART
+ uintptr_t shmresult;
+ uint64_t payload = PAYLOAD_MOD | buf_id;
+ uint32 block_key = SETUP_BLOCK_KEY(tagPtr);
+
+ Assert(subtree);
+
+ shmresult = (uintptr_t) artree_insert(
+ subtree, (const uint8 *) &block_key, (void *) payload);
+#endif
+#ifdef USE_HASH
result = (BufferLookupEnt *)
hash_search_with_hash_value(SharedBufHash,
(void *) tagPtr,
hashcode,
HASH_ENTER,
&found);
+#endif
+#ifdef VALIDATE_ART
+ if (shmresult) /* found existing tag */
+ {
+ if (result != NULL)
+ {
+ if (result->id != (shmresult & ~PAYLOAD_MOD))
+ {
+ elog(WARNING, "insert:artree mismatch. hash=%d told=%zu tnew=%d",
+ result->id, shmresult & ~PAYLOAD_MOD, buf_id);
+ }
+ }
+ else
+ {
+ elog(WARNING, "insert:artree found old, hash did not");
+ }
+ }
+ else /* didnt find anything */
+ {
+ if (found)
+ {
+ elog(WARNING, "insert:artree did not find, but hash did");
+ }
+ result->id = buf_id;
+ }
+#endif
+#ifdef USE_ART
+ if (shmresult)
+ return shmresult & ~PAYLOAD_MOD;
+ return -1;
+#endif
+#ifdef USE_HASH
if (found) /* found something already in the table */
return result->id;
result->id = buf_id;
return -1;
+#endif
}
/*
@@ -147,17 +338,120 @@ BufTableInsert(BufferTag *tagPtr, uint32 hashcode, int buf_id)
* Caller must hold exclusive lock on BufMappingLock for tag's partition
*/
void
-BufTableDelete(BufferTag *tagPtr, uint32 hashcode)
+BufTableDelete(ARTREE *subtree, BufferTag *tagPtr, uint32 hashcode)
{
BufferLookupEnt *result;
+ uintptr_t shmresult;
+
+#ifdef USE_ART
+ Assert(subtree);
+ uint32 block_key = SETUP_BLOCK_KEY(tagPtr);
+ shmresult = (uintptr_t) artree_delete(
+ subtree, (const uint8 *) &block_key);
+#endif
+#ifdef USE_HASH
result = (BufferLookupEnt *)
hash_search_with_hash_value(SharedBufHash,
(void *) tagPtr,
hashcode,
HASH_REMOVE,
NULL);
-
+#endif
+#ifdef USE_ART
+ if (!shmresult)
+ {
+ elog(WARNING, "delete:artree corrupted");
+ }
+#endif
+#ifdef VALIDATE_ART
+ else
+ {
+ if (result->id != (shmresult & ~PAYLOAD_MOD))
+ {
+ elog(WARNING, "delete:artree mismatch. hash=%d tree=%zu",
+ result->id, shmresult & ~PAYLOAD_MOD);
+ }
+ }
+#endif
+#ifdef USE_HASH
if (!result) /* shouldn't happen */
elog(ERROR, "shared buffer hash table corrupted");
+#endif
+}
+
+long *
+BufTreeStats(void)
+{
+ return artree_nodes_used(SharedBufTree, SharedBlockSubtrees);
+}
+
+ARTREE *
+BufInstallSubtree(SMgrRelation smgr, BufferTag *tagPtr)
+{
+ ARTREE *subtree;
+ uintptr_t shmresult;
+
+ subtree = smgr->cached_forks[tagPtr->forkNum];
+ if (!subtree)
+ {
+ subtree = artree_alloc_subtree(SharedBlockSubtrees);
+ shmresult = (uintptr_t) artree_insert(
+ SharedBufTree, (const uint8 *) tagPtr, (void *) subtree);
+ if (shmresult != 0)
+ {
+ artree_dealloc_subtree(SharedBlockSubtrees, subtree);
+ subtree = (ARTREE *) shmresult;
+ elog(DEBUG1, "BufInstallSubtree: collision occured.");
+ }
+ smgr->cached_forks[tagPtr->forkNum] = subtree;
+ }
+ return subtree;
+}
+
+void
+BufUnistallSubtree(BufferTag *tagPtr)
+{
+ ARTREE *subtree;
+
+ subtree = (ARTREE *) artree_delete(
+ SharedBufTree, (const uint8 *) tagPtr);
+
+ if (subtree)
+ {
+ artree_dealloc_subtree(SharedBlockSubtrees, subtree);
+ }
+ // todo: send message to backends to invalidate cache...
+ // or it is(probably) already done in CacheInvalidateSmgr(rnode);
+ // need to check that functionality, so we can move towards subtree recycling
+ // smgrdounlinkfork
+}
+
+ARTREE *
+BufLookupSubtree(SMgrRelation smgr, BufferTag *tagPtr)
+{
+ ARTREE *subtree;
+
+ // todo: need somehow invalidate subtree (add & check flag inside art_tree?)
+ // but first recycle functionaly required
+ subtree = smgr->cached_forks[tagPtr->forkNum];
+ if (!subtree)
+ {
+ subtree = (ARTREE *) artree_search(
+ SharedBufTree, (const uint8 *) tagPtr);
+ smgr->cached_forks[tagPtr->forkNum] = subtree;
+ }
+
+ return subtree;
+}
+
+ARTREE *
+BufLookupSubtreeNoCache(BufferTag *tagPtr)
+{
+ ARTREE *subtree;
+
+ subtree = (ARTREE *) artree_search(
+ SharedBufTree, (const uint8 *) tagPtr);
+
+ return subtree;
}
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 01eabe5706..2950b7aa56 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -53,6 +53,13 @@
#include "utils/timestamp.h"
+#define USE_ART 1
+#define USE_ART_DROP 1
+
+// #define USE_HASH 1
+// #define USE_HASH_DROP 1
+// #define USE_HASH_DROP_VALIDATE 1
+
/* Note: these two macros only work on shared buffers, not local ones! */
#define BufHdrGetBlock(bufHdr) ((Block) (BufferBlocks + ((Size) (bufHdr)->buf_id) * BLCKSZ))
#define BufferGetLSN(bufHdr) (PageGetLSN(BufHdrGetBlock(bufHdr)))
@@ -552,19 +559,36 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
uint32 newHash; /* hash value for newTag */
LWLock *newPartitionLock; /* buffer partition lock for it */
int buf_id;
+ ARTREE *subtree;
/* create a tag so we can lookup the buffer */
INIT_BUFFERTAG(newTag, reln->rd_smgr->smgr_rnode.node,
forkNum, blockNum);
+#ifdef USE_HASH
/* determine its hash code and partition lock ID */
newHash = BufTableHashCode(&newTag);
newPartitionLock = BufMappingPartitionLock(newHash);
/* see if the block is in the buffer pool already */
LWLockAcquire(newPartitionLock, LW_SHARED);
- buf_id = BufTableLookup(&newTag, newHash);
+#else
+ newHash = 0;
+ newPartitionLock = NULL;
+#endif
+#ifdef USE_ART
+ BufLockMainTree(LW_SHARED);
+ subtree = BufLookupSubtree(reln->rd_smgr, &newTag);
+ BufTryLockTree(subtree, LW_SHARED);
+ BufUnLockMainTree();
+#endif
+ buf_id = BufTableLookup(subtree, &newTag, newHash);
+#ifdef USE_HASH
LWLockRelease(newPartitionLock);
+#endif
+#ifdef USE_ART
+ BufTryUnLockTree(subtree);
+#endif
/* If not in buffers, initiate prefetch */
if (buf_id < 0)
@@ -1000,17 +1024,30 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
BufferDesc *buf;
bool valid;
uint32 buf_state;
+ ARTREE *newSubtree;
+ ARTREE *oldSubtree;
/* create a tag so we can lookup the buffer */
INIT_BUFFERTAG(newTag, smgr->smgr_rnode.node, forkNum, blockNum);
+#ifdef USE_HASH
/* determine its hash code and partition lock ID */
newHash = BufTableHashCode(&newTag);
newPartitionLock = BufMappingPartitionLock(newHash);
/* see if the block is in the buffer pool already */
LWLockAcquire(newPartitionLock, LW_SHARED);
- buf_id = BufTableLookup(&newTag, newHash);
+#else
+ newHash = 0;
+ newPartitionLock = NULL;
+#endif
+#ifdef USE_ART
+ BufLockMainTree(LW_SHARED);
+ newSubtree = BufLookupSubtree(smgr, &newTag);
+ BufTryLockTree(newSubtree, LW_SHARED);
+ BufUnLockMainTree();
+#endif
+ buf_id = BufTableLookup(newSubtree, &newTag, newHash);
if (buf_id >= 0)
{
/*
@@ -1022,8 +1059,13 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
valid = PinBuffer(buf, strategy);
+#ifdef USE_HASH
/* Can release the mapping lock as soon as we've pinned it */
LWLockRelease(newPartitionLock);
+#endif
+#ifdef USE_ART
+ BufTryUnLockTree(newSubtree);
+#endif
*foundPtr = true;
@@ -1049,11 +1091,26 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
return buf;
}
+#ifdef USE_HASH
/*
* Didn't find it in the buffer pool. We'll have to initialize a new
* buffer. Remember to unlock the mapping lock while doing the work.
*/
LWLockRelease(newPartitionLock);
+#endif
+#ifdef USE_ART
+ /*
+ * If we are here then it means we are going to insert new tag,
+ * so it is better to make sure newSubtree allocated beforehand.
+ */
+ BufTryUnLockTree(newSubtree);
+ if (!newSubtree)
+ {
+ BufLockMainTree(LW_EXCLUSIVE);
+ newSubtree = BufInstallSubtree(smgr, &newTag);
+ BufUnLockMainTree();
+ }
+#endif
/* Loop here in case we have to try another victim buffer */
for (;;)
@@ -1171,6 +1228,7 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
* not recompute it here? Probably not.
*/
oldTag = buf->tag;
+#ifdef USE_HASH
oldHash = BufTableHashCode(&oldTag);
oldPartitionLock = BufMappingPartitionLock(oldHash);
@@ -1193,11 +1251,38 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
/* only one partition, only one lock */
LWLockAcquire(newPartitionLock, LW_EXCLUSIVE);
}
+#endif
+#ifdef USE_ART
+ BufLockMainTree(LW_SHARED);
+ oldSubtree = BufLookupSubtreeNoCache(&oldTag);
+ BufUnLockMainTree();
+ if (oldSubtree < newSubtree)
+ {
+ BufTryLockTree(oldSubtree, LW_EXCLUSIVE);
+ BufTryLockTree(newSubtree, LW_EXCLUSIVE);
+ }
+ else if (oldSubtree > newSubtree)
+ {
+ BufTryLockTree(newSubtree, LW_EXCLUSIVE);
+ BufTryLockTree(oldSubtree, LW_EXCLUSIVE);
+ }
+ else
+ {
+ /* only one subtree, only one lock */
+ BufTryLockTree(newSubtree, LW_EXCLUSIVE);
+ }
+#endif
}
else
{
+#ifdef USE_HASH
/* if it wasn't valid, we need only the new partition */
LWLockAcquire(newPartitionLock, LW_EXCLUSIVE);
+#endif
+#ifdef USE_ART
+ BufTryLockTree(newSubtree, LW_EXCLUSIVE);
+ oldSubtree = NULL;
+#endif
/* remember we have no old-partition lock or tag */
oldPartitionLock = NULL;
/* this just keeps the compiler quiet about uninit variables */
@@ -1211,7 +1296,7 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
* Note that we have not yet removed the hashtable entry for the old
* tag.
*/
- buf_id = BufTableInsert(&newTag, newHash, buf->buf_id);
+ buf_id = BufTableInsert(newSubtree, &newTag, newHash, buf->buf_id);
if (buf_id >= 0)
{
@@ -1223,10 +1308,16 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
*/
UnpinBuffer(buf, true);
+#ifdef USE_HASH
/* Can give up that buffer's mapping partition lock now */
if (oldPartitionLock != NULL &&
oldPartitionLock != newPartitionLock)
LWLockRelease(oldPartitionLock);
+#endif
+#ifdef USE_ART
+ if (oldSubtree != NULL && oldSubtree != newSubtree)
+ BufTryUnLockTree(oldSubtree);
+#endif
/* remaining code should match code at top of routine */
@@ -1234,8 +1325,13 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
valid = PinBuffer(buf, strategy);
+#ifdef USE_HASH
/* Can release the mapping lock as soon as we've pinned it */
LWLockRelease(newPartitionLock);
+#endif
+#ifdef USE_ART
+ BufTryUnLockTree(newSubtree);
+#endif
*foundPtr = true;
@@ -1277,11 +1373,18 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
break;
UnlockBufHdr(buf, buf_state);
- BufTableDelete(&newTag, newHash);
+ BufTableDelete(newSubtree, &newTag, newHash);
+#ifdef USE_HASH
if (oldPartitionLock != NULL &&
oldPartitionLock != newPartitionLock)
LWLockRelease(oldPartitionLock);
LWLockRelease(newPartitionLock);
+#endif
+#ifdef USE_ART
+ if (oldSubtree != NULL && oldSubtree != newSubtree)
+ BufTryUnLockTree(oldSubtree);
+ BufTryUnLockTree(newSubtree);
+#endif
UnpinBuffer(buf, true);
}
@@ -1309,14 +1412,28 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
UnlockBufHdr(buf, buf_state);
+ bool here = false;
+#ifdef USE_HASH
if (oldPartitionLock != NULL)
{
- BufTableDelete(&oldTag, oldHash);
+ here = true;
+ BufTableDelete(oldSubtree, &oldTag, oldHash);
if (oldPartitionLock != newPartitionLock)
LWLockRelease(oldPartitionLock);
}
LWLockRelease(newPartitionLock);
+#endif
+#ifdef USE_ART
+ if (oldSubtree != NULL)
+ {
+ if (!here) // only to test with hash => delete later
+ BufTableDelete(oldSubtree, &oldTag, oldHash);
+ if (oldSubtree != newSubtree)
+ BufTryUnLockTree(oldSubtree);
+ }
+ BufTryUnLockTree(newSubtree);
+#endif
/*
* Buffer contents are currently invalid. Try to get the io_in_progress
@@ -1356,6 +1473,7 @@ InvalidateBuffer(BufferDesc *buf)
LWLock *oldPartitionLock; /* buffer partition lock for it */
uint32 oldFlags;
uint32 buf_state;
+ ARTREE *oldSubtree;
/* Save the original buffer tag before dropping the spinlock */