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" |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 13 | #include <string.h> |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 14 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 15 | static int writeFile(FILE *File) { |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame^] | 16 | /* Match logic in __llvm_profile_write_buffer(). */ |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 17 | const __llvm_profile_data *DataBegin = __llvm_profile_data_begin(); |
| 18 | const __llvm_profile_data *DataEnd = __llvm_profile_data_end(); |
| 19 | const uint64_t *CountersBegin = __llvm_profile_counters_begin(); |
| 20 | const uint64_t *CountersEnd = __llvm_profile_counters_end(); |
| 21 | const char *NamesBegin = __llvm_profile_names_begin(); |
| 22 | const char *NamesEnd = __llvm_profile_names_end(); |
| 23 | |
| 24 | /* Calculate size of sections. */ |
| 25 | const uint64_t DataSize = DataEnd - DataBegin; |
| 26 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 27 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
| 28 | |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame^] | 29 | /* Create the header. */ |
| 30 | uint64_t Header[PROFILE_HEADER_SIZE] = { |
| 31 | __llvm_profile_get_magic(), |
| 32 | __llvm_profile_get_version(), |
| 33 | DataSize, |
| 34 | CountersSize, |
| 35 | NamesSize, |
| 36 | (uint64_t)CountersBegin, |
| 37 | (uint64_t)NamesBegin |
| 38 | }; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 39 | |
| 40 | /* Write the data. */ |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame^] | 41 | #define CHECK_fwrite(Data, Size, Length, File) \ |
| 42 | do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0) |
| 43 | CHECK_fwrite(Header, sizeof(uint64_t), PROFILE_HEADER_SIZE, File); |
| 44 | CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 45 | CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File); |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame^] | 46 | CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 47 | #undef CHECK_fwrite |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 52 | static int writeFileWithName(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 53 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 54 | FILE *OutputFile; |
| 55 | if (!OutputName || !OutputName[0]) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 56 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 57 | OutputFile = fopen(OutputName, "w"); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 58 | if (!OutputFile) |
| 59 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 60 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 61 | RetVal = writeFile(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 62 | |
| 63 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 64 | return RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 67 | static const char *CurrentFilename = NULL; |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 68 | void __llvm_profile_set_filename(const char *Filename) { |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 69 | CurrentFilename = Filename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 72 | int getpid(void); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 73 | int __llvm_profile_write_file(void) { |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 74 | char *AllocatedFilename = NULL; |
| 75 | int I, J; |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 76 | int RetVal; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 77 | |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 78 | #define MAX_PID_SIZE 16 |
| 79 | char PidChars[MAX_PID_SIZE] = { 0 }; |
| 80 | int PidLength = 0; |
| 81 | int NumPids = 0; |
| 82 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 83 | /* Get the filename. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 84 | const char *Filename = CurrentFilename; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 85 | #define UPDATE_FILENAME(NextFilename) \ |
| 86 | if (!Filename || !Filename[0]) Filename = NextFilename |
| 87 | UPDATE_FILENAME(getenv("LLVM_PROFILE_FILE")); |
| 88 | UPDATE_FILENAME("default.profdata"); |
| 89 | #undef UPDATE_FILENAME |
| 90 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 91 | /* 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] | 92 | for (I = 0; Filename[I]; ++I) |
| 93 | if (Filename[I] == '%' && Filename[++I] == 'p') |
| 94 | if (!NumPids++) { |
| 95 | PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()); |
| 96 | if (PidLength <= 0) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 97 | return -1; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 98 | } |
| 99 | if (NumPids) { |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 100 | /* Allocate enough space for the substituted filename. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 101 | AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1); |
| 102 | if (!AllocatedFilename) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 103 | return -1; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 104 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 105 | /* Construct the new filename. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 106 | for (I = 0, J = 0; Filename[I]; ++I) |
| 107 | if (Filename[I] == '%') { |
| 108 | if (Filename[++I] == 'p') { |
| 109 | memcpy(AllocatedFilename + J, PidChars, PidLength); |
| 110 | J += PidLength; |
| 111 | } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 112 | /* Drop any unknown substitutions. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 113 | } else |
| 114 | AllocatedFilename[J++] = Filename[I]; |
| 115 | AllocatedFilename[J] = 0; |
| 116 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 117 | /* Actually use the computed name. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 118 | Filename = AllocatedFilename; |
| 119 | } |
| 120 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 121 | /* Write the file. */ |
| 122 | RetVal = writeFileWithName(Filename); |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 123 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 124 | /* Free the filename. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 125 | if (AllocatedFilename) |
| 126 | free(AllocatedFilename); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 127 | |
| 128 | return RetVal; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 131 | static void writeFileWithoutReturn(void) { |
| 132 | __llvm_profile_write_file(); |
| 133 | } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 134 | |
| 135 | int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 136 | static int HasBeenRegistered = 0; |
| 137 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 138 | if (HasBeenRegistered) |
| 139 | return 0; |
| 140 | |
| 141 | HasBeenRegistered = 1; |
| 142 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 143 | } |