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