summaryrefslogtreecommitdiff
path: root/runtime/libtrace/tracelib.c
blob: 5b4c7f68319b2e1c25ae6d9302bda1ccd999f3b1 (plain)
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
/*===-- tracelib.c - Runtime routines for tracing ---------------*- C++ -*-===*
 *
 * Runtime routines for supporting tracing of execution for code generated by
 * LLVM.
 *
 *===----------------------------------------------------------------------===*/

#include "tracelib.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Support/DataTypes.h"

/*===---------------------------------------------------------------------=====
 * HASH FUNCTIONS
 *===---------------------------------------------------------------------===*/

/* use #defines until we have inlining */
typedef uintptr_t Index;                 /* type of keys, size for hash table */
typedef uint32_t  Generic;               /* type of values stored in table */ 

/* Index IntegerHashFunc(const Generic value, const Index size) */
#define IntegerHashFunc(value, size) \
  ( ((((Index) value) << 3) ^ (((Index) value) >> 3)) % size )

/* Index IntegerRehashFunc(const Generic oldHashValue, const Index size) */
#define IntegerRehashFunc(oldHashValue, size) \
  ((Index) ((oldHashValue+16) % size)) /* 16 is relatively prime to a Mersenne prime! */

/* Index PointerHashFunc(const void* value, const Index size) */
#define PointerHashFunc(value, size) \
  IntegerHashFunc((Index) value, size)

/* Index PointerRehashFunc(const void* value, const Index size) */
#define PointerRehashFunc(value, size) \
  IntegerRehashFunc((Index) value, size)

/*===---------------------------------------------------------------------=====
 * POINTER-TO-GENERIC HASH TABLE.
 * These should be moved to a separate location: HashTable.[ch]
 *===---------------------------------------------------------------------===*/

typedef enum { FIND, ENTER } ACTION;
typedef char FULLEMPTY;
const FULLEMPTY EMPTY = '\0';
const FULLEMPTY FULL  = '\1';

// List of primes closest to powers of 2 in [2^20 -- 2^30], obtained from
// http://www.utm.edu/research/primes/lists/2small/0bit.html.
// Use these as the successive sizes of the hash table.
#define NUMPRIMES 11
#define FIRSTENTRY 2
const uint PRIMES[NUMPRIMES] = { (1<<20)-3,  (1<<21)-9,  (1<<22)-3, (1<<23)-15,
                                 (1<<24)-3,  (1<<25)-39, (1<<26)-5, (1<<27)-39,
                                 (1<<28)-57, (1<<29)-3,  (1<<30)-35 };
uint CurrentSizeEntry = FIRSTENTRY;

const uint MAX_NUM_PROBES = 4;

typedef struct PtrValueHashEntry_struct {
  void*   key;
  Generic value;
} PtrValueHashEntry;

typedef struct PtrValueHashTable_struct {
  PtrValueHashEntry* table;
  FULLEMPTY* fullEmptyFlags;
  Index capacity;
  Index size;
} PtrValueHashTable;


static Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr,
                                 ACTION action, Generic value);

static void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);

static void Delete(PtrValueHashTable* ptrTable, void* ptr);

/* Returns 0 if the item is not found. */
/* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
#define LookupPtr(ptrTable, ptr) \
  LookupOrInsertPtr(ptrTable, ptr, FIND, (Generic) 0)

void
InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
{
  ptrTable->table = (PtrValueHashEntry*) calloc(newSize,
                                                sizeof(PtrValueHashEntry));
  ptrTable->fullEmptyFlags = (FULLEMPTY*) calloc(newSize, sizeof(FULLEMPTY));
  ptrTable->capacity = newSize;
  ptrTable->size = 0;
}

PtrValueHashTable*
CreateTable(Index initialSize)
{
  PtrValueHashTable* ptrTable =
    (PtrValueHashTable*) malloc(sizeof(PtrValueHashTable));
  InitializeTable(ptrTable, initialSize);
  return ptrTable;
}

void
ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
{
  if (newSize <= ptrTable->capacity)
    return;

#ifndef NDEBUG
  printf("\n***\n*** REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
  printf("*** oldSize = %ld, oldCapacity = %ld\n***\n\n",
         (long) ptrTable->size, (long) ptrTable->capacity); 
#endif

  unsigned int i;
  PtrValueHashEntry* oldTable = ptrTable->table;
  FULLEMPTY* oldFlags = ptrTable->fullEmptyFlags; 
  Index oldSize = ptrTable->size;
  Index oldCapacity = ptrTable->capacity;
  
  /* allocate the new storage and flags and re-insert the old entries */
  InitializeTable(ptrTable, newSize);
  for (i=0; i < oldCapacity; ++i)
    if (oldFlags[i] == FULL)
      Insert(ptrTable, oldTable[i].key, oldTable[i].value);

  assert(ptrTable->size == oldSize && "Incorrect number of entries copied?");

#ifndef NDEBUG
  for (i=0; i < oldCapacity; ++i)
    if (oldFlags[i] == FULL)
      assert(LookupPtr(ptrTable, oldTable[i].key) == oldTable[i].value);
#endif
  
  free(oldTable);
  free(oldFlags);
}

void
DeleteTable(PtrValueHashTable* ptrTable)
{
  free(ptrTable->table);
  free(ptrTable->fullEmptyFlags);
  memset(ptrTable, '\0', sizeof(PtrValueHashTable));
  free(ptrTable);
}

void
InsertAtIndex(PtrValueHashTable* ptrTable, void* ptr, Generic value, Index index)
{
  assert(ptrTable->fullEmptyFlags[index] == EMPTY && "Slot is in use!");
  ptrTable->table[index].key = ptr; 
  ptrTable->table[index].value = value; 
  ptrTable->fullEmptyFlags[index] = FULL;
  ptrTable->size++;
}

void
DeleteAtIndex(PtrValueHashTable* ptrTable, Index index)
{
  assert(ptrTable->fullEmptyFlags[index] == FULL && "Deleting empty slot!");
  ptrTable->table[index].key = 0; 
  ptrTable->table[index].value = (Generic) 0; 
  ptrTable->fullEmptyFlags[index] = EMPTY;
  ptrTable->size--;
}

Index
FindIndex(PtrValueHashTable* ptrTable, void* ptr)
{
  uint numProbes = 1;
  Index index = PointerHashFunc(ptr, ptrTable->capacity);
  if (ptrTable->fullEmptyFlags[index] == FULL)
    {
      if (ptrTable->table[index].key == ptr)
        return index;
      
      /* First lookup failed on non-empty slot: probe further */
      while (numProbes < MAX_NUM_PROBES)
        {
          index = PointerRehashFunc(index, ptrTable->capacity);
          if (ptrTable->fullEmptyFlags[index] == EMPTY)
            break;
          else if (ptrTable->table[index].key == ptr)
            return index;
          ++numProbes;
        }
    }
  
  /* Lookup failed: item is not in the table. */
  /* If last slot is empty, use that slot. */
  /* Otherwise, table must have been reallocated, so search again. */
  
  if (numProbes == MAX_NUM_PROBES)
    { /* table is too full: reallocate and search again */
      if (CurrentSizeEntry >= NUMPRIMES-1) {
        fprintf(stderr, "Out of PRIME Numbers!!!");
        abort();
      }
      ReallocTable(ptrTable, PRIMES[++CurrentSizeEntry]);
      return FindIndex(ptrTable, ptr);
    }
  else
    {
      assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
             "Stopped before finding an empty slot and before MAX probes!");
      return index;
    }
}

/* Look up hash table using 'ptr' as the key.  If an entry exists, return
 * the value mapped to 'ptr'.  If not, and if action==ENTER is specified,
 * create a new entry with value 'value', but return 0 in any case.
 */
Generic
LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action,
                  Generic value)
{
  Index index = FindIndex(ptrTable, ptr);
  if (ptrTable->fullEmptyFlags[index] == FULL &&
      ptrTable->table[index].key == ptr)
    return ptrTable->table[index].value;
  
  /* Lookup failed: item is not in the table */
  /* If action is ENTER, insert item into the table.  Return 0 in any case. */ 
  if (action == ENTER)
    InsertAtIndex(ptrTable, ptr, value, index);

  return (Generic) 0;
}

void
Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value)
{
  Index index = FindIndex(ptrTable, ptr);
  assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
         "ptr is already in the table: delete it first!");
  InsertAtIndex(ptrTable, ptr, value, index);
}

void
Delete(PtrValueHashTable* ptrTable, void* ptr)
{
  Index index = FindIndex(ptrTable, ptr);
  if (ptrTable->fullEmptyFlags[index] == FULL &&
      ptrTable->table[index].key == ptr)
    {
      DeleteAtIndex(ptrTable, index);
    }
}

/*===---------------------------------------------------------------------=====
 * RUNTIME ROUTINES TO MAP POINTERS TO SEQUENCE NUMBERS
 *===---------------------------------------------------------------------===*/

PtrValueHashTable* SequenceNumberTable = NULL;
#define INITIAL_SIZE (PRIMES[FIRSTENTRY])

#define MAX_NUM_SAVED 1024

typedef struct PointerSet_struct {
  char* savedPointers[MAX_NUM_SAVED];   /* 1024 alloca'd ptrs shd suffice */
  unsigned int numSaved;
  struct PointerSet_struct* nextOnStack;     /* implement a cheap stack */ 
} PointerSet;

PointerSet* topOfStack = NULL;

SequenceNumber
HashPointerToSeqNum(char* ptr)
{
  static SequenceNumber count = 0;
  SequenceNumber seqnum;
  if (SequenceNumberTable == NULL) {
    assert(MAX_NUM_PROBES < INITIAL_SIZE+1 && "Initial size too small");
    SequenceNumberTable = CreateTable(INITIAL_SIZE);
  }
  seqnum = (SequenceNumber)
    LookupOrInsertPtr(SequenceNumberTable, ptr, ENTER, count+1);

  if (seqnum == 0)    /* new entry was created with value count+1 */
    seqnum = ++count;    /* so increment counter */

  assert(seqnum <= count && "Invalid sequence number in table!");
  return seqnum;
}

void
ReleasePointerSeqNum(char* ptr)
{ /* if a sequence number was assigned to this ptr, release it */
  if (SequenceNumberTable != NULL)
    Delete(SequenceNumberTable, ptr);
}

void
PushPointerSet()
{
  PointerSet* newSet = (PointerSet*) malloc(sizeof(PointerSet));
  newSet->numSaved = 0;
  newSet->nextOnStack = topOfStack;
  topOfStack = newSet;
}

void
PopPointerSet()
{
  PointerSet* oldSet;
  assert(topOfStack != NULL && "popping from empty stack!");
  oldSet = topOfStack; 
  topOfStack = oldSet->nextOnStack;
  assert(oldSet->numSaved == 0);
  free(oldSet);
}

/* free the pointers! */
static void
ReleaseRecordedPointers(char* savedPointers[MAX_NUM_SAVED],
                        unsigned int numSaved)
{
  unsigned int i;
  for (i=0; i < topOfStack->numSaved; ++i)
    ReleasePointerSeqNum(topOfStack->savedPointers[i]);  
}

void
ReleasePointersPopSet()
{
  ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
  topOfStack->numSaved = 0;
  PopPointerSet();
}

void
RecordPointer(char* ptr)
{ /* record pointers for release later */
  if (topOfStack->numSaved == MAX_NUM_SAVED) {
    printf("***\n*** WARNING: OUT OF ROOM FOR SAVED POINTERS."
           " ALL POINTERS ARE BEING FREED.\n"
           "*** THE SEQUENCE NUMBERS OF SAVED POINTERS WILL CHANGE!\n*** \n");
    ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
    topOfStack->numSaved = 0;
  }
  topOfStack->savedPointers[topOfStack->numSaved++] = ptr;
}

/*===---------------------------------------------------------------------=====
 * TEST DRIVER FOR INSTRUMENTATION LIBRARY
 *===---------------------------------------------------------------------===*/

#ifndef TEST_INSTRLIB
#undef  TEST_INSTRLIB /* #define this to turn on by default */
#endif

#ifdef TEST_INSTRLIB
int
main(int argc, char** argv)
{
  int i, j;
  int doRelease = 0;

  INITIAL_SIZE = 5; /* start with small table to test realloc's*/
  
  if (argc > 1 && ! strcmp(argv[1], "-r"))
    {
      PushPointerSet();
      doRelease = 1;
    }
  
  for (i=0; i < argc; ++i)
    for (j=0; argv[i][j]; ++j)
      {
        printf("Sequence number for argc[%d][%d] (%c) = Hash(%p) = %d\n",
               i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
        
        if (doRelease)
          RecordPointer(argv[i]+j);
      }
  
  if (doRelease)
    ReleasePointersPopSet();     
  
  /* print sequence numbers out again to compare with (-r) and w/o release */
  for (i=argc-1; i >= 0; --i)
    for (j=0; argv[i][j]; ++j)
      printf("Sequence number for argc[%d][%d] (%c) = %d\n",
             i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
  
  return 0;
}
#endif