blob: 3008db9c4a6070b7a65c5f56854bdbeb9f398125 [file] [log] [blame]
Andrew Trick04317cc2011-01-29 01:09:53 +00001/*===-- PathProfiling.c - Support library for path profiling --------------===*\
2|*
3|* The LLVM Compiler Infrastructure
4|*
5|* This file is distributed under the University of Illinois Open Source
6|* License. See LICENSE.TXT for details.
7|*
8|*===----------------------------------------------------------------------===*|
9|*
10|* This file implements the call back routines for the path profiling
11|* instrumentation pass. This should be used with the -insert-path-profiling
12|* LLVM pass.
13|*
14\*===----------------------------------------------------------------------===*/
15
16#include "Profiling.h"
17#include "llvm/Analysis/ProfileInfoTypes.h"
18#include <sys/types.h>
19#include <unistd.h>
20#include <string.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <stdint.h>
24#include <stdio.h>
25
26/* note that this is used for functions with large path counts,
27 but it is unlikely those paths will ALL be executed */
28#define ARBITRARY_HASH_BIN_COUNT 100
29
30typedef struct pathHashEntry_s {
31 uint32_t pathNumber;
32 uint32_t pathCount;
33 struct pathHashEntry_s* next;
34} pathHashEntry_t;
35
36typedef struct pathHashTable_s {
37 pathHashEntry_t* hashBins[ARBITRARY_HASH_BIN_COUNT];
38 uint32_t pathCounts;
39} pathHashTable_t;
40
41typedef struct {
42 enum ProfilingStorageType type;
43 uint32_t size;
44 void* array;
45} ftEntry_t;
46
47/* pointer to the function table allocated in the instrumented program */
48ftEntry_t* ft;
49uint32_t ftSize;
50
51/* write an array table to file */
52void writeArrayTable(uint32_t fNumber, ftEntry_t* ft, uint32_t* funcCount) {
53 int outFile = getOutFile();
54 uint32_t arrayHeaderLocation = 0;
55 uint32_t arrayCurrentLocation = 0;
56 uint32_t arrayIterator = 0;
57 uint32_t functionUsed = 0;
58 uint32_t pathCounts = 0;
59
60 /* look through each entry in the array to determine whether the function
61 was executed at all */
62 for( arrayIterator = 0; arrayIterator < ft->size; arrayIterator++ ) {
63 uint32_t pc = ((uint32_t*)ft->array)[arrayIterator];
64
65 /* was this path executed? */
66 if( pc ) {
67 PathProfileTableEntry pte;
68 pte.pathNumber = arrayIterator;
69 pte.pathCounter = pc;
70 pathCounts++;
71
72 /* one-time initialization stuff */
73 if(!functionUsed) {
74 arrayHeaderLocation = lseek(outFile, 0, SEEK_CUR);
75 lseek(outFile, sizeof(PathProfileHeader), SEEK_CUR);
76 functionUsed = 1;
77 (*funcCount)++;
78 }
79
80 /* write path data */
81 if (write(outFile, &pte, sizeof(PathProfileTableEntry)) < 0) {
82 fprintf(stderr, "error: unable to write path entry to output file.\n");
83 return;
84 }
85 }
86 }
87
88 /* If this function was executed, write the header */
89 if( functionUsed ) {
90 PathProfileHeader fHeader;
91 fHeader.fnNumber = fNumber;
92 fHeader.numEntries = pathCounts;
93
94 arrayCurrentLocation = lseek(outFile, 0, SEEK_CUR);
95 lseek(outFile, arrayHeaderLocation, SEEK_SET);
96
97 if (write(outFile, &fHeader, sizeof(PathProfileHeader)) < 0) {
98 fprintf(stderr,
99 "error: unable to write function header to output file.\n");
100 return;
101 }
102
103 lseek(outFile, arrayCurrentLocation, SEEK_SET);
104 }
105}
106
Nick Lewycky3f5c8322011-04-27 03:22:17 +0000107static inline uint32_t hash (uint32_t key) {
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000108 /* this may benefit from a proper hash function */
Andrew Trick04317cc2011-01-29 01:09:53 +0000109 return key%ARBITRARY_HASH_BIN_COUNT;
110}
111
112/* output a specific function's hash table to the profile file */
113void writeHashTable(uint32_t functionNumber, pathHashTable_t* hashTable) {
114 int outFile = getOutFile();
115 PathProfileHeader header;
116 uint32_t i;
117
118 header.fnNumber = functionNumber;
119 header.numEntries = hashTable->pathCounts;
120
121 if (write(outFile, &header, sizeof(PathProfileHeader)) < 0) {
122 fprintf(stderr, "error: unable to write function header to output file.\n");
123 return;
124 }
125
126 for (i = 0; i < ARBITRARY_HASH_BIN_COUNT; i++) {
127 pathHashEntry_t* hashEntry = hashTable->hashBins[i];
128
129 while (hashEntry) {
130 pathHashEntry_t* temp;
131
132 PathProfileTableEntry pte;
133 pte.pathNumber = hashEntry->pathNumber;
134 pte.pathCounter = hashEntry->pathCount;
135
136 if (write(outFile, &pte, sizeof(PathProfileTableEntry)) < 0) {
137 fprintf(stderr, "error: unable to write path entry to output file.\n");
138 return;
139 }
140
141 temp = hashEntry;
142 hashEntry = hashEntry->next;
143 free (temp);
144
145 }
146 }
147}
148
149/* Return a pointer to this path's specific path counter */
Nick Lewycky3f5c8322011-04-27 03:22:17 +0000150static inline uint32_t* getPathCounter(uint32_t functionNumber,
151 uint32_t pathNumber) {
Andrew Trick04317cc2011-01-29 01:09:53 +0000152 pathHashTable_t* hashTable;
153 pathHashEntry_t* hashEntry;
154 uint32_t index = hash(pathNumber);
155
156 if( ft[functionNumber-1].array == 0)
157 ft[functionNumber-1].array = calloc(sizeof(pathHashTable_t), 1);
158
159 hashTable = (pathHashTable_t*)((ftEntry_t*)ft)[functionNumber-1].array;
160 hashEntry = hashTable->hashBins[index];
161
162 while (hashEntry) {
163 if (hashEntry->pathNumber == pathNumber) {
164 return &hashEntry->pathCount;
165 }
166
167 hashEntry = hashEntry->next;
168 }
169
170 hashEntry = malloc(sizeof(pathHashEntry_t));
171 hashEntry->pathNumber = pathNumber;
172 hashEntry->pathCount = 0;
173 hashEntry->next = hashTable->hashBins[index];
174 hashTable->hashBins[index] = hashEntry;
175 hashTable->pathCounts++;
176 return &hashEntry->pathCount;
177}
178
179/* Increment a specific path's count */
180void llvm_increment_path_count (uint32_t functionNumber, uint32_t pathNumber) {
181 uint32_t* pathCounter = getPathCounter(functionNumber, pathNumber);
182 if( *pathCounter < 0xffffffff )
183 (*pathCounter)++;
184}
185
186/* Increment a specific path's count */
187void llvm_decrement_path_count (uint32_t functionNumber, uint32_t pathNumber) {
188 uint32_t* pathCounter = getPathCounter(functionNumber, pathNumber);
189 (*pathCounter)--;
190}
191
192/*
193 * Writes out a path profile given a function table, in the following format.
194 *
195 *
196 * | <-- 32 bits --> |
197 * +-----------------+-----------------+
198 * 0x00 | profileType | functionCount |
199 * +-----------------+-----------------+
200 * 0x08 | functionNum | profileEntries | // function 1
201 * +-----------------+-----------------+
202 * 0x10 | pathNumber | pathCounter | // entry 1.1
203 * +-----------------+-----------------+
204 * 0x18 | pathNumber | pathCounter | // entry 1.2
205 * +-----------------+-----------------+
206 * ... | ... | ... | // entry 1.n
207 * +-----------------+-----------------+
208 * ... | functionNum | profileEntries | // function 2
209 * +-----------------+-----------------+
210 * ... | pathNumber | pathCounter | // entry 2.1
211 * +-----------------+-----------------+
212 * ... | pathNumber | pathCounter | // entry 2.2
213 * +-----------------+-----------------+
214 * ... | ... | ... | // entry 2.n
215 * +-----------------+-----------------+
216 *
217 */
218static void pathProfAtExitHandler() {
219 int outFile = getOutFile();
220 uint32_t i;
221 uint32_t header[2] = { PathInfo, 0 };
222 uint32_t headerLocation;
223 uint32_t currentLocation;
224
225 /* skip over the header for now */
226 headerLocation = lseek(outFile, 0, SEEK_CUR);
227 lseek(outFile, 2*sizeof(uint32_t), SEEK_CUR);
228
229 /* Iterate through each function */
230 for( i = 0; i < ftSize; i++ ) {
231 if( ft[i].type == ProfilingArray ) {
232 writeArrayTable(i+1,&ft[i],header + 1);
233
234 } else if( ft[i].type == ProfilingHash ) {
235 /* If the hash exists, write it to file */
236 if( ft[i].array ) {
237 writeHashTable(i+1,ft[i].array);
238 header[1]++;
239 free(ft[i].array);
240 }
241 }
242 }
243
244 /* Setup and write the path profile header */
245 currentLocation = lseek(outFile, 0, SEEK_CUR);
246 lseek(outFile, headerLocation, SEEK_SET);
247
248 if (write(outFile, header, sizeof(header)) < 0) {
249 fprintf(stderr,
250 "error: unable to write path profile header to output file.\n");
251 return;
252 }
253
254 lseek(outFile, currentLocation, SEEK_SET);
255}
256/* llvm_start_path_profiling - This is the main entry point of the path
257 * profiling library. It is responsible for setting up the atexit handler.
258 */
259int llvm_start_path_profiling(int argc, const char** argv,
260 void* functionTable, uint32_t numElements) {
261 int Ret = save_arguments(argc, argv);
262 ft = functionTable;
263 ftSize = numElements;
264 atexit(pathProfAtExitHandler);
265
266 return Ret;
267}