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