Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
this example needs to forcefully destroy the VMs
[simgrid.git] / src / xbt / mmalloc / mmprivate.h
1 /* Declarations for `mmalloc' and friends. */
2
3 /* Copyright (c) 2010-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 /* Copyright 1990, 1991, 1992 Free Software Foundation
10
11    Written May 1989 by Mike Haertel.
12    Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */
13
14 #ifndef __MMPRIVATE_H
15 #define __MMPRIVATE_H 1
16
17 #include "portable.h"
18 #include "xbt/xbt_os_thread.h"
19 #include "xbt/mmalloc.h"
20 #include "xbt/ex.h"
21 #include "xbt/dynar.h"
22 #include "xbt/swag.h"
23
24 #include <pthread.h>
25 #include <stdint.h>
26
27 #ifdef HAVE_LIMITS_H
28 #  include <limits.h>
29 #else
30 #  ifndef CHAR_BIT
31 #    define CHAR_BIT 8
32 #  endif
33 #endif
34
35 #define MMALLOC_MAGIC    "mmalloc"       /* Mapped file magic number */
36 #define MMALLOC_MAGIC_SIZE  8       /* Size of magic number buf */
37 #define MMALLOC_VERSION    2       /* Current mmalloc version */
38
39 /* The allocator divides the heap into blocks of fixed size; large
40    requests receive one or more whole blocks, and small requests
41    receive a fragment of a block.  Fragment sizes are powers of two,
42    and all fragments of a block are the same size.  When all the
43    fragments in a block have been freed, the block itself is freed.
44
45    FIXME: we are not targeting 16bits machines anymore; update values */
46
47 #define INT_BIT    (CHAR_BIT * sizeof(int))
48 #define BLOCKLOG  (INT_BIT > 16 ? 12 : 9)
49 #define BLOCKSIZE  ((unsigned int) 1 << BLOCKLOG)
50 #define BLOCKIFY(SIZE)  (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
51
52 /* We keep fragment-specific meta-data for introspection purposes, and these
53  * information are kept in fixed lenght arrays. Here is the computation of
54  * that size.
55  *
56  * Never make SMALLEST_POSSIBLE_MALLOC smaller than sizeof(list) because we
57  * need to enlist the free fragments.
58  */
59
60 //#define SMALLEST_POSSIBLE_MALLOC (sizeof(struct list))
61 #define SMALLEST_POSSIBLE_MALLOC (16*sizeof(struct list))
62 #define MAX_FRAGMENT_PER_BLOCK (BLOCKSIZE / SMALLEST_POSSIBLE_MALLOC)
63
64 /* The difference between two pointers is a signed int.  On machines where
65    the data addresses have the high bit set, we need to ensure that the
66    difference becomes an unsigned int when we are using the address as an
67    integral value.  In addition, when using with the '%' operator, the
68    sign of the result is machine dependent for negative values, so force
69    it to be treated as an unsigned int. */
70
71 #define ADDR2UINT(addr)  ((uintptr_t) ((char*) (addr) - (char*) NULL))
72 #define RESIDUAL(addr,bsize) ((uintptr_t) (ADDR2UINT (addr) % (bsize)))
73
74 /* Determine the amount of memory spanned by the initial heap table
75    (not an absolute limit).  */
76
77 #define HEAP    (INT_BIT > 16 ? 4194304 : 65536)
78
79 /* Number of contiguous free blocks allowed to build up at the end of
80    memory before they will be returned to the system.
81    FIXME: this is not used anymore: we never return memory to the system. */
82 #define FINAL_FREE_BLOCKS  8
83
84 /* Where to start searching the free list when looking for new memory.
85    The two possible values are 0 and heapindex.  Starting at 0 seems
86    to reduce total memory usage, while starting at heapindex seems to
87    run faster.  */
88
89 #define MALLOC_SEARCH_START  mdp -> heapindex
90
91 /* Address to block number and vice versa.  */
92
93 #define BLOCK(A) (((char*) (A) - (char*) mdp -> heapbase) / BLOCKSIZE + 1)
94
95 #define ADDRESS(B) ((void*) (((ADDR2UINT(B)) - 1) * BLOCKSIZE + (char*) mdp -> heapbase))
96
97 /* Doubly linked lists of free fragments.  */
98 struct list {
99   struct list *next;
100   struct list *prev;
101 };
102
103 /* Statistics available to the user. */
104 struct mstats
105 {
106   size_t bytes_total;    /* Total size of the heap. */
107   size_t chunks_used;    /* Chunks allocated by the user. */
108   size_t bytes_used;    /* Byte total of user-allocated chunks. */
109   size_t chunks_free;    /* Chunks in the free list. */
110   size_t bytes_free;    /* Byte total of chunks in the free list. */
111 };
112
113 typedef struct s_heap_area{
114   int valid;
115   int block;
116   int fragment;
117 }s_heap_area_t, *heap_area_t;
118
119 typedef struct s_heap_area_pair{
120   int block1;
121   int fragment1;
122   int block2;
123   int fragment2;
124 }s_heap_area_pair_t, *heap_area_pair_t;
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( 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 static XBT_INLINE void  mmalloc_paranoia(struct mdesc *mdp){
307
308   /* nothing to fear for no */
309
310 }
311
312 static inline int mmalloc_get_increment(malloc_info* heapinfo) {
313   if (heapinfo->type < 0) {
314     return heapinfo->free_block.size;
315   } else if (heapinfo->type == 0) {
316     return heapinfo->busy_block.size;
317   } else {
318     return 1;
319   }
320 }
321
322 void mmcheck(xbt_mheap_t heap);
323
324 int malloc_use_mmalloc(void);
325
326 int mmalloc_exec_using_mm(int argc, const char** argv);
327 void mmalloc_ensure_using_mm(int argc, const char** argv);
328
329 size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapinfo);
330
331 #endif                          /* __MMPRIVATE_H */