blob: 745bcb0db8fa746222c51611ba59935706b6fd13 [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 Smith54cc0e22014-03-20 19:23:55 +000012static void __llvm_pgo_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
19 /* TODO: mmap file to buffer of size __llvm_pgo_get_size_for_buffer() and
20 * pass the buffer in, instead of the file.
21 */
22 __llvm_pgo_write_buffer(OutputFile);
23
24 fclose(OutputFile);
25}
26
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000027static const char *CurrentFilename = NULL;
28void __llvm_pgo_set_filename(const char *Filename) {
29 CurrentFilename = Filename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000030}
31
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000032void __llvm_pgo_write_file() {
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
41 __llvm_pgo_write_file_with_name(Filename);
42}
43
44void __llvm_pgo_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 Smith54cc0e22014-03-20 19:23:55 +000049 atexit(__llvm_pgo_write_file);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000050 }
51}