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