Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 1 | /*===- InstrProfilingExtras.c - Support library for PGO instrumentation ---===*\ |
| 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" |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame^] | 11 | #include <string.h> |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 12 | |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 13 | static void __llvm_profile_write_file_with_name(const char *OutputName) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 14 | FILE *OutputFile; |
| 15 | if (!OutputName || !OutputName[0]) |
| 16 | return; |
| 17 | OutputFile = fopen(OutputName, "w"); |
| 18 | if (!OutputFile) return; |
| 19 | |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 20 | /* TODO: mmap file to buffer of size __llvm_profile_get_size_for_buffer() and |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 21 | * pass the buffer in, instead of the file. |
| 22 | */ |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 23 | __llvm_profile_write_buffer(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 24 | |
| 25 | fclose(OutputFile); |
| 26 | } |
| 27 | |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 28 | static const char *CurrentFilename = NULL; |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 29 | void __llvm_profile_set_filename(const char *Filename) { |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 30 | CurrentFilename = Filename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame^] | 33 | int getpid(void); |
Duncan P. N. Exon Smith | 7170475 | 2014-03-20 20:55:00 +0000 | [diff] [blame] | 34 | void __llvm_profile_write_file(void) { |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame^] | 35 | char *AllocatedFilename = NULL; |
| 36 | int I, J; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 37 | |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame^] | 38 | #define MAX_PID_SIZE 16 |
| 39 | char PidChars[MAX_PID_SIZE] = { 0 }; |
| 40 | int PidLength = 0; |
| 41 | int NumPids = 0; |
| 42 | |
| 43 | // Get the filename. |
| 44 | const char *Filename = CurrentFilename; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 45 | #define UPDATE_FILENAME(NextFilename) \ |
| 46 | if (!Filename || !Filename[0]) Filename = NextFilename |
| 47 | UPDATE_FILENAME(getenv("LLVM_PROFILE_FILE")); |
| 48 | UPDATE_FILENAME("default.profdata"); |
| 49 | #undef UPDATE_FILENAME |
| 50 | |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame^] | 51 | // Check the filename for "%p", which indicates a pid-substitution. |
| 52 | for (I = 0; Filename[I]; ++I) |
| 53 | if (Filename[I] == '%' && Filename[++I] == 'p') |
| 54 | if (!NumPids++) { |
| 55 | PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()); |
| 56 | if (PidLength <= 0) |
| 57 | return; |
| 58 | } |
| 59 | if (NumPids) { |
| 60 | // Allocate enough space for the substituted filename. |
| 61 | AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1); |
| 62 | if (!AllocatedFilename) |
| 63 | return; |
| 64 | |
| 65 | // Construct the new filename. |
| 66 | for (I = 0, J = 0; Filename[I]; ++I) |
| 67 | if (Filename[I] == '%') { |
| 68 | if (Filename[++I] == 'p') { |
| 69 | memcpy(AllocatedFilename + J, PidChars, PidLength); |
| 70 | J += PidLength; |
| 71 | } |
| 72 | // Drop any unknown substitutions. |
| 73 | } else |
| 74 | AllocatedFilename[J++] = Filename[I]; |
| 75 | AllocatedFilename[J] = 0; |
| 76 | |
| 77 | // Actually use the computed name. |
| 78 | Filename = AllocatedFilename; |
| 79 | } |
| 80 | |
| 81 | // Write the file. |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 82 | __llvm_profile_write_file_with_name(Filename); |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame^] | 83 | |
| 84 | // Free the filename. |
| 85 | if (AllocatedFilename) |
| 86 | free(AllocatedFilename); |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Duncan P. N. Exon Smith | 7170475 | 2014-03-20 20:55:00 +0000 | [diff] [blame] | 89 | void __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 90 | static int HasBeenRegistered = 0; |
| 91 | |
| 92 | if (!HasBeenRegistered) { |
| 93 | HasBeenRegistered = 1; |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 94 | atexit(__llvm_profile_write_file); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 95 | } |
| 96 | } |