blob: 908900423daa23bdf846a88ccb00a91957bc31a5 [file] [log] [blame]
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00001/*===- 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 Smith9edbae02014-03-20 20:00:44 +000012static void __llvm_profile_write_file_with_name(const char *OutputName) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000013 FILE *OutputFile;
14 if (!OutputName || !OutputName[0])
15 return;
16 OutputFile = fopen(OutputName, "w");
17 if (!OutputFile) return;
18
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000019 /* TODO: mmap file to buffer of size __llvm_profile_get_size_for_buffer() and
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000020 * pass the buffer in, instead of the file.
21 */
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000022 __llvm_profile_write_buffer(OutputFile);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000023
24 fclose(OutputFile);
25}
26
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000027static const char *CurrentFilename = NULL;
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000028void __llvm_profile_set_filename(const char *Filename) {
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000029 CurrentFilename = Filename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000030}
31
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000032void __llvm_profile_write_file() {
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000033 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 Smith9edbae02014-03-20 20:00:44 +000041 __llvm_profile_write_file_with_name(Filename);
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000042}
43
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000044void __llvm_profile_register_write_file_atexit() {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000045 static int HasBeenRegistered = 0;
46
47 if (!HasBeenRegistered) {
48 HasBeenRegistered = 1;
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000049 atexit(__llvm_profile_write_file);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000050 }
51}