]> pd.if.org Git - nbds/blob - runtime/mem.c
fix potential blocking behavior in ht,c
[nbds] / runtime / mem.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  *
5  * Extreamly fast multi-threaded malloc. 64 bit platforms only!
6  */
7 #include <sys/mman.h>
8 #include <stdio.h>
9 #include <errno.h>
10 #include "common.h"
11 #include "runtime_local.h"
12 #include "lwt.h"
13
14 #define GET_SCALE(n) (sizeof(n)*8-__builtin_clzl((n)-1)) // log2 of <n>, rounded up
15 #define MAX_SCALE 31 // allocate blocks up to 4GB in size (arbitrary, could be bigger)
16 #define REGION_SCALE 22 // 4MB regions
17 #define REGION_SIZE (1 << REGION_SCALE)
18 #define HEADER_REGION_SCALE 22 // 4MB is space enough for headers for over 2,000,000 regions
19
20 typedef struct block {
21     struct block *next;
22 } block_t;
23
24 // region header
25 typedef struct header {
26     uint8_t owner; // thread id of owner
27     uint8_t scale; // log2 of the block size
28 } header_t;
29
30 typedef struct private_list {
31     block_t *head;
32     uint32_t next_pub;
33     uint32_t count;
34 } private_list_t;
35
36 static header_t *region_header_ = NULL;
37
38 static block_t *pub_free_list_[MAX_NUM_THREADS][MAX_SCALE+1][MAX_NUM_THREADS] = {};
39 static private_list_t pri_free_list_[MAX_NUM_THREADS][MAX_SCALE+1] = {};
40
41 static void *get_new_region (int scale) {
42     if (scale < REGION_SCALE) {
43         scale = REGION_SCALE;
44     }
45     TRACE("m0", "get_new_region(): mmap new region scale: %llu", scale, 0);
46     void *region = mmap(NULL, (1 << scale), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
47     if (region == (void *)-1) {
48         perror("get_new_region: mmap");
49         exit(-1);
50     }
51     assert(region);
52     return region;
53 }
54
55 void mem_init (void) {
56     assert(region_header_ == NULL);
57     region_header_ = (header_t *)get_new_region(HEADER_REGION_SCALE);
58     memset(region_header_, 0, REGION_SIZE);
59 }
60
61 // Put <x> onto its owner's public free list (in the appropriate size bin).
62 //
63 // TODO: maybe we want to munmap() larger size blocks to reclaim virtual address space?
64 void nbd_free (void *x) {
65     LOCALIZE_THREAD_LOCAL(tid_, int);
66     block_t  *b = (block_t *)x;
67     assert(((size_t)b >> REGION_SCALE) < ((1 << HEADER_REGION_SCALE) / sizeof(header_t)));
68     header_t *h = region_header_ + ((size_t)b >> REGION_SCALE);
69     TRACE("m0", "nbd_free(): block %p scale %llu", b, h->scale);
70     if (h->owner == tid_) {
71         TRACE("m0", "nbd_free(): private block, free list head %p", 
72                     h->owner, pri_free_list_[tid_][h->scale].head);
73         b->next = pri_free_list_[tid_][h->scale].head;
74         pri_free_list_[tid_][h->scale].head = b;
75     } else {
76         TRACE("m0", "nbd_free(): owner %llu free list head %p", 
77                     h->owner, pub_free_list_[h->owner][h->scale][tid_]);
78         b->next = pub_free_list_[h->owner][h->scale][tid_];
79         pub_free_list_[h->owner][h->scale][tid_] = b;
80     }
81 }
82
83 // Allocate a block of memory at least size <n>. Blocks are binned in powers-of-two. Round up
84 // <n> to the nearest power-of-two. 
85 //
86 // First check the current thread's private free list for an available block. If no blocks are on
87 // the private free list, pull blocks off of the current thread's public free lists and put them
88 // on the private free list. If we didn't find any blocks on the public free lists, allocate a new
89 // region, break it up into blocks and put them on the private free list.
90 void *nbd_malloc (size_t n) {
91     LOCALIZE_THREAD_LOCAL(tid_, int);
92     if (n < sizeof(block_t)) {
93         n = sizeof(block_t);
94     }
95     int b_scale = GET_SCALE(n);
96     assert(b_scale <= MAX_SCALE);
97     TRACE("m0", "nbd_malloc(): size %llu scale %llu", n, b_scale);
98     private_list_t *pri = &pri_free_list_[tid_][b_scale]; // our private free list
99     TRACE("m0", "nbd_malloc(): private free list %p first block %p", pri->list, pri->head);
100
101     // If our private free list is empty, try to find blocks on our public free list. If that fails,
102     // allocate a new region.
103     if (EXPECT_FALSE(pri->head == NULL)) {
104         block_t **pubs = pub_free_list_[tid_][b_scale]; // our public free lists
105         while (1) {
106             // look for blocks on our public free lists round robin
107             pri->next_pub = (pri->next_pub+1) & (MAX_NUM_THREADS-1);
108
109             TRACE("m0", "nbd_malloc(): searching public free list %llu", pri->next_pub, 0);
110             if (pri->next_pub == tid_) {
111                 uint32_t count = pri->count;
112                 pri->count = 0;
113                 // If our private list is empty and we haven't gotten at least half a region's worth 
114                 // of block's from our public lists, we break open a new region. This guarentees 
115                 // that we are amortizing the cost of accessing our public lists accross enough 
116                 // nbd_malloc() calls.
117                 uint32_t min_count = b_scale > REGION_SCALE ? 1 << (b_scale-REGION_SCALE-1) : 1;
118                 if (count < min_count) {
119                     char  *region = get_new_region(b_scale);
120                     size_t b_size = 1 << b_scale;
121                     size_t region_size = (b_size < REGION_SIZE) ? REGION_SIZE : b_size;
122                     for (int i = region_size; i != 0; i -= b_size) {
123                         block_t *b = (block_t *)(region + i - b_size);
124                         b->next = pri->head;
125                         pri->head = b;
126                     }
127                     break;
128                 }
129                 continue;
130             }
131
132             if (pubs[pri->next_pub] != NULL) {
133                 block_t *stolen = SYNC_SWAP(&pubs[pri->next_pub], NULL);
134                 TRACE("m0", "nbd_malloc(): stole list %p first block %p", stolen);
135                 if (stolen == NULL)
136                     continue;
137                 pri->head = stolen;
138                 break;
139             }
140         }
141         assert(pri->head);
142     }
143
144     // Pull a block off of our private free list.
145     block_t *b = pri->head;
146     TRACE("m0", "nbd_malloc(): take block %p off of of private list (new head is %p)", b, pri->next);
147     assert(b);
148     pri->head = b->next;
149     pri->count++;
150     return b;
151 }