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