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