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