-
Notifications
You must be signed in to change notification settings - Fork 16
/
shmem.c
1467 lines (1288 loc) · 37.5 KB
/
shmem.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
/*
* shmem.c
*
* Management of shared memory segment
* ----
* Copyright 2011-2014 (C) KaiGai Kohei <[email protected]>
* Copyright 2014 (C) The PG-Strom Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "lib/ilist.h"
#include "storage/barrier.h"
#include "storage/ipc.h"
#include "storage/shmem.h"
#include "storage/spin.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/pg_crc.h"
#include "pg_strom.h"
#include <limits.h>
#include <unistd.h>
/*
* management of shared memory segment in PG-Strom
*
* Once a shared memory segment is allocated, PG-Strom split it into
* multiple zones. A zone usually has more than 500MB, according to
* the capability of OpenCL driver to map a parciular area as page-locked
* memory. Also, it shall be associated with a particular NUMA node for
* better memory access latency, in the future version.
*
* A zone contains a certain number of fixed-length (= SHMEM_BLOCKSZ) blocks.
* Block allocation system allocates 2^n blocks for the request.
* On the other hand, context based allocation also allows to assign smaller
* chunks on blocks being allocated by block allocation system.
*/
typedef struct
{
dlist_node chain; /* to be chained free_list of shmem_zone */
Size blocksz; /* block size in bytes if head */
} shmem_block;
#define BLOCK_IS_ACTIVE(block) \
(((block)->chain.next == NULL && \
(block)->chain.prev == NULL) && \
(block)->blocksz > 0)
#define BLOCK_IS_FREE(block) \
(((block)->chain.next != NULL || \
(block)->chain.prev != NULL) && \
(block)->blocksz > 0)
typedef struct
{
slock_t lock;
long num_blocks; /* number of total blocks */
long num_active[SHMEM_BLOCKSZ_BITS_RANGE + 1];
long num_free[SHMEM_BLOCKSZ_BITS_RANGE + 1];
dlist_head free_list[SHMEM_BLOCKSZ_BITS_RANGE + 1];
void *private; /* cl_mem being mapped (Only OpenCL server) */
void *block_baseaddr;
shmem_block blocks[FLEXIBLE_ARRAY_MEMBER];
} shmem_zone;
typedef struct {
dlist_node chain; /* link to the free list */
const char *filename;
uint32 lineno;
pid_t owner;
Datum data[FLEXIBLE_ARRAY_MEMBER];
} shmem_slab;
typedef struct {
dlist_node chain;
int slab_index; /* index of slab_sizes */
int slab_nums; /* number of slabs per block */
shmem_slab entry; /* first entry in this block */
} shmem_slab_head;
#define SHMEM_SLAB_SIZE(div) \
MAXALIGN_DOWN((SHMEM_BLOCKSZ - SHMEM_ALLOC_COST) / (div) - \
(offsetof(shmem_slab, data) + sizeof(cl_uint)))
static size_t slab_sizes[] = {
SHMEM_SLAB_SIZE(56), /* about 100B */
SHMEM_SLAB_SIZE(30), /* about 240B */
SHMEM_SLAB_SIZE(15), /* about 512B */
SHMEM_SLAB_SIZE(6), /* about 1.2KB */
SHMEM_SLAB_SIZE(3), /* about 2.5KB */
};
#undef SHMEM_SLAB_SIZE
typedef struct
{
slock_t slab_locks[lengthof(slab_sizes)];
dlist_head slab_freelist[lengthof(slab_sizes)];
dlist_head slab_blocklist[lengthof(slab_sizes)];
/* for zone management */
bool is_ready;
int num_zones;
void *zone_baseaddr;
Size zone_length;
shmem_zone *zones[FLEXIBLE_ARRAY_MEMBER];
} shmem_head;
typedef struct {
uint32 magic; /* = SHMEM_BODY_MAGIC */
pid_t owner;
const char *filename;
int lineno;
Datum data[FLEXIBLE_ARRAY_MEMBER];
} shmem_body;
/* XXX - we need to ensure SHMEM_ALLOC_COST is enough large */
#define SHMEM_BODY_MAGIC 0xabadcafe
#define SHMEM_BLOCK_MAGIC 0xdeadbeaf
#define SHMEM_SLAB_MAGIC 0xabadf11e
#define ADDRESS_IN_SHMEM(address) \
((Size)(address) >= (Size)pgstrom_shmem_head->zone_baseaddr && \
(Size)(address) < ((Size)pgstrom_shmem_head->zone_baseaddr + \
pgstrom_shmem_totalsize))
#define ADDRESS_IN_SHMEM_ZONE(zone,address) \
((Size)(address) >= (Size)(zone)->block_baseaddr && \
(Size)(address) < ((Size)(zone)->block_baseaddr + \
(zone)->num_blocks) * SHMEM_BLOCKSZ)
/* static variables */
static shmem_startup_hook_type shmem_startup_hook_next;
static Size pgstrom_shmem_totalsize;
static int pgstrom_shmem_maxzones;
static shmem_head *pgstrom_shmem_head;
/*
* find_least_pot
*
* It looks up the least power of two that is equal or larger than
* the provided size.
*/
static inline int
find_least_pot(Size size)
{
int shift = 0;
size--;
#if SIZEOF_VOID_P == 8
if ((size & 0xffffffff00000000UL) != 0)
{
size >>= 32;
shift += 32;
}
#endif
if ((size & 0xffff0000UL) != 0)
{
size >>= 16;
shift += 16;
}
if ((size & 0x0000ff00UL) != 0)
{
size >>= 8;
shift += 8;
}
if ((size & 0x000000f0UL) != 0)
{
size >>= 4;
shift += 4;
}
if ((size & 0x0000000cUL) != 0)
{
size >>= 2;
shift += 2;
}
if ((size & 0x00000002UL) != 0)
{
size >>= 1;
shift += 1;
}
if ((size & 0x00000001UL) != 0)
shift += 1;
return Max(shift, SHMEM_BLOCKSZ_BITS) - SHMEM_BLOCKSZ_BITS;
}
/*
*
* XXX - caller must have lock of supplied zone
*/
static bool
pgstrom_shmem_zone_block_split(shmem_zone *zone, int shift)
{
shmem_block *block;
dlist_node *dnode;
int index;
int i;
Assert(shift > 0 && shift <= SHMEM_BLOCKSZ_BITS_RANGE);
if (dlist_is_empty(&zone->free_list[shift]))
{
if (shift == SHMEM_BLOCKSZ_BITS_RANGE ||
!pgstrom_shmem_zone_block_split(zone, shift+1))
return false;
}
Assert(!dlist_is_empty(&zone->free_list[shift]));
dnode = dlist_pop_head_node(&zone->free_list[shift]);
zone->num_free[shift]--;
block = dlist_container(shmem_block, chain, dnode);
block->blocksz = (1 << (shift-1)) * SHMEM_BLOCKSZ;
index = block - &zone->blocks[0];
Assert((index & ((1UL << shift) - 1)) == 0);
dlist_push_tail(&zone->free_list[shift-1], &block->chain);
/* is it exactly block head? */
for (i=1; i < (1 << shift); i++)
Assert(!BLOCK_IS_ACTIVE(block+i) && !BLOCK_IS_FREE(block+i));
block += (1 << (shift - 1));
block->blocksz = (1 << (shift-1)) * SHMEM_BLOCKSZ;
dlist_push_tail(&zone->free_list[shift-1], &block->chain);
zone->num_free[shift-1] += 2;
return true;
}
static void *
pgstrom_shmem_zone_block_alloc(shmem_zone *zone,
const char *filename, int lineno, Size size)
{
shmem_block *block;
shmem_body *body;
dlist_node *dnode;
Size total_size;
int shift;
int index;
void *address;
int i;
total_size = offsetof(shmem_body, data[0]) + size + sizeof(cl_uint);
if (total_size > (1UL << SHMEM_BLOCKSZ_BITS_MAX))
return NULL; /* too large size required */
shift = find_least_pot(total_size);
if (dlist_is_empty(&zone->free_list[shift]))
{
if (!pgstrom_shmem_zone_block_split(zone, shift+1))
return NULL;
}
Assert(!dlist_is_empty(&zone->free_list[shift]));
dnode = dlist_pop_head_node(&zone->free_list[shift]);
block = dlist_container(shmem_block, chain, dnode);
Assert(block->blocksz == (1UL << shift) * SHMEM_BLOCKSZ);
memset(block, 0, sizeof(shmem_block));
block->blocksz = size;
/* non-head block are zero cleared? */
for (i=1; i < (1 << shift); i++)
Assert(!BLOCK_IS_ACTIVE(block+i) && !BLOCK_IS_FREE(block+i));
index = block - &zone->blocks[0];
body = (shmem_body *)((char *)zone->block_baseaddr +
index * SHMEM_BLOCKSZ);
zone->num_free[shift]--;
zone->num_active[shift]++;
/* tracking info */
body->magic = SHMEM_BODY_MAGIC;
body->owner = getpid();
body->filename = filename; /* must be static cstring! */
body->lineno = lineno;
address = (void *)body->data;
/* to detect overrun */
*((cl_uint *)((uintptr_t)address + size)) = SHMEM_BLOCK_MAGIC;
return address;
}
static void
pgstrom_shmem_zone_block_free(shmem_zone *zone, shmem_body *body)
{
shmem_block *block;
long index;
long shift;
Assert(ADDRESS_IN_SHMEM_ZONE(zone, body));
index = ((uintptr_t)body -
(uintptr_t)zone->block_baseaddr) / SHMEM_BLOCKSZ;
block = &zone->blocks[index];
Assert(BLOCK_IS_ACTIVE(block));
/* detect overrun */
Assert(*((cl_uint *)((uintptr_t)body->data +
block->blocksz)) == SHMEM_BLOCK_MAGIC);
shift = find_least_pot(block->blocksz +
offsetof(shmem_body, data[0]) +
sizeof(cl_uint));
Assert(shift <= SHMEM_BLOCKSZ_BITS_RANGE);
Assert((index & ~((1UL << shift) - 1)) == index);
zone->num_active[shift]--;
/* try to merge buddy blocks if it is also free */
while (shift < SHMEM_BLOCKSZ_BITS_RANGE)
{
shmem_block *buddy;
long buddy_index = index ^ (1UL << shift);
if (buddy_index + (1UL << shift) >= zone->num_blocks)
break;
buddy = &zone->blocks[buddy_index];
/*
* The buddy block can be merged if it is also free and same size.
*/
if (BLOCK_IS_ACTIVE(buddy) ||
buddy->blocksz != (1UL << shift) * SHMEM_BLOCKSZ)
break;
/* ensure buddy is block head */
Assert(BLOCK_IS_FREE(buddy));
dlist_delete(&buddy->chain);
if (buddy_index < index)
{
/* mark this block is not a head */
memset(block, 0, sizeof(shmem_block));
block = buddy;
index = buddy_index;
}
else
{
/* mark this block is not a head */
memset(buddy, 0, sizeof(shmem_block));
}
zone->num_free[shift]--;
shift++;
}
zone->num_free[shift]++;
block->blocksz = (1UL << shift) * SHMEM_BLOCKSZ;
dlist_push_head(&zone->free_list[shift], &block->chain);
}
/*
* pgstrom_alloc_slab
*/
static void *
pgstrom_alloc_slab(const char *filename, int lineno, int index)
{
shmem_slab_head *sblock;
shmem_slab *entry;
dlist_node *dnode;
Size slab_sz = slab_sizes[index];
Size unitsz = MAXALIGN(offsetof(shmem_slab, data[0]) +
INTALIGN(slab_sz) + sizeof(cl_uint));
SpinLockAcquire(&pgstrom_shmem_head->slab_locks[index]);
if (dlist_is_empty(&pgstrom_shmem_head->slab_freelist[index]))
{
Size length;
Size offset;
int count;
/* allocate a block */
sblock = __pgstrom_shmem_alloc_alap(filename, lineno,
sizeof(shmem_slab_head), &length);
if (!sblock)
{
SpinLockRelease(&pgstrom_shmem_head->slab_locks[index]);
return NULL;
}
dlist_push_tail(&pgstrom_shmem_head->slab_blocklist[index],
&sblock->chain);
for (offset = offsetof(shmem_slab_head, entry), count=0;
offset + unitsz <= length;
offset += unitsz, count++)
{
entry = (shmem_slab *)((char *)sblock + offset);
/* set magic number */
*((uint32 *)((char *)entry->data +
INTALIGN(slab_sz))) = SHMEM_SLAB_MAGIC;
dlist_push_head(&pgstrom_shmem_head->slab_freelist[index],
&entry->chain);
}
sblock->slab_index = index;
sblock->slab_nums = count;
}
Assert(!dlist_is_empty(&pgstrom_shmem_head->slab_freelist[index]));
dnode = dlist_pop_head_node(&pgstrom_shmem_head->slab_freelist[index]);
entry = dlist_container(shmem_slab, chain, dnode);
memset(&entry->chain, 0, sizeof(dlist_node));
entry->owner = getpid();
entry->filename = filename;
entry->lineno = lineno;
SpinLockRelease(&pgstrom_shmem_head->slab_locks[index]);
return (void *)entry->data;
}
/*
* pgstrom_shmem_block_alloc
*
* It is an internal API; that allocates a continuous 2^N blocks from
* a particular shared memory zone. It tries to split a larger memory blocks
* if suitable memory blocks are not free. If no memory blocks are available,
* it goes into another zone to allocate memory.
*/
void *
__pgstrom_shmem_alloc(const char *filename, int lineno, Size size)
{
static int zone_index = 0;
int start;
shmem_zone *zone;
void *address;
int i;
/* does shared memory segment already set up? */
if (!pgstrom_shmem_head->is_ready)
{
elog(LOG, "PG-Strom's shared memory segment has not been ready");
return NULL;
}
/*
* Size check whether we should allocate bare-blocks, or a piece of
* slabs. If required size is unable to allocate, return NULL soon.
*/
if (size == 0 || size > (1UL << SHMEM_BLOCKSZ_BITS_MAX))
return NULL;
for (i=0; i < lengthof(slab_sizes); i++)
{
if (size <= slab_sizes[i])
return pgstrom_alloc_slab(filename, lineno, i);
}
/*
* find a zone we should allocate.
*
* XXX - To be put more wise zone selection
* - NUMA aware
* - Memory reclaim when no blocks are available
*/
start = zone_index;
do {
zone = pgstrom_shmem_head->zones[zone_index];
SpinLockAcquire(&zone->lock);
address = pgstrom_shmem_zone_block_alloc(zone, filename, lineno, size);
SpinLockRelease(&zone->lock);
if (address)
break;
zone_index = (zone_index + 1) % pgstrom_shmem_head->num_zones;
} while (zone_index != start);
#ifdef PGSTROM_DEBUG
/* For debugging, we dump current status of shared memory segment
* if we have to return "out of shared memory" error */
if (!address)
{
pgstrom_shmem_dump();
clserv_log("%s:%d required %zu bytes of shared memory",
filename, lineno, size);
}
#endif
return address;
}
/*
* pgstrom_shmem_alloc_alap
*
* pgstrom_shmem_alloc "as large as possible"
* It is unavoidable to make unused memory area in buddy memory allocation
* algorithm. In case when we want to acquire a memory block larget than
* a particular size, likely toast buffer, best storategy is to allocate
* least 2^N block larger than required size.
* This function round up the required size into the best-fit one.
*
* Also note that, it never falls to slab.
*/
void *
__pgstrom_shmem_alloc_alap(const char *filename, int lineno,
Size required, Size *allocated)
{
int shift = find_least_pot(required + sizeof(cl_uint));
void *result;
required = (1UL << (shift + SHMEM_BLOCKSZ_BITS))
- offsetof(shmem_body, data[0])
- sizeof(cl_uint);
result = __pgstrom_shmem_alloc(filename, lineno, required);
if (result && allocated)
*allocated = required;
return result;
}
/*
* pgstrom_free_slab
*/
static void
pgstrom_free_slab(shmem_slab_head *sblock, shmem_slab *entry)
{
int index = sblock->slab_index;
Assert(!entry->chain.next && !entry->chain.prev);
Assert(*((uint32 *)((char *)entry->data +
INTALIGN(slab_sizes[index]))) == SHMEM_SLAB_MAGIC);
SpinLockAcquire(&pgstrom_shmem_head->slab_locks[index]);
dlist_push_head(&pgstrom_shmem_head->slab_freelist[index],
&entry->chain);
SpinLockRelease(&pgstrom_shmem_head->slab_locks[index]);
}
void
pgstrom_shmem_free(void *address)
{
shmem_zone *zone;
shmem_body *body;
void *zone_baseaddr;
Size zone_length ;
int zone_index;
Size offset;
offset = ((uintptr_t)address & (SHMEM_BLOCKSZ - 1));
if (offset != offsetof(shmem_body, data[0]))
{
shmem_slab_head *sblock = (shmem_slab_head *)
((char *)address - offset + offsetof(shmem_body, data));
shmem_slab *entry = (shmem_slab *)
((char *)address - offsetof(shmem_slab, data[0]));
pgstrom_free_slab(sblock, entry);
return;
}
/* elsewhere, the supplied address is bare blocks */
zone_baseaddr = pgstrom_shmem_head->zone_baseaddr;
zone_length = pgstrom_shmem_head->zone_length;
Assert(pgstrom_shmem_sanitycheck(address));
body = (shmem_body *)((char *)address -
offsetof(shmem_body, data[0]));
zone_index = ((uintptr_t)body - (uintptr_t)zone_baseaddr) / zone_length;
Assert(zone_index >= 0 && zone_index < pgstrom_shmem_head->num_zones);
zone = pgstrom_shmem_head->zones[zone_index];
SpinLockAcquire(&zone->lock);
pgstrom_shmem_zone_block_free(zone, body);
SpinLockRelease(&zone->lock);
}
/*
* pgstrom_shmem_realloc
*
* It allocate a shared memory block, and copy the contents in the supplied
* oldaddr to the new one, then release shared memory block.
*/
void *
__pgstrom_shmem_realloc(const char *filename, int lineno,
void *oldaddr, Size newsize)
{
void *newaddr;
newaddr = __pgstrom_shmem_alloc(filename, lineno, newsize);
if (!newaddr)
return NULL;
if (oldaddr)
{
Size oldsize = pgstrom_shmem_getsize(oldaddr);
memcpy(newaddr, oldaddr, Min(newsize, oldsize));
pgstrom_shmem_free(oldaddr);
}
return newaddr;
}
/*
* pgstrom_shmem_getsize
*
* It returns size of the supplied active block
*/
Size
pgstrom_shmem_getsize(void *address)
{
shmem_zone *zone;
shmem_body *body;
shmem_block *block;
void *zone_baseaddr = pgstrom_shmem_head->zone_baseaddr;
Size zone_length = pgstrom_shmem_head->zone_length;
Size blocksz;
long index;
/* find a zone on which address belongs to */
Assert(pgstrom_shmem_sanitycheck(address));
body = (shmem_body *)((char *)address -
offsetof(shmem_body, data[0]));
index = ((uintptr_t)body -
(uintptr_t)zone_baseaddr) / zone_length;
Assert(index >= 0 && index < pgstrom_shmem_head->num_zones);
zone = pgstrom_shmem_head->zones[index];
/* find shmem_block and get its status */
SpinLockAcquire(&zone->lock);
index = ((uintptr_t)body -
(uintptr_t)zone->block_baseaddr) / SHMEM_BLOCKSZ;
block = &zone->blocks[index];
Assert(BLOCK_IS_ACTIVE(block));
blocksz = block->blocksz;
pgstrom_shmem_zone_block_free(zone, body);
SpinLockRelease(&zone->lock);
return blocksz;
}
/*
* pgstrom_shmem_zone_length
*
* it returns the configured zone length
*/
Size
pgstrom_shmem_zone_length(void)
{
return pgstrom_shmem_head->zone_length;
}
/*
* pgstrom_shmem_maxalloc
*
* it returns the length of maximum allocatable length
*/
Size
pgstrom_shmem_maxalloc(void)
{
static Size maxalloc_length = 0;
if (!maxalloc_length)
{
Size zone_length = pgstrom_shmem_head->zone_length;
int nbits;
zone_length = Min(zone_length, (1UL << SHMEM_BLOCKSZ_BITS_MAX));
nbits = get_next_log2(zone_length + 1);
maxalloc_length = ((1UL << (nbits - 1)) - /* half of zone */
offsetof(shmem_body, data[0]) -
sizeof(cl_uint));
}
return maxalloc_length;
}
/*
* pgstrom_init_slab
*
* init slab management structure
*/
static void
pgstrom_init_slab(void)
{
int i;
for (i=0; i < lengthof(slab_sizes); i++)
{
SpinLockInit(&pgstrom_shmem_head->slab_locks[i]);
dlist_init(&pgstrom_shmem_head->slab_freelist[i]);
dlist_init(&pgstrom_shmem_head->slab_blocklist[i]);
}
}
/*
* pgstrom_shmem_slab_info
*
* shows list of slabs being allocated
*/
typedef struct
{
void *address;
const char *filename;
int lineno;
int index;
uint32 owner;
bool active;
bool broken;
} shmem_slab_info;
static void
collect_shmem_slab_info(List **p_results,
slock_t *slab_lock,
dlist_head *slab_blocklist,
size_t slab_size)
{
SpinLockAcquire(slab_lock);
PG_TRY();
{
dlist_iter iter;
Size unitsz = MAXALIGN(offsetof(shmem_slab, data[0]) +
INTALIGN(slab_size) +
sizeof(uint32));
dlist_foreach (iter, slab_blocklist)
{
shmem_slab_head *sblock;
shmem_slab *entry;
uint32 *magic;
int count;
sblock = dlist_container(shmem_slab_head, chain, iter.cur);
for (count=0; count < sblock->slab_nums; count++)
{
shmem_slab_info *slinfo = palloc0(sizeof(shmem_slab_info));
entry = (shmem_slab *)((char *)&sblock->entry +
unitsz * count);
magic = (uint32 *)((char *)entry->data + INTALIGN(slab_size));
slinfo->address = entry->data;
slinfo->filename = entry->filename;
slinfo->lineno = entry->lineno;
slinfo->index = sblock->slab_index;
slinfo->owner = entry->owner;
slinfo->active = (!entry->chain.prev && !entry->chain.next);
if (*magic != SHMEM_SLAB_MAGIC ||
(!entry->chain.prev && entry->chain.next) ||
(entry->chain.prev && !entry->chain.next))
slinfo->broken = true;
else
slinfo->broken = false;
*p_results = lappend(*p_results, slinfo);
}
}
}
PG_CATCH();
{
SpinLockRelease(slab_lock);
PG_RE_THROW();
}
PG_END_TRY();
SpinLockRelease(slab_lock);
}
Datum
pgstrom_shmem_slab_info(PG_FUNCTION_ARGS)
{
FuncCallContext *fncxt;
shmem_slab_info *slinfo;
HeapTuple tuple;
Datum values[6];
bool isnull[6];
char buf[256];
if (SRF_IS_FIRSTCALL())
{
TupleDesc tupdesc;
MemoryContext oldcxt;
int i;
fncxt = SRF_FIRSTCALL_INIT();
oldcxt = MemoryContextSwitchTo(fncxt->multi_call_memory_ctx);
tupdesc = CreateTemplateTupleDesc(6, false);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "address",
INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "slabname",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "owner",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "location",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "active",
BOOLOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "broken",
BOOLOID, -1, 0);
fncxt->tuple_desc = BlessTupleDesc(tupdesc);
for (i=0; i < lengthof(slab_sizes); i++)
{
collect_shmem_slab_info((List **)&fncxt->user_fctx,
&pgstrom_shmem_head->slab_locks[i],
&pgstrom_shmem_head->slab_blocklist[i],
slab_sizes[i]);
}
MemoryContextSwitchTo(oldcxt);
}
fncxt = SRF_PERCALL_SETUP();
if (fncxt->user_fctx == NIL)
SRF_RETURN_DONE(fncxt);
slinfo = linitial((List *) fncxt->user_fctx);
fncxt->user_fctx = list_delete_first((List *)fncxt->user_fctx);
memset(isnull, 0, sizeof(isnull));
values[0] = Int64GetDatum((uint64)slinfo->address);
snprintf(buf, sizeof(buf), "slab-%zu", slab_sizes[slinfo->index]);
values[1] = CStringGetTextDatum(buf);
if (slinfo->active)
{
values[2] = Int32GetDatum((uint32)slinfo->owner);
snprintf(buf, sizeof(buf), "%s:%d", slinfo->filename, slinfo->lineno);
values[3] = CStringGetTextDatum(buf);
}
else
{
isnull[2] = true;
isnull[3] = true;
}
values[4] = BoolGetDatum(slinfo->active);
values[5] = BoolGetDatum(slinfo->broken);
tuple = heap_form_tuple(fncxt->tuple_desc, values, isnull);
SRF_RETURN_NEXT(fncxt, HeapTupleGetDatum(tuple));
}
PG_FUNCTION_INFO_V1(pgstrom_shmem_slab_info);
/*
* pgstrom_shmem_sanitycheck
*
* it checks whether magic number of the supplied shared-memory block is
* still valid, or not. If someone overuses the block, magic number should
* be broken and we can detect it.
*/
bool
pgstrom_shmem_sanitycheck(const void *address)
{
shmem_zone *zone;
shmem_block *block;
shmem_body *body;
void *zone_baseaddr = pgstrom_shmem_head->zone_baseaddr;
Size zone_length = pgstrom_shmem_head->zone_length;
int zone_index;
int block_index;
cl_uint *p_magic;
body = (shmem_body *)((char *)address -
offsetof(shmem_body, data[0]));
Assert((uintptr_t)body % SHMEM_BLOCKSZ == 0);
Assert(body->magic == SHMEM_BODY_MAGIC);
zone_index = ((uintptr_t)body - (uintptr_t)zone_baseaddr) / zone_length;
Assert(zone_index >= 0 && zone_index < pgstrom_shmem_head->num_zones);
zone = pgstrom_shmem_head->zones[zone_index];
Assert(ADDRESS_IN_SHMEM_ZONE(zone, body));
block_index = ((uintptr_t)body -
(uintptr_t)zone->block_baseaddr) / SHMEM_BLOCKSZ;
block = &zone->blocks[block_index];
Assert(BLOCK_IS_ACTIVE(block));
p_magic = (cl_uint *)((char *)address + block->blocksz);
return (*p_magic == SHMEM_BLOCK_MAGIC ? true : false);
}
/*
* pgstrom_shmem_dump
*
* it logs current layout of shared memory segment
*/
#define DUMP(fmt,...) \
do { \
if (pgstrom_i_am_clserv) \
clserv_log(fmt,__VA_ARGS__); \
else \
elog(INFO,fmt,__VA_ARGS__); \
} while(0)
static void
pgstrom_shmem_dump_zone(shmem_zone *zone, int zone_index)
{
long i = 0;
while (i < zone->num_blocks)
{
shmem_block *block = &zone->blocks[i];
if (BLOCK_IS_ACTIVE(block))
{
shmem_body *body;
cl_uint *p_magic;
int nshift;
nshift = find_least_pot(offsetof(shmem_body, data[0]) +
block->blocksz + sizeof(cl_uint));
body = (shmem_body *)((char *)zone->block_baseaddr +
i * SHMEM_BLOCKSZ);
p_magic = (cl_uint *)((char *)body->data + block->blocksz);
DUMP("[%d:% 6ld] %p (size=%zu, owner=%u, %s:%d%s%s)",
zone_index, i, body,
block->blocksz,
body->owner,
body->filename,
body->lineno,
body->magic != SHMEM_BODY_MAGIC ? ", broken" : "",
*p_magic != SHMEM_BLOCK_MAGIC ? ", overrun" : "");
i += (1 << nshift);
}
else if (BLOCK_IS_FREE(block))
{
int nshift = find_least_pot(block->blocksz);
Assert(nshift <= SHMEM_BLOCKSZ_BITS_RANGE);
i += (1 << nshift);
}
else
{
DUMP("[%d:% 6ld] %p corrupted; neither active nor free",
zone_index, i,
(char *)zone->block_baseaddr + i * SHMEM_BLOCKSZ);
break;
}
}
}
#undef DUMP
void
pgstrom_shmem_dump(void)
{
int i;
for (i=0; i < pgstrom_shmem_head->num_zones; i++)
{
shmem_zone *zone = pgstrom_shmem_head->zones[i];
SpinLockAcquire(&zone->lock);
pgstrom_shmem_dump_zone(zone, i);
SpinLockRelease(&zone->lock);
}
}
/*
* collect_shmem_info
*
* It collects statistical information of shared memory zone.
* Note that it does not trust statistical values if debug build, thus
* it may take longer time because of walking of shared memory zone.
*/
typedef struct
{
int zone;
int shift;
int num_active;
int num_free;
} shmem_block_info;
static List *
collect_shmem_block_info(shmem_zone *zone, int zone_index)
{
List *results = NIL;
long num_active[SHMEM_BLOCKSZ_BITS_RANGE + 1];
long num_free[SHMEM_BLOCKSZ_BITS_RANGE + 1];
long i;
/*
* For debugging, we don't trust statistical information. Even though
* it takes block counting under the execlusive lock, we try to pick
* up raw data.
* Elsewhere, we just pick up statistical data.
*/
#ifdef PGSTROM_DEBUG
memset(num_active, 0, sizeof(num_active));
memset(num_free, 0, sizeof(num_free));
i = 0;
while (i < zone->num_blocks)
{
shmem_block *block = &zone->blocks[i];
if (BLOCK_IS_ACTIVE(block))
{
int nshift = find_least_pot(offsetof(shmem_body, data[0]) +
block->blocksz +
sizeof(cl_uint));
Assert(nshift <= SHMEM_BLOCKSZ_BITS_RANGE);
num_active[nshift]++;
i += (1 << nshift);
}
else if (BLOCK_IS_FREE(block))
{
int nshift = find_least_pot(block->blocksz);
Assert(nshift <= SHMEM_BLOCKSZ_BITS_RANGE);
num_free[nshift]++;
i += (1 << nshift);
}
else
elog(ERROR, "block %ld is neither active nor free", i);