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" |
| 11 | |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 12 | 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] | 13 | FILE *OutputFile; |
| 14 | if (!OutputName || !OutputName[0]) |
| 15 | return; |
| 16 | OutputFile = fopen(OutputName, "w"); |
| 17 | if (!OutputFile) return; |
| 18 | |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 19 | /* 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] | 20 | * pass the buffer in, instead of the file. |
| 21 | */ |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 22 | __llvm_profile_write_buffer(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 23 | |
| 24 | fclose(OutputFile); |
| 25 | } |
| 26 | |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 27 | static const char *CurrentFilename = NULL; |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 28 | void __llvm_profile_set_filename(const char *Filename) { |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 29 | CurrentFilename = Filename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 32 | void __llvm_profile_write_file() { |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 33 | const char *Filename = CurrentFilename; |
| 34 | |
| 35 | #define UPDATE_FILENAME(NextFilename) \ |
| 36 | if (!Filename || !Filename[0]) Filename = NextFilename |
| 37 | UPDATE_FILENAME(getenv("LLVM_PROFILE_FILE")); |
| 38 | UPDATE_FILENAME("default.profdata"); |
| 39 | #undef UPDATE_FILENAME |
| 40 | |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 41 | __llvm_profile_write_file_with_name(Filename); |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 44 | void __llvm_profile_register_write_file_atexit() { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 45 | static int HasBeenRegistered = 0; |
| 46 | |
| 47 | if (!HasBeenRegistered) { |
| 48 | HasBeenRegistered = 1; |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame^] | 49 | atexit(__llvm_profile_write_file); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 50 | } |
| 51 | } |