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