Enable two-step allocator on FreeBSD
[ghc.git] / rts / RetainerSet.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2001
4 * Author: Sungwoo Park
5 *
6 * Retainer set implementation for retainer profiling (see RetainerProfile.c)
7 *
8 * ---------------------------------------------------------------------------*/
9
10 #if defined(PROFILING)
11
12 #include "PosixSource.h"
13 #include "Rts.h"
14
15 #include "Stats.h"
16 #include "RtsUtils.h"
17 #include "RetainerSet.h"
18 #include "Arena.h"
19 #include "Profiling.h"
20
21 #include <string.h>
22
23 #define HASH_TABLE_SIZE 255
24 #define hash(hk) (hk % HASH_TABLE_SIZE)
25 static RetainerSet *hashTable[HASH_TABLE_SIZE];
26
27 static Arena *arena; // arena in which we store retainer sets
28
29 static int nextId; // id of next retainer set
30
31 /* -----------------------------------------------------------------------------
32 * rs_MANY is a distinguished retainer set, such that
33 *
34 * isMember(e, rs_MANY) = True
35 *
36 * addElement(e, rs) = rs_MANY, if rs->num >= maxRetainerSetSize
37 * addElement(e, rs_MANY) = rs_MANY
38 *
39 * The point of rs_MANY is to keep the total number of retainer sets
40 * from growing too large.
41 * -------------------------------------------------------------------------- */
42 RetainerSet rs_MANY = {
43 .num = 0,
44 .hashKey = 0,
45 .link = NULL,
46 .id = 1,
47 .element = {}
48 };
49
50 /* -----------------------------------------------------------------------------
51 * calculate the size of a RetainerSet structure
52 * -------------------------------------------------------------------------- */
53 STATIC_INLINE size_t
54 sizeofRetainerSet( int elems )
55 {
56 return (sizeof(RetainerSet) + elems * sizeof(retainer));
57 }
58
59 /* -----------------------------------------------------------------------------
60 * Creates the first pool and initializes hashTable[].
61 * Frees all pools if any.
62 * -------------------------------------------------------------------------- */
63 void
64 initializeAllRetainerSet(void)
65 {
66 int i;
67
68 arena = newArena();
69
70 for (i = 0; i < HASH_TABLE_SIZE; i++)
71 hashTable[i] = NULL;
72 nextId = 2; // Initial value must be positive, 2 is MANY.
73 }
74
75 /* -----------------------------------------------------------------------------
76 * Refreshes all pools for reuse and initializes hashTable[].
77 * -------------------------------------------------------------------------- */
78 void
79 refreshAllRetainerSet(void)
80 {
81 #if defined(FIRST_APPROACH)
82 int i;
83
84 // first approach: completely refresh
85 arenaFree(arena);
86 arena = newArena();
87
88 for (i = 0; i < HASH_TABLE_SIZE; i++)
89 hashTable[i] = NULL;
90 nextId = 2;
91 #endif /* FIRST_APPROACH */
92 }
93
94 /* -----------------------------------------------------------------------------
95 * Frees all pools.
96 * -------------------------------------------------------------------------- */
97 void
98 closeAllRetainerSet(void)
99 {
100 arenaFree(arena);
101 }
102
103 /* -----------------------------------------------------------------------------
104 * Finds or creates if needed a singleton retainer set.
105 * -------------------------------------------------------------------------- */
106 RetainerSet *
107 singleton(retainer r)
108 {
109 RetainerSet *rs;
110 StgWord hk;
111
112 hk = hashKeySingleton(r);
113 for (rs = hashTable[hash(hk)]; rs != NULL; rs = rs->link)
114 if (rs->num == 1 && rs->element[0] == r) return rs; // found it
115
116 // create it
117 rs = arenaAlloc( arena, sizeofRetainerSet(1) );
118 rs->num = 1;
119 rs->hashKey = hk;
120 rs->link = hashTable[hash(hk)];
121 rs->id = nextId++;
122 rs->element[0] = r;
123
124 // The new retainer set is placed at the head of the linked list.
125 hashTable[hash(hk)] = rs;
126
127 return rs;
128 }
129
130 /* -----------------------------------------------------------------------------
131 * Finds or creates a retainer set *rs augmented with r.
132 * Invariants:
133 * r is not a member of rs, i.e., isMember(r, rs) returns false.
134 * rs is not NULL.
135 * Note:
136 * We could check if rs is NULL, in which case this function call
137 * reverts to singleton(). We do not choose this strategy because
138 * in most cases addElement() is invoked with non-NULL rs.
139 * -------------------------------------------------------------------------- */
140 RetainerSet *
141 addElement(retainer r, RetainerSet *rs)
142 {
143 uint32_t i;
144 uint32_t nl; // Number of retainers in *rs Less than r
145 RetainerSet *nrs; // New Retainer Set
146 StgWord hk; // Hash Key
147
148 #if defined(DEBUG_RETAINER)
149 // debugBelch("addElement(%p, %p) = ", r, rs);
150 #endif
151
152 ASSERT(rs != NULL);
153 ASSERT(rs->num <= RtsFlags.ProfFlags.maxRetainerSetSize);
154
155 if (rs == &rs_MANY || rs->num == RtsFlags.ProfFlags.maxRetainerSetSize) {
156 return &rs_MANY;
157 }
158
159 ASSERT(!isMember(r, rs));
160
161 for (nl = 0; nl < rs->num; nl++)
162 if (r < rs->element[nl]) break;
163 // Now nl is the index for r into the new set.
164 // Also it denotes the number of retainers less than r in *rs.
165 // Thus, compare the first nl retainers, then r itself, and finally the
166 // remaining (rs->num - nl) retainers.
167
168 hk = hashKeyAddElement(r, rs);
169 for (nrs = hashTable[hash(hk)]; nrs != NULL; nrs = nrs->link) {
170 // test *rs and *nrs for equality
171
172 // check their size
173 if (rs->num + 1 != nrs->num) continue;
174
175 // compare the first nl retainers and find the first non-matching one.
176 for (i = 0; i < nl; i++)
177 if (rs->element[i] != nrs->element[i]) break;
178 if (i < nl) continue;
179
180 // compare r itself
181 if (r != nrs->element[i]) continue; // i == nl
182
183 // compare the remaining retainers
184 for (; i < rs->num; i++)
185 if (rs->element[i] != nrs->element[i + 1]) break;
186 if (i < rs->num) continue;
187
188 #if defined(DEBUG_RETAINER)
189 // debugBelch("%p\n", nrs);
190 #endif
191 // The set we are seeking already exists!
192 return nrs;
193 }
194
195 // create a new retainer set
196 nrs = arenaAlloc( arena, sizeofRetainerSet(rs->num + 1) );
197 nrs->num = rs->num + 1;
198 nrs->hashKey = hk;
199 nrs->link = hashTable[hash(hk)];
200 nrs->id = nextId++;
201 for (i = 0; i < nl; i++) { // copy the first nl retainers
202 nrs->element[i] = rs->element[i];
203 }
204 nrs->element[i] = r; // copy r
205 for (; i < rs->num; i++) { // copy the remaining retainers
206 nrs->element[i + 1] = rs->element[i];
207 }
208
209 hashTable[hash(hk)] = nrs;
210
211 #if defined(DEBUG_RETAINER)
212 // debugBelch("%p\n", nrs);
213 #endif
214 return nrs;
215 }
216
217 /* -----------------------------------------------------------------------------
218 * printRetainer() prints the full information on a given retainer,
219 * not a retainer set.
220 * -------------------------------------------------------------------------- */
221 static void
222 printRetainer(FILE *f, retainer ccs)
223 {
224 fprintCCS(f, ccs);
225 }
226
227 /* -----------------------------------------------------------------------------
228 * printRetainerSetShort() should always display the same output for
229 * a given retainer set regardless of the time of invocation.
230 * -------------------------------------------------------------------------- */
231 #if defined(SECOND_APPROACH)
232 void
233 printRetainerSetShort(FILE *f, RetainerSet *rs, uint32_t max_length)
234 {
235 char tmp[max_length + 1];
236 uint32_t size;
237 uint32_t j;
238
239 ASSERT(rs->id < 0);
240
241 tmp[max_length] = '\0';
242
243 // No blank characters are allowed.
244 sprintf(tmp + 0, "(%d)", -(rs->id));
245 size = strlen(tmp);
246 ASSERT(size < max_length);
247
248 for (j = 0; j < rs->num; j++) {
249 if (j < rs->num - 1) {
250 strncpy(tmp + size, rs->element[j]->cc->label, max_length - size);
251 size = strlen(tmp);
252 if (size == max_length)
253 break;
254 strncpy(tmp + size, ",", max_length - size);
255 size = strlen(tmp);
256 if (size == max_length)
257 break;
258 }
259 else {
260 strncpy(tmp + size, rs->element[j]->cc->label, max_length - size);
261 // size = strlen(tmp);
262 }
263 }
264 fputs(tmp, f);
265 }
266 #endif /* SECOND_APPROACH */
267
268 /* -----------------------------------------------------------------------------
269 * Dump the contents of each retainer set into the log file at the end
270 * of the run, so the user can find out for a given retainer set ID
271 * the full contents of that set.
272 * -------------------------------------------------------------------------- */
273 #if defined(SECOND_APPROACH)
274 void
275 outputAllRetainerSet(FILE *prof_file)
276 {
277 uint32_t i, j;
278 uint32_t numSet;
279 RetainerSet *rs, **rsArray, *tmp;
280
281 // find out the number of retainer sets which have had a non-zero cost at
282 // least once during retainer profiling
283 numSet = 0;
284 for (i = 0; i < HASH_TABLE_SIZE; i++)
285 for (rs = hashTable[i]; rs != NULL; rs = rs->link) {
286 if (rs->id < 0)
287 numSet++;
288 }
289
290 if (numSet == 0) // retainer profiling was not done at all.
291 return;
292
293 // allocate memory
294 rsArray = stgMallocBytes(numSet * sizeof(RetainerSet *),
295 "outputAllRetainerSet()");
296
297 // prepare for sorting
298 j = 0;
299 for (i = 0; i < HASH_TABLE_SIZE; i++)
300 for (rs = hashTable[i]; rs != NULL; rs = rs->link) {
301 if (rs->id < 0) {
302 rsArray[j] = rs;
303 j++;
304 }
305 }
306
307 ASSERT(j == numSet);
308
309 // sort rsArray[] according to the id of each retainer set
310 for (i = numSet - 1; i > 0; i--) {
311 for (j = 0; j <= i - 1; j++) {
312 // if (-(rsArray[j]->id) < -(rsArray[j + 1]->id))
313 if (rsArray[j]->id < rsArray[j + 1]->id) {
314 tmp = rsArray[j];
315 rsArray[j] = rsArray[j + 1];
316 rsArray[j + 1] = tmp;
317 }
318 }
319 }
320
321 fprintf(prof_file, "\nRetainer sets created during profiling:\n");
322 for (i = 0;i < numSet; i++) {
323 fprintf(prof_file, "SET %u = {", -(rsArray[i]->id));
324 for (j = 0; j < rsArray[i]->num - 1; j++) {
325 printRetainer(prof_file, rsArray[i]->element[j]);
326 fprintf(prof_file, ", ");
327 }
328 printRetainer(prof_file, rsArray[i]->element[j]);
329 fprintf(prof_file, "}\n");
330 }
331
332 stgFree(rsArray);
333 }
334 #endif /* SECOND_APPROACH */
335
336 #endif /* PROFILING */