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 | |
| 12 | /*! \brief Write instrumentation data to the given file. */ |
| 13 | void __llvm_pgo_write_file(const char *OutputName) { |
| 14 | /* TODO: Requires libc: move to separate translation unit. */ |
| 15 | FILE *OutputFile; |
| 16 | if (!OutputName || !OutputName[0]) |
| 17 | return; |
| 18 | OutputFile = fopen(OutputName, "w"); |
| 19 | if (!OutputFile) return; |
| 20 | |
| 21 | /* TODO: mmap file to buffer of size __llvm_pgo_get_size_for_buffer() and |
| 22 | * pass the buffer in, instead of the file. |
| 23 | */ |
| 24 | __llvm_pgo_write_buffer(OutputFile); |
| 25 | |
| 26 | fclose(OutputFile); |
| 27 | } |
| 28 | |
| 29 | /*! \brief Write instrumentation data to the default file. */ |
| 30 | void __llvm_pgo_write_default_file() { |
| 31 | /* TODO: Requires libc: move to separate translation unit. */ |
| 32 | const char *OutputName = getenv("LLVM_PROFILE_FILE"); |
| 33 | if (OutputName == NULL || OutputName[0] == '\0') |
| 34 | OutputName = "default.profdata"; |
| 35 | __llvm_pgo_write_file(OutputName); |
| 36 | } |
| 37 | |
| 38 | /*! |
| 39 | * \brief Register to write instrumentation data to the default file at exit. |
| 40 | */ |
| 41 | void __llvm_pgo_register_write_atexit() { |
| 42 | /* TODO: Requires libc: move to separate translation unit. */ |
| 43 | static int HasBeenRegistered = 0; |
| 44 | |
| 45 | if (!HasBeenRegistered) { |
| 46 | HasBeenRegistered = 1; |
| 47 | atexit(__llvm_pgo_write_default_file); |
| 48 | } |
| 49 | } |