-
Notifications
You must be signed in to change notification settings - Fork 33
/
wd_mempool.c
1013 lines (841 loc) · 21.7 KB
/
wd_mempool.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-License-Identifier: Apache-2.0 */
/*
* Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved.
* Copyright 2020-2021 Linaro ltd.
*/
#include <dirent.h>
#include <errno.h>
#include <numa.h>
#include <numaif.h>
#include <stdbool.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <pthread.h>
#include "wd.h"
#define SYSFS_NODE_PATH "/sys/devices/system/node/node"
#define MAX_HP_STR_SIZE 64
#define HUGETLB_FLAG_ENCODE_SHIFT 26
#define BITS_PER_LONG ((unsigned int)sizeof(unsigned long) * 8)
#define BITS_TO_LONGS(bits) \
(((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
#define BIT_MASK(nr) ((unsigned long)(1) << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
#define BITMAP_FIRST_WORD_MASK(start) \
(~0UL << ((start) & (BITS_PER_LONG - 1)))
#define wd_round_mask(x, y) ((__typeof__(x))((y)-1))
#define round_down(x, y) ((x) & ~wd_round_mask(x, y))
#define WD_MEMPOOL_BLOCK_SIZE ((unsigned long)1 << 12)
#define WD_MEMPOOL_SIZE_MASK (WD_MEMPOOL_BLOCK_SIZE - 1)
#define WD_HUNDRED 100
#define PAGE_SIZE_OFFSET 10
struct wd_ref {
__u32 ref;
};
/*
* wd_atomic_test_add - add unless the number is already a given value
* @ref: pointer of type struct wd_ref
* @a: the amount to add to ref->ref...
* @u: ...unless ref->ref is equal to u.
*
* Return number of ref->ref if successful; On error, u is returned.
*/
static inline int wd_atomic_test_add(struct wd_ref *ref, int a, int u)
{
int c;
do {
c = __atomic_load_n(&ref->ref, __ATOMIC_RELAXED);
if (c == u)
break;
} while (!__atomic_compare_exchange_n(&ref->ref, (__u32 *)&c, c + a, true,
__ATOMIC_RELAXED, __ATOMIC_RELAXED));
return c;
}
static inline void wd_atomic_add(struct wd_ref *ref, int a)
{
__atomic_add_fetch(&ref->ref, a, __ATOMIC_RELAXED);
}
static inline void wd_atomic_sub(struct wd_ref *ref, int a)
{
__atomic_sub_fetch(&ref->ref, a, __ATOMIC_RELAXED);
}
static inline int wd_atomic_load(struct wd_ref *ref)
{
return __atomic_load_n(&ref->ref, __ATOMIC_RELAXED);
}
/*
* one memzone may include some continuous block in mempool
* @addr: Base address of blocks in this memzone
* @blk_num: Number of blocks in this memzone
* @begin: Begin position in mempool bitmap
* @end: End position in mempool bitmap
*/
struct memzone {
void *addr;
size_t blk_num;
size_t begin;
size_t end;
TAILQ_ENTRY(memzone) node;
};
TAILQ_HEAD(memzone_list, memzone);
/*
* @blk_elem: All the block unit addrs saved in blk_elem
* @depth: The block pool deph, stack depth
* @top: The stack top pos for blk_elem
* @blk_size: The size of one block
* @mp: Record from which mempool
* @mz_list: List of memzone allocated from mempool
* @free_block_num: Number of free blocks currently
* @lock: lock of blkpool
* @ref: ref of blkpool
*/
struct blkpool {
void **blk_elem;
size_t depth;
size_t top;
size_t blk_size;
struct mempool *mp;
struct memzone_list mz_list;
unsigned long free_block_num;
pthread_spinlock_t lock;
struct wd_ref ref;
};
struct sys_hugepage_config {
/* unit is Byte */
unsigned long page_size;
size_t total_num;
size_t free_num;
TAILQ_ENTRY(sys_hugepage_config) node;
};
TAILQ_HEAD(sys_hugepage_list, sys_hugepage_config);
struct bitmap {
unsigned long *map;
unsigned long bits;
unsigned long map_byte;
};
struct mempool {
enum wd_page_type page_type;
unsigned long page_size;
unsigned int page_num;
unsigned long blk_size;
unsigned int blk_num;
/* numa node id */
int node;
/* fd for page pin */
int fd;
int mp_ref;
void *addr;
size_t size;
size_t real_size;
struct bitmap *bitmap;
pthread_spinlock_t lock;
struct wd_ref ref;
struct sys_hugepage_list hp_list;
unsigned long free_blk_num;
};
/*
* This function is copied from kernel head file. It finds first bit in word.
* @word: The word to search
*
* Undefined if no bit exists, so code should check against 0 first.
*/
static __always_inline unsigned long wd_ffs(unsigned long target_word)
{
unsigned long word = target_word;
int num = 0;
if ((word & 0xffffffff) == 0) {
num += 32;
word >>= 32;
}
if ((word & 0xffff) == 0) {
num += 16;
word >>= 16;
}
if ((word & 0xff) == 0) {
num += 8;
word >>= 8;
}
if ((word & 0xf) == 0) {
num += 4;
word >>= 4;
}
if ((word & 0x3) == 0) {
num += 2;
word >>= 2;
}
if ((word & 0x1) == 0)
num += 1;
return num;
}
static struct bitmap *create_bitmap(int bits)
{
struct bitmap *bm;
bm = calloc(1, sizeof(*bm));
if (!bm) {
WD_ERR("failed to alloc memory for bitmap!\n");
return NULL;
}
bm->map = calloc(BITS_TO_LONGS(bits), sizeof(unsigned long));
if (!bm->map) {
WD_ERR("failed to alloc memory for bitmap map!\n");
free(bm);
return NULL;
}
bm->bits = bits;
bm->map_byte = BITS_TO_LONGS(bits);
return bm;
}
static void destroy_bitmap(struct bitmap *bm)
{
if (bm) {
free(bm->map);
free(bm);
}
}
static unsigned long _find_next_bit(unsigned long *map, unsigned long bits,
unsigned long begin_position,
unsigned long invert)
{
unsigned long tmp, mask, next_bit;
unsigned long start = begin_position;
if (start >= bits)
return bits;
tmp = map[start / BITS_PER_LONG];
tmp ^= invert;
mask = BITMAP_FIRST_WORD_MASK(start);
tmp &= mask;
start = round_down(start, BITS_PER_LONG);
while (!tmp) {
start += BITS_PER_LONG;
if (start > bits)
return bits;
tmp = map[start / BITS_PER_LONG];
tmp ^= invert;
}
next_bit = start + wd_ffs(tmp);
return MIN(next_bit, bits);
}
static unsigned long find_next_zero_bit(struct bitmap *bm, unsigned long start)
{
return _find_next_bit(bm->map, bm->bits, start, ~0UL);
}
static void set_bit(struct bitmap *bm, unsigned int pos)
{
unsigned long *map = bm->map;
unsigned long mask = BIT_MASK(pos);
unsigned long *p;
p = (void *)((uintptr_t)map + BIT_WORD(pos) * sizeof(unsigned long));
*p |= mask;
}
static void clear_bit(struct bitmap *bm, unsigned int pos)
{
unsigned long *map = bm->map;
unsigned long mask = BIT_MASK(pos);
unsigned long *p;
p = (void *)((uintptr_t)map + BIT_WORD(pos) * sizeof(unsigned long));
*p &= ~mask;
}
static int test_bit(struct bitmap *bm, unsigned int nr)
{
unsigned long *p = (void *)((uintptr_t)bm->map + BIT_WORD(nr) * sizeof(unsigned long));
unsigned long mask = BIT_MASK(nr);
return !(*p & mask);
}
void *wd_block_alloc(handle_t blkpool)
{
struct blkpool *bp = (struct blkpool*)blkpool;
void *p;
if (!bp) {
WD_ERR("invalid: block pool is NULL!\n");
return NULL;
}
if (!wd_atomic_test_add(&bp->ref, 1, 0)) {
WD_ERR("failed to alloc block, block pool is busy now!\n");
return NULL;
}
pthread_spin_lock(&bp->lock);
if (bp->top > 0) {
bp->top--;
bp->free_block_num--;
p = bp->blk_elem[bp->top];
pthread_spin_unlock(&bp->lock);
return p;
}
pthread_spin_unlock(&bp->lock);
wd_atomic_sub(&bp->ref, 1);
return NULL;
}
void wd_block_free(handle_t blkpool, void *addr)
{
struct blkpool *bp = (struct blkpool*)blkpool;
if (!bp || !addr)
return;
pthread_spin_lock(&bp->lock);
if (bp->top < bp->depth) {
bp->blk_elem[bp->top] = addr;
bp->top++;
bp->free_block_num++;
pthread_spin_unlock(&bp->lock);
wd_atomic_sub(&bp->ref, 1);
return;
}
pthread_spin_unlock(&bp->lock);
}
static int alloc_memzone(struct blkpool *bp, void *addr, size_t blk_num,
size_t begin, size_t end)
{
struct memzone *zone;
zone = calloc(1, sizeof(struct memzone));
if (!zone) {
WD_ERR("failed to alloc memory for memzone!\n");
return -WD_ENOMEM;
}
zone->addr = addr;
zone->blk_num = blk_num;
zone->begin = begin;
zone->end = end;
TAILQ_INSERT_TAIL(&bp->mz_list, zone, node);
return 0;
}
static void free_mem_to_mempool_nolock(struct blkpool *bp)
{
struct mempool *mp = bp->mp;
struct memzone *iter;
size_t blks;
__u32 i;
while ((iter = TAILQ_LAST(&bp->mz_list, memzone_list))) {
for (i = iter->begin; i <= iter->end; i++)
clear_bit(mp->bitmap, i);
blks = iter->end - iter->begin + 1;
mp->free_blk_num += blks;
mp->real_size += blks * mp->blk_size;
TAILQ_REMOVE(&bp->mz_list, iter, node);
free(iter);
}
}
static void free_mem_to_mempool(struct blkpool *bp)
{
struct mempool *mp = bp->mp;
pthread_spin_lock(&mp->lock);
free_mem_to_mempool_nolock(bp);
pthread_spin_unlock(&mp->lock);
}
static int check_mempool_real_size(struct mempool *mp, struct blkpool *bp)
{
if (bp->blk_size * bp->depth > mp->real_size) {
WD_ERR("invalid: mempool size is too small: %lu!\n",
mp->real_size);
return -WD_ENOMEM;
}
return 0;
}
static int alloc_block_from_mempool(struct mempool *mp,
struct blkpool *bp,
int pos,
int mem_combined_num,
int mem_splited_num)
{
int pos_last = pos;
int pos_first;
int i, ret;
do {
pos_first = find_next_zero_bit(mp->bitmap, pos_last);
if ((__u32)pos_first == mp->bitmap->bits) {
WD_ERR("failed to find free block from mempool!\n");
return -WD_ENOMEM;
}
pos_last = pos_first;
for (i = 0; i < mem_combined_num - 1; i++)
if (!test_bit(mp->bitmap, ++pos_last))
break;
} while (i != mem_combined_num - 1);
for (i = pos_last; i >= pos_first; i--)
set_bit(mp->bitmap, i);
ret = alloc_memzone(bp, mp->addr + pos_first * mp->blk_size,
mem_splited_num, pos_first, pos_last);
if (ret < 0)
goto err_clear_bit;
return pos_last;
err_clear_bit:
for (i = pos_last; i >= pos_first; i--)
clear_bit(mp->bitmap, i);
return -WD_ENOMEM;
}
/* In this case, multiple blocks are in one mem block */
static int alloc_mem_multi_in_one(struct mempool *mp, struct blkpool *bp)
{
int mem_splited_num = mp->blk_size / bp->blk_size;
int blk_num = bp->depth;
int ret = -WD_ENOMEM;
int pos = 0;
pthread_spin_lock(&mp->lock);
if (check_mempool_real_size(mp, bp))
goto err_check_size;
while (blk_num > 0) {
ret = alloc_block_from_mempool(mp, bp, pos, 1,
MIN(blk_num, mem_splited_num));
if (ret < 0)
goto err_free_memzone;
mp->free_blk_num--;
mp->real_size -= mp->blk_size;
blk_num -= mem_splited_num;
pos = ret;
}
pthread_spin_unlock(&mp->lock);
return 0;
err_free_memzone:
free_mem_to_mempool_nolock(bp);
err_check_size:
pthread_spin_unlock(&mp->lock);
return ret;
}
/*
* In this case, multiple continuous mem blocks should be allocated for one
* block in blkpool
*/
static int alloc_mem_one_need_multi(struct mempool *mp, struct blkpool *bp)
{
int mem_combined_num = bp->blk_size / mp->blk_size +
(bp->blk_size % mp->blk_size ? 1 : 0);
int blk_num = bp->depth;
int ret = -WD_ENOMEM;
int pos = 0;
pthread_spin_lock(&mp->lock);
if (check_mempool_real_size(mp, bp))
goto err_check_size;
while (blk_num > 0) {
ret = alloc_block_from_mempool(mp, bp, pos,
mem_combined_num, 1);
if (ret < 0)
goto err_free_memzone;
pos = ret;
blk_num--;
mp->free_blk_num -= mem_combined_num;
mp->real_size -= mp->blk_size * mem_combined_num;
}
pthread_spin_unlock(&mp->lock);
return 0;
err_free_memzone:
free_mem_to_mempool_nolock(bp);
err_check_size:
pthread_spin_unlock(&mp->lock);
return ret;
}
static int alloc_mem_from_mempool(struct mempool *mp, struct blkpool *bp)
{
TAILQ_INIT(&bp->mz_list);
if (mp->blk_size >= bp->blk_size)
return alloc_mem_multi_in_one(mp, bp);
return alloc_mem_one_need_multi(mp, bp);
}
static int init_blkpool_elem(struct blkpool *bp)
{
struct memzone *iter;
int idx = 0;
__u32 i;
bp->blk_elem = calloc(bp->depth, sizeof(void *));
if (!bp->blk_elem) {
WD_ERR("failed to alloc memory for blk_elem!\n");
return -WD_ENOMEM;
}
TAILQ_FOREACH(iter, &bp->mz_list, node)
for (i = 0; i < iter->blk_num; i++)
bp->blk_elem[idx++] = iter->addr + i * bp->blk_size;
return 0;
}
handle_t wd_blockpool_create(handle_t mempool, size_t block_size,
size_t block_num)
{
struct mempool *mp = (struct mempool*)mempool;
struct blkpool *bp;
int ret;
if (!mp || !block_size || !block_num) {
WD_ERR("invalid: mempool is NULL or block param is 0!\n");
return (handle_t)(-WD_EINVAL);
}
if (!wd_atomic_test_add(&mp->ref, 1, 0)) {
WD_ERR("failed to create blockpool, mempool is busy now!\n");
return (handle_t)(-WD_EBUSY);
}
bp = calloc(1, sizeof(struct blkpool));
if (!bp) {
WD_ERR("failed to alloc memory for blkpool!\n");
goto err_sub_ref;
}
bp->top = block_num;
bp->depth = block_num;
bp->blk_size = block_size;
bp->free_block_num = block_num;
bp->mp = mp;
ret = pthread_spin_init(&bp->lock, PTHREAD_PROCESS_PRIVATE);
if (ret < 0)
goto err_free_bp;
ret = alloc_mem_from_mempool(mp, bp);
if (ret < 0)
goto err_uninit_lock;
ret = init_blkpool_elem(bp);
if (ret < 0)
goto err_free_mem;
wd_atomic_add(&bp->ref, 1);
return (handle_t)bp;
err_free_mem:
free_mem_to_mempool(bp);
err_uninit_lock:
pthread_spin_destroy(&bp->lock);
err_free_bp:
free(bp);
err_sub_ref:
wd_atomic_sub(&mp->ref, 1);
return (handle_t)(-WD_ENOMEM);
}
void wd_blockpool_destroy(handle_t blkpool)
{
struct blkpool *bp = (struct blkpool *)blkpool;
struct mempool *mp;
if (!bp) {
WD_ERR("invalid: blkpool is NULL!\n");
return;
}
mp = bp->mp;
wd_atomic_sub(&bp->ref, 1);
while (wd_atomic_load(&bp->ref))
sched_yield();
free_mem_to_mempool(bp);
pthread_spin_destroy(&bp->lock);
free(bp->blk_elem);
free(bp);
wd_atomic_sub(&mp->ref, 1);
}
static int get_value_from_sysfs(const char *path, ssize_t path_size)
{
char buf[MAX_ATTR_STR_SIZE] = {'\0'};
char dev_path[PATH_MAX];
char *ptrRet = NULL;
ssize_t size;
int fd, ret;
ptrRet = realpath(path, dev_path);
if (!ptrRet) {
WD_ERR("failed to resolve path: %s!\n", path);
return -WD_ENODEV;
}
fd = open(dev_path, O_RDONLY, 0);
if (fd < 0) {
WD_ERR("failed to open %s!\n", dev_path);
goto err_open;
}
size = read(fd, buf, MAX_ATTR_STR_SIZE - 1);
if (size <= 0) {
WD_ERR("failed to read %s!\n", dev_path);
goto err_read;
}
ret = strtol(buf, NULL, 10);
if (errno == ERANGE) {
WD_ERR("failed to strtol %s, out of range!\n", buf);
goto err_read;
}
close(fd);
return ret;
err_read:
close(fd);
err_open:
return -errno;
}
/* hp_dir is e.g. /sys/devices/system/node/nodex/hugepages/hugepages-64kB */
static int get_hugepage_info_per_type(const char *hugepage_path, int path_size, struct dirent *hp_dir,
struct sys_hugepage_config *cfg)
{
char path[MAX_ATTR_STR_SIZE];
char *name = hp_dir->d_name;
unsigned long size;
char *size_pos;
int ret;
size_pos = index(name, '-');
if (!size_pos) {
WD_ERR("failed to resolve dir name: %s!\n", name);
return -WD_EINVAL;
}
size_pos++;
errno = 0;
size = strtol(size_pos, NULL, 10);
if (errno) {
WD_ERR("failed to resolve size pos to number: %s!\n", size_pos);
return -errno;
}
cfg->page_size = size << PAGE_SIZE_OFFSET;
ret = snprintf(path, sizeof(path), "%s/%s/nr_hugepages", hugepage_path,
name);
if (ret < 0) {
WD_ERR("failed to get nr hugepages path!\n");
return -WD_EINVAL;
}
ret = get_value_from_sysfs(path, MAX_ATTR_STR_SIZE);
if (ret < 0)
return ret;
cfg->total_num = ret;
ret = snprintf(path, sizeof(path), "%s/%s/free_hugepages",
hugepage_path, name);
if (ret < 0) {
WD_ERR("failed to get free hugepages path!\n");
return -WD_EINVAL;
}
ret = get_value_from_sysfs(path, MAX_ATTR_STR_SIZE);
if (ret < 0)
return ret;
cfg->free_num = ret;
return 0;
}
static void put_hugepage_info(struct mempool *mp)
{
struct sys_hugepage_config *tmp;
while ((tmp = TAILQ_LAST(&mp->hp_list, sys_hugepage_list))) {
TAILQ_REMOVE(&mp->hp_list, tmp, node);
free(tmp);
}
}
/* This function also sorts hugepage from small to big */
static int get_hugepage_info(struct mempool *mp)
{
struct sys_hugepage_config *tmp, *iter;
char hugepage_path[MAX_HP_STR_SIZE];
struct dirent *hp_dir;
DIR *dir;
int ret;
ret = snprintf(hugepage_path, sizeof(hugepage_path), "%s%d/hugepages",
SYSFS_NODE_PATH, mp->node);
if (ret < 0) {
WD_ERR("failed to get hugepages path!\n");
return -WD_EINVAL;
}
dir = opendir(hugepage_path);
if (!dir) {
WD_ERR("failed to open %s\n!", hugepage_path);
return -errno;
}
TAILQ_INIT(&mp->hp_list);
for (hp_dir = readdir(dir); hp_dir != NULL; hp_dir = readdir(dir)) {
if (!strncmp(hp_dir->d_name, ".", LINUX_CRTDIR_SIZE) ||
!strncmp(hp_dir->d_name, "..", LINUX_PRTDIR_SIZE))
continue;
tmp = calloc(1, sizeof(*tmp));
if (!tmp) {
WD_ERR("failed to calloc for sys_hugepage_config!\n");
goto err_free_list;
}
ret = get_hugepage_info_per_type(hugepage_path, MAX_HP_STR_SIZE,
hp_dir, tmp);
if (ret < 0)
goto err_free;
/* list: page size small -> big */
TAILQ_FOREACH(iter, &mp->hp_list, node) {
if (tmp->page_size < iter->page_size) {
TAILQ_INSERT_BEFORE(iter, tmp, node);
break;
}
}
if (!iter)
TAILQ_INSERT_TAIL(&mp->hp_list, tmp, node);
}
closedir(dir);
return 0;
err_free:
free(tmp);
err_free_list:
put_hugepage_info(mp);
closedir(dir);
return -WD_EIO;
}
static int mbind_memory(void *addr, size_t size, int node)
{
unsigned long max_node = numa_max_node() + 2;
unsigned long node_mask;
int ret = 0;
node_mask = 1UL << (unsigned int)node;
ret = mbind(addr, size, MPOL_BIND, &node_mask, max_node, 0);
if (ret < 0) {
WD_ERR("failed to mbind memory, ret is %d!\n", ret);
return ret;
}
return ret;
}
static int alloc_mem_from_hugepage(struct mempool *mp)
{
struct sys_hugepage_config *iter;
unsigned long bits = sizeof(iter->page_size) * 8;
size_t page_num, real_size;
unsigned long flags = 0;
void *p;
int ret;
ret = get_hugepage_info(mp);
if (ret < 0)
return ret;
/* find proper hugepage: use small huge page if possible */
TAILQ_FOREACH(iter, &mp->hp_list, node)
if (iter->page_size * iter->free_num >= mp->size)
break;
if (!iter) {
WD_ERR("failed to find proper hugepage!\n");
ret = -WD_ENOMEM;
goto err_put_info;
}
/* alloc hugepage and bind */
page_num = mp->size / iter->page_size +
(mp->size % iter->page_size ? 1 : 0);
real_size = page_num * iter->page_size;
/*
* man mmap will tell, flags of mmap can be used to indicate hugepage
* size. In fact, after kernel 3.18, it has been supported. See more
* in kernel header file: linux/include/uapi/linux/mman.h. As related
* macro has not been put into glibc, we caculate them here, e.g.
* flags for 64KB is 16 << 26.
*/
flags = _find_next_bit(&iter->page_size, bits, 0, 0UL) <<
HUGETLB_FLAG_ENCODE_SHIFT;
p = mmap(NULL, real_size, PROT_READ | PROT_WRITE, MAP_PRIVATE |
MAP_ANONYMOUS | MAP_HUGETLB | flags, -1, 0);
if (p == MAP_FAILED) {
WD_ERR("failed to allocate huge page!\n");
ret = -WD_ENOMEM;
goto err_put_info;
}
ret = mbind_memory(p, real_size, mp->node);
if (ret < 0)
goto err_unmap;
mp->page_type = WD_HUGE_PAGE;
mp->page_size = iter->page_size;
mp->page_num = page_num;
mp->addr = p;
mp->real_size = real_size;
return 0;
err_unmap:
munmap(p, real_size);
err_put_info:
put_hugepage_info(mp);
return ret;
}
static void free_hugepage_mem(struct mempool *mp)
{
munmap(mp->addr, mp->page_size * mp->page_num);
put_hugepage_info(mp);
}
static int init_mempool(struct mempool *mp)
{
/* size of mp should align to 4KB */
int bits = mp->size / mp->blk_size;
struct bitmap *bm;
bm = create_bitmap(bits);
if (!bm)
return -WD_ENOMEM;
mp->bitmap = bm;
mp->free_blk_num = bits;
mp->blk_num = bits;
return 0;
}
static void uninit_mempool(struct mempool *mp)
{
if (mp->bitmap) {
destroy_bitmap(mp->bitmap);
mp->bitmap = NULL;
}
}
handle_t wd_mempool_create(size_t size, int node)
{
struct mempool *mp;
size_t tmp = size;
int ret;
if (!tmp || node < 0 || node > numa_max_node()) {
WD_ERR("invalid: numa node is %d, size is %ld!\n", node, tmp);
return (handle_t)(-WD_EINVAL);
}
if (WD_MEMPOOL_SIZE_MASK & tmp)
tmp += WD_MEMPOOL_BLOCK_SIZE - (WD_MEMPOOL_SIZE_MASK & tmp);
mp = calloc(1, sizeof(*mp));
if (!mp) {
WD_ERR("failed to alloc memory for mempool!\n");
return (handle_t)(-WD_ENOMEM);
}
mp->node = node;
mp->size = tmp;
mp->blk_size = WD_MEMPOOL_BLOCK_SIZE;
ret = pthread_spin_init(&mp->lock, PTHREAD_PROCESS_PRIVATE);
if (ret < 0)
goto free_pool;
ret = alloc_mem_from_hugepage(mp);
if (ret < 0)
goto uninit_lock;
ret = init_mempool(mp);
if (ret < 0)
goto free_pool_memory;
wd_atomic_add(&mp->ref, 1);
return (handle_t)mp;
free_pool_memory:
free_hugepage_mem(mp);
uninit_lock:
pthread_spin_destroy(&mp->lock);
free_pool:
free(mp);
return (handle_t)(-WD_ENOMEM);
}
void wd_mempool_destroy(handle_t mempool)
{
struct mempool *mp = (struct mempool *)mempool;
if (!mp) {
WD_ERR("invalid: mempool is NULL!\n");
return;
}
wd_atomic_sub(&mp->ref, 1);
while(wd_atomic_load(&mp->ref));
uninit_mempool(mp);
free_hugepage_mem(mp);
pthread_spin_destroy(&mp->lock);
free(mp);
}
void wd_mempool_stats(handle_t mempool, struct wd_mempool_stats *stats)
{
struct mempool *mp = (struct mempool *)mempool;
if (!mp) {
WD_ERR("invalid: mempool is NULL!\n");
return;
}
if (!stats) {
WD_ERR("invalid: mempool stats is NULL!\n");
return;
}
pthread_spin_lock(&mp->lock);
stats->page_type = mp->page_type;
stats->page_size = mp->page_size;
stats->page_num = mp->page_num;
stats->blk_size = mp->blk_size;
stats->blk_num = mp->blk_num;
stats->free_blk_num = mp->free_blk_num;
stats->blk_usage_rate = (stats->blk_num - mp->free_blk_num) /
stats->blk_num * WD_HUNDRED;
pthread_spin_unlock(&mp->lock);
}
void wd_blockpool_stats(handle_t blkpool, struct wd_blockpool_stats *stats)
{
struct blkpool *bp = (struct blkpool*)blkpool;
unsigned long size = 0;
struct memzone *iter;
if (!bp || !stats) {
WD_ERR("invalid: blkpool or stats is NULL!\n");
return;
}
pthread_spin_lock(&bp->lock);
stats->block_size = bp->blk_size;
stats->block_num = bp->depth;
stats->free_block_num = bp->free_block_num;
stats->block_usage_rate = (bp->depth - bp->free_block_num) /
bp->depth * WD_HUNDRED;
TAILQ_FOREACH(iter, &bp->mz_list, node)