summaryrefslogtreecommitdiff
path: root/runtime/libprofile/PathProfiling.c
blob: 161dfa0b24064c7335e3cb067b0b485e69add90e (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
/*===-- PathProfiling.c - Support library for path profiling --------------===*\
|*
|*                     The LLVM Compiler Infrastructure
|*
|* This file is distributed under the University of Illinois Open Source
|* License. See LICENSE.TXT for details.
|*
|*===----------------------------------------------------------------------===*|
|*
|* This file implements the call back routines for the path profiling
|* instrumentation pass.  This should be used with the -insert-path-profiling
|* LLVM pass.
|*
\*===----------------------------------------------------------------------===*/

#include "Profiling.h"
#include "llvm/Analysis/ProfileInfoTypes.h"
#include <sys/types.h>
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#else
#include <io.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

// Must use __inline in Microsoft C
#if defined(_MSC_VER)
#define inline __inline
#endif

/* note that this is used for functions with large path counts,
         but it is unlikely those paths will ALL be executed */
#define ARBITRARY_HASH_BIN_COUNT 100

typedef struct pathHashEntry_s {
  uint32_t pathNumber;
  uint32_t pathCount;
  struct pathHashEntry_s* next;
} pathHashEntry_t;

typedef struct pathHashTable_s {
  pathHashEntry_t* hashBins[ARBITRARY_HASH_BIN_COUNT];
  uint32_t pathCounts;
} pathHashTable_t;

typedef struct {
  enum ProfilingStorageType type;
  uint32_t size;
  void* array;
} ftEntry_t;

/* pointer to the function table allocated in the instrumented program */
ftEntry_t* ft;
uint32_t ftSize;

/* write an array table to file */
void writeArrayTable(uint32_t fNumber, ftEntry_t* ft, uint32_t* funcCount) {
  int outFile = getOutFile();
  uint32_t arrayHeaderLocation = 0;
  uint32_t arrayCurrentLocation = 0;
  uint32_t arrayIterator = 0;
  uint32_t functionUsed = 0;
  uint32_t pathCounts = 0;

  /* look through each entry in the array to determine whether the function
     was executed at all */
  for( arrayIterator = 0; arrayIterator < ft->size; arrayIterator++ ) {
    uint32_t pc = ((uint32_t*)ft->array)[arrayIterator];

    /* was this path executed? */
    if( pc ) {
      PathProfileTableEntry pte;
      pte.pathNumber = arrayIterator;
      pte.pathCounter = pc;
      pathCounts++;

      /* one-time initialization stuff */
      if(!functionUsed) {
        arrayHeaderLocation = lseek(outFile, 0, SEEK_CUR);
        lseek(outFile, sizeof(PathProfileHeader), SEEK_CUR);
        functionUsed = 1;
        (*funcCount)++;
      }

      /* write path data */
      if (write(outFile, &pte, sizeof(PathProfileTableEntry)) < 0) {
        fprintf(stderr, "error: unable to write path entry to output file.\n");
        return;
      }
    }
  }

  /* If this function was executed, write the header */
  if( functionUsed ) {
    PathProfileHeader fHeader;
    fHeader.fnNumber = fNumber;
    fHeader.numEntries = pathCounts;

    arrayCurrentLocation = lseek(outFile, 0, SEEK_CUR);
    lseek(outFile, arrayHeaderLocation, SEEK_SET);

    if (write(outFile, &fHeader, sizeof(PathProfileHeader)) < 0) {
      fprintf(stderr,
              "error: unable to write function header to output file.\n");
      return;
    }

    lseek(outFile, arrayCurrentLocation, SEEK_SET);
  }
}

static inline uint32_t hash (uint32_t key) {
  /* this may benefit from a proper hash function */
  return key%ARBITRARY_HASH_BIN_COUNT;
}

/* output a specific function's hash table to the profile file */
void writeHashTable(uint32_t functionNumber, pathHashTable_t* hashTable) {
  int outFile = getOutFile();
  PathProfileHeader header;
  uint32_t i;

  header.fnNumber = functionNumber;
  header.numEntries = hashTable->pathCounts;

  if (write(outFile, &header, sizeof(PathProfileHeader)) < 0) {
    fprintf(stderr, "error: unable to write function header to output file.\n");
    return;
  }

  for (i = 0; i < ARBITRARY_HASH_BIN_COUNT; i++) {
    pathHashEntry_t* hashEntry = hashTable->hashBins[i];

    while (hashEntry) {
      pathHashEntry_t* temp;

      PathProfileTableEntry pte;
      pte.pathNumber = hashEntry->pathNumber;
      pte.pathCounter = hashEntry->pathCount;

      if (write(outFile, &pte, sizeof(PathProfileTableEntry)) < 0) {
        fprintf(stderr, "error: unable to write path entry to output file.\n");
        return;
      }

      temp = hashEntry;
      hashEntry = hashEntry->next;
      free (temp);

    }
  }
}

/* Return a pointer to this path's specific path counter */
static inline uint32_t* getPathCounter(uint32_t functionNumber,
                                       uint32_t pathNumber) {
  pathHashTable_t* hashTable;
  pathHashEntry_t* hashEntry;
  uint32_t index = hash(pathNumber);

  if( ft[functionNumber-1].array == 0)
    ft[functionNumber-1].array = calloc(sizeof(pathHashTable_t), 1);

  hashTable = (pathHashTable_t*)((ftEntry_t*)ft)[functionNumber-1].array;
  hashEntry = hashTable->hashBins[index];

  while (hashEntry) {
    if (hashEntry->pathNumber == pathNumber) {
      return &hashEntry->pathCount;
    }

    hashEntry = hashEntry->next;
  }

  hashEntry = malloc(sizeof(pathHashEntry_t));
  hashEntry->pathNumber = pathNumber;
  hashEntry->pathCount = 0;
  hashEntry->next = hashTable->hashBins[index];
  hashTable->hashBins[index] = hashEntry;
  hashTable->pathCounts++;
  return &hashEntry->pathCount;
}

/* Increment a specific path's count */
void llvm_increment_path_count (uint32_t functionNumber, uint32_t pathNumber) {
  uint32_t* pathCounter = getPathCounter(functionNumber, pathNumber);
  if( *pathCounter < 0xffffffff )
    (*pathCounter)++;
}

/* Increment a specific path's count */
void llvm_decrement_path_count (uint32_t functionNumber, uint32_t pathNumber) {
  uint32_t* pathCounter = getPathCounter(functionNumber, pathNumber);
  (*pathCounter)--;
}

/*
 * Writes out a path profile given a function table, in the following format.
 *
 *
 *      | <-- 32 bits --> |
 *      +-----------------+-----------------+
 * 0x00 | profileType     | functionCount   |
 *      +-----------------+-----------------+
 * 0x08 | functionNum     | profileEntries  |  // function 1
 *      +-----------------+-----------------+
 * 0x10 | pathNumber      | pathCounter     |  // entry 1.1
 *      +-----------------+-----------------+
 * 0x18 | pathNumber      | pathCounter     |  // entry 1.2
 *      +-----------------+-----------------+
 *  ... |       ...       |       ...       |  // entry 1.n
 *      +-----------------+-----------------+
 *  ... | functionNum     | profileEntries  |  // function 2
 *      +-----------------+-----------------+
 *  ... | pathNumber      | pathCounter     |  // entry 2.1
 *      +-----------------+-----------------+
 *  ... | pathNumber      | pathCounter     |  // entry 2.2
 *      +-----------------+-----------------+
 *  ... |       ...       |       ...       |  // entry 2.n
 *      +-----------------+-----------------+
 *
 */
static void pathProfAtExitHandler(void) {
  int outFile = getOutFile();
  uint32_t i;
  uint32_t header[2] = { PathInfo, 0 };
  uint32_t headerLocation;
  uint32_t currentLocation;

  /* skip over the header for now */
  headerLocation = lseek(outFile, 0, SEEK_CUR);
  lseek(outFile, 2*sizeof(uint32_t), SEEK_CUR);

  /* Iterate through each function */
  for( i = 0; i < ftSize; i++ ) {
    if( ft[i].type == ProfilingArray ) {
      writeArrayTable(i+1,&ft[i],header + 1);

    } else if( ft[i].type == ProfilingHash ) {
      /* If the hash exists, write it to file */
      if( ft[i].array ) {
        writeHashTable(i+1,ft[i].array);
        header[1]++;
        free(ft[i].array);
      }
    }
  }

  /* Setup and write the path profile header */
  currentLocation = lseek(outFile, 0, SEEK_CUR);
  lseek(outFile, headerLocation, SEEK_SET);

  if (write(outFile, header, sizeof(header)) < 0) {
    fprintf(stderr,
            "error: unable to write path profile header to output file.\n");
    return;
  }

  lseek(outFile, currentLocation, SEEK_SET);
}
/* llvm_start_path_profiling - This is the main entry point of the path
 * profiling library.  It is responsible for setting up the atexit handler.
 */
int llvm_start_path_profiling(int argc, const char** argv,
                              void* functionTable, uint32_t numElements) {
  int Ret = save_arguments(argc, argv);
  ft = functionTable;
  ftSize = numElements;
  atexit(pathProfAtExitHandler);

  return Ret;
}