[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / runtime / profiling / Indexing.lc
1 Only have cost centres etc if @USE_COST_CENTRES@ defined
2
3 \begin{code}
4 #define NULL_REG_MAP    /* Not threaded */
5 #include "../storage/SMinternal.h"  /* for xmalloc */
6 #if defined (USE_COST_CENTRES)
7 \end{code}
8
9 %************************************************************************
10 %*                                                                      *
11 \subsection[indexing]{Indexing Cost Centres and Closure Categories}
12 %*                                                                      *
13 %************************************************************************
14
15 See \tr{CostCentre.lh} for an overview.
16
17 \begin{code}
18
19 CostCentre *index_cc_table = 0;
20 hash_t max_cc_no = DEFAULT_MAX_CC;
21
22 static hash_t index_cc_no = 0;
23 static hash_t mask_cc;
24
25 hash_t
26 init_index_cc()
27 {
28     hash_t max2 = 1, count;
29
30     if (index_cc_table) {
31         if (max_cc_no != mask_cc + 1) {
32             fprintf(stderr, "init_index_cc: twice %ld %ld\n", max_cc_no, mask_cc + 1);
33             abort();
34         }
35         return mask_cc + 1;
36     }
37
38     while (max2 < max_cc_no) max2 <<= 1;
39
40     max_cc_no = max2;
41     mask_cc = max2 - 1;
42
43     index_cc_table = (CostCentre *) xmalloc(max2 * sizeof(CostCentre));
44     for (count = 0; count < max2; count++) index_cc_table[count] = 0;
45
46     return max2;
47 }
48
49 hash_t index_cc(cc)
50     CostCentre cc;
51 {
52     if (cc->index_val == UNHASHED) {
53
54         hash_t h = hash_fixed((char *)&cc, sizeof(CostCentre)) & mask_cc;
55         while (index_cc_table[h])
56             h = (h + 1) & mask_cc;
57
58         cc->index_val = h;
59         index_cc_table[h] = cc; 
60
61         if (++index_cc_no > mask_cc - (mask_cc >> 6)) {
62             fprintf(stderr, "Cost Centre hash table full: %ld entries (in %ld table)\n",
63                     index_cc_no, mask_cc+1);
64             fprintf(stderr, "   +RTS -z%c<size> option will increase the hash table size\n", CCchar);
65             EXIT(EXIT_FAILURE);
66         }
67     }
68     return cc->index_val;
69 }
70 \end{code}
71
72 \begin{code}
73
74 CostCentre *index_mod_table = 0;
75 hash_t max_mod_no = DEFAULT_MAX_MOD;
76
77 static hash_t index_mod_no = 0;
78 static hash_t mask_mod;
79
80 hash_t
81 init_index_mod()
82 {
83     hash_t max2 = 1, count;
84
85     if (index_mod_table) {
86         if (max_mod_no != mask_mod + 1) {
87             fprintf(stderr, "init_index_mod: twice %ld %ld\n", max_mod_no, mask_mod + 1);
88             abort();
89         }
90         return mask_mod + 1;
91     }
92
93     while (max2 < max_mod_no) max2 <<= 1;
94
95     max_mod_no = max2;
96     mask_mod = max2 - 1;
97
98     index_mod_table = (CostCentre *) xmalloc(max2 * sizeof(CostCentre));
99     for (count = 0; count < max2; count++) index_mod_table[count] = 0;
100
101     return max2;
102 }
103
104 hash_t index_mod(cc)
105     CostCentre cc;
106 {
107     if (cc->index_val == UNHASHED) {
108
109         hash_t h = hash_str(cc->module) & mask_mod;
110
111         while (index_mod_table[h] && (strcmp(index_mod_table[h]->module, cc->module) != 0))
112             h = (h + 1) & mask_mod;
113
114         cc->index_val = h;
115         index_mod_table[h] = cc;  /* may replace existing cc at h index */
116
117         if (++index_mod_no > mask_mod - (mask_mod >> 6)) {
118             fprintf(stderr, "Module hash table full: %ld entries (in %ld table)\n",
119                     index_mod_no, mask_mod+1);
120             fprintf(stderr, "   +RTS -z%c<size> option will increase the hash table size\n", MODchar);
121             EXIT(EXIT_FAILURE);
122         }
123     }
124     return cc->index_val;
125 }
126 \end{code}
127
128
129 \begin{code}
130
131 CostCentre *index_grp_table = 0;
132 hash_t max_grp_no = DEFAULT_MAX_GRP;
133
134 static hash_t index_grp_no = 0;
135 static hash_t mask_grp;
136
137 hash_t
138 init_index_grp()
139 {
140     hash_t max2 = 1, count;
141
142     if (index_grp_table) {
143         if (max_grp_no != mask_grp + 1) {
144             fprintf(stderr, "init_index_grp: twice %ld %ld\n", max_grp_no, mask_grp + 1);
145             abort();
146         }
147         return mask_grp + 1;
148     }
149
150     while (max2 < max_grp_no) max2 <<= 1;
151
152     max_grp_no = max2;
153     mask_grp = max2 - 1;
154
155     index_grp_table = (CostCentre *) xmalloc(max2 * sizeof(CostCentre));
156     for (count = 0; count < max2; count++) index_grp_table[count] = 0;
157
158     return max2;
159 }
160
161 hash_t index_grp(cc)
162     CostCentre cc;
163 {
164     if (cc->index_val == UNHASHED) {
165
166         hash_t h = hash_str(cc->group) & mask_grp;
167
168         while (index_grp_table[h] && (strcmp(index_grp_table[h]->group, cc->group) != 0))
169             h = (h + 1) & mask_grp;
170
171         cc->index_val = h;
172         index_grp_table[h] = cc;  /* may replace existing cc at h index */
173
174         if (++index_grp_no > mask_grp - (mask_grp >> 6)) {
175             fprintf(stderr, "Group hash table full: %ld entries (in %ld table)\n",
176                     index_grp_no, mask_grp+1);
177             fprintf(stderr, "   +RTS -z%c<size> option will increase the hash table size\n", GRPchar);
178             EXIT(EXIT_FAILURE);
179         }
180     }
181     return cc->index_val;
182 }
183 \end{code}
184
185
186 \begin{code}
187
188 ClCategory *index_descr_table = 0;
189 hash_t max_descr_no = DEFAULT_MAX_DESCR;
190
191 static hash_t index_descr_no = 0;
192 static hash_t mask_descr;
193
194 hash_t
195 init_index_descr()
196 {
197     hash_t max2 = 1, count;
198
199     if (index_descr_table) {
200         if (max_descr_no != mask_descr + 1) {
201             fprintf(stderr, "init_index_descr: twice %ld %ld\n", max_descr_no, mask_descr + 1);
202             abort();
203         }
204         return mask_descr + 1;
205     }
206
207     while (max2 < max_descr_no) max2 <<= 1;
208            
209     max_descr_no = max2;
210     mask_descr = max2 - 1;
211
212     index_descr_table = (ClCategory *) xmalloc(max2 * sizeof(ClCategory));
213     for (count = 0; count < max2; count++) index_descr_table[count] = 0;
214
215     return max2;
216 }
217
218 hash_t index_descr(clcat)
219     ClCategory clcat;
220 {
221     if (clcat->index_val == UNHASHED) {
222
223         hash_t h = hash_str(clcat->descr) & mask_descr;
224
225         while (index_descr_table[h] && (strcmp(index_descr_table[h]->descr, clcat->descr) != 0))
226             h = (h + 1) & mask_descr;
227
228         clcat->index_val = h;
229         index_descr_table[h] = clcat;  /* may replace existing clcat at h index */
230
231         if (++index_descr_no > mask_descr - (mask_descr >> 6)) {
232             fprintf(stderr, "Closure Description hash table full: %ld entries (in %ld table)\n",
233                     index_descr_no, mask_descr+1);
234             fprintf(stderr, "   +RTS -z%c<size> option will increase the hash table size\n", DESCRchar);
235             EXIT(EXIT_FAILURE);
236         }
237     }
238     return clcat->index_val;
239 }
240 \end{code}
241
242
243 \begin{code}
244
245 ClCategory *index_type_table = 0;
246 hash_t max_type_no = DEFAULT_MAX_TYPE;
247
248 static hash_t index_type_no = 0;
249 static hash_t mask_type;
250
251 hash_t
252 init_index_type()
253 {
254     hash_t max2 = 1, count;
255
256     if (index_type_table) {
257         if (max_type_no != mask_type + 1) {
258             fprintf(stderr, "init_index_type: twice %ld %ld\n", max_type_no, mask_type + 1);
259             abort();
260         }
261         return mask_type + 1;
262     }
263
264     while (max2 < max_type_no) max2 <<= 1;
265            
266     max_type_no = max2;
267     mask_type = max2 - 1;
268
269     index_type_table = (ClCategory *) xmalloc(max2 * sizeof(ClCategory));
270     for (count = 0; count < max2; count++) index_type_table[count] = 0;
271
272     return max2;
273 }
274
275 hash_t index_type(clcat)
276     ClCategory clcat;
277 {
278     if (clcat->index_val == UNHASHED) {
279
280         hash_t h = hash_str(clcat->type) & mask_type;
281
282         while (index_type_table[h] && (strcmp(index_type_table[h]->type, clcat->type) != 0))
283             h = (h + 1) & mask_type;
284
285         clcat->index_val = h;
286         index_type_table[h] = clcat;  /* may replace existing clcat at h index */
287
288         if (++index_type_no > mask_type - (mask_type >> 6)) {
289             fprintf(stderr, "Type Description hash table full: %ld entries (in %ld table)\n",
290                     index_type_no, mask_type+1);
291             fprintf(stderr, "   +RTS -z%c<size> option will increase the hash table size\n", TYPEchar);
292             EXIT(EXIT_FAILURE);
293         }
294     }
295     return clcat->index_val;
296 }
297 \end{code}
298
299 \begin{code}
300 #endif /* USE_COST_CENTRES */
301 \end{code}