blob: bf7270680501a503c86f32cbcf365497af0ad58f [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"
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000011#include <string.h>
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000012
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000013static void __llvm_profile_write_file_with_name(const char *OutputName) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000014 FILE *OutputFile;
15 if (!OutputName || !OutputName[0])
16 return;
17 OutputFile = fopen(OutputName, "w");
18 if (!OutputFile) return;
19
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000020 /* TODO: mmap file to buffer of size __llvm_profile_get_size_for_buffer() and
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000021 * pass the buffer in, instead of the file.
22 */
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000023 __llvm_profile_write_buffer(OutputFile);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000024
25 fclose(OutputFile);
26}
27
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000028static const char *CurrentFilename = NULL;
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000029void __llvm_profile_set_filename(const char *Filename) {
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000030 CurrentFilename = Filename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000031}
32
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000033int getpid(void);
Duncan P. N. Exon Smith71704752014-03-20 20:55:00 +000034void __llvm_profile_write_file(void) {
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000035 char *AllocatedFilename = NULL;
36 int I, J;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000037
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000038#define MAX_PID_SIZE 16
39 char PidChars[MAX_PID_SIZE] = { 0 };
40 int PidLength = 0;
41 int NumPids = 0;
42
43 // Get the filename.
44 const char *Filename = CurrentFilename;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000045#define UPDATE_FILENAME(NextFilename) \
46 if (!Filename || !Filename[0]) Filename = NextFilename
47 UPDATE_FILENAME(getenv("LLVM_PROFILE_FILE"));
48 UPDATE_FILENAME("default.profdata");
49#undef UPDATE_FILENAME
50
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000051 // Check the filename for "%p", which indicates a pid-substitution.
52 for (I = 0; Filename[I]; ++I)
53 if (Filename[I] == '%' && Filename[++I] == 'p')
54 if (!NumPids++) {
55 PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
56 if (PidLength <= 0)
57 return;
58 }
59 if (NumPids) {
60 // Allocate enough space for the substituted filename.
61 AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1);
62 if (!AllocatedFilename)
63 return;
64
65 // Construct the new filename.
66 for (I = 0, J = 0; Filename[I]; ++I)
67 if (Filename[I] == '%') {
68 if (Filename[++I] == 'p') {
69 memcpy(AllocatedFilename + J, PidChars, PidLength);
70 J += PidLength;
71 }
72 // Drop any unknown substitutions.
73 } else
74 AllocatedFilename[J++] = Filename[I];
75 AllocatedFilename[J] = 0;
76
77 // Actually use the computed name.
78 Filename = AllocatedFilename;
79 }
80
81 // Write the file.
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000082 __llvm_profile_write_file_with_name(Filename);
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000083
84 // Free the filename.
85 if (AllocatedFilename)
86 free(AllocatedFilename);
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +000087}
88
Duncan P. N. Exon Smith71704752014-03-20 20:55:00 +000089void __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000090 static int HasBeenRegistered = 0;
91
92 if (!HasBeenRegistered) {
93 HasBeenRegistered = 1;
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000094 atexit(__llvm_profile_write_file);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000095 }
96}