1 | /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\ |
2 | |* |
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 |
6 | |* |
7 | \*===----------------------------------------------------------------------===*/ |
8 | |
9 | #if !defined(__Fuchsia__) |
10 | |
11 | #include <assert.h> |
12 | #include <errno.h> |
13 | #include <stdio.h> |
14 | #include <stdlib.h> |
15 | #include <string.h> |
16 | #ifdef _MSC_VER |
17 | /* For _alloca. */ |
18 | #include <malloc.h> |
19 | #endif |
20 | #if defined(_WIN32) |
21 | #include "WindowsMMap.h" |
22 | /* For _chsize_s */ |
23 | #include <io.h> |
24 | #include <process.h> |
25 | #else |
26 | #include <sys/file.h> |
27 | #include <sys/mman.h> |
28 | #include <unistd.h> |
29 | #if defined(__linux__) |
30 | #include <sys/types.h> |
31 | #endif |
32 | #endif |
33 | |
34 | #include "InstrProfiling.h" |
35 | #include "InstrProfilingInternal.h" |
36 | #include "InstrProfilingPort.h" |
37 | #include "InstrProfilingUtil.h" |
38 | |
39 | /* From where is profile name specified. |
40 | * The order the enumerators define their |
41 | * precedence. Re-order them may lead to |
42 | * runtime behavior change. */ |
43 | typedef enum ProfileNameSpecifier { |
44 | PNS_unknown = 0, |
45 | PNS_default, |
46 | PNS_command_line, |
47 | PNS_environment, |
48 | PNS_runtime_api |
49 | } ProfileNameSpecifier; |
50 | |
51 | static const char *getPNSStr(ProfileNameSpecifier PNS) { |
52 | switch (PNS) { |
53 | case PNS_default: |
54 | return "default setting" ; |
55 | case PNS_command_line: |
56 | return "command line" ; |
57 | case PNS_environment: |
58 | return "environment variable" ; |
59 | case PNS_runtime_api: |
60 | return "runtime API" ; |
61 | default: |
62 | return "Unknown" ; |
63 | } |
64 | } |
65 | |
66 | #define MAX_PID_SIZE 16 |
67 | /* Data structure holding the result of parsed filename pattern. */ |
68 | typedef struct lprofFilename { |
69 | /* File name string possibly with %p or %h specifiers. */ |
70 | const char *FilenamePat; |
71 | /* A flag indicating if FilenamePat's memory is allocated |
72 | * by runtime. */ |
73 | unsigned OwnsFilenamePat; |
74 | const char *ProfilePathPrefix; |
75 | char PidChars[MAX_PID_SIZE]; |
76 | char *TmpDir; |
77 | char Hostname[COMPILER_RT_MAX_HOSTLEN]; |
78 | unsigned NumPids; |
79 | unsigned NumHosts; |
80 | /* When in-process merging is enabled, this parameter specifies |
81 | * the total number of profile data files shared by all the processes |
82 | * spawned from the same binary. By default the value is 1. If merging |
83 | * is not enabled, its value should be 0. This parameter is specified |
84 | * by the %[0-9]m specifier. For instance %2m enables merging using |
85 | * 2 profile data files. %1m is equivalent to %m. Also %m specifier |
86 | * can only appear once at the end of the name pattern. */ |
87 | unsigned MergePoolSize; |
88 | ProfileNameSpecifier PNS; |
89 | } lprofFilename; |
90 | |
91 | static lprofFilename lprofCurFilename = {0, 0, 0, {0}, NULL, |
92 | {0}, 0, 0, 0, PNS_unknown}; |
93 | |
94 | static int ProfileMergeRequested = 0; |
95 | static int getProfileFileSizeForMerging(FILE *ProfileFile, |
96 | uint64_t *ProfileFileSize); |
97 | |
98 | #if defined(__APPLE__) |
99 | static const int ContinuousModeSupported = 1; |
100 | static const int UseBiasVar = 0; |
101 | static const char *FileOpenMode = "a+b" ; |
102 | static void *BiasAddr = NULL; |
103 | static void *BiasDefaultAddr = NULL; |
104 | static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) { |
105 | /* Get the sizes of various profile data sections. Taken from |
106 | * __llvm_profile_get_size_for_buffer(). */ |
107 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
108 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
109 | const char *CountersBegin = __llvm_profile_begin_counters(); |
110 | const char *CountersEnd = __llvm_profile_end_counters(); |
111 | const char *BitmapBegin = __llvm_profile_begin_bitmap(); |
112 | const char *BitmapEnd = __llvm_profile_end_bitmap(); |
113 | const char *NamesBegin = __llvm_profile_begin_names(); |
114 | const char *NamesEnd = __llvm_profile_end_names(); |
115 | const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char); |
116 | uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); |
117 | uint64_t CountersSize = |
118 | __llvm_profile_get_counters_size(CountersBegin, CountersEnd); |
119 | uint64_t NumBitmapBytes = |
120 | __llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd); |
121 | |
122 | /* Check that the counter, bitmap, and data sections in this image are |
123 | * page-aligned. */ |
124 | unsigned PageSize = getpagesize(); |
125 | if ((intptr_t)CountersBegin % PageSize != 0) { |
126 | PROF_ERR("Counters section not page-aligned (start = %p, pagesz = %u).\n" , |
127 | CountersBegin, PageSize); |
128 | return 1; |
129 | } |
130 | if ((intptr_t)BitmapBegin % PageSize != 0) { |
131 | PROF_ERR("Bitmap section not page-aligned (start = %p, pagesz = %u).\n" , |
132 | BitmapBegin, PageSize); |
133 | return 1; |
134 | } |
135 | if ((intptr_t)DataBegin % PageSize != 0) { |
136 | PROF_ERR("Data section not page-aligned (start = %p, pagesz = %u).\n" , |
137 | DataBegin, PageSize); |
138 | return 1; |
139 | } |
140 | |
141 | int Fileno = fileno(File); |
142 | /* Determine how much padding is needed before/after the counters and |
143 | * after the names. */ |
144 | uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters, |
145 | PaddingBytesAfterNames, PaddingBytesAfterBitmapBytes, |
146 | PaddingBytesAfterVTable, PaddingBytesAfterVNames; |
147 | __llvm_profile_get_padding_sizes_for_counters( |
148 | DataSize, CountersSize, NumBitmapBytes, NamesSize, /*VTableSize=*/0, |
149 | /*VNameSize=*/0, &PaddingBytesBeforeCounters, &PaddingBytesAfterCounters, |
150 | &PaddingBytesAfterBitmapBytes, &PaddingBytesAfterNames, |
151 | &PaddingBytesAfterVTable, &PaddingBytesAfterVNames); |
152 | |
153 | uint64_t PageAlignedCountersLength = CountersSize + PaddingBytesAfterCounters; |
154 | uint64_t FileOffsetToCounters = CurrentFileOffset + |
155 | sizeof(__llvm_profile_header) + DataSize + |
156 | PaddingBytesBeforeCounters; |
157 | void *CounterMmap = mmap((void *)CountersBegin, PageAlignedCountersLength, |
158 | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, |
159 | Fileno, FileOffsetToCounters); |
160 | if (CounterMmap != CountersBegin) { |
161 | PROF_ERR( |
162 | "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" |
163 | " - CountersBegin: %p\n" |
164 | " - PageAlignedCountersLength: %" PRIu64 "\n" |
165 | " - Fileno: %d\n" |
166 | " - FileOffsetToCounters: %" PRIu64 "\n" , |
167 | strerror(errno), CountersBegin, PageAlignedCountersLength, Fileno, |
168 | FileOffsetToCounters); |
169 | return 1; |
170 | } |
171 | |
172 | /* Also mmap MCDC bitmap bytes. If there aren't any bitmap bytes, mmap() |
173 | * will fail with EINVAL. */ |
174 | if (NumBitmapBytes == 0) |
175 | return 0; |
176 | |
177 | uint64_t PageAlignedBitmapLength = |
178 | NumBitmapBytes + PaddingBytesAfterBitmapBytes; |
179 | uint64_t FileOffsetToBitmap = |
180 | CurrentFileOffset + sizeof(__llvm_profile_header) + DataSize + |
181 | PaddingBytesBeforeCounters + CountersSize + PaddingBytesAfterCounters; |
182 | void *BitmapMmap = |
183 | mmap((void *)BitmapBegin, PageAlignedBitmapLength, PROT_READ | PROT_WRITE, |
184 | MAP_FIXED | MAP_SHARED, Fileno, FileOffsetToBitmap); |
185 | if (BitmapMmap != BitmapBegin) { |
186 | PROF_ERR( |
187 | "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" |
188 | " - BitmapBegin: %p\n" |
189 | " - PageAlignedBitmapLength: %" PRIu64 "\n" |
190 | " - Fileno: %d\n" |
191 | " - FileOffsetToBitmap: %" PRIu64 "\n" , |
192 | strerror(errno), BitmapBegin, PageAlignedBitmapLength, Fileno, |
193 | FileOffsetToBitmap); |
194 | return 1; |
195 | } |
196 | return 0; |
197 | } |
198 | #elif defined(__ELF__) || defined(_WIN32) |
199 | |
200 | #define INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR \ |
201 | INSTR_PROF_CONCAT(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR, _default) |
202 | COMPILER_RT_VISIBILITY intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR = 0; |
203 | |
204 | /* This variable is a weak external reference which could be used to detect |
205 | * whether or not the compiler defined this symbol. */ |
206 | #if defined(_MSC_VER) |
207 | COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR; |
208 | #if defined(_M_IX86) || defined(__i386__) |
209 | #define WIN_SYM_PREFIX "_" |
210 | #else |
211 | #define WIN_SYM_PREFIX |
212 | #endif |
213 | #pragma comment( \ |
214 | linker, "/alternatename:" WIN_SYM_PREFIX INSTR_PROF_QUOTE( \ |
215 | INSTR_PROF_PROFILE_COUNTER_BIAS_VAR) "=" WIN_SYM_PREFIX \ |
216 | INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR)) |
217 | #else |
218 | COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR |
219 | __attribute__((weak, alias(INSTR_PROF_QUOTE( |
220 | INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR)))); |
221 | #endif |
222 | static const int ContinuousModeSupported = 1; |
223 | static const int UseBiasVar = 1; |
224 | /* TODO: If there are two DSOs, the second DSO initilization will truncate the |
225 | * first profile file. */ |
226 | static const char *FileOpenMode = "w+b" ; |
227 | /* This symbol is defined by the compiler when runtime counter relocation is |
228 | * used and runtime provides a weak alias so we can check if it's defined. */ |
229 | static void *BiasAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_VAR; |
230 | static void *BiasDefaultAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR; |
231 | static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) { |
232 | /* Get the sizes of various profile data sections. Taken from |
233 | * __llvm_profile_get_size_for_buffer(). */ |
234 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
235 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
236 | const char *CountersBegin = __llvm_profile_begin_counters(); |
237 | const char *CountersEnd = __llvm_profile_end_counters(); |
238 | const char *BitmapBegin = __llvm_profile_begin_bitmap(); |
239 | const char *BitmapEnd = __llvm_profile_end_bitmap(); |
240 | uint64_t DataSize = __llvm_profile_get_data_size(Begin: DataBegin, End: DataEnd); |
241 | /* Get the file size. */ |
242 | uint64_t FileSize = 0; |
243 | if (getProfileFileSizeForMerging(ProfileFile: File, ProfileFileSize: &FileSize)) |
244 | return 1; |
245 | |
246 | /* Map the profile. */ |
247 | char *Profile = (char *)mmap(NULL, len: FileSize, PROT_READ | PROT_WRITE, |
248 | MAP_SHARED, fd: fileno(stream: File), offset: 0); |
249 | if (Profile == MAP_FAILED) { |
250 | PROF_ERR("Unable to mmap profile: %s\n" , strerror(errno)); |
251 | return 1; |
252 | } |
253 | const uint64_t CountersOffsetInBiasMode = |
254 | sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + DataSize; |
255 | /* Update the profile fields based on the current mapping. */ |
256 | INSTR_PROF_PROFILE_COUNTER_BIAS_VAR = |
257 | (intptr_t)Profile - (uintptr_t)CountersBegin + CountersOffsetInBiasMode; |
258 | |
259 | /* Return the memory allocated for counters to OS. */ |
260 | lprofReleaseMemoryPagesToOS(Begin: (uintptr_t)CountersBegin, End: (uintptr_t)CountersEnd); |
261 | |
262 | /* BIAS MODE not supported yet for Bitmap (MCDC). */ |
263 | |
264 | /* Return the memory allocated for counters to OS. */ |
265 | lprofReleaseMemoryPagesToOS(Begin: (uintptr_t)BitmapBegin, End: (uintptr_t)BitmapEnd); |
266 | return 0; |
267 | } |
268 | #else |
269 | static const int ContinuousModeSupported = 0; |
270 | static const int UseBiasVar = 0; |
271 | static const char *FileOpenMode = "a+b" ; |
272 | static void *BiasAddr = NULL; |
273 | static void *BiasDefaultAddr = NULL; |
274 | static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) { |
275 | return 0; |
276 | } |
277 | #endif |
278 | |
279 | static int isProfileMergeRequested(void) { return ProfileMergeRequested; } |
280 | static void setProfileMergeRequested(int EnableMerge) { |
281 | ProfileMergeRequested = EnableMerge; |
282 | } |
283 | |
284 | static FILE *ProfileFile = NULL; |
285 | static FILE *getProfileFile(void) { return ProfileFile; } |
286 | static void setProfileFile(FILE *File) { ProfileFile = File; } |
287 | |
288 | static int getCurFilenameLength(void); |
289 | static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf); |
290 | static unsigned doMerging(void) { |
291 | return lprofCurFilename.MergePoolSize || isProfileMergeRequested(); |
292 | } |
293 | |
294 | /* Return 1 if there is an error, otherwise return 0. */ |
295 | static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, |
296 | uint32_t NumIOVecs) { |
297 | uint32_t I; |
298 | FILE *File = (FILE *)This->WriterCtx; |
299 | char Zeroes[sizeof(uint64_t)] = {0}; |
300 | for (I = 0; I < NumIOVecs; I++) { |
301 | if (IOVecs[I].Data) { |
302 | if (fwrite(ptr: IOVecs[I].Data, size: IOVecs[I].ElmSize, n: IOVecs[I].NumElm, s: File) != |
303 | IOVecs[I].NumElm) |
304 | return 1; |
305 | } else if (IOVecs[I].UseZeroPadding) { |
306 | size_t BytesToWrite = IOVecs[I].ElmSize * IOVecs[I].NumElm; |
307 | while (BytesToWrite > 0) { |
308 | size_t PartialWriteLen = |
309 | (sizeof(uint64_t) > BytesToWrite) ? BytesToWrite : sizeof(uint64_t); |
310 | if (fwrite(ptr: Zeroes, size: sizeof(uint8_t), n: PartialWriteLen, s: File) != |
311 | PartialWriteLen) { |
312 | return 1; |
313 | } |
314 | BytesToWrite -= PartialWriteLen; |
315 | } |
316 | } else { |
317 | if (fseek(stream: File, off: IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1) |
318 | return 1; |
319 | } |
320 | } |
321 | return 0; |
322 | } |
323 | |
324 | /* TODO: make buffer size controllable by an internal option, and compiler can pass the size |
325 | to runtime via a variable. */ |
326 | static uint32_t orderFileWriter(FILE *File, const uint32_t *DataStart) { |
327 | if (fwrite(ptr: DataStart, size: sizeof(uint32_t), INSTR_ORDER_FILE_BUFFER_SIZE, s: File) != |
328 | INSTR_ORDER_FILE_BUFFER_SIZE) |
329 | return 1; |
330 | return 0; |
331 | } |
332 | |
333 | static void initFileWriter(ProfDataWriter *This, FILE *File) { |
334 | This->Write = fileWriter; |
335 | This->WriterCtx = File; |
336 | } |
337 | |
338 | COMPILER_RT_VISIBILITY ProfBufferIO * |
339 | lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) { |
340 | FreeHook = &free; |
341 | DynamicBufferIOBuffer = (uint8_t *)calloc(nmemb: 1, size: BufferSz); |
342 | VPBufferSize = BufferSz; |
343 | ProfDataWriter *fileWriter = |
344 | (ProfDataWriter *)calloc(nmemb: 1, size: sizeof(ProfDataWriter)); |
345 | initFileWriter(This: fileWriter, File); |
346 | ProfBufferIO *IO = lprofCreateBufferIO(FileWriter: fileWriter); |
347 | IO->OwnFileWriter = 1; |
348 | return IO; |
349 | } |
350 | |
351 | static void setupIOBuffer(void) { |
352 | const char *BufferSzStr = 0; |
353 | BufferSzStr = getenv(name: "LLVM_VP_BUFFER_SIZE" ); |
354 | if (BufferSzStr && BufferSzStr[0]) { |
355 | VPBufferSize = atoi(nptr: BufferSzStr); |
356 | DynamicBufferIOBuffer = (uint8_t *)calloc(nmemb: VPBufferSize, size: 1); |
357 | } |
358 | } |
359 | |
360 | /* Get the size of the profile file. If there are any errors, print the |
361 | * message under the assumption that the profile is being read for merging |
362 | * purposes, and return -1. Otherwise return the file size in the inout param |
363 | * \p ProfileFileSize. */ |
364 | static int getProfileFileSizeForMerging(FILE *ProfileFile, |
365 | uint64_t *ProfileFileSize) { |
366 | if (fseek(stream: ProfileFile, off: 0L, SEEK_END) == -1) { |
367 | PROF_ERR("Unable to merge profile data, unable to get size: %s\n" , |
368 | strerror(errno)); |
369 | return -1; |
370 | } |
371 | *ProfileFileSize = ftell(stream: ProfileFile); |
372 | |
373 | /* Restore file offset. */ |
374 | if (fseek(stream: ProfileFile, off: 0L, SEEK_SET) == -1) { |
375 | PROF_ERR("Unable to merge profile data, unable to rewind: %s\n" , |
376 | strerror(errno)); |
377 | return -1; |
378 | } |
379 | |
380 | if (*ProfileFileSize > 0 && |
381 | *ProfileFileSize < sizeof(__llvm_profile_header)) { |
382 | PROF_WARN("Unable to merge profile data: %s\n" , |
383 | "source profile file is too small." ); |
384 | return -1; |
385 | } |
386 | return 0; |
387 | } |
388 | |
389 | /* mmap() \p ProfileFile for profile merging purposes, assuming that an |
390 | * exclusive lock is held on the file and that \p ProfileFileSize is the |
391 | * length of the file. Return the mmap'd buffer in the inout variable |
392 | * \p ProfileBuffer. Returns -1 on failure. On success, the caller is |
393 | * responsible for unmapping the mmap'd buffer in \p ProfileBuffer. */ |
394 | static int mmapProfileForMerging(FILE *ProfileFile, uint64_t ProfileFileSize, |
395 | char **ProfileBuffer) { |
396 | *ProfileBuffer = mmap(NULL, len: ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE, |
397 | fd: fileno(stream: ProfileFile), offset: 0); |
398 | if (*ProfileBuffer == MAP_FAILED) { |
399 | PROF_ERR("Unable to merge profile data, mmap failed: %s\n" , |
400 | strerror(errno)); |
401 | return -1; |
402 | } |
403 | |
404 | if (__llvm_profile_check_compatibility(Profile: *ProfileBuffer, Size: ProfileFileSize)) { |
405 | (void)munmap(addr: *ProfileBuffer, len: ProfileFileSize); |
406 | PROF_WARN("Unable to merge profile data: %s\n" , |
407 | "source profile file is not compatible." ); |
408 | return -1; |
409 | } |
410 | return 0; |
411 | } |
412 | |
413 | /* Read profile data in \c ProfileFile and merge with in-memory |
414 | profile counters. Returns -1 if there is fatal error, otheriwse |
415 | 0 is returned. Returning 0 does not mean merge is actually |
416 | performed. If merge is actually done, *MergeDone is set to 1. |
417 | */ |
418 | static int doProfileMerging(FILE *ProfileFile, int *MergeDone) { |
419 | uint64_t ProfileFileSize; |
420 | char *ProfileBuffer; |
421 | |
422 | /* Get the size of the profile on disk. */ |
423 | if (getProfileFileSizeForMerging(ProfileFile, ProfileFileSize: &ProfileFileSize) == -1) |
424 | return -1; |
425 | |
426 | /* Nothing to merge. */ |
427 | if (!ProfileFileSize) |
428 | return 0; |
429 | |
430 | /* mmap() the profile and check that it is compatible with the data in |
431 | * the current image. */ |
432 | if (mmapProfileForMerging(ProfileFile, ProfileFileSize, ProfileBuffer: &ProfileBuffer) == -1) |
433 | return -1; |
434 | |
435 | /* Now start merging */ |
436 | if (__llvm_profile_merge_from_buffer(Profile: ProfileBuffer, Size: ProfileFileSize)) { |
437 | PROF_ERR("%s\n" , "Invalid profile data to merge" ); |
438 | (void)munmap(addr: ProfileBuffer, len: ProfileFileSize); |
439 | return -1; |
440 | } |
441 | |
442 | // Truncate the file in case merging of value profile did not happen to |
443 | // prevent from leaving garbage data at the end of the profile file. |
444 | (void)COMPILER_RT_FTRUNCATE(ProfileFile, |
445 | __llvm_profile_get_size_for_buffer()); |
446 | |
447 | (void)munmap(addr: ProfileBuffer, len: ProfileFileSize); |
448 | *MergeDone = 1; |
449 | |
450 | return 0; |
451 | } |
452 | |
453 | /* Create the directory holding the file, if needed. */ |
454 | static void createProfileDir(const char *Filename) { |
455 | size_t Length = strlen(s: Filename); |
456 | if (lprofFindFirstDirSeparator(Path: Filename)) { |
457 | char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1); |
458 | strncpy(dest: Copy, src: Filename, n: Length + 1); |
459 | __llvm_profile_recursive_mkdir(Pathname: Copy); |
460 | } |
461 | } |
462 | |
463 | /* Open the profile data for merging. It opens the file in r+b mode with |
464 | * file locking. If the file has content which is compatible with the |
465 | * current process, it also reads in the profile data in the file and merge |
466 | * it with in-memory counters. After the profile data is merged in memory, |
467 | * the original profile data is truncated and gets ready for the profile |
468 | * dumper. With profile merging enabled, each executable as well as any of |
469 | * its instrumented shared libraries dump profile data into their own data file. |
470 | */ |
471 | static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) { |
472 | FILE *ProfileFile = getProfileFile(); |
473 | int rc; |
474 | // initializeProfileForContinuousMode will lock the profile, but if |
475 | // ProfileFile is set by user via __llvm_profile_set_file_object, it's assumed |
476 | // unlocked at this point. |
477 | if (ProfileFile && !__llvm_profile_is_continuous_mode_enabled()) { |
478 | lprofLockFileHandle(F: ProfileFile); |
479 | } |
480 | if (!ProfileFile) { |
481 | createProfileDir(Filename: ProfileFileName); |
482 | ProfileFile = lprofOpenFileEx(Filename: ProfileFileName); |
483 | } |
484 | if (!ProfileFile) |
485 | return NULL; |
486 | |
487 | rc = doProfileMerging(ProfileFile, MergeDone); |
488 | if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) || |
489 | fseek(stream: ProfileFile, off: 0L, SEEK_SET) == -1) { |
490 | PROF_ERR("Profile Merging of file %s failed: %s\n" , ProfileFileName, |
491 | strerror(errno)); |
492 | fclose(stream: ProfileFile); |
493 | return NULL; |
494 | } |
495 | return ProfileFile; |
496 | } |
497 | |
498 | static FILE *getFileObject(const char *OutputName) { |
499 | FILE *File; |
500 | File = getProfileFile(); |
501 | if (File != NULL) { |
502 | return File; |
503 | } |
504 | |
505 | return fopen(filename: OutputName, modes: "ab" ); |
506 | } |
507 | |
508 | /* Write profile data to file \c OutputName. */ |
509 | static int writeFile(const char *OutputName) { |
510 | int RetVal; |
511 | FILE *OutputFile; |
512 | |
513 | int MergeDone = 0; |
514 | VPMergeHook = &lprofMergeValueProfData; |
515 | if (doMerging()) |
516 | OutputFile = openFileForMerging(ProfileFileName: OutputName, MergeDone: &MergeDone); |
517 | else |
518 | OutputFile = getFileObject(OutputName); |
519 | |
520 | if (!OutputFile) |
521 | return -1; |
522 | |
523 | FreeHook = &free; |
524 | setupIOBuffer(); |
525 | ProfDataWriter fileWriter; |
526 | initFileWriter(This: &fileWriter, File: OutputFile); |
527 | RetVal = lprofWriteData(Writer: &fileWriter, VPDataReader: lprofGetVPDataReader(), SkipNameDataWrite: MergeDone); |
528 | |
529 | if (OutputFile == getProfileFile()) { |
530 | fflush(stream: OutputFile); |
531 | if (doMerging() && !__llvm_profile_is_continuous_mode_enabled()) { |
532 | lprofUnlockFileHandle(F: OutputFile); |
533 | } |
534 | } else { |
535 | fclose(stream: OutputFile); |
536 | } |
537 | |
538 | return RetVal; |
539 | } |
540 | |
541 | /* Write order data to file \c OutputName. */ |
542 | static int writeOrderFile(const char *OutputName) { |
543 | int RetVal; |
544 | FILE *OutputFile; |
545 | |
546 | OutputFile = fopen(filename: OutputName, modes: "w" ); |
547 | |
548 | if (!OutputFile) { |
549 | PROF_WARN("can't open file with mode ab: %s\n" , OutputName); |
550 | return -1; |
551 | } |
552 | |
553 | FreeHook = &free; |
554 | setupIOBuffer(); |
555 | const uint32_t *DataBegin = __llvm_profile_begin_orderfile(); |
556 | RetVal = orderFileWriter(File: OutputFile, DataStart: DataBegin); |
557 | |
558 | fclose(stream: OutputFile); |
559 | return RetVal; |
560 | } |
561 | |
562 | #define LPROF_INIT_ONCE_ENV "__LLVM_PROFILE_RT_INIT_ONCE" |
563 | |
564 | static void truncateCurrentFile(void) { |
565 | const char *Filename; |
566 | char *FilenameBuf; |
567 | FILE *File; |
568 | int Length; |
569 | |
570 | Length = getCurFilenameLength(); |
571 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
572 | Filename = getCurFilename(FilenameBuf, ForceUseBuf: 0); |
573 | if (!Filename) |
574 | return; |
575 | |
576 | /* Only create the profile directory and truncate an existing profile once. |
577 | * In continuous mode, this is necessary, as the profile is written-to by the |
578 | * runtime initializer. */ |
579 | int initialized = getenv(LPROF_INIT_ONCE_ENV) != NULL; |
580 | if (initialized) |
581 | return; |
582 | #if defined(_WIN32) |
583 | _putenv(LPROF_INIT_ONCE_ENV "=" LPROF_INIT_ONCE_ENV); |
584 | #else |
585 | setenv(LPROF_INIT_ONCE_ENV, LPROF_INIT_ONCE_ENV, replace: 1); |
586 | #endif |
587 | |
588 | /* Create the profile dir (even if online merging is enabled), so that |
589 | * the profile file can be set up if continuous mode is enabled. */ |
590 | createProfileDir(Filename); |
591 | |
592 | /* By pass file truncation to allow online raw profile merging. */ |
593 | if (lprofCurFilename.MergePoolSize) |
594 | return; |
595 | |
596 | /* Truncate the file. Later we'll reopen and append. */ |
597 | File = fopen(filename: Filename, modes: "w" ); |
598 | if (!File) |
599 | return; |
600 | fclose(stream: File); |
601 | } |
602 | |
603 | /* Write a partial profile to \p Filename, which is required to be backed by |
604 | * the open file object \p File. */ |
605 | static int writeProfileWithFileObject(const char *Filename, FILE *File) { |
606 | setProfileFile(File); |
607 | int rc = writeFile(OutputName: Filename); |
608 | if (rc) |
609 | PROF_ERR("Failed to write file \"%s\": %s\n" , Filename, strerror(errno)); |
610 | setProfileFile(NULL); |
611 | return rc; |
612 | } |
613 | |
614 | static void initializeProfileForContinuousMode(void) { |
615 | if (!__llvm_profile_is_continuous_mode_enabled()) |
616 | return; |
617 | if (!ContinuousModeSupported) { |
618 | PROF_ERR("%s\n" , "continuous mode is unsupported on this platform" ); |
619 | return; |
620 | } |
621 | if (UseBiasVar && BiasAddr == BiasDefaultAddr) { |
622 | PROF_ERR("%s\n" , "__llvm_profile_counter_bias is undefined" ); |
623 | return; |
624 | } |
625 | |
626 | /* Get the sizes of counter section. */ |
627 | uint64_t = __llvm_profile_get_counters_size( |
628 | Begin: __llvm_profile_begin_counters(), End: __llvm_profile_end_counters()); |
629 | |
630 | int Length = getCurFilenameLength(); |
631 | char *FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
632 | const char *Filename = getCurFilename(FilenameBuf, ForceUseBuf: 0); |
633 | if (!Filename) |
634 | return; |
635 | |
636 | FILE *File = NULL; |
637 | uint64_t CurrentFileOffset = 0; |
638 | if (doMerging()) { |
639 | /* We are merging profiles. Map the counter section as shared memory into |
640 | * the profile, i.e. into each participating process. An increment in one |
641 | * process should be visible to every other process with the same counter |
642 | * section mapped. */ |
643 | File = lprofOpenFileEx(Filename); |
644 | if (!File) |
645 | return; |
646 | |
647 | uint64_t ProfileFileSize = 0; |
648 | if (getProfileFileSizeForMerging(ProfileFile: File, ProfileFileSize: &ProfileFileSize) == -1) { |
649 | lprofUnlockFileHandle(F: File); |
650 | fclose(stream: File); |
651 | return; |
652 | } |
653 | if (ProfileFileSize == 0) { |
654 | /* Grow the profile so that mmap() can succeed. Leak the file handle, as |
655 | * the file should stay open. */ |
656 | if (writeProfileWithFileObject(Filename, File) != 0) { |
657 | lprofUnlockFileHandle(F: File); |
658 | fclose(stream: File); |
659 | return; |
660 | } |
661 | } else { |
662 | /* The merged profile has a non-zero length. Check that it is compatible |
663 | * with the data in this process. */ |
664 | char *ProfileBuffer; |
665 | if (mmapProfileForMerging(ProfileFile: File, ProfileFileSize, ProfileBuffer: &ProfileBuffer) == -1) { |
666 | lprofUnlockFileHandle(F: File); |
667 | fclose(stream: File); |
668 | return; |
669 | } |
670 | (void)munmap(addr: ProfileBuffer, len: ProfileFileSize); |
671 | } |
672 | } else { |
673 | File = fopen(filename: Filename, modes: FileOpenMode); |
674 | if (!File) |
675 | return; |
676 | /* Check that the offset within the file is page-aligned. */ |
677 | CurrentFileOffset = ftell(stream: File); |
678 | unsigned PageSize = getpagesize(); |
679 | if (CurrentFileOffset % PageSize != 0) { |
680 | PROF_ERR("Continuous counter sync mode is enabled, but raw profile is not" |
681 | "page-aligned. CurrentFileOffset = %" PRIu64 ", pagesz = %u.\n" , |
682 | (uint64_t)CurrentFileOffset, PageSize); |
683 | fclose(stream: File); |
684 | return; |
685 | } |
686 | if (writeProfileWithFileObject(Filename, File) != 0) { |
687 | fclose(stream: File); |
688 | return; |
689 | } |
690 | } |
691 | |
692 | /* mmap() the profile counters so long as there is at least one counter. |
693 | * If there aren't any counters, mmap() would fail with EINVAL. */ |
694 | if (CountersSize > 0) |
695 | mmapForContinuousMode(CurrentFileOffset, File); |
696 | |
697 | if (doMerging()) { |
698 | lprofUnlockFileHandle(F: File); |
699 | } |
700 | if (File != NULL) { |
701 | fclose(stream: File); |
702 | } |
703 | } |
704 | |
705 | static const char *DefaultProfileName = "default.profraw" ; |
706 | static void resetFilenameToDefault(void) { |
707 | if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) { |
708 | #ifdef __GNUC__ |
709 | #pragma GCC diagnostic push |
710 | #pragma GCC diagnostic ignored "-Wcast-qual" |
711 | #elif defined(__clang__) |
712 | #pragma clang diagnostic push |
713 | #pragma clang diagnostic ignored "-Wcast-qual" |
714 | #endif |
715 | free(ptr: (void *)lprofCurFilename.FilenamePat); |
716 | #ifdef __GNUC__ |
717 | #pragma GCC diagnostic pop |
718 | #elif defined(__clang__) |
719 | #pragma clang diagnostic pop |
720 | #endif |
721 | } |
722 | memset(s: &lprofCurFilename, c: 0, n: sizeof(lprofCurFilename)); |
723 | lprofCurFilename.FilenamePat = DefaultProfileName; |
724 | lprofCurFilename.PNS = PNS_default; |
725 | } |
726 | |
727 | static unsigned getMergePoolSize(const char *FilenamePat, int *I) { |
728 | unsigned J = 0, Num = 0; |
729 | for (;; ++J) { |
730 | char C = FilenamePat[*I + J]; |
731 | if (C == 'm') { |
732 | *I += J; |
733 | return Num ? Num : 1; |
734 | } |
735 | if (C < '0' || C > '9') |
736 | break; |
737 | Num = Num * 10 + C - '0'; |
738 | |
739 | /* If FilenamePat[*I+J] is between '0' and '9', the next byte is guaranteed |
740 | * to be in-bound as the string is null terminated. */ |
741 | } |
742 | return 0; |
743 | } |
744 | |
745 | /* Assert that Idx does index past a string null terminator. Return the |
746 | * result of the check. */ |
747 | static int checkBounds(int Idx, int Strlen) { |
748 | assert(Idx <= Strlen && "Indexing past string null terminator" ); |
749 | return Idx <= Strlen; |
750 | } |
751 | |
752 | /* Parses the pattern string \p FilenamePat and stores the result to |
753 | * lprofcurFilename structure. */ |
754 | static int parseFilenamePattern(const char *FilenamePat, |
755 | unsigned CopyFilenamePat) { |
756 | int NumPids = 0, NumHosts = 0, I; |
757 | char *PidChars = &lprofCurFilename.PidChars[0]; |
758 | char *Hostname = &lprofCurFilename.Hostname[0]; |
759 | int MergingEnabled = 0; |
760 | int FilenamePatLen = strlen(s: FilenamePat); |
761 | |
762 | #ifdef __GNUC__ |
763 | #pragma GCC diagnostic push |
764 | #pragma GCC diagnostic ignored "-Wcast-qual" |
765 | #elif defined(__clang__) |
766 | #pragma clang diagnostic push |
767 | #pragma clang diagnostic ignored "-Wcast-qual" |
768 | #endif |
769 | /* Clean up cached prefix and filename. */ |
770 | if (lprofCurFilename.ProfilePathPrefix) |
771 | free(ptr: (void *)lprofCurFilename.ProfilePathPrefix); |
772 | |
773 | if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) { |
774 | free(ptr: (void *)lprofCurFilename.FilenamePat); |
775 | } |
776 | #ifdef __GNUC__ |
777 | #pragma GCC diagnostic pop |
778 | #elif defined(__clang__) |
779 | #pragma clang diagnostic pop |
780 | #endif |
781 | |
782 | memset(s: &lprofCurFilename, c: 0, n: sizeof(lprofCurFilename)); |
783 | |
784 | if (!CopyFilenamePat) |
785 | lprofCurFilename.FilenamePat = FilenamePat; |
786 | else { |
787 | lprofCurFilename.FilenamePat = strdup(s: FilenamePat); |
788 | lprofCurFilename.OwnsFilenamePat = 1; |
789 | } |
790 | /* Check the filename for "%p", which indicates a pid-substitution. */ |
791 | for (I = 0; checkBounds(Idx: I, Strlen: FilenamePatLen) && FilenamePat[I]; ++I) { |
792 | if (FilenamePat[I] == '%') { |
793 | ++I; /* Advance to the next character. */ |
794 | if (!checkBounds(Idx: I, Strlen: FilenamePatLen)) |
795 | break; |
796 | if (FilenamePat[I] == 'p') { |
797 | if (!NumPids++) { |
798 | if (snprintf(s: PidChars, MAX_PID_SIZE, format: "%ld" , (long)getpid()) <= 0) { |
799 | PROF_WARN("Unable to get pid for filename pattern %s. Using the " |
800 | "default name." , |
801 | FilenamePat); |
802 | return -1; |
803 | } |
804 | } |
805 | } else if (FilenamePat[I] == 'h') { |
806 | if (!NumHosts++) |
807 | if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) { |
808 | PROF_WARN("Unable to get hostname for filename pattern %s. Using " |
809 | "the default name." , |
810 | FilenamePat); |
811 | return -1; |
812 | } |
813 | } else if (FilenamePat[I] == 't') { |
814 | lprofCurFilename.TmpDir = getenv(name: "TMPDIR" ); |
815 | if (!lprofCurFilename.TmpDir) { |
816 | PROF_WARN("Unable to get the TMPDIR environment variable, referenced " |
817 | "in %s. Using the default path." , |
818 | FilenamePat); |
819 | return -1; |
820 | } |
821 | } else if (FilenamePat[I] == 'c') { |
822 | if (__llvm_profile_is_continuous_mode_enabled()) { |
823 | PROF_WARN("%%c specifier can only be specified once in %s.\n" , |
824 | FilenamePat); |
825 | __llvm_profile_disable_continuous_mode(); |
826 | return -1; |
827 | } |
828 | #if defined(__APPLE__) || defined(__ELF__) || defined(_WIN32) |
829 | __llvm_profile_set_page_size(PageSize: getpagesize()); |
830 | __llvm_profile_enable_continuous_mode(); |
831 | #else |
832 | PROF_WARN("%s" , "Continous mode is currently only supported for Mach-O," |
833 | " ELF and COFF formats." ); |
834 | return -1; |
835 | #endif |
836 | } else { |
837 | unsigned MergePoolSize = getMergePoolSize(FilenamePat, I: &I); |
838 | if (!MergePoolSize) |
839 | continue; |
840 | if (MergingEnabled) { |
841 | PROF_WARN("%%m specifier can only be specified once in %s.\n" , |
842 | FilenamePat); |
843 | return -1; |
844 | } |
845 | MergingEnabled = 1; |
846 | lprofCurFilename.MergePoolSize = MergePoolSize; |
847 | } |
848 | } |
849 | } |
850 | |
851 | lprofCurFilename.NumPids = NumPids; |
852 | lprofCurFilename.NumHosts = NumHosts; |
853 | return 0; |
854 | } |
855 | |
856 | static void parseAndSetFilename(const char *FilenamePat, |
857 | ProfileNameSpecifier PNS, |
858 | unsigned CopyFilenamePat) { |
859 | |
860 | const char *OldFilenamePat = lprofCurFilename.FilenamePat; |
861 | ProfileNameSpecifier OldPNS = lprofCurFilename.PNS; |
862 | |
863 | /* The old profile name specifier takes precedence over the old one. */ |
864 | if (PNS < OldPNS) |
865 | return; |
866 | |
867 | if (!FilenamePat) |
868 | FilenamePat = DefaultProfileName; |
869 | |
870 | if (OldFilenamePat && !strcmp(s1: OldFilenamePat, s2: FilenamePat)) { |
871 | lprofCurFilename.PNS = PNS; |
872 | return; |
873 | } |
874 | |
875 | /* When PNS >= OldPNS, the last one wins. */ |
876 | if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat)) |
877 | resetFilenameToDefault(); |
878 | lprofCurFilename.PNS = PNS; |
879 | |
880 | if (!OldFilenamePat) { |
881 | if (getenv(name: "LLVM_PROFILE_VERBOSE" )) |
882 | PROF_NOTE("Set profile file path to \"%s\" via %s.\n" , |
883 | lprofCurFilename.FilenamePat, getPNSStr(PNS)); |
884 | } else { |
885 | if (getenv(name: "LLVM_PROFILE_VERBOSE" )) |
886 | PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n" , |
887 | OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat, |
888 | getPNSStr(PNS)); |
889 | } |
890 | |
891 | truncateCurrentFile(); |
892 | if (__llvm_profile_is_continuous_mode_enabled()) |
893 | initializeProfileForContinuousMode(); |
894 | } |
895 | |
896 | /* Return buffer length that is required to store the current profile |
897 | * filename with PID and hostname substitutions. */ |
898 | /* The length to hold uint64_t followed by 3 digits pool id including '_' */ |
899 | #define SIGLEN 24 |
900 | static int getCurFilenameLength(void) { |
901 | int Len; |
902 | if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) |
903 | return 0; |
904 | |
905 | if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts || |
906 | lprofCurFilename.TmpDir || lprofCurFilename.MergePoolSize)) |
907 | return strlen(s: lprofCurFilename.FilenamePat); |
908 | |
909 | Len = strlen(s: lprofCurFilename.FilenamePat) + |
910 | lprofCurFilename.NumPids * (strlen(s: lprofCurFilename.PidChars) - 2) + |
911 | lprofCurFilename.NumHosts * (strlen(s: lprofCurFilename.Hostname) - 2) + |
912 | (lprofCurFilename.TmpDir ? (strlen(s: lprofCurFilename.TmpDir) - 1) : 0); |
913 | if (lprofCurFilename.MergePoolSize) |
914 | Len += SIGLEN; |
915 | return Len; |
916 | } |
917 | |
918 | /* Return the pointer to the current profile file name (after substituting |
919 | * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer |
920 | * to store the resulting filename. If no substitution is needed, the |
921 | * current filename pattern string is directly returned, unless ForceUseBuf |
922 | * is enabled. */ |
923 | static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) { |
924 | int I, J, PidLength, HostNameLength, TmpDirLength, FilenamePatLength; |
925 | const char *FilenamePat = lprofCurFilename.FilenamePat; |
926 | |
927 | if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) |
928 | return 0; |
929 | |
930 | if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts || |
931 | lprofCurFilename.TmpDir || lprofCurFilename.MergePoolSize || |
932 | __llvm_profile_is_continuous_mode_enabled())) { |
933 | if (!ForceUseBuf) |
934 | return lprofCurFilename.FilenamePat; |
935 | |
936 | FilenamePatLength = strlen(s: lprofCurFilename.FilenamePat); |
937 | memcpy(dest: FilenameBuf, src: lprofCurFilename.FilenamePat, n: FilenamePatLength); |
938 | FilenameBuf[FilenamePatLength] = '\0'; |
939 | return FilenameBuf; |
940 | } |
941 | |
942 | PidLength = strlen(s: lprofCurFilename.PidChars); |
943 | HostNameLength = strlen(s: lprofCurFilename.Hostname); |
944 | TmpDirLength = lprofCurFilename.TmpDir ? strlen(s: lprofCurFilename.TmpDir) : 0; |
945 | /* Construct the new filename. */ |
946 | for (I = 0, J = 0; FilenamePat[I]; ++I) |
947 | if (FilenamePat[I] == '%') { |
948 | if (FilenamePat[++I] == 'p') { |
949 | memcpy(dest: FilenameBuf + J, src: lprofCurFilename.PidChars, n: PidLength); |
950 | J += PidLength; |
951 | } else if (FilenamePat[I] == 'h') { |
952 | memcpy(dest: FilenameBuf + J, src: lprofCurFilename.Hostname, n: HostNameLength); |
953 | J += HostNameLength; |
954 | } else if (FilenamePat[I] == 't') { |
955 | memcpy(dest: FilenameBuf + J, src: lprofCurFilename.TmpDir, n: TmpDirLength); |
956 | FilenameBuf[J + TmpDirLength] = DIR_SEPARATOR; |
957 | J += TmpDirLength + 1; |
958 | } else { |
959 | if (!getMergePoolSize(FilenamePat, I: &I)) |
960 | continue; |
961 | char LoadModuleSignature[SIGLEN + 1]; |
962 | int S; |
963 | int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize; |
964 | S = snprintf(s: LoadModuleSignature, SIGLEN + 1, format: "%" PRIu64 "_%d" , |
965 | lprofGetLoadModuleSignature(), ProfilePoolId); |
966 | if (S == -1 || S > SIGLEN) |
967 | S = SIGLEN; |
968 | memcpy(dest: FilenameBuf + J, src: LoadModuleSignature, n: S); |
969 | J += S; |
970 | } |
971 | /* Drop any unknown substitutions. */ |
972 | } else |
973 | FilenameBuf[J++] = FilenamePat[I]; |
974 | FilenameBuf[J] = 0; |
975 | |
976 | return FilenameBuf; |
977 | } |
978 | |
979 | /* Returns the pointer to the environment variable |
980 | * string. Returns null if the env var is not set. */ |
981 | static const char *getFilenamePatFromEnv(void) { |
982 | const char *Filename = getenv(name: "LLVM_PROFILE_FILE" ); |
983 | if (!Filename || !Filename[0]) |
984 | return 0; |
985 | return Filename; |
986 | } |
987 | |
988 | COMPILER_RT_VISIBILITY |
989 | const char *__llvm_profile_get_path_prefix(void) { |
990 | int Length; |
991 | char *FilenameBuf, *Prefix; |
992 | const char *Filename, *PrefixEnd; |
993 | |
994 | if (lprofCurFilename.ProfilePathPrefix) |
995 | return lprofCurFilename.ProfilePathPrefix; |
996 | |
997 | Length = getCurFilenameLength(); |
998 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
999 | Filename = getCurFilename(FilenameBuf, ForceUseBuf: 0); |
1000 | if (!Filename) |
1001 | return "\0" ; |
1002 | |
1003 | PrefixEnd = lprofFindLastDirSeparator(Path: Filename); |
1004 | if (!PrefixEnd) |
1005 | return "\0" ; |
1006 | |
1007 | Length = PrefixEnd - Filename + 1; |
1008 | Prefix = (char *)malloc(size: Length + 1); |
1009 | if (!Prefix) { |
1010 | PROF_ERR("Failed to %s\n" , "allocate memory." ); |
1011 | return "\0" ; |
1012 | } |
1013 | memcpy(dest: Prefix, src: Filename, n: Length); |
1014 | Prefix[Length] = '\0'; |
1015 | lprofCurFilename.ProfilePathPrefix = Prefix; |
1016 | return Prefix; |
1017 | } |
1018 | |
1019 | COMPILER_RT_VISIBILITY |
1020 | const char *__llvm_profile_get_filename(void) { |
1021 | int Length; |
1022 | char *FilenameBuf; |
1023 | const char *Filename; |
1024 | |
1025 | Length = getCurFilenameLength(); |
1026 | FilenameBuf = (char *)malloc(size: Length + 1); |
1027 | if (!FilenameBuf) { |
1028 | PROF_ERR("Failed to %s\n" , "allocate memory." ); |
1029 | return "\0" ; |
1030 | } |
1031 | Filename = getCurFilename(FilenameBuf, ForceUseBuf: 1); |
1032 | if (!Filename) |
1033 | return "\0" ; |
1034 | |
1035 | return FilenameBuf; |
1036 | } |
1037 | |
1038 | /* This API initializes the file handling, both user specified |
1039 | * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE |
1040 | * environment variable can override this default value. |
1041 | */ |
1042 | COMPILER_RT_VISIBILITY |
1043 | void __llvm_profile_initialize_file(void) { |
1044 | const char *EnvFilenamePat; |
1045 | const char *SelectedPat = NULL; |
1046 | ProfileNameSpecifier PNS = PNS_unknown; |
1047 | int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0); |
1048 | |
1049 | EnvFilenamePat = getFilenamePatFromEnv(); |
1050 | if (EnvFilenamePat) { |
1051 | /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid |
1052 | at the moment when __llvm_profile_write_file() gets executed. */ |
1053 | parseAndSetFilename(FilenamePat: EnvFilenamePat, PNS: PNS_environment, CopyFilenamePat: 1); |
1054 | return; |
1055 | } else if (hasCommandLineOverrider) { |
1056 | SelectedPat = INSTR_PROF_PROFILE_NAME_VAR; |
1057 | PNS = PNS_command_line; |
1058 | } else { |
1059 | SelectedPat = NULL; |
1060 | PNS = PNS_default; |
1061 | } |
1062 | |
1063 | parseAndSetFilename(FilenamePat: SelectedPat, PNS, CopyFilenamePat: 0); |
1064 | } |
1065 | |
1066 | /* This method is invoked by the runtime initialization hook |
1067 | * InstrProfilingRuntime.o if it is linked in. |
1068 | */ |
1069 | COMPILER_RT_VISIBILITY |
1070 | void __llvm_profile_initialize(void) { |
1071 | __llvm_profile_initialize_file(); |
1072 | if (!__llvm_profile_is_continuous_mode_enabled()) |
1073 | __llvm_profile_register_write_file_atexit(); |
1074 | } |
1075 | |
1076 | /* This API is directly called by the user application code. It has the |
1077 | * highest precedence compared with LLVM_PROFILE_FILE environment variable |
1078 | * and command line option -fprofile-instr-generate=<profile_name>. |
1079 | */ |
1080 | COMPILER_RT_VISIBILITY |
1081 | void __llvm_profile_set_filename(const char *FilenamePat) { |
1082 | if (__llvm_profile_is_continuous_mode_enabled()) |
1083 | return; |
1084 | parseAndSetFilename(FilenamePat, PNS: PNS_runtime_api, CopyFilenamePat: 1); |
1085 | } |
1086 | |
1087 | /* The public API for writing profile data into the file with name |
1088 | * set by previous calls to __llvm_profile_set_filename or |
1089 | * __llvm_profile_override_default_filename or |
1090 | * __llvm_profile_initialize_file. */ |
1091 | COMPILER_RT_VISIBILITY |
1092 | int __llvm_profile_write_file(void) { |
1093 | int rc, Length; |
1094 | const char *Filename; |
1095 | char *FilenameBuf; |
1096 | |
1097 | // Temporarily suspend getting SIGKILL when the parent exits. |
1098 | int PDeathSig = lprofSuspendSigKill(); |
1099 | |
1100 | if (lprofProfileDumped() || __llvm_profile_is_continuous_mode_enabled()) { |
1101 | PROF_NOTE("Profile data not written to file: %s.\n" , "already written" ); |
1102 | if (PDeathSig == 1) |
1103 | lprofRestoreSigKill(); |
1104 | return 0; |
1105 | } |
1106 | |
1107 | Length = getCurFilenameLength(); |
1108 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
1109 | Filename = getCurFilename(FilenameBuf, ForceUseBuf: 0); |
1110 | |
1111 | /* Check the filename. */ |
1112 | if (!Filename) { |
1113 | PROF_ERR("Failed to write file : %s\n" , "Filename not set" ); |
1114 | if (PDeathSig == 1) |
1115 | lprofRestoreSigKill(); |
1116 | return -1; |
1117 | } |
1118 | |
1119 | /* Check if there is llvm/runtime version mismatch. */ |
1120 | if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { |
1121 | PROF_ERR("Runtime and instrumentation version mismatch : " |
1122 | "expected %d, but get %d\n" , |
1123 | INSTR_PROF_RAW_VERSION, |
1124 | (int)GET_VERSION(__llvm_profile_get_version())); |
1125 | if (PDeathSig == 1) |
1126 | lprofRestoreSigKill(); |
1127 | return -1; |
1128 | } |
1129 | |
1130 | /* Write profile data to the file. */ |
1131 | rc = writeFile(OutputName: Filename); |
1132 | if (rc) |
1133 | PROF_ERR("Failed to write file \"%s\": %s\n" , Filename, strerror(errno)); |
1134 | |
1135 | // Restore SIGKILL. |
1136 | if (PDeathSig == 1) |
1137 | lprofRestoreSigKill(); |
1138 | |
1139 | return rc; |
1140 | } |
1141 | |
1142 | COMPILER_RT_VISIBILITY |
1143 | int __llvm_profile_dump(void) { |
1144 | if (!doMerging()) |
1145 | PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering " |
1146 | " of previously dumped profile data : %s. Either use %%m " |
1147 | "in profile name or change profile name before dumping.\n" , |
1148 | "online profile merging is not on" ); |
1149 | int rc = __llvm_profile_write_file(); |
1150 | lprofSetProfileDumped(1); |
1151 | return rc; |
1152 | } |
1153 | |
1154 | /* Order file data will be saved in a file with suffx .order. */ |
1155 | static const char *OrderFileSuffix = ".order" ; |
1156 | |
1157 | COMPILER_RT_VISIBILITY |
1158 | int __llvm_orderfile_write_file(void) { |
1159 | int rc, Length, LengthBeforeAppend, SuffixLength; |
1160 | const char *Filename; |
1161 | char *FilenameBuf; |
1162 | |
1163 | // Temporarily suspend getting SIGKILL when the parent exits. |
1164 | int PDeathSig = lprofSuspendSigKill(); |
1165 | |
1166 | SuffixLength = strlen(s: OrderFileSuffix); |
1167 | Length = getCurFilenameLength() + SuffixLength; |
1168 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
1169 | Filename = getCurFilename(FilenameBuf, ForceUseBuf: 1); |
1170 | |
1171 | /* Check the filename. */ |
1172 | if (!Filename) { |
1173 | PROF_ERR("Failed to write file : %s\n" , "Filename not set" ); |
1174 | if (PDeathSig == 1) |
1175 | lprofRestoreSigKill(); |
1176 | return -1; |
1177 | } |
1178 | |
1179 | /* Append order file suffix */ |
1180 | LengthBeforeAppend = strlen(s: Filename); |
1181 | memcpy(dest: FilenameBuf + LengthBeforeAppend, src: OrderFileSuffix, n: SuffixLength); |
1182 | FilenameBuf[LengthBeforeAppend + SuffixLength] = '\0'; |
1183 | |
1184 | /* Check if there is llvm/runtime version mismatch. */ |
1185 | if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { |
1186 | PROF_ERR("Runtime and instrumentation version mismatch : " |
1187 | "expected %d, but get %d\n" , |
1188 | INSTR_PROF_RAW_VERSION, |
1189 | (int)GET_VERSION(__llvm_profile_get_version())); |
1190 | if (PDeathSig == 1) |
1191 | lprofRestoreSigKill(); |
1192 | return -1; |
1193 | } |
1194 | |
1195 | /* Write order data to the file. */ |
1196 | rc = writeOrderFile(OutputName: Filename); |
1197 | if (rc) |
1198 | PROF_ERR("Failed to write file \"%s\": %s\n" , Filename, strerror(errno)); |
1199 | |
1200 | // Restore SIGKILL. |
1201 | if (PDeathSig == 1) |
1202 | lprofRestoreSigKill(); |
1203 | |
1204 | return rc; |
1205 | } |
1206 | |
1207 | COMPILER_RT_VISIBILITY |
1208 | int __llvm_orderfile_dump(void) { |
1209 | int rc = __llvm_orderfile_write_file(); |
1210 | return rc; |
1211 | } |
1212 | |
1213 | static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); } |
1214 | |
1215 | COMPILER_RT_VISIBILITY |
1216 | int __llvm_profile_register_write_file_atexit(void) { |
1217 | static int HasBeenRegistered = 0; |
1218 | |
1219 | if (HasBeenRegistered) |
1220 | return 0; |
1221 | |
1222 | lprofSetupValueProfiler(); |
1223 | |
1224 | HasBeenRegistered = 1; |
1225 | return atexit(func: writeFileWithoutReturn); |
1226 | } |
1227 | |
1228 | COMPILER_RT_VISIBILITY int __llvm_profile_set_file_object(FILE *File, |
1229 | int EnableMerge) { |
1230 | if (__llvm_profile_is_continuous_mode_enabled()) { |
1231 | if (!EnableMerge) { |
1232 | PROF_WARN("__llvm_profile_set_file_object(fd=%d) not supported in " |
1233 | "continuous sync mode when merging is disabled\n" , |
1234 | fileno(File)); |
1235 | return 1; |
1236 | } |
1237 | if (lprofLockFileHandle(F: File) != 0) { |
1238 | PROF_WARN("Data may be corrupted during profile merging : %s\n" , |
1239 | "Fail to obtain file lock due to system limit." ); |
1240 | } |
1241 | uint64_t ProfileFileSize = 0; |
1242 | if (getProfileFileSizeForMerging(ProfileFile: File, ProfileFileSize: &ProfileFileSize) == -1) { |
1243 | lprofUnlockFileHandle(F: File); |
1244 | return 1; |
1245 | } |
1246 | if (ProfileFileSize == 0) { |
1247 | FreeHook = &free; |
1248 | setupIOBuffer(); |
1249 | ProfDataWriter fileWriter; |
1250 | initFileWriter(This: &fileWriter, File); |
1251 | if (lprofWriteData(Writer: &fileWriter, VPDataReader: 0, SkipNameDataWrite: 0)) { |
1252 | lprofUnlockFileHandle(F: File); |
1253 | PROF_ERR("Failed to write file \"%d\": %s\n" , fileno(File), |
1254 | strerror(errno)); |
1255 | return 1; |
1256 | } |
1257 | fflush(stream: File); |
1258 | } else { |
1259 | /* The merged profile has a non-zero length. Check that it is compatible |
1260 | * with the data in this process. */ |
1261 | char *ProfileBuffer; |
1262 | if (mmapProfileForMerging(ProfileFile: File, ProfileFileSize, ProfileBuffer: &ProfileBuffer) == -1) { |
1263 | lprofUnlockFileHandle(F: File); |
1264 | return 1; |
1265 | } |
1266 | (void)munmap(addr: ProfileBuffer, len: ProfileFileSize); |
1267 | } |
1268 | mmapForContinuousMode(CurrentFileOffset: 0, File); |
1269 | lprofUnlockFileHandle(F: File); |
1270 | } else { |
1271 | setProfileFile(File); |
1272 | setProfileMergeRequested(EnableMerge); |
1273 | } |
1274 | return 0; |
1275 | } |
1276 | |
1277 | #endif |
1278 | |