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> |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 17 | |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 18 | #define UNCONST(ptr) ((void *)(uintptr_t)(ptr)) |
| 19 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 20 | /* Return 1 if there is an error, otherwise return 0. */ |
| 21 | static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs, |
| 22 | void **WriterCtx) { |
| 23 | uint32_t I; |
| 24 | FILE *File = (FILE *)*WriterCtx; |
| 25 | for (I = 0; I < NumIOVecs; I++) { |
| 26 | if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) != |
| 27 | IOVecs[I].NumElm) |
| 28 | return 1; |
| 29 | } |
| 30 | return 0; |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 31 | } |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 32 | |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 33 | COMPILER_RT_VISIBILITY ProfBufferIO * |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 34 | lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) { |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame^] | 35 | FreeHook = &free; |
| 36 | DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1); |
| 37 | VPBufferSize = BufferSz; |
| 38 | return lprofCreateBufferIO(fileWriter, File); |
| 39 | } |
| 40 | |
| 41 | static void setupIOBuffer() { |
| 42 | const char *BufferSzStr = 0; |
| 43 | BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE"); |
| 44 | if (BufferSzStr && BufferSzStr[0]) { |
| 45 | VPBufferSize = atoi(BufferSzStr); |
| 46 | DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1); |
| 47 | } |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 50 | static int writeFile(FILE *File) { |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 51 | FreeHook = &free; |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame^] | 52 | setupIOBuffer(); |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 53 | return lprofWriteData(fileWriter, File, lprofGatherValueProfData); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 56 | static int writeFileWithName(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 57 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 58 | FILE *OutputFile; |
| 59 | if (!OutputName || !OutputName[0]) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 60 | return -1; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 61 | |
| 62 | /* Append to the file to support profiling multiple shared objects. */ |
Xinliang David Li | be49271 | 2015-12-15 22:18:11 +0000 | [diff] [blame] | 63 | OutputFile = fopen(OutputName, "ab"); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 64 | if (!OutputFile) |
| 65 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 66 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 67 | RetVal = writeFile(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 68 | |
| 69 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 70 | return RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 73 | COMPILER_RT_WEAK int __llvm_profile_OwnsFilename = 0; |
| 74 | COMPILER_RT_WEAK const char *__llvm_profile_CurrentFilename = NULL; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 75 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 76 | static void truncateCurrentFile(void) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 77 | const char *Filename; |
| 78 | FILE *File; |
| 79 | |
| 80 | Filename = __llvm_profile_CurrentFilename; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 81 | if (!Filename || !Filename[0]) |
| 82 | return; |
| 83 | |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 84 | /* Create the directory holding the file, if needed. */ |
Sean Silva | c2feac7 | 2016-03-28 21:32:46 +0000 | [diff] [blame] | 85 | if (strchr(Filename, '/') || strchr(Filename, '\\')) { |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 86 | char *Copy = malloc(strlen(Filename) + 1); |
| 87 | strcpy(Copy, Filename); |
| 88 | __llvm_profile_recursive_mkdir(Copy); |
Alexey Samsonov | 6585b76 | 2015-07-15 22:50:39 +0000 | [diff] [blame] | 89 | free(Copy); |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 92 | /* Truncate the file. Later we'll reopen and append. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 93 | File = fopen(Filename, "w"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 94 | if (!File) |
| 95 | return; |
| 96 | fclose(File); |
| 97 | } |
| 98 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 99 | static void setFilename(const char *Filename, int OwnsFilename) { |
| 100 | /* Check if this is a new filename and therefore needs truncation. */ |
| 101 | int NewFile = !__llvm_profile_CurrentFilename || |
| 102 | (Filename && strcmp(Filename, __llvm_profile_CurrentFilename)); |
| 103 | if (__llvm_profile_OwnsFilename) |
| 104 | free(UNCONST(__llvm_profile_CurrentFilename)); |
| 105 | |
| 106 | __llvm_profile_CurrentFilename = Filename; |
| 107 | __llvm_profile_OwnsFilename = OwnsFilename; |
| 108 | |
| 109 | /* If not a new file, append to support profiling multiple shared objects. */ |
| 110 | if (NewFile) |
| 111 | truncateCurrentFile(); |
| 112 | } |
| 113 | |
| 114 | static void resetFilenameToDefault(void) { setFilename("default.profraw", 0); } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 115 | |
| 116 | int getpid(void); |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 117 | static int setFilenamePossiblyWithPid(const char *Filename) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 118 | #define MAX_PID_SIZE 16 |
| 119 | char PidChars[MAX_PID_SIZE] = {0}; |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 120 | int NumPids = 0, PidLength = 0, NumHosts = 0, HostNameLength = 0; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 121 | char *Allocated; |
| 122 | int I, J; |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 123 | char Hostname[COMPILER_RT_MAX_HOSTLEN]; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 124 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 125 | /* Reset filename on NULL, except with env var which is checked by caller. */ |
| 126 | if (!Filename) { |
| 127 | resetFilenameToDefault(); |
| 128 | return 0; |
| 129 | } |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 130 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 131 | /* Check the filename for "%p", which indicates a pid-substitution. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 132 | for (I = 0; Filename[I]; ++I) |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 133 | if (Filename[I] == '%') { |
| 134 | if (Filename[++I] == 'p') { |
| 135 | if (!NumPids++) { |
| 136 | PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()); |
| 137 | if (PidLength <= 0) |
| 138 | return -1; |
| 139 | } |
| 140 | } else if (Filename[I] == 'h') { |
| 141 | if (!NumHosts++) |
| 142 | if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) |
| 143 | return -1; |
| 144 | HostNameLength = strlen(Hostname); |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 145 | } |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | if (!(NumPids || NumHosts)) { |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 149 | setFilename(Filename, 0); |
| 150 | return 0; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 153 | /* Allocate enough space for the substituted filename. */ |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 154 | Allocated = malloc(I + NumPids*(PidLength - 2) + |
| 155 | NumHosts*(HostNameLength - 2) + 1); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 156 | if (!Allocated) |
| 157 | return -1; |
| 158 | |
| 159 | /* Construct the new filename. */ |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 160 | for (I = 0, J = 0; Filename[I]; ++I) |
| 161 | if (Filename[I] == '%') { |
| 162 | if (Filename[++I] == 'p') { |
| 163 | memcpy(Allocated + J, PidChars, PidLength); |
| 164 | J += PidLength; |
| 165 | } |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 166 | else if (Filename[I] == 'h') { |
| 167 | memcpy(Allocated + J, Hostname, HostNameLength); |
| 168 | J += HostNameLength; |
| 169 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 170 | /* Drop any unknown substitutions. */ |
| 171 | } else |
| 172 | Allocated[J++] = Filename[I]; |
| 173 | Allocated[J] = 0; |
| 174 | |
| 175 | /* Use the computed name. */ |
| 176 | setFilename(Allocated, 1); |
| 177 | return 0; |
| 178 | } |
| 179 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 180 | static int setFilenameFromEnvironment(void) { |
| 181 | const char *Filename = getenv("LLVM_PROFILE_FILE"); |
| 182 | |
| 183 | if (!Filename || !Filename[0]) |
| 184 | return -1; |
| 185 | |
| 186 | return setFilenamePossiblyWithPid(Filename); |
| 187 | } |
| 188 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 189 | static void setFilenameAutomatically(void) { |
| 190 | if (!setFilenameFromEnvironment()) |
| 191 | return; |
| 192 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 193 | resetFilenameToDefault(); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 196 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 197 | void __llvm_profile_initialize_file(void) { |
| 198 | /* Check if the filename has been initialized. */ |
| 199 | if (__llvm_profile_CurrentFilename) |
| 200 | return; |
| 201 | |
| 202 | /* Detect the filename and truncate. */ |
| 203 | setFilenameAutomatically(); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 206 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 207 | void __llvm_profile_set_filename(const char *Filename) { |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 208 | setFilenamePossiblyWithPid(Filename); |
| 209 | } |
| 210 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 211 | COMPILER_RT_VISIBILITY |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 212 | void __llvm_profile_override_default_filename(const char *Filename) { |
| 213 | /* If the env var is set, skip setting filename from argument. */ |
| 214 | const char *Env_Filename = getenv("LLVM_PROFILE_FILE"); |
| 215 | if (Env_Filename && Env_Filename[0]) |
| 216 | return; |
| 217 | setFilenamePossiblyWithPid(Filename); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 220 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 221 | int __llvm_profile_write_file(void) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 222 | int rc; |
| 223 | |
Xinliang David Li | 55d927a | 2015-12-07 21:18:16 +0000 | [diff] [blame] | 224 | GetEnvHook = &getenv; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 225 | /* Check the filename. */ |
Xinliang David Li | 9ea3064 | 2015-12-03 18:31:59 +0000 | [diff] [blame] | 226 | if (!__llvm_profile_CurrentFilename) { |
| 227 | PROF_ERR("LLVM Profile: Failed to write file : %s\n", "Filename not set"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 228 | return -1; |
Xinliang David Li | 9ea3064 | 2015-12-03 18:31:59 +0000 | [diff] [blame] | 229 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 230 | |
Xinliang David Li | d85c32c | 2016-01-08 23:42:28 +0000 | [diff] [blame] | 231 | /* Check if there is llvm/runtime version mismatch. */ |
Xinliang David Li | a692421 | 2016-01-08 23:31:57 +0000 | [diff] [blame] | 232 | if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { |
| 233 | PROF_ERR("LLVM Profile: runtime and instrumentation version mismatch : " |
| 234 | "expected %d, but get %d\n", |
| 235 | INSTR_PROF_RAW_VERSION, |
| 236 | (int)GET_VERSION(__llvm_profile_get_version())); |
| 237 | return -1; |
| 238 | } |
| 239 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 240 | /* Write the file. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 241 | rc = writeFileWithName(__llvm_profile_CurrentFilename); |
Xinliang David Li | 9ea3064 | 2015-12-03 18:31:59 +0000 | [diff] [blame] | 242 | if (rc) |
| 243 | PROF_ERR("LLVM Profile: Failed to write file \"%s\": %s\n", |
Justin Bogner | 66fd5c9 | 2015-01-16 20:10:56 +0000 | [diff] [blame] | 244 | __llvm_profile_CurrentFilename, strerror(errno)); |
| 245 | return rc; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Xinliang David Li | 6fe18f4 | 2015-11-23 04:38:17 +0000 | [diff] [blame] | 248 | static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 249 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 250 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 251 | int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 252 | static int HasBeenRegistered = 0; |
| 253 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 254 | if (HasBeenRegistered) |
| 255 | return 0; |
| 256 | |
| 257 | HasBeenRegistered = 1; |
| 258 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 259 | } |