1 /* -----------------------------------------------------------------------------
2 * $Id: RetainerSet.h,v 1.3 2004/09/03 15:28:39 simonmar Exp $
4 * (c) The GHC Team, 2001
7 * Retainer set interface for retainer profiling.
9 * ---------------------------------------------------------------------------*/
19 Type 'retainer' defines the retainer identity.
22 1. The retainer identity of a given retainer cannot change during
23 program execution, no matter where it is actually stored.
24 For instance, the memory address of a retainer cannot be used as
25 its retainer identity because its location may change during garbage
27 2. Type 'retainer' must come with comparison operations as well as
28 an equality operation. That it, <, >, and == must be supported -
29 this is necessary to store retainers in a sorted order in retainer sets.
30 Therefore, you cannot use a huge structure type as 'retainer', for instance.
32 We illustrate three possibilities of defining 'retainer identity'.
33 Choose one of the following three compiler directives:
35 Retainer scheme 1 (RETAINER_SCHEME_INFO) : retainer = info table
36 Retainer scheme 2 (RETAINER_SCHEME_CCS) : retainer = cost centre stack
37 Retainer scheme 3 (RETAINER_SCHEME_CC) : retainer = cost centre
40 // #define RETAINER_SCHEME_INFO
41 #define RETAINER_SCHEME_CCS
42 // #define RETAINER_SCHEME_CC
44 #ifdef RETAINER_SCHEME_INFO
46 typedef struct _StgInfoTable *retainer;
49 #ifdef RETAINER_SCHEME_CCS
50 typedef CostCentreStack *retainer;
53 #ifdef RETAINER_SCHEME_CC
54 typedef CostCentre *retainer;
58 Type 'retainerSet' defines an abstract datatype for sets of retainers.
61 A retainer set stores its elements in increasing order (in element[] array).
64 typedef struct _RetainerSet {
65 nat num; // number of elements
66 StgWord hashKey; // hash key for this retainer set
67 struct _RetainerSet *link; // link to the next retainer set in the bucket
68 int id; // unique id of this retainer set (used when printing)
69 // Its absolute value is interpreted as its true id; if id is
70 // negative, it indicates that this retainer set has had a postive
71 // cost after some retainer profiling.
72 retainer element[0]; // elements of this retainer set
73 // do not put anything below here!
78 There are two ways of maintaining all retainer sets. The first is simply by
79 freeing all the retainer sets and re-initialize the hash table at each
80 retainer profiling. The second is by setting the cost field of each
81 retainer set. The second is preferred to the first if most retainer sets
82 are likely to be observed again during the next retainer profiling. Note
83 that in the first approach, we do not free the memory allocated for
84 retainer sets; we just invalidate all retainer sets.
87 // In thise case, FIRST_APPROACH must be turned on because the memory pool
88 // for retainer sets is freed each time.
89 #define FIRST_APPROACH
91 // #define FIRST_APPROACH
92 #define SECOND_APPROACH
95 // Creates the first pool and initializes a hash table. Frees all pools if any.
96 void initializeAllRetainerSet(void);
98 // Refreshes all pools for reuse and initializes a hash table.
99 void refreshAllRetainerSet(void);
102 void closeAllRetainerSet(void);
104 // Finds or creates if needed a singleton retainer set.
105 RetainerSet *singleton(retainer r);
107 extern RetainerSet rs_MANY;
109 // Checks if a given retainer is a memeber of the retainer set.
111 // Note & (maybe) Todo:
112 // This function needs to be declared as an inline function, so it is declared
113 // as an inline static function here.
114 // This make the interface really bad, but isMember() returns a value, so
115 // it is not easy either to write it as a macro (due to my lack of C
116 // programming experience). Sungwoo
118 // rtsBool isMember(retainer, retainerSet *);
120 Returns rtsTrue if r is a member of *rs.
124 The efficiency of this function is subject to the typical size of
125 retainer sets. If it is small, linear scan is better. If it
126 is large in most cases, binary scan is better.
127 The current implementation mixes the two search strategies.
130 #define BINARY_SEARCH_THRESHOLD 8
131 static inline rtsBool
132 isMember(retainer r, RetainerSet *rs)
134 int i, left, right; // must be int, not nat (because -1 can appear)
137 if (rs == &rs_MANY) { return rtsTrue; }
139 if (rs->num < BINARY_SEARCH_THRESHOLD) {
140 for (i = 0; i < (int)rs->num; i++) {
142 if (r == ri) return rtsTrue;
143 else if (r < ri) return rtsFalse;
148 while (left <= right) {
149 i = (left + right) / 2;
151 if (r == ri) return rtsTrue;
152 else if (r < ri) right = i - 1;
159 // Finds or creates a retainer set augmented with a new retainer.
160 RetainerSet *addElement(retainer, RetainerSet *);
162 // Call f() for each retainer set.
163 void traverseAllRetainerSet(void (*f)(RetainerSet *));
165 #ifdef SECOND_APPROACH
166 // Prints a single retainer set.
167 void printRetainerSetShort(FILE *, RetainerSet *);
170 // Print the statistics on all the retainer sets.
171 // store the sum of all costs and the number of all retainer sets.
172 void outputRetainerSet(FILE *, nat *, nat *);
174 #ifdef SECOND_APPROACH
175 // Print all retainer sets at the exit of the program.
176 void outputAllRetainerSet(FILE *);
182 Once either initializeAllRetainerSet() or refreshAllRetainerSet()
183 is called, there exists only one copy of any retainer set created
184 through singleton() and addElement(). The pool (the storage for
185 retainer sets) is consumed linearly. All the retainer sets of the
186 same hash function value are linked together from an element in
187 hashTable[]. See the invariants of allocateInPool() for the
188 maximum size of retainer sets. The hashing function is defined by
189 hashKeySingleton() and hashKeyAddElement(). The hash key for a set
190 must be unique regardless of the order its elements are inserted,
191 i.e., the hashing function must be additive(?).
193 #define hashKeySingleton(r) ((StgWord)(r))
194 #define hashKeyAddElement(r, s) (hashKeySingleton((r)) + (s)->hashKey)
196 // Prints the full information on a given retainer.
197 // Note: This function is not part of retainerSet interface, but this is
198 // the best place to define it.
199 void printRetainer(FILE *, retainer);
202 #endif // RETAINERSET_H