Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Parameter 'fd' is always -1 for xbt_mheap_new. Kill dead code.
[simgrid.git] / src / xbt / mmalloc / mm_module.c
1 /* Initialization for access to a mmap'd malloc managed region. */
2
3 /* Copyright (c) 2012-2021. 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 1992, 2000 Free Software Foundation, Inc.
10
11    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
12
13    This file is part of the GNU C Library.
14
15    The GNU C Library is free software; you can redistribute it and/or
16    modify it under the terms of the GNU Library General Public License as
17    published by the Free Software Foundation; either version 2 of the
18    License, or (at your option) any later version.
19
20    The GNU C Library is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    Library General Public License for more details.
24
25    You should have received a copy of the GNU Library General Public
26    License along with the GNU C Library; see the file COPYING.LIB.  If
27    not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28    Boston, MA 02111-1307, USA.  */
29
30 #include "src/internal_config.h"
31 #include <sys/types.h>
32 #include <fcntl.h>              /* After sys/types.h, at least for dpx/2.  */
33 #include <sys/stat.h>
34 #include <string.h>
35 #include "mmprivate.h"
36 #include "xbt/ex.h"
37 #include "xbt/xbt_modinter.h" /* declarations of mmalloc_preinit and friends that live here */
38
39 /* Initialize access to a mmalloc managed region.
40
41    The mapping is established starting at the specified address BASEADDR
42    in the process address space.
43
44    The provided BASEADDR should be chosen carefully in order to avoid
45    bumping into existing mapped regions or future mapped regions.
46
47    On success, returns a "malloc descriptor" which is used in subsequent
48    calls to other mmalloc package functions.  It is explicitly "void *"
49    so that users of the package don't have to worry about the actual
50    implementation details.
51
52    On failure returns NULL. */
53
54 xbt_mheap_t xbt_mheap_new(void* baseaddr, int options)
55 {
56   /* NULL is not a valid baseaddr as we cannot map anything there. C'mon, user. Think! */
57   if (baseaddr == NULL)
58     return NULL;
59
60   /* We start off with the malloc descriptor allocated on the stack, until we build it up enough to
61    * call _mmalloc_mmap_morecore() to allocate the first page of the region and copy it there.  Ensure that it is
62    * zero'd and then initialize the fields that we know values for. */
63
64   struct mdesc mtemp;
65   xbt_mheap_t mdp = &mtemp;
66   memset((char *) mdp, 0, sizeof(mtemp));
67   strncpy(mdp->magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
68   mdp->headersize = sizeof(mtemp);
69   mdp->version = MMALLOC_VERSION;
70   mdp->base = mdp->breakval = mdp->top = baseaddr;
71   mdp->next_mdesc = NULL;
72   mdp->options = options;
73
74   pthread_mutex_init(&mdp->mutex, NULL);
75   /* If we have not been passed a valid open file descriptor for the file
76      to map to, then open /dev/zero and use that to map to. */
77
78   /* Now try to map in the first page, copy the malloc descriptor structure there, and arrange to return a pointer to
79    * this new copy.  If the mapping fails, then close the file descriptor if it was opened by us, and arrange to return
80    * a NULL. */
81
82   void* mbase = mmorecore(mdp, sizeof(mtemp));
83   if (mbase == NULL) {
84     fprintf(stderr, "morecore failed to get some more memory!\n");
85     abort();
86   }
87   memcpy(mbase, mdp, sizeof(mtemp));
88
89   /* Add the new heap to the linked list of heaps attached by mmalloc */
90   if(__mmalloc_default_mdp){
91     mdp = __mmalloc_default_mdp;
92     while(mdp->next_mdesc)
93       mdp = mdp->next_mdesc;
94
95     LOCK(mdp);
96     mdp->next_mdesc = (struct mdesc *)mbase;
97     UNLOCK(mdp);
98   }
99
100   return mbase;
101 }
102
103
104
105 /** Terminate access to a mmalloc managed region, but do not free its content.
106  *
107  * This is for example useful for the base region where ldl stores its data
108  *   because it leaves the place after us.
109  */
110 void xbt_mheap_destroy_no_free(xbt_mheap_t md)
111 {
112   struct mdesc *mdp = md;
113
114   pthread_mutex_destroy(&mdp->mutex);
115 }
116
117 /** Terminate access to a mmalloc managed region by unmapping all memory pages associated with the region, and closing
118  *  the file descriptor if it is one that we opened.
119
120     Returns NULL on success.
121
122     Returns the malloc descriptor on failure, which can subsequently be used for further action, such as obtaining more
123     information about the nature of the failure.
124
125     Note that the malloc descriptor that we are using is currently located in region we are about to unmap, so we first
126     make a local copy of it on the stack and use the copy. */
127
128 void *xbt_mheap_destroy(xbt_mheap_t mdp)
129 {
130   if (mdp != NULL) {
131     /* Remove the heap from the linked list of heaps attached by mmalloc */
132     struct mdesc* mdptemp = __mmalloc_default_mdp;
133     while(mdptemp->next_mdesc != mdp )
134       mdptemp = mdptemp->next_mdesc;
135
136     mdptemp->next_mdesc = mdp->next_mdesc;
137
138     xbt_mheap_destroy_no_free(mdp);
139     struct mdesc mtemp = *mdp;
140
141     /* Now unmap all the pages associated with this region by asking for a
142        negative increment equal to the current size of the region. */
143
144     if (mmorecore(&mtemp, (char *)mtemp.base - (char *)mtemp.breakval) == NULL) {
145       /* Deallocating failed.  Update the original malloc descriptor with any changes */
146       *mdp = mtemp;
147     } else {
148       mdp = NULL;
149     }
150   }
151
152   return mdp;
153 }
154
155 /* Safety gap from the heap's break address.
156  * Try to increase this first if you experience strange errors under valgrind. */
157 #define HEAP_OFFSET   (128UL<<20)
158
159 static void mmalloc_fork_prepare(void)
160 {
161   xbt_mheap_t mdp = NULL;
162   if ((mdp =__mmalloc_default_mdp)){
163     while(mdp){
164       LOCK(mdp);
165       mdp = mdp->next_mdesc;
166     }
167   }
168 }
169
170 static void mmalloc_fork_parent(void)
171 {
172   xbt_mheap_t mdp = NULL;
173   if ((mdp =__mmalloc_default_mdp)){
174     while(mdp){
175       UNLOCK(mdp);
176       mdp = mdp->next_mdesc;
177     }
178   }
179 }
180
181 static void mmalloc_fork_child(void)
182 {
183   struct mdesc* mdp = NULL;
184   if ((mdp =__mmalloc_default_mdp)){
185     while(mdp){
186       UNLOCK(mdp);
187       mdp = mdp->next_mdesc;
188     }
189   }
190 }
191
192 /* Initialize the default malloc descriptor. */
193 xbt_mheap_t mmalloc_preinit(void)
194 {
195   if (__mmalloc_default_mdp == NULL) {
196     if(!xbt_pagesize)
197       xbt_pagesize = getpagesize();
198     unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
199     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
200     __mmalloc_default_mdp = xbt_mheap_new(addr, XBT_MHEAP_OPTION_MEMSET);
201
202     // atfork mandated at least on FreeBSD, or simgrid-mc will fail to fork the verified app
203     int res = pthread_atfork(mmalloc_fork_prepare, mmalloc_fork_parent, mmalloc_fork_child);
204     xbt_assert(res == 0, "pthread_atfork() failed: return value %d", res);
205   }
206   xbt_assert(__mmalloc_default_mdp != NULL);
207
208   return __mmalloc_default_mdp;
209 }
210
211 void mmalloc_postexit(void)
212 {
213   /* Do not destroy the default mdp or ldl won't be able to free the memory it
214    * allocated since we're in memory */
215   // xbt_mheap_destroy_no_free(__mmalloc_default_mdp)
216 }
217
218 // This is the underlying implementation of mmalloc_get_bytes_used_remote.
219 // Is it used directly in order to evaluate the bytes used from a different
220 // process.
221 size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapinfo)
222 {
223   int bytes = 0;
224   for (size_t i=0; i < heaplimit; ++i){
225     if (heapinfo[i].type == MMALLOC_TYPE_UNFRAGMENTED){
226       if (heapinfo[i].busy_block.busy_size > 0)
227         bytes += heapinfo[i].busy_block.busy_size;
228     } else if (heapinfo[i].type > 0) {
229       for (size_t j=0; j < (size_t) (BLOCKSIZE >> heapinfo[i].type); j++){
230         if(heapinfo[i].busy_frag.frag_size[j] > 0)
231           bytes += heapinfo[i].busy_frag.frag_size[j];
232       }
233     }
234   }
235   return bytes;
236 }