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