blob: 35c9ccc9ca3e84e83d7b3815468bae345c0272b5 [file] [log] [blame]
Vikram S. Adve2df1f742002-05-19 15:49:58 +00001/*===-- Libraries/tracelib.c - Runtime routines for tracing -----*- C++ -*--===
2 *
3 * Runtime routines for supporting tracing of execution
4 * for code generated by LLVM.
5 *
6 *===---------------------------------------------------------------------===*/
7
8#include "tracelib.h"
9#include <assert.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13
14
15/*===---------------------------------------------------------------------=====
16 * HASH FUNCTIONS
17 *===---------------------------------------------------------------------===*/
18
19/* use #defines until we have inlining */
20
21typedef int64_t Generic;
22typedef uint64_t Index;
23typedef ptrdiff_t Pointer;
24
25/* Index IntegerHashFunc(const Generic value, const Index size) */
26#define IntegerHashFunc(value, size) \
27 (value % size)
28
29/* Index IntegerRehashFunc(const Generic oldHashValue, const Index size) */
30#define IntegerRehashFunc(oldHashValue, size) \
31 ((oldHashValue+16) % size) /* 16 is relatively prime to a Mersenne prime! */
32
33/* Index PointerHashFunc(const void* value, const Index size) */
34#define PointerHashFunc(value, size) \
35 IntegerHashFunc((Pointer) value, size)
36
37/* Index PointerRehashFunc(const void* value, const Index size) */
38#define PointerRehashFunc(value, size) \
39 IntegerRehashFunc((Pointer) value, size)
40
41
42/*===---------------------------------------------------------------------=====
43 * POINTER-TO-GENERIC HASH TABLE.
44 * These should be moved to a separate location: HashTable.[ch]
45 *===---------------------------------------------------------------------===*/
46
47typedef enum { FIND, ENTER } ACTION;
48typedef char FULLEMPTY;
49const FULLEMPTY EMPTY = '\0';
50const FULLEMPTY FULL = '\1';
51
52const uint MAX_NUM_PROBES = 4;
53
54typedef struct PtrValueHashEntry_struct {
55 void* key;
56 Generic value;
57} PtrValueHashEntry;
58
59typedef struct PtrValueHashTable_struct {
60 PtrValueHashEntry* table;
61 FULLEMPTY* fullEmptyFlags;
62 Index capacity;
63 Index size;
64} PtrValueHashTable;
65
66
67extern Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable,
68 void* ptr, ACTION action);
69
70extern void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
71
72extern void Delete(PtrValueHashTable* ptrTable, void* ptr);
73
74
75void
76InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
77{
78 ptrTable->table = (PtrValueHashEntry*) malloc(newSize *
79 sizeof(PtrValueHashEntry));
80 ptrTable->fullEmptyFlags = (FULLEMPTY*) malloc(newSize * sizeof(FULLEMPTY));
81 memset(ptrTable->fullEmptyFlags, '\0', newSize * sizeof(FULLEMPTY));
82 ptrTable->capacity = newSize;
83 ptrTable->size = 0;
84}
85
86PtrValueHashTable*
87CreateTable(Index initialSize)
88{
89 PtrValueHashTable* ptrTable =
90 (PtrValueHashTable*) malloc(sizeof(PtrValueHashTable));
91 InitializeTable(ptrTable, initialSize);
92 return ptrTable;
93}
94
95void
96ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
97{
98 if (newSize > ptrTable->capacity)
99 {
100 unsigned int i;
101
102 PtrValueHashEntry* oldTable = ptrTable->table;
103 FULLEMPTY* oldFlags = ptrTable->fullEmptyFlags;
104 Index oldSize = ptrTable->size;
105
106 /* allocate the new storage and flags and re-insert the old entries */
107 InitializeTable(ptrTable, newSize);
108 for (i=0; i < oldSize; ++i)
109 Insert(ptrTable, oldTable[i].key, oldTable[i].value);
110 assert(ptrTable->size == oldSize);
111
112 free(oldTable);
113 free(oldFlags);
114 }
115}
116
117void
118DeleteTable(PtrValueHashTable* ptrTable)
119{
120 free(ptrTable->table);
121 free(ptrTable->fullEmptyFlags);
122 memset(ptrTable, '\0', sizeof(PtrValueHashTable));
123 free(ptrTable);
124}
125
126void
127InsertAtIndex(PtrValueHashTable* ptrTable, void* ptr, Generic value, Index index)
128{
129 assert(ptrTable->fullEmptyFlags[index] == EMPTY && "Slot is in use!");
130 ptrTable->table[index].key = ptr;
131 ptrTable->table[index].value = value;
132 ptrTable->fullEmptyFlags[index] = FULL;
133 ptrTable->size++;
134}
135
136void
137DeleteAtIndex(PtrValueHashTable* ptrTable, Index index)
138{
139 assert(ptrTable->fullEmptyFlags[index] == FULL && "Deleting empty slot!");
140 ptrTable->table[index].key = NULL;
141 ptrTable->table[index].value = (Generic) NULL;
142 ptrTable->fullEmptyFlags[index] = EMPTY;
143 ptrTable->size--;
144}
145
146Index
147FindIndex(PtrValueHashTable* ptrTable, void* ptr)
148{
149 uint numProbes = 1;
150 Index index = PointerHashFunc(ptr, ptrTable->capacity);
151 if (ptrTable->fullEmptyFlags[index] == FULL)
152 {
153 if (ptrTable->table[index].key == ptr)
154 return index;
155
156 /* First lookup failed on non-empty slot: probe further */
157 while (numProbes < MAX_NUM_PROBES)
158 {
159 index = PointerRehashFunc(index, ptrTable->capacity);
160 if (ptrTable->fullEmptyFlags[index] == EMPTY)
161 break;
162 else if (ptrTable->table[index].key == ptr)
163 return index;
164 ++numProbes;
165 }
166 }
167
168 /* Lookup failed: item is not in the table. */
169 /* If last slot is empty, use that slot. */
170 /* Otherwise, table must have been reallocated, so search again. */
171
172 if (numProbes == MAX_NUM_PROBES)
173 { /* table is too full: reallocate and search again */
174 ReallocTable(ptrTable, 1 + 2*ptrTable->capacity);
175 return FindIndex(ptrTable, ptr);
176 }
177 else
178 {
179 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
180 "Stopped before finding an empty slot and before MAX probes!");
181 return index;
182 }
183}
184
185Generic
186LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action)
187{
188 Index index = FindIndex(ptrTable, ptr);
189 if (ptrTable->fullEmptyFlags[index] == FULL &&
190 ptrTable->table[index].key == ptr)
191 return ptrTable->table[index].value;
192
193 /* Lookup failed: item is not in the table */
194 if (action != ENTER)
195 return (Generic) NULL;
196
197 /* Insert item into the table and return its index */
198 InsertAtIndex(ptrTable, ptr, (Generic) NULL, index);
199 return (Generic) NULL;
200}
201
202/* Returns NULL if the item is not found. */
203/* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
204#define LookupPtr(ptrTable, ptr) \
205 LookupOrInsertPtr(ptrTable, ptr, FIND)
206
207void
208Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value)
209{
210 Index index = FindIndex(ptrTable, ptr);
211 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
212 "ptr is already in the table: delete it first!");
213 InsertAtIndex(ptrTable, ptr, value, index);
214}
215
216void
217Delete(PtrValueHashTable* ptrTable, void* ptr)
218{
219 Index index = FindIndex(ptrTable, ptr);
220 if (ptrTable->fullEmptyFlags[index] == FULL &&
221 ptrTable->table[index].key == ptr)
222 {
223 DeleteAtIndex(ptrTable, index);
224 }
225}
226
227/*===---------------------------------------------------------------------=====
228 * RUNTIME ROUTINES TO MAP POINTERS TO SEQUENCE NUMBERS
229 *===---------------------------------------------------------------------===*/
230
231PtrValueHashTable* SequenceNumberTable = NULL;
232Index INITIAL_SIZE = 1 << 18;
233
234#define MAX_NUM_SAVED 1024
235
236typedef struct PointerSet_struct {
237 char* savedPointers[MAX_NUM_SAVED]; /* 1024 alloca'd ptrs shd suffice */
238 unsigned int numSaved;
239 struct PointerSet_struct* nextOnStack; /* implement a cheap stack */
240} PointerSet;
241
242PointerSet* topOfStack = NULL;
243
244SequenceNumber
245HashPointerToSeqNum(char* ptr)
246{
247 static SequenceNumber count = 0;
248 SequenceNumber seqnum;
249 if (SequenceNumberTable == NULL) {
250 assert(MAX_NUM_PROBES < INITIAL_SIZE+1 && "Initial size too small");
251 SequenceNumberTable = CreateTable(INITIAL_SIZE);
252 }
253 seqnum = (SequenceNumber) LookupPtr(SequenceNumberTable, ptr);
254 if (seqnum == 0)
255 {
256 Insert(SequenceNumberTable, ptr, ++count);
257 seqnum = count;
258 }
259 return seqnum;
260}
261
262void
263ReleasePointerSeqNum(char* ptr)
264{ /* if a sequence number was assigned to this ptr, release it */
265 SequenceNumber seqnum;
266 if (SequenceNumberTable != NULL)
267 Delete(SequenceNumberTable, ptr);
268}
269
270void
271PushPointerSet()
272{
273 PointerSet* newSet = (PointerSet*) malloc(sizeof(PointerSet));
274 newSet->numSaved = 0;
275 newSet->nextOnStack = topOfStack;
276 topOfStack = newSet;
277}
278
279void
280PopPointerSet()
281{
282 PointerSet* oldSet;
283 assert(topOfStack != NULL && "popping from empty stack!");
284 oldSet = topOfStack;
285 topOfStack = oldSet->nextOnStack;
286 assert(oldSet->numSaved == 0);
287 free(oldSet);
288}
289
290/* free the pointers! */
291static void
292ReleaseRecordedPointers(char* savedPointers[MAX_NUM_SAVED],
293 unsigned int numSaved)
294{
295 unsigned int i;
296 for (i=0; i < topOfStack->numSaved; ++i)
297 ReleasePointerSeqNum(topOfStack->savedPointers[i]);
298}
299
300void
301ReleasePointersPopSet()
302{
303 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
304 topOfStack->numSaved = 0;
305 PopPointerSet();
306}
307
308void
309RecordPointer(char* ptr)
310{ /* record pointers for release later */
311 if (topOfStack->numSaved == MAX_NUM_SAVED) {
312 printf("***\n*** WARNING: OUT OF ROOM FOR SAVED POINTERS."
313 " ALL POINTERS ARE BEING FREED.\n"
314 "*** THE SEQUENCE NUMBERS OF SAVED POINTERS WILL CHANGE!\n*** \n");
315 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
316 topOfStack->numSaved = 0;
317 }
318 topOfStack->savedPointers[topOfStack->numSaved++] = ptr;
319}
320
321/*===---------------------------------------------------------------------=====
322 * TEST DRIVER FOR INSTRUMENTATION LIBRARY
323 *===---------------------------------------------------------------------===*/
324
325#ifndef TEST_INSTRLIB
326#undef TEST_INSTRLIB /* #define this to turn on by default */
327#endif
328
329#ifdef TEST_INSTRLIB
330int
331main(int argc, char** argv)
332{
333 int i, j;
334 int doRelease = 0;
335
336 INITIAL_SIZE = 5; /* start with small table to test realloc's*/
337
338 if (argc > 1 && ! strcmp(argv[1], "-r"))
339 {
340 PushPointerSet();
341 doRelease = 1;
342 }
343
344 for (i=0; i < argc; ++i)
345 for (j=0; argv[i][j]; ++j)
346 {
347 printf("Sequence number for argc[%d][%d] (%c) = Hash(%p) = %d\n",
348 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
349
350 if (doRelease)
351 RecordPointer(argv[i]+j);
352 }
353
354 if (doRelease)
355 ReleasePointersPopSet();
356
357 /* print sequence numbers out again to compare with (-r) and w/o release */
358 for (i=argc-1; i >= 0; --i)
359 for (j=0; argv[i][j]; ++j)
360 printf("Sequence number for argc[%d][%d] (%c) = Hash(%p) = %d\n",
361 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
362
363 return 0;
364}
365#endif