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