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