tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_malloc / malloc_heap.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <stdint.h>
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <errno.h>
39 #include <sys/queue.h>
40
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_eal.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_launch.h>
46 #include <rte_per_lcore.h>
47 #include <rte_lcore.h>
48 #include <rte_common.h>
49 #include <rte_string_fns.h>
50 #include <rte_spinlock.h>
51 #include <rte_memcpy.h>
52 #include <rte_atomic.h>
53
54 #include "malloc_elem.h"
55 #include "malloc_heap.h"
56
57 /* since the memzone size starts with a digit, it will appear unquoted in
58  * rte_config.h, so quote it so it can be passed to rte_str_to_size */
59 #define MALLOC_MEMZONE_SIZE RTE_STR(RTE_MALLOC_MEMZONE_SIZE)
60
61 /*
62  * returns the configuration setting for the memzone size as a size_t value
63  */
64 static inline size_t
65 get_malloc_memzone_size(void)
66 {
67         return rte_str_to_size(MALLOC_MEMZONE_SIZE);
68 }
69
70 /*
71  * reserve an extra memory zone and make it available for use by a particular
72  * heap. This reserves the zone and sets a dummy malloc_elem header at the end
73  * to prevent overflow. The rest of the zone is added to free list as a single
74  * large free block
75  */
76 static int
77 malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align)
78 {
79         const unsigned mz_flags = 0;
80         const size_t block_size = get_malloc_memzone_size();
81         /* ensure the data we want to allocate will fit in the memzone */
82         const size_t min_size = size + align + MALLOC_ELEM_OVERHEAD * 2;
83         const struct rte_memzone *mz = NULL;
84         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
85         unsigned numa_socket = heap - mcfg->malloc_heaps;
86
87         size_t mz_size = min_size;
88         if (mz_size < block_size)
89                 mz_size = block_size;
90
91         char mz_name[RTE_MEMZONE_NAMESIZE];
92         snprintf(mz_name, sizeof(mz_name), "MALLOC_S%u_HEAP_%u",
93                      numa_socket, heap->mz_count++);
94
95         /* try getting a block. if we fail and we don't need as big a block
96          * as given in the config, we can shrink our request and try again
97          */
98         do {
99                 mz = rte_memzone_reserve(mz_name, mz_size, numa_socket,
100                                          mz_flags);
101                 if (mz == NULL)
102                         mz_size /= 2;
103         } while (mz == NULL && mz_size > min_size);
104         if (mz == NULL)
105                 return -1;
106
107         /* allocate the memory block headers, one at end, one at start */
108         struct malloc_elem *start_elem = (struct malloc_elem *)mz->addr;
109         struct malloc_elem *end_elem = RTE_PTR_ADD(mz->addr,
110                         mz_size - MALLOC_ELEM_OVERHEAD);
111         end_elem = RTE_PTR_ALIGN_FLOOR(end_elem, RTE_CACHE_LINE_SIZE);
112
113         const unsigned elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem;
114         malloc_elem_init(start_elem, heap, mz, elem_size);
115         malloc_elem_mkend(end_elem, start_elem);
116         malloc_elem_free_list_insert(start_elem);
117
118         /* increase heap total size by size of new memzone */
119         heap->total_size+=mz_size - MALLOC_ELEM_OVERHEAD;
120         return 0;
121 }
122
123 /*
124  * Iterates through the freelist for a heap to find a free element
125  * which can store data of the required size and with the requested alignment.
126  * Returns null on failure, or pointer to element on success.
127  */
128 static struct malloc_elem *
129 find_suitable_element(struct malloc_heap *heap, size_t size, unsigned align)
130 {
131         size_t idx;
132         struct malloc_elem *elem;
133
134         for (idx = malloc_elem_free_list_index(size);
135                 idx < RTE_HEAP_NUM_FREELISTS; idx++)
136         {
137                 for (elem = LIST_FIRST(&heap->free_head[idx]);
138                         !!elem; elem = LIST_NEXT(elem, free_list))
139                 {
140                         if (malloc_elem_can_hold(elem, size, align))
141                                 return elem;
142                 }
143         }
144         return NULL;
145 }
146
147 /*
148  * Main function called by malloc to allocate a block of memory from the
149  * heap. It locks the free list, scans it, and adds a new memzone if the
150  * scan fails. Once the new memzone is added, it re-scans and should return
151  * the new element after releasing the lock.
152  */
153 void *
154 malloc_heap_alloc(struct malloc_heap *heap,
155                 const char *type __attribute__((unused)), size_t size, unsigned align)
156 {
157         size = RTE_CACHE_LINE_ROUNDUP(size);
158         align = RTE_CACHE_LINE_ROUNDUP(align);
159         rte_spinlock_lock(&heap->lock);
160         struct malloc_elem *elem = find_suitable_element(heap, size, align);
161         if (elem == NULL){
162                 if ((malloc_heap_add_memzone(heap, size, align)) == 0)
163                         elem = find_suitable_element(heap, size, align);
164         }
165
166         if (elem != NULL){
167                 elem = malloc_elem_alloc(elem, size, align);
168                 /* increase heap's count of allocated elements */
169                 heap->alloc_count++;
170         }
171         rte_spinlock_unlock(&heap->lock);
172         return elem == NULL ? NULL : (void *)(&elem[1]);
173
174 }
175
176 /*
177  * Function to retrieve data for heap on given socket
178  */
179 int
180 malloc_heap_get_stats(const struct malloc_heap *heap,
181                 struct rte_malloc_socket_stats *socket_stats)
182 {
183         size_t idx;
184         struct malloc_elem *elem;
185
186         /* Initialise variables for heap */
187         socket_stats->free_count = 0;
188         socket_stats->heap_freesz_bytes = 0;
189         socket_stats->greatest_free_size = 0;
190
191         /* Iterate through free list */
192         for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
193                 for (elem = LIST_FIRST(&heap->free_head[idx]);
194                         !!elem; elem = LIST_NEXT(elem, free_list))
195                 {
196                         socket_stats->free_count++;
197                         socket_stats->heap_freesz_bytes += elem->size;
198                         if (elem->size > socket_stats->greatest_free_size)
199                                 socket_stats->greatest_free_size = elem->size;
200                 }
201         }
202         /* Get stats on overall heap and allocated memory on this heap */
203         socket_stats->heap_totalsz_bytes = heap->total_size;
204         socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
205                         socket_stats->heap_freesz_bytes);
206         socket_stats->alloc_count = heap->alloc_count;
207         return 0;
208 }
209