bba34dc9d089cf64bcdd601dffd53170654aa127
[dpdk.git] / lib / librte_malloc / malloc_heap.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34 #include <stdint.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_tailq.h>
45 #include <rte_eal.h>
46 #include <rte_eal_memconfig.h>
47 #include <rte_launch.h>
48 #include <rte_per_lcore.h>
49 #include <rte_lcore.h>
50 #include <rte_common.h>
51 #include <rte_string_fns.h>
52 #include <rte_spinlock.h>
53 #include <rte_memcpy.h>
54 #include <rte_atomic.h>
55
56 #include "malloc_elem.h"
57 #include "malloc_heap.h"
58
59 /* since the memzone size starts with a digit, it will appear unquoted in
60  * rte_config.h, so quote it so it can be passed to rte_str_to_size */
61 #define MALLOC_MEMZONE_SIZE RTE_STR(RTE_MALLOC_MEMZONE_SIZE)
62
63 /*
64  * returns the configuration setting for the memzone size as a size_t value
65  */
66 static inline size_t
67 get_malloc_memzone_size(void)
68 {
69         return rte_str_to_size(MALLOC_MEMZONE_SIZE);
70 }
71
72 /*
73  * reserve an extra memory zone and make it available for use by a particular
74  * heap. This reserves the zone and sets a dummy malloc_elem header at the end
75  * to prevent overflow. The rest of the zone is added to free list as a single
76  * large free block
77  */
78 static int
79 malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align)
80 {
81         const unsigned mz_flags = 0;
82         const size_t min_size = get_malloc_memzone_size();
83         /* ensure the data we want to allocate will fit in the memzone */
84         size_t mz_size = size + align + MALLOC_ELEM_OVERHEAD * 2;
85         if (mz_size < min_size)
86                 mz_size = min_size;
87
88         char mz_name[RTE_MEMZONE_NAMESIZE];
89         rte_snprintf(mz_name, sizeof(mz_name), "MALLOC_S%u_HEAP_%u",
90                         heap->numa_socket, heap->mz_count++);
91         const struct rte_memzone *mz = rte_memzone_reserve(mz_name, mz_size,
92                         heap->numa_socket, mz_flags);
93         if (mz == NULL)
94                 return -1;
95
96         /* allocate the memory block headers, one at end, one at start */
97         struct malloc_elem *start_elem = (struct malloc_elem *)mz->addr;
98         struct malloc_elem *end_elem = RTE_PTR_ADD(mz->addr,
99                         mz_size - MALLOC_ELEM_OVERHEAD);
100         end_elem = RTE_PTR_ALIGN_FLOOR(end_elem, CACHE_LINE_SIZE);
101
102         const unsigned elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem;
103         malloc_elem_init(start_elem, heap, elem_size);
104         malloc_elem_mkend(end_elem, start_elem);
105
106         start_elem->next_free = heap->free_head;
107         heap->free_head = start_elem;
108         /* increase heap total size by size of new memzone */
109         heap->total_size+=mz_size - MALLOC_ELEM_OVERHEAD;
110         return 0;
111 }
112
113 /*
114  * initialise a malloc heap object. The heap is locked with a private
115  * lock while being initialised. This function should only be called the
116  * first time a thread calls malloc - if even then, as heaps are per-socket
117  * not per-thread.
118  */
119 static void
120 malloc_heap_init(struct malloc_heap *heap)
121 {
122         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
123
124         rte_eal_mcfg_wait_complete(mcfg);
125         while (heap->initialised != INITIALISED) {
126                 if (rte_atomic32_cmpset(
127                                 (volatile uint32_t*)&heap->initialised,
128                                 NOT_INITIALISED, INITIALISING)) {
129
130                         heap->free_head = NULL;
131                         heap->mz_count = 0;
132                         heap->alloc_count = 0;
133                         heap->total_size = 0;
134                         /*
135                          * Find NUMA socket of heap that is being initialised, so that
136                          * malloc_heaps[n].numa_socket == n
137                          */
138                         heap->numa_socket = heap - mcfg->malloc_heaps;
139                         rte_spinlock_init(&heap->lock);
140                         heap->initialised = INITIALISED;
141                 }
142         }
143 }
144
145 /*
146  * Iterates through the freelist for a heap to find a free element
147  * which can store data of the required size and with the requested alignment.
148  * Returns null on failure, or pointer to element on success, with the pointer
149  * to the previous element in the list, if any, being returned in a parameter
150  * (to make removing the element from the free list faster).
151  */
152 static struct malloc_elem *
153 find_suitable_element(struct malloc_heap *heap, size_t size,
154                 unsigned align, struct malloc_elem **prev)
155 {
156         struct malloc_elem *elem = heap->free_head;
157         *prev = NULL;
158         while(elem){
159                 if (malloc_elem_can_hold(elem, size, align))
160                         break;
161                 *prev = elem;
162                 elem = elem->next_free;
163         }
164         return elem;
165 }
166
167 /*
168  * Main function called by malloc to allocate a block of memory from the
169  * heap. It locks the free list, scans it, and adds a new memzone if the
170  * scan fails. Once the new memzone is added, it re-scans and should return
171  * the new element after releasing the lock.
172  */
173 void *
174 malloc_heap_alloc(struct malloc_heap *heap,
175                 const char *type __attribute__((unused)), size_t size, unsigned align)
176 {
177         if (!heap->initialised)
178                 malloc_heap_init(heap);
179
180         size = CACHE_LINE_ROUNDUP(size);
181         align = CACHE_LINE_ROUNDUP(align);
182         rte_spinlock_lock(&heap->lock);
183         struct malloc_elem *prev, *elem = find_suitable_element(heap,
184                         size, align, &prev);
185         if (elem == NULL){
186                 if ((malloc_heap_add_memzone(heap, size, align)) == 0)
187                         elem = find_suitable_element(heap, size, align, &prev);
188         }
189
190         if (elem != NULL){
191                 elem = malloc_elem_alloc(elem, size, align, prev);
192                 /* increase heap's count of allocated elements */
193                 heap->alloc_count++;
194         }
195         rte_spinlock_unlock(&heap->lock);
196         return elem == NULL ? NULL : (void *)(&elem[1]);
197
198 }
199
200 /*
201  * Function to retrieve data for heap on given socket
202  */
203 int
204 malloc_heap_get_stats(struct malloc_heap *heap,
205                 struct rte_malloc_socket_stats *socket_stats)
206 {
207         if (!heap->initialised)
208                 return -1;
209
210         struct malloc_elem *elem = heap->free_head;
211
212         /* Initialise variables for heap */
213         socket_stats->free_count = 0;
214         socket_stats->heap_freesz_bytes = 0;
215         socket_stats->greatest_free_size = 0;
216
217         /* Iterate through free list */
218         while(elem) {
219                 socket_stats->free_count++;
220                 socket_stats->heap_freesz_bytes += elem->size;
221                 if (elem->size > socket_stats->greatest_free_size)
222                         socket_stats->greatest_free_size = elem->size;
223
224                 elem = elem->next_free;
225         }
226         /* Get stats on overall heap and allocated memory on this heap */
227         socket_stats->heap_totalsz_bytes = heap->total_size;
228         socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
229                         socket_stats->heap_freesz_bytes);
230         socket_stats->alloc_count = heap->alloc_count;
231         return 0;
232 }
233