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