1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2001
6 * Retainer set interface for retainer profiling.
8 * ---------------------------------------------------------------------------*/
20 Type 'retainer' defines the retainer identity.
23 1. The retainer identity of a given retainer cannot change during
24 program execution, no matter where it is actually stored.
25 For instance, the memory address of a retainer cannot be used as
26 its retainer identity because its location may change during garbage
28 2. Type 'retainer' must come with comparison operations as well as
29 an equality operation. That it, <, >, and == must be supported -
30 this is necessary to store retainers in a sorted order in retainer sets.
31 Therefore, you cannot use a huge structure type as 'retainer', for instance.
33 We illustrate three possibilities of defining 'retainer identity'.
34 Choose one of the following three compiler directives:
36 Retainer scheme 1 (RETAINER_SCHEME_INFO) : retainer = info table
37 Retainer scheme 2 (RETAINER_SCHEME_CCS) : retainer = cost centre stack
38 Retainer scheme 3 (RETAINER_SCHEME_CC) : retainer = cost centre
41 // #define RETAINER_SCHEME_INFO
42 #define RETAINER_SCHEME_CCS
43 // #define RETAINER_SCHEME_CC
45 #ifdef RETAINER_SCHEME_INFO
47 typedef struct _StgInfoTable *retainer;
50 #ifdef RETAINER_SCHEME_CCS
51 typedef CostCentreStack *retainer;
54 #ifdef RETAINER_SCHEME_CC
55 typedef CostCentre *retainer;
59 Type 'retainerSet' defines an abstract datatype for sets of retainers.
62 A retainer set stores its elements in increasing order (in element[] array).
65 typedef struct _RetainerSet {
66 nat num; // number of elements
67 StgWord hashKey; // hash key for this retainer set
68 struct _RetainerSet *link; // link to the next retainer set in the bucket
69 int id; // unique id of this retainer set (used when printing)
70 // Its absolute value is interpreted as its true id; if id is
71 // negative, it indicates that this retainer set has had a postive
72 // cost after some retainer profiling.
73 retainer element[0]; // elements of this retainer set
74 // do not put anything below here!
79 There are two ways of maintaining all retainer sets. The first is simply by
80 freeing all the retainer sets and re-initialize the hash table at each
81 retainer profiling. The second is by setting the cost field of each
82 retainer set. The second is preferred to the first if most retainer sets
83 are likely to be observed again during the next retainer profiling. Note
84 that in the first approach, we do not free the memory allocated for
85 retainer sets; we just invalidate all retainer sets.
88 // In thise case, FIRST_APPROACH must be turned on because the memory pool
89 // for retainer sets is freed each time.
90 #define FIRST_APPROACH
92 // #define FIRST_APPROACH
93 #define SECOND_APPROACH
96 // Creates the first pool and initializes a hash table. Frees all pools if any.
97 void initializeAllRetainerSet(void);
99 // Refreshes all pools for reuse and initializes a hash table.
100 void refreshAllRetainerSet(void);
103 void closeAllRetainerSet(void);
105 // Finds or creates if needed a singleton retainer set.
106 RetainerSet *singleton(retainer r);
108 extern RetainerSet rs_MANY;
110 // Checks if a given retainer is a memeber of the retainer set.
112 // Note & (maybe) Todo:
113 // This function needs to be declared as an inline function, so it is declared
114 // as an inline static function here.
115 // This make the interface really bad, but isMember() returns a value, so
116 // it is not easy either to write it as a macro (due to my lack of C
117 // programming experience). Sungwoo
119 // rtsBool isMember(retainer, retainerSet *);
121 Returns rtsTrue if r is a member of *rs.
125 The efficiency of this function is subject to the typical size of
126 retainer sets. If it is small, linear scan is better. If it
127 is large in most cases, binary scan is better.
128 The current implementation mixes the two search strategies.
131 #define BINARY_SEARCH_THRESHOLD 8
132 INLINE_HEADER rtsBool
133 isMember(retainer r, RetainerSet *rs)
135 int i, left, right; // must be int, not nat (because -1 can appear)
138 if (rs == &rs_MANY) { return rtsTrue; }
140 if (rs->num < BINARY_SEARCH_THRESHOLD) {
141 for (i = 0; i < (int)rs->num; i++) {
143 if (r == ri) return rtsTrue;
144 else if (r < ri) return rtsFalse;
149 while (left <= right) {
150 i = (left + right) / 2;
152 if (r == ri) return rtsTrue;
153 else if (r < ri) right = i - 1;
160 // Finds or creates a retainer set augmented with a new retainer.
161 RetainerSet *addElement(retainer, RetainerSet *);
163 // Call f() for each retainer set.
164 void traverseAllRetainerSet(void (*f)(RetainerSet *));
166 #ifdef SECOND_APPROACH
167 // Prints a single retainer set.
168 void printRetainerSetShort(FILE *, RetainerSet *);
171 // Print the statistics on all the retainer sets.
172 // store the sum of all costs and the number of all retainer sets.
173 void outputRetainerSet(FILE *, nat *, nat *);
175 #ifdef SECOND_APPROACH
176 // Print all retainer sets at the exit of the program.
177 void outputAllRetainerSet(FILE *);
183 Once either initializeAllRetainerSet() or refreshAllRetainerSet()
184 is called, there exists only one copy of any retainer set created
185 through singleton() and addElement(). The pool (the storage for
186 retainer sets) is consumed linearly. All the retainer sets of the
187 same hash function value are linked together from an element in
188 hashTable[]. See the invariants of allocateInPool() for the
189 maximum size of retainer sets. The hashing function is defined by
190 hashKeySingleton() and hashKeyAddElement(). The hash key for a set
191 must be unique regardless of the order its elements are inserted,
192 i.e., the hashing function must be additive(?).
194 #define hashKeySingleton(r) ((StgWord)(r))
195 #define hashKeyAddElement(r, s) (hashKeySingleton((r)) + (s)->hashKey)
197 // Prints the full information on a given retainer.
198 // Note: This function is not part of retainerSet interface, but this is
199 // the best place to define it.
200 void printRetainer(FILE *, retainer);
204 #endif /* PROFILING */
205 #endif /* RETAINERSET_H */