1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/* -----------------------------------------------------------------------------
*
* (c) The GHC Team, 2001
* Author: Sungwoo Park
*
* Retainer set implementation for retainer profiling (see RetainerProfile.c)
*
* ---------------------------------------------------------------------------*/
#ifdef PROFILING
#include "PosixSource.h"
#include "Rts.h"
#include "Stats.h"
#include "RtsUtils.h"
#include "RetainerSet.h"
#include "Arena.h"
#include "Profiling.h"
#include <string.h>
#define HASH_TABLE_SIZE 255
#define hash(hk) (hk % HASH_TABLE_SIZE)
static RetainerSet *hashTable[HASH_TABLE_SIZE];
static Arena *arena; // arena in which we store retainer sets
static int nextId; // id of next retainer set
/* -----------------------------------------------------------------------------
* rs_MANY is a distinguished retainer set, such that
*
* isMember(e, rs_MANY) = True
*
* addElement(e, rs) = rs_MANY, if rs->num >= maxRetainerSetSize
* addElement(e, rs_MANY) = rs_MANY
*
* The point of rs_MANY is to keep the total number of retainer sets
* from growing too large.
* -------------------------------------------------------------------------- */
RetainerSet rs_MANY = {
.num = 0,
.hashKey = 0,
.link = NULL,
.id = 1,
.element = {}
};
/* -----------------------------------------------------------------------------
* calculate the size of a RetainerSet structure
* -------------------------------------------------------------------------- */
STATIC_INLINE size_t
sizeofRetainerSet( int elems )
{
return (sizeof(RetainerSet) + elems * sizeof(retainer));
}
/* -----------------------------------------------------------------------------
* Creates the first pool and initializes hashTable[].
* Frees all pools if any.
* -------------------------------------------------------------------------- */
void
initializeAllRetainerSet(void)
{
int i;
arena = newArena();
for (i = 0; i < HASH_TABLE_SIZE; i++)
hashTable[i] = NULL;
nextId = 2; // Initial value must be positive, 2 is MANY.
}
/* -----------------------------------------------------------------------------
* Refreshes all pools for reuse and initializes hashTable[].
* -------------------------------------------------------------------------- */
void
refreshAllRetainerSet(void)
{
#ifdef FIRST_APPROACH
int i;
// first approach: completely refresh
arenaFree(arena);
arena = newArena();
for (i = 0; i < HASH_TABLE_SIZE; i++)
hashTable[i] = NULL;
nextId = 2;
#endif /* FIRST_APPROACH */
}
/* -----------------------------------------------------------------------------
* Frees all pools.
* -------------------------------------------------------------------------- */
void
closeAllRetainerSet(void)
{
arenaFree(arena);
}
/* -----------------------------------------------------------------------------
* Finds or creates if needed a singleton retainer set.
* -------------------------------------------------------------------------- */
RetainerSet *
singleton(retainer r)
{
RetainerSet *rs;
StgWord hk;
hk = hashKeySingleton(r);
for (rs = hashTable[hash(hk)]; rs != NULL; rs = rs->link)
if (rs->num == 1 && rs->element[0] == r) return rs; // found it
// create it
rs = arenaAlloc( arena, sizeofRetainerSet(1) );
rs->num = 1;
rs->hashKey = hk;
rs->link = hashTable[hash(hk)];
rs->id = nextId++;
rs->element[0] = r;
// The new retainer set is placed at the head of the linked list.
hashTable[hash(hk)] = rs;
return rs;
}
/* -----------------------------------------------------------------------------
* Finds or creates a retainer set *rs augmented with r.
* Invariants:
* r is not a member of rs, i.e., isMember(r, rs) returns rtsFalse.
* rs is not NULL.
* Note:
* We could check if rs is NULL, in which case this function call
* reverts to singleton(). We do not choose this strategy because
* in most cases addElement() is invoked with non-NULL rs.
* -------------------------------------------------------------------------- */
RetainerSet *
addElement(retainer r, RetainerSet *rs)
{
uint32_t i;
uint32_t nl; // Number of retainers in *rs Less than r
RetainerSet *nrs; // New Retainer Set
StgWord hk; // Hash Key
#ifdef DEBUG_RETAINER
// debugBelch("addElement(%p, %p) = ", r, rs);
#endif
ASSERT(rs != NULL);
ASSERT(rs->num <= RtsFlags.ProfFlags.maxRetainerSetSize);
if (rs == &rs_MANY || rs->num == RtsFlags.ProfFlags.maxRetainerSetSize) {
return &rs_MANY;
}
ASSERT(!isMember(r, rs));
for (nl = 0; nl < rs->num; nl++)
if (r < rs->element[nl]) break;
// Now nl is the index for r into the new set.
// Also it denotes the number of retainers less than r in *rs.
// Thus, compare the first nl retainers, then r itself, and finally the
// remaining (rs->num - nl) retainers.
hk = hashKeyAddElement(r, rs);
for (nrs = hashTable[hash(hk)]; nrs != NULL; nrs = nrs->link) {
// test *rs and *nrs for equality
// check their size
if (rs->num + 1 != nrs->num) continue;
// compare the first nl retainers and find the first non-matching one.
for (i = 0; i < nl; i++)
if (rs->element[i] != nrs->element[i]) break;
if (i < nl) continue;
// compare r itself
if (r != nrs->element[i]) continue; // i == nl
// compare the remaining retainers
for (; i < rs->num; i++)
if (rs->element[i] != nrs->element[i + 1]) break;
if (i < rs->num) continue;
#ifdef DEBUG_RETAINER
// debugBelch("%p\n", nrs);
#endif
// The set we are seeking already exists!
return nrs;
}
// create a new retainer set
nrs = arenaAlloc( arena, sizeofRetainerSet(rs->num + 1) );
nrs->num = rs->num + 1;
nrs->hashKey = hk;
nrs->link = hashTable[hash(hk)];
nrs->id = nextId++;
for (i = 0; i < nl; i++) { // copy the first nl retainers
nrs->element[i] = rs->element[i];
}
nrs->element[i] = r; // copy r
for (; i < rs->num; i++) { // copy the remaining retainers
nrs->element[i + 1] = rs->element[i];
}
hashTable[hash(hk)] = nrs;
#ifdef DEBUG_RETAINER
// debugBelch("%p\n", nrs);
#endif
return nrs;
}
/* -----------------------------------------------------------------------------
* printRetainer() prints the full information on a given retainer,
* not a retainer set.
* -------------------------------------------------------------------------- */
#if defined(RETAINER_SCHEME_INFO)
// Retainer scheme 1: retainer = info table
static void
printRetainer(FILE *f, retainer itbl)
{
fprintf(f, "%s[%s]", GET_PROF_DESC(itbl), itbl->prof.closure_type);
}
#elif defined(RETAINER_SCHEME_CCS)
// Retainer scheme 2: retainer = cost centre stack
static void
printRetainer(FILE *f, retainer ccs)
{
fprintCCS(f, ccs);
}
#elif defined(RETAINER_SCHEME_CC)
// Retainer scheme 3: retainer = cost centre
static void
printRetainer(FILE *f, retainer cc)
{
fprintf(f,"%s.%s", cc->module, cc->label);
}
#endif
/* -----------------------------------------------------------------------------
* printRetainerSetShort() should always display the same output for
* a given retainer set regardless of the time of invocation.
* -------------------------------------------------------------------------- */
#ifdef SECOND_APPROACH
#if defined(RETAINER_SCHEME_INFO)
// Retainer scheme 1: retainer = info table
void
printRetainerSetShort(FILE *f, RetainerSet *rs, uint32_t max_length)
{
char tmp[max_length + 1];
int size;
uint32_t j;
ASSERT(rs->id < 0);
tmp[max_length] = '\0';
// No blank characters are allowed.
sprintf(tmp + 0, "(%d)", -(rs->id));
size = strlen(tmp);
ASSERT(size < max_length);
for (j = 0; j < rs->num; j++) {
if (j < rs->num - 1) {
strncpy(tmp + size, GET_PROF_DESC(rs->element[j]), max_length - size);
size = strlen(tmp);
if (size == max_length)
break;
strncpy(tmp + size, ",", max_length - size);
size = strlen(tmp);
if (size == max_length)
break;
}
else {
strncpy(tmp + size, GET_PROF_DESC(rs->element[j]), max_length - size);
// size = strlen(tmp);
}
}
fprintf(f, tmp);
}
#elif defined(RETAINER_SCHEME_CC)
// Retainer scheme 3: retainer = cost centre
void
printRetainerSetShort(FILE *f, RetainerSet *rs, uint32_t max_length)
{
char tmp[max_length + 1];
int size;
uint32_t j;
}
#elif defined(RETAINER_SCHEME_CCS)
// Retainer scheme 2: retainer = cost centre stack
void
printRetainerSetShort(FILE *f, RetainerSet *rs, uint32_t max_length)
{
char tmp[max_length + 1];
uint32_t size;
uint32_t j;
ASSERT(rs->id < 0);
tmp[max_length] = '\0';
// No blank characters are allowed.
sprintf(tmp + 0, "(%d)", -(rs->id));
size = strlen(tmp);
ASSERT(size < max_length);
for (j = 0; j < rs->num; j++) {
if (j < rs->num - 1) {
strncpy(tmp + size, rs->element[j]->cc->label, max_length - size);
size = strlen(tmp);
if (size == max_length)
break;
strncpy(tmp + size, ",", max_length - size);
size = strlen(tmp);
if (size == max_length)
break;
}
else {
strncpy(tmp + size, rs->element[j]->cc->label, max_length - size);
// size = strlen(tmp);
}
}
fputs(tmp, f);
}
#elif defined(RETAINER_SCHEME_CC)
// Retainer scheme 3: retainer = cost centre
static void
printRetainerSetShort(FILE *f, retainerSet *rs, uint32_t max_length)
{
char tmp[max_length + 1];
int size;
uint32_t j;
ASSERT(rs->id < 0);
tmp[max_length] = '\0';
// No blank characters are allowed.
sprintf(tmp + 0, "(%d)", -(rs->id));
size = strlen(tmp);
ASSERT(size < max_length);
for (j = 0; j < rs->num; j++) {
if (j < rs->num - 1) {
strncpy(tmp + size, rs->element[j]->label,
max_length - size);
size = strlen(tmp);
if (size == max_length)
break;
strncpy(tmp + size, ",", max_length - size);
size = strlen(tmp);
if (size == max_length)
break;
}
else {
strncpy(tmp + size, rs->element[j]->label,
max_length - size);
// size = strlen(tmp);
}
}
fprintf(f, tmp);
/*
#define DOT_NUMBER 3
// 1. 32 > max_length + 1 (1 for '\0')
// 2. (max_length - DOT_NUMBER ) characters should be enough for
// printing one natural number (plus '(' and ')').
char tmp[32];
int size, ts;
uint32_t j;
ASSERT(rs->id < 0);
// No blank characters are allowed.
sprintf(tmp + 0, "(%d)", -(rs->id));
size = strlen(tmp);
ASSERT(size < max_length - DOT_NUMBER);
for (j = 0; j < rs->num; j++) {
ts = strlen(rs->element[j]->label);
if (j < rs->num - 1) {
if (size + ts + 1 > max_length - DOT_NUMBER) {
sprintf(tmp + size, "...");
break;
}
sprintf(tmp + size, "%s,", rs->element[j]->label);
size += ts + 1;
}
else {
if (size + ts > max_length - DOT_NUMBER) {
sprintf(tmp + size, "...");
break;
}
sprintf(tmp + size, "%s", rs->element[j]->label);
size += ts;
}
}
fprintf(f, tmp);
*/
}
#endif /* RETAINER_SCHEME_CC */
#endif /* SECOND_APPROACH */
/* -----------------------------------------------------------------------------
* Dump the contents of each retainer set into the log file at the end
* of the run, so the user can find out for a given retainer set ID
* the full contents of that set.
* -------------------------------------------------------------------------- */
#ifdef SECOND_APPROACH
void
outputAllRetainerSet(FILE *prof_file)
{
uint32_t i, j;
uint32_t numSet;
RetainerSet *rs, **rsArray, *tmp;
// find out the number of retainer sets which have had a non-zero cost at
// least once during retainer profiling
numSet = 0;
for (i = 0; i < HASH_TABLE_SIZE; i++)
for (rs = hashTable[i]; rs != NULL; rs = rs->link) {
if (rs->id < 0)
numSet++;
}
if (numSet == 0) // retainer profiling was not done at all.
return;
// allocate memory
rsArray = stgMallocBytes(numSet * sizeof(RetainerSet *),
"outputAllRetainerSet()");
// prepare for sorting
j = 0;
for (i = 0; i < HASH_TABLE_SIZE; i++)
for (rs = hashTable[i]; rs != NULL; rs = rs->link) {
if (rs->id < 0) {
rsArray[j] = rs;
j++;
}
}
ASSERT(j == numSet);
// sort rsArray[] according to the id of each retainer set
for (i = numSet - 1; i > 0; i--) {
for (j = 0; j <= i - 1; j++) {
// if (-(rsArray[j]->id) < -(rsArray[j + 1]->id))
if (rsArray[j]->id < rsArray[j + 1]->id) {
tmp = rsArray[j];
rsArray[j] = rsArray[j + 1];
rsArray[j + 1] = tmp;
}
}
}
fprintf(prof_file, "\nRetainer sets created during profiling:\n");
for (i = 0;i < numSet; i++) {
fprintf(prof_file, "SET %u = {", -(rsArray[i]->id));
for (j = 0; j < rsArray[i]->num - 1; j++) {
printRetainer(prof_file, rsArray[i]->element[j]);
fprintf(prof_file, ", ");
}
printRetainer(prof_file, rsArray[i]->element[j]);
fprintf(prof_file, "}\n");
}
stgFree(rsArray);
}
#endif /* SECOND_APPROACH */
#endif /* PROFILING */
|