Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d545fccc92b3486cdfb345216748f7b576edb224
[simgrid.git] / src / xbt / mmalloc / mmprivate.h
1 /* Declarations for `mmalloc' and friends. */
2
3 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 /* Copyright 1990, 1991, 1992 Free Software Foundation
9
10    Written May 1989 by Mike Haertel.
11    Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */
12
13 #ifndef XBT_MMPRIVATE_H
14 #define XBT_MMPRIVATE_H 1
15
16 #include <xbt/base.h>
17 #include <xbt/misc.h>
18
19 #include "swag.h"
20 #include "src/internal_config.h"
21 #include "xbt/xbt_os_thread.h"
22 #include "xbt/mmalloc.h"
23 #include "xbt/ex.h"
24 #include "xbt/dynar.h"
25
26 #include <pthread.h>
27 #include <stdint.h>
28
29 #ifdef MIN
30 #undef MIN
31 #endif
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
33 #ifdef MAX
34 #undef MAX
35 #endif
36 #define MAX(a, b) ((a) > (b) ? (a) : (b))
37
38 #ifdef HAVE_LIMITS_H
39 #  include <limits.h>
40 #else
41 #  ifndef CHAR_BIT
42 #    define CHAR_BIT 8
43 #  endif
44 #endif
45
46 #define MMALLOC_MAGIC    "mmalloc"       /* Mapped file magic number */
47 #define MMALLOC_MAGIC_SIZE  8       /* Size of magic number buf */
48 #define MMALLOC_VERSION    2       /* Current mmalloc version */
49
50 /* The allocator divides the heap into blocks of fixed size; large
51    requests receive one or more whole blocks, and small requests
52    receive a fragment of a block.  Fragment sizes are powers of two,
53    and all fragments of a block are the same size.  When all the
54    fragments in a block have been freed, the block itself is freed.
55
56    FIXME: we are not targeting 16bits machines anymore; update values */
57
58 #define INT_BIT    (CHAR_BIT * sizeof(int))
59 #define BLOCKLOG  (INT_BIT > 16 ? 12 : 9)
60 #define BLOCKSIZE  ((unsigned int) 1 << BLOCKLOG)
61 #define BLOCKIFY(SIZE)  (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
62
63 /* We keep fragment-specific meta-data for introspection purposes, and these
64  * information are kept in fixed lenght arrays. Here is the computation of
65  * that size.
66  *
67  * Never make SMALLEST_POSSIBLE_MALLOC smaller than sizeof(list) because we
68  * need to enlist the free fragments.
69  */
70
71 //#define SMALLEST_POSSIBLE_MALLOC (sizeof(struct list))
72 #define SMALLEST_POSSIBLE_MALLOC (16*sizeof(struct list))
73 #define MAX_FRAGMENT_PER_BLOCK (BLOCKSIZE / SMALLEST_POSSIBLE_MALLOC)
74
75 /* The difference between two pointers is a signed int.  On machines where
76    the data addresses have the high bit set, we need to ensure that the
77    difference becomes an unsigned int when we are using the address as an
78    integral value.  In addition, when using with the '%' operator, the
79    sign of the result is machine dependent for negative values, so force
80    it to be treated as an unsigned int. */
81
82 #define ADDR2UINT(addr)  ((uintptr_t) ((char*) (addr) - (char*) NULL))
83 #define RESIDUAL(addr,bsize) ((uintptr_t) (ADDR2UINT (addr) % (bsize)))
84
85 /* Determine the amount of memory spanned by the initial heap table
86    (not an absolute limit).  */
87
88 #define HEAP    (INT_BIT > 16 ? 4194304 : 65536)
89
90 /* Number of contiguous free blocks allowed to build up at the end of
91    memory before they will be returned to the system.
92    FIXME: this is not used anymore: we never return memory to the system. */
93 #define FINAL_FREE_BLOCKS  8
94
95 /* Where to start searching the free list when looking for new memory.
96    The two possible values are 0 and heapindex.  Starting at 0 seems
97    to reduce total memory usage, while starting at heapindex seems to
98    run faster.  */
99
100 #define MALLOC_SEARCH_START  mdp -> heapindex
101
102 /* Address to block number and vice versa.  */
103
104 #define BLOCK(A) (((char*) (A) - (char*) mdp -> heapbase) / BLOCKSIZE + 1)
105
106 #define ADDRESS(B) ((void*) (((ADDR2UINT(B)) - 1) * BLOCKSIZE + (char*) mdp -> heapbase))
107
108 SG_BEGIN_DECL()
109
110 /* Doubly linked lists of free fragments.  */
111 struct list {
112   struct list *next;
113   struct list *prev;
114 };
115
116 /* Statistics available to the user. */
117 struct mstats
118 {
119   size_t bytes_total;    /* Total size of the heap. */
120   size_t chunks_used;    /* Chunks allocated by the user. */
121   size_t bytes_used;    /* Byte total of user-allocated chunks. */
122   size_t chunks_free;    /* Chunks in the free list. */
123   size_t bytes_free;    /* Byte total of chunks in the free list. */
124 };
125
126 #define MMALLOC_TYPE_HEAPINFO (-2)
127 #define MMALLOC_TYPE_FREE (-1)
128 #define MMALLOC_TYPE_UNFRAGMENTED 0
129 /* >0 values are fragmented blocks */
130
131 /* Data structure giving per-block information.
132  *
133  * There is one such structure in the mdp->heapinfo array per block used in that heap,
134  *    the array index is the block number.
135  *
136  * There is several types of blocks in memory:
137  *  - full busy blocks: used when we are asked to malloc a block which size is > BLOCKSIZE/2
138  *    In this situation, the full block is given to the malloc.
139  *
140  *  - fragmented busy blocks: when asked for smaller amount of memory.
141  *    Fragment sizes are only power of 2. When looking for such a free fragment,
142  *    we get one from mdp->fraghead (that contains a linked list of blocks fragmented at that
143  *    size and containing a free fragment), or we get a fresh block that we fragment.
144  *
145  *  - free blocks are grouped by clusters, that are chained together.
146  *    When looking for free blocks, we traverse the mdp->heapinfo looking
147  *    for a cluster of free blocks that would be large enough.
148  *
149  *    The size of the cluster is only to be trusted in the first block of the cluster, not in the middle blocks.
150  *
151  * The type field is consistently updated for every blocks, even within clusters of blocks.
152  * You can crawl the array and rely on that value.
153  *
154  */
155 typedef struct {
156   s_xbt_swag_hookup_t freehook; /* to register this block as having empty frags when needed */
157   int type; /*  0: busy large block
158                 >0: busy fragmented (fragments of size 2^type bytes)
159                 <0: free block */
160
161   union {
162     /* Heap information for a busy block.  */
163     struct {
164       size_t nfree;               /* Free fragments in a fragmented block.  */
165       ssize_t frag_size[MAX_FRAGMENT_PER_BLOCK];
166       //void *bt[MAX_FRAGMENT_PER_BLOCK][XBT_BACKTRACE_SIZE]; /* Where it was malloced (or realloced lastly) */
167       int ignore[MAX_FRAGMENT_PER_BLOCK];
168     } busy_frag;
169     struct {
170       size_t size; /* Size (in blocks) of a large cluster.  */
171       size_t busy_size; /* Actually used space, in bytes */
172       //void *bt[XBT_BACKTRACE_SIZE]; /* Where it was malloced (or realloced lastly) */
173       //int bt_size;
174       int ignore;
175     } busy_block;
176     /* Heap information for a free block (that may be the first of a free cluster).  */
177     struct {
178       size_t size;                /* Size (in blocks) of a free cluster.  */
179       size_t next;                /* Index of next free cluster.  */
180       size_t prev;                /* Index of previous free cluster.  */
181     } free_block;
182   };
183 } malloc_info;
184
185 /** @brief Descriptor of a mmalloc area
186  *
187  * Internal structure that defines the format of the malloc-descriptor.
188  * This gets written to the base address of the region that mmalloc is
189  * managing, and thus also becomes the file header for the mapped file,
190  * if such a file exists.
191  * */
192 struct mdesc {
193
194   /** @brief Mutex locking the access to the heap */
195   pthread_mutex_t mutex;
196
197   /** @brief Number of processes that attached the heap */
198   unsigned int refcount;
199
200   /** @brief Chained lists of mdescs */
201   struct mdesc *next_mdesc;
202
203   /** @brief The "magic number" for an mmalloc file. */
204   char magic[MMALLOC_MAGIC_SIZE];
205
206   /** @brief The size in bytes of this structure
207    *
208    * Used as a sanity check when reusing a previously created mapped file.
209    * */
210   unsigned int headersize;
211
212   /** @brief Version number of the mmalloc package that created this file. */
213   unsigned char version;
214
215   unsigned int options;
216
217   /** @brief Some flag bits to keep track of various internal things. */
218   unsigned int flags;
219
220   /** @brief Number of info entries.  */
221   size_t heapsize;
222
223   /** @brief Pointer to first block of the heap (base of the first block).  */
224   void *heapbase;
225
226   /** @brief Current search index for the heap table.
227    *
228    *  Search index in the info table.
229    */
230   size_t heapindex;
231
232   /** @brief Limit of valid info table indices.  */
233   size_t heaplimit;
234
235   /** @brief Block information table.
236    *
237    * Table indexed by block number giving per-block information.
238    */
239   malloc_info *heapinfo;
240
241   /* @brief List of all blocks containing free fragments of a given size.
242    *
243    * The array indice is the log2 of requested size.
244    * Actually only the sizes 8->11 seem to be used, but who cares? */
245   s_xbt_swag_t fraghead[BLOCKLOG];
246
247   /* @brief Base address of the memory region for this malloc heap
248    *
249    * This is the location where the bookkeeping data for mmap and
250    * for malloc begins.
251    */
252   void *base;
253
254   /** @brief End of memory in use
255    *
256    *  Some memory might be already mapped by the OS but not used
257    *  by the heap.
258    * */
259   void *breakval;
260
261   /** @brief End of the current memory region for this malloc heap.
262    *
263    *  This is the first location past the end of mapped memory.
264    *
265    *  Compared to breakval, this value is rounded to the next memory page.
266    */
267   void *top;
268
269   /** @brief Open file descriptor for the file to which this malloc heap is mapped
270    *
271    * If this value is negative, MAP_ANONYMOUS memory is used.
272    *
273    * Also note that it may change each time the region is mapped and unmapped. */
274   int fd;
275
276   /* @brief Instrumentation */
277   struct mstats heapstats;
278
279 };
280
281 /* Bits to look at in the malloc descriptor flags word */
282
283 #define MMALLOC_DEVZERO    (1 << 0)        /* Have mapped to /dev/zero */
284 #define MMALLOC_ANONYMOUS (1 << 1)      /* Use anonymous mapping */
285 #define MMALLOC_INITIALIZED  (1 << 2)        /* Initialized mmalloc */
286
287 /* A default malloc descriptor for the single sbrk() managed region. */
288
289 XBT_PUBLIC_DATA struct mdesc* __mmalloc_default_mdp;
290
291 /* Remap a mmalloc region that was previously mapped. */
292
293 XBT_PUBLIC void* __mmalloc_remap_core(xbt_mheap_t mdp);
294
295 XBT_PUBLIC void* mmorecore(struct mdesc* mdp, ssize_t size);
296
297 /** Thread-safety (if the mutex is already created)
298  *
299  * This is mandatory in the case where the user runs a parallel simulation
300  * in a model-checking enabled tree. Without this protection, our malloc
301  * implementation will not like multi-threading AT ALL.
302  */
303 #define LOCK(mdp) pthread_mutex_lock(&mdp->mutex)
304 #define UNLOCK(mdp) pthread_mutex_unlock(&mdp->mutex)
305
306 XBT_PRIVATE int malloc_use_mmalloc(void);
307
308 XBT_PRIVATE size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapinfo);
309
310 SG_END_DECL()
311
312 #endif