Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 1 | /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\ |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 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 | #include "InstrProfiling.h" |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 11 | #include "InstrProfilingInternal.h" |
Xinliang David Li | 6e55716 | 2015-11-18 21:11:46 +0000 | [diff] [blame] | 12 | #include "InstrProfilingUtil.h" |
Joerg Sonnenberger | ef24b41 | 2015-03-09 11:23:29 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 16 | #include <string.h> |
Xinliang David Li | 71eddbf | 2016-05-20 04:52:27 +0000 | [diff] [blame] | 17 | #ifdef _MSC_VER |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 18 | /* For _alloca. */ |
Xinliang David Li | 71eddbf | 2016-05-20 04:52:27 +0000 | [diff] [blame] | 19 | #include <malloc.h> |
Xinliang David Li | fb320a1 | 2016-05-20 05:40:07 +0000 | [diff] [blame] | 20 | #endif |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 21 | #if defined(_WIN32) |
| 22 | #include "WindowsMMap.h" |
| 23 | /* For _chsize_s */ |
| 24 | #include <io.h> |
| 25 | #else |
| 26 | #include <sys/file.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <unistd.h> |
| 29 | #if defined(__linux__) |
| 30 | #include <sys/types.h> |
| 31 | #endif |
| 32 | #endif |
Xinliang David Li | 71eddbf | 2016-05-20 04:52:27 +0000 | [diff] [blame] | 33 | |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 34 | /* From where is profile name specified. |
| 35 | * The order the enumerators define their |
| 36 | * precedence. Re-order them may lead to |
| 37 | * runtime behavior change. */ |
| 38 | typedef enum ProfileNameSpecifier { |
| 39 | PNS_unknown = 0, |
| 40 | PNS_default, |
| 41 | PNS_command_line, |
| 42 | PNS_environment, |
| 43 | PNS_runtime_api |
| 44 | } ProfileNameSpecifier; |
| 45 | |
| 46 | static const char *getPNSStr(ProfileNameSpecifier PNS) { |
| 47 | switch (PNS) { |
| 48 | case PNS_default: |
| 49 | return "default setting"; |
| 50 | case PNS_command_line: |
| 51 | return "command line"; |
| 52 | case PNS_environment: |
| 53 | return "environment variable"; |
| 54 | case PNS_runtime_api: |
| 55 | return "runtime API"; |
| 56 | default: |
| 57 | return "Unknown"; |
| 58 | } |
| 59 | } |
| 60 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 61 | #define MAX_PID_SIZE 16 |
| 62 | /* Data structure holding the result of parsed filename pattern. */ |
| 63 | typedef struct lprofFilename { |
| 64 | /* File name string possibly with %p or %h specifiers. */ |
| 65 | const char *FilenamePat; |
| 66 | char PidChars[MAX_PID_SIZE]; |
| 67 | char Hostname[COMPILER_RT_MAX_HOSTLEN]; |
| 68 | unsigned NumPids; |
| 69 | unsigned NumHosts; |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 70 | /* When in-process merging is enabled, this parameter specifies |
| 71 | * the total number of profile data files shared by all the processes |
| 72 | * spawned from the same binary. By default the value is 1. If merging |
| 73 | * is not enabled, its value should be 0. This parameter is specified |
| 74 | * by the %[0-9]m specifier. For instance %2m enables merging using |
| 75 | * 2 profile data files. %1m is equivalent to %m. Also %m specifier |
| 76 | * can only appear once at the end of the name pattern. */ |
| 77 | unsigned MergePoolSize; |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 78 | ProfileNameSpecifier PNS; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 79 | } lprofFilename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 80 | |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 81 | lprofFilename lprofCurFilename = {0, {0}, {0}, 0, 0, 0, PNS_unknown}; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 82 | |
| 83 | int getpid(void); |
| 84 | static int getCurFilenameLength(); |
| 85 | static const char *getCurFilename(char *FilenameBuf); |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 86 | static unsigned doMerging() { return lprofCurFilename.MergePoolSize; } |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 87 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 88 | /* Return 1 if there is an error, otherwise return 0. */ |
| 89 | static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs, |
| 90 | void **WriterCtx) { |
| 91 | uint32_t I; |
| 92 | FILE *File = (FILE *)*WriterCtx; |
| 93 | for (I = 0; I < NumIOVecs; I++) { |
| 94 | if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) != |
| 95 | IOVecs[I].NumElm) |
| 96 | return 1; |
| 97 | } |
| 98 | return 0; |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 99 | } |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 100 | |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 101 | COMPILER_RT_VISIBILITY ProfBufferIO * |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 102 | lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) { |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 103 | FreeHook = &free; |
| 104 | DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1); |
| 105 | VPBufferSize = BufferSz; |
| 106 | return lprofCreateBufferIO(fileWriter, File); |
| 107 | } |
| 108 | |
| 109 | static void setupIOBuffer() { |
| 110 | const char *BufferSzStr = 0; |
| 111 | BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE"); |
| 112 | if (BufferSzStr && BufferSzStr[0]) { |
| 113 | VPBufferSize = atoi(BufferSzStr); |
| 114 | DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1); |
| 115 | } |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 118 | /* Read profile data in \c ProfileFile and merge with in-memory |
| 119 | profile counters. Returns -1 if there is fatal error, otheriwse |
| 120 | 0 is returned. |
| 121 | */ |
| 122 | static int doProfileMerging(FILE *ProfileFile) { |
| 123 | uint64_t ProfileFileSize; |
| 124 | char *ProfileBuffer; |
| 125 | |
| 126 | if (fseek(ProfileFile, 0L, SEEK_END) == -1) { |
| 127 | PROF_ERR("Unable to merge profile data, unable to get size: %s\n", |
| 128 | strerror(errno)); |
| 129 | return -1; |
| 130 | } |
| 131 | ProfileFileSize = ftell(ProfileFile); |
| 132 | |
| 133 | /* Restore file offset. */ |
| 134 | if (fseek(ProfileFile, 0L, SEEK_SET) == -1) { |
| 135 | PROF_ERR("Unable to merge profile data, unable to rewind: %s\n", |
| 136 | strerror(errno)); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | /* Nothing to merge. */ |
| 141 | if (ProfileFileSize < sizeof(__llvm_profile_header)) { |
| 142 | if (ProfileFileSize) |
| 143 | PROF_WARN("Unable to merge profile data: %s\n", |
| 144 | "source profile file is too small."); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE, |
| 149 | fileno(ProfileFile), 0); |
| 150 | if (ProfileBuffer == MAP_FAILED) { |
| 151 | PROF_ERR("Unable to merge profile data, mmap failed: %s\n", |
| 152 | strerror(errno)); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) { |
| 157 | (void)munmap(ProfileBuffer, ProfileFileSize); |
| 158 | PROF_WARN("Unable to merge profile data: %s\n", |
| 159 | "source profile file is not compatible."); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /* Now start merging */ |
| 164 | __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize); |
| 165 | (void)munmap(ProfileBuffer, ProfileFileSize); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* Open the profile data for merging. It opens the file in r+b mode with |
| 171 | * file locking. If the file has content which is compatible with the |
| 172 | * current process, it also reads in the profile data in the file and merge |
| 173 | * it with in-memory counters. After the profile data is merged in memory, |
| 174 | * the original profile data is truncated and gets ready for the profile |
| 175 | * dumper. With profile merging enabled, each executable as well as any of |
| 176 | * its instrumented shared libraries dump profile data into their own data file. |
| 177 | */ |
| 178 | static FILE *openFileForMerging(const char *ProfileFileName) { |
| 179 | FILE *ProfileFile; |
| 180 | int rc; |
| 181 | |
| 182 | ProfileFile = lprofOpenFileEx(ProfileFileName); |
| 183 | if (!ProfileFile) |
| 184 | return NULL; |
| 185 | |
| 186 | rc = doProfileMerging(ProfileFile); |
| 187 | if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) || |
| 188 | fseek(ProfileFile, 0L, SEEK_SET) == -1) { |
| 189 | PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName, |
| 190 | strerror(errno)); |
| 191 | fclose(ProfileFile); |
| 192 | return NULL; |
| 193 | } |
| 194 | fseek(ProfileFile, 0L, SEEK_SET); |
| 195 | return ProfileFile; |
| 196 | } |
| 197 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 198 | /* Write profile data to file \c OutputName. */ |
| 199 | static int writeFile(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 200 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 201 | FILE *OutputFile; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 202 | |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 203 | if (!doMerging()) |
| 204 | OutputFile = fopen(OutputName, "ab"); |
| 205 | else |
| 206 | OutputFile = openFileForMerging(OutputName); |
| 207 | |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 208 | if (!OutputFile) |
| 209 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 210 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 211 | FreeHook = &free; |
| 212 | setupIOBuffer(); |
| 213 | RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader()); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 214 | |
| 215 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 216 | return RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 219 | static void truncateCurrentFile(void) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 220 | const char *Filename; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 221 | char *FilenameBuf; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 222 | FILE *File; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 223 | int Length; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 224 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 225 | Length = getCurFilenameLength(); |
| 226 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
| 227 | Filename = getCurFilename(FilenameBuf); |
| 228 | if (!Filename) |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 229 | return; |
| 230 | |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 231 | /* Create the directory holding the file, if needed. */ |
Sean Silva | c2feac7 | 2016-03-28 21:32:46 +0000 | [diff] [blame] | 232 | if (strchr(Filename, '/') || strchr(Filename, '\\')) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 233 | char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1); |
| 234 | strncpy(Copy, Filename, Length + 1); |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 235 | __llvm_profile_recursive_mkdir(Copy); |
| 236 | } |
| 237 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 238 | /* Truncate the file. Later we'll reopen and append. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 239 | File = fopen(Filename, "w"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 240 | if (!File) |
| 241 | return; |
| 242 | fclose(File); |
| 243 | } |
| 244 | |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 245 | static const char *DefaultProfileName = "default.profraw"; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 246 | static void resetFilenameToDefault(void) { |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 247 | memset(&lprofCurFilename, 0, sizeof(lprofCurFilename)); |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 248 | lprofCurFilename.FilenamePat = DefaultProfileName; |
| 249 | lprofCurFilename.PNS = PNS_default; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 250 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 251 | |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 252 | static int containsMergeSpecifier(const char *FilenamePat, int I) { |
| 253 | return (FilenamePat[I] == 'm' || |
| 254 | (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' && |
| 255 | /* If FilenamePat[I] is not '\0', the next byte is guaranteed |
| 256 | * to be in-bound as the string is null terminated. */ |
| 257 | FilenamePat[I + 1] == 'm')); |
| 258 | } |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 259 | |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 260 | /* Parses the pattern string \p FilenamePat and stores the result to |
| 261 | * lprofcurFilename structure. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 262 | static int parseFilenamePattern(const char *FilenamePat) { |
| 263 | int NumPids = 0, NumHosts = 0, I; |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 264 | char *PidChars = &lprofCurFilename.PidChars[0]; |
| 265 | char *Hostname = &lprofCurFilename.Hostname[0]; |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 266 | int MergingEnabled = 0; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 267 | |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 268 | lprofCurFilename.FilenamePat = FilenamePat; |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 269 | /* Check the filename for "%p", which indicates a pid-substitution. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 270 | for (I = 0; FilenamePat[I]; ++I) |
| 271 | if (FilenamePat[I] == '%') { |
| 272 | if (FilenamePat[++I] == 'p') { |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 273 | if (!NumPids++) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 274 | if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) { |
| 275 | PROF_WARN( |
| 276 | "Unable to parse filename pattern %s. Using the default name.", |
| 277 | FilenamePat); |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 278 | return -1; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 279 | } |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 280 | } |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 281 | } else if (FilenamePat[I] == 'h') { |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 282 | if (!NumHosts++) |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 283 | if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) { |
| 284 | PROF_WARN( |
| 285 | "Unable to parse filename pattern %s. Using the default name.", |
| 286 | FilenamePat); |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 287 | return -1; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 288 | } |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 289 | } else if (containsMergeSpecifier(FilenamePat, I)) { |
| 290 | if (MergingEnabled) { |
| 291 | PROF_WARN( |
| 292 | "%%m specifier can only be specified once at the end of %s.\n", |
| 293 | FilenamePat); |
| 294 | return -1; |
| 295 | } |
| 296 | MergingEnabled = 1; |
| 297 | if (FilenamePat[I] == 'm') |
| 298 | lprofCurFilename.MergePoolSize = 1; |
| 299 | else { |
| 300 | lprofCurFilename.MergePoolSize = FilenamePat[I] - '0'; |
| 301 | I++; /* advance to 'm' */ |
| 302 | } |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 303 | } |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 306 | lprofCurFilename.NumPids = NumPids; |
| 307 | lprofCurFilename.NumHosts = NumHosts; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 311 | static void parseAndSetFilename(const char *FilenamePat, |
| 312 | ProfileNameSpecifier PNS) { |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 313 | |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 314 | const char *OldFilenamePat = lprofCurFilename.FilenamePat; |
| 315 | ProfileNameSpecifier OldPNS = lprofCurFilename.PNS; |
| 316 | |
| 317 | if (PNS < OldPNS) |
| 318 | return; |
| 319 | |
| 320 | if (!FilenamePat) |
| 321 | FilenamePat = DefaultProfileName; |
| 322 | |
| 323 | /* When -fprofile-instr-generate=<path> is specified on the |
| 324 | * command line, each module will be instrumented with runtime |
| 325 | * init call to __llvm_profile_init function which calls |
| 326 | * __llvm_profile_override_default_filename. In most of the cases, |
| 327 | * the path will be identical, so bypass the parsing completely. |
| 328 | */ |
| 329 | if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) { |
| 330 | lprofCurFilename.PNS = PNS; |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | /* When PNS >= OldPNS, the last one wins. */ |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 335 | if (!FilenamePat || parseFilenamePattern(FilenamePat)) |
| 336 | resetFilenameToDefault(); |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 337 | lprofCurFilename.PNS = PNS; |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 338 | |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 339 | if (!OldFilenamePat) { |
| 340 | PROF_NOTE("Set profile file path to \"%s\" via %s.\n", |
| 341 | lprofCurFilename.FilenamePat, getPNSStr(PNS)); |
| 342 | } else { |
| 343 | PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n", |
| 344 | OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat, |
| 345 | getPNSStr(PNS)); |
| 346 | } |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 347 | |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 348 | if (!lprofCurFilename.MergePoolSize) |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 349 | truncateCurrentFile(); |
| 350 | } |
| 351 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 352 | /* Return buffer length that is required to store the current profile |
| 353 | * filename with PID and hostname substitutions. */ |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 354 | /* The length to hold uint64_t followed by 2 digit pool id including '_' */ |
| 355 | #define SIGLEN 24 |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 356 | static int getCurFilenameLength() { |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 357 | int Len; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 358 | if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 359 | return 0; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 360 | |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 361 | if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts || |
| 362 | lprofCurFilename.MergePoolSize)) |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 363 | return strlen(lprofCurFilename.FilenamePat); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 364 | |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 365 | Len = strlen(lprofCurFilename.FilenamePat) + |
| 366 | lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) + |
| 367 | lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2); |
| 368 | if (lprofCurFilename.MergePoolSize) |
| 369 | Len += SIGLEN; |
| 370 | return Len; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* Return the pointer to the current profile file name (after substituting |
| 374 | * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer |
| 375 | * to store the resulting filename. If no substitution is needed, the |
| 376 | * current filename pattern string is directly returned. */ |
| 377 | static const char *getCurFilename(char *FilenameBuf) { |
| 378 | int I, J, PidLength, HostNameLength; |
| 379 | const char *FilenamePat = lprofCurFilename.FilenamePat; |
| 380 | |
| 381 | if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) |
| 382 | return 0; |
| 383 | |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 384 | if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts || |
| 385 | lprofCurFilename.MergePoolSize)) |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 386 | return lprofCurFilename.FilenamePat; |
| 387 | |
| 388 | PidLength = strlen(lprofCurFilename.PidChars); |
| 389 | HostNameLength = strlen(lprofCurFilename.Hostname); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 390 | /* Construct the new filename. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 391 | for (I = 0, J = 0; FilenamePat[I]; ++I) |
| 392 | if (FilenamePat[I] == '%') { |
| 393 | if (FilenamePat[++I] == 'p') { |
| 394 | memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 395 | J += PidLength; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 396 | } else if (FilenamePat[I] == 'h') { |
| 397 | memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength); |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 398 | J += HostNameLength; |
Xinliang David Li | e2ce2e0 | 2016-06-08 23:43:56 +0000 | [diff] [blame] | 399 | } else if (containsMergeSpecifier(FilenamePat, I)) { |
| 400 | char LoadModuleSignature[SIGLEN]; |
| 401 | int S; |
| 402 | int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize; |
| 403 | S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d", |
| 404 | lprofGetLoadModuleSignature(), ProfilePoolId); |
| 405 | if (S == -1 || S > SIGLEN) |
| 406 | S = SIGLEN; |
| 407 | memcpy(FilenameBuf + J, LoadModuleSignature, S); |
| 408 | J += S; |
| 409 | if (FilenamePat[I] != 'm') |
| 410 | I++; |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 411 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 412 | /* Drop any unknown substitutions. */ |
| 413 | } else |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 414 | FilenameBuf[J++] = FilenamePat[I]; |
| 415 | FilenameBuf[J] = 0; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 416 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 417 | return FilenameBuf; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 420 | /* Returns the pointer to the environment variable |
| 421 | * string. Returns null if the env var is not set. */ |
| 422 | static const char *getFilenamePatFromEnv(void) { |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 423 | const char *Filename = getenv("LLVM_PROFILE_FILE"); |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 424 | if (!Filename || !Filename[0]) |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | return Filename; |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 429 | /* This method is invoked by the runtime initialization hook |
| 430 | * InstrProfilingRuntime.o if it is linked in. Both user specified |
| 431 | * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE |
| 432 | * environment variable can override this default value. */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 433 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 434 | void __llvm_profile_initialize_file(void) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 435 | const char *FilenamePat; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 436 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 437 | FilenamePat = getFilenamePatFromEnv(); |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 438 | parseAndSetFilename(FilenamePat, FilenamePat ? PNS_environment : PNS_default); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 441 | /* This API is directly called by the user application code. It has the |
| 442 | * highest precedence compared with LLVM_PROFILE_FILE environment variable |
Xinliang David Li | 4151894 | 2016-05-24 02:37:07 +0000 | [diff] [blame] | 443 | * and command line option -fprofile-instr-generate=<profile_name>. |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 444 | */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 445 | COMPILER_RT_VISIBILITY |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 446 | void __llvm_profile_set_filename(const char *FilenamePat) { |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 447 | parseAndSetFilename(FilenamePat, PNS_runtime_api); |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 450 | /* |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 451 | * This API is invoked by the global initializers emitted by Clang/LLVM when |
| 452 | * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate |
| 453 | * without an argument). This option has lower precedence than the |
| 454 | * LLVM_PROFILE_FILE environment variable. |
| 455 | */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 456 | COMPILER_RT_VISIBILITY |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 457 | void __llvm_profile_override_default_filename(const char *FilenamePat) { |
Xinliang David Li | 153e8b6 | 2016-06-10 20:35:01 +0000 | [diff] [blame^] | 458 | parseAndSetFilename(FilenamePat, PNS_command_line); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 461 | /* The public API for writing profile data into the file with name |
| 462 | * set by previous calls to __llvm_profile_set_filename or |
| 463 | * __llvm_profile_override_default_filename or |
| 464 | * __llvm_profile_initialize_file. */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 465 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 466 | int __llvm_profile_write_file(void) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 467 | int rc, Length; |
| 468 | const char *Filename; |
| 469 | char *FilenameBuf; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 470 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 471 | Length = getCurFilenameLength(); |
| 472 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
| 473 | Filename = getCurFilename(FilenameBuf); |
| 474 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 475 | /* Check the filename. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 476 | if (!Filename) { |
Xinliang David Li | 690c31f | 2016-05-20 05:15:42 +0000 | [diff] [blame] | 477 | PROF_ERR("Failed to write file : %s\n", "Filename not set"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 478 | return -1; |
Xinliang David Li | 9ea3064 | 2015-12-03 18:31:59 +0000 | [diff] [blame] | 479 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 480 | |
Xinliang David Li | d85c32c | 2016-01-08 23:42:28 +0000 | [diff] [blame] | 481 | /* Check if there is llvm/runtime version mismatch. */ |
Xinliang David Li | a692421 | 2016-01-08 23:31:57 +0000 | [diff] [blame] | 482 | if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { |
Xinliang David Li | 690c31f | 2016-05-20 05:15:42 +0000 | [diff] [blame] | 483 | PROF_ERR("Runtime and instrumentation version mismatch : " |
Xinliang David Li | a692421 | 2016-01-08 23:31:57 +0000 | [diff] [blame] | 484 | "expected %d, but get %d\n", |
| 485 | INSTR_PROF_RAW_VERSION, |
| 486 | (int)GET_VERSION(__llvm_profile_get_version())); |
| 487 | return -1; |
| 488 | } |
| 489 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 490 | /* Write profile data to the file. */ |
| 491 | rc = writeFile(Filename); |
Xinliang David Li | 9ea3064 | 2015-12-03 18:31:59 +0000 | [diff] [blame] | 492 | if (rc) |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 493 | PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno)); |
Justin Bogner | 66fd5c9 | 2015-01-16 20:10:56 +0000 | [diff] [blame] | 494 | return rc; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Xinliang David Li | 6fe18f4 | 2015-11-23 04:38:17 +0000 | [diff] [blame] | 497 | static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 498 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 499 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 500 | int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 501 | static int HasBeenRegistered = 0; |
| 502 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 503 | if (HasBeenRegistered) |
| 504 | return 0; |
| 505 | |
Xinliang David Li | 4617aa7 | 2016-05-18 22:34:05 +0000 | [diff] [blame] | 506 | lprofSetupValueProfiler(); |
| 507 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 508 | HasBeenRegistered = 1; |
| 509 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 510 | } |