blob: 880059a994020eebc310952381d6f397d98e651b [file] [log] [blame]
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +00001/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00002|*
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"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000011#include "InstrProfilingInternal.h"
Xinliang David Li6e557162015-11-18 21:11:46 +000012#include "InstrProfilingUtil.h"
Joerg Sonnenbergeref24b412015-03-09 11:23:29 +000013#include <errno.h>
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000014#include <stdio.h>
15#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000016#include <string.h>
Xinliang David Li71eddbf2016-05-20 04:52:27 +000017#ifdef _MSC_VER
Xinliang David Li315e49d2016-05-24 21:29:18 +000018/* For _alloca. */
Xinliang David Li71eddbf2016-05-20 04:52:27 +000019#include <malloc.h>
Xinliang David Lifb320a12016-05-20 05:40:07 +000020#endif
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000021#if defined(_WIN32)
22#include "WindowsMMap.h"
23/* For _chsize_s */
24#include <io.h>
25#else
26#include <sys/file.h>
27#include <sys/mman.h>
28#include <unistd.h>
29#if defined(__linux__)
30#include <sys/types.h>
31#endif
32#endif
Xinliang David Li71eddbf2016-05-20 04:52:27 +000033
Xinliang David Li315e49d2016-05-24 21:29:18 +000034#define MAX_PID_SIZE 16
35/* Data structure holding the result of parsed filename pattern. */
36typedef struct lprofFilename {
37 /* File name string possibly with %p or %h specifiers. */
38 const char *FilenamePat;
39 char PidChars[MAX_PID_SIZE];
40 char Hostname[COMPILER_RT_MAX_HOSTLEN];
41 unsigned NumPids;
42 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000043 /* When in-process merging is enabled, this parameter specifies
44 * the total number of profile data files shared by all the processes
45 * spawned from the same binary. By default the value is 1. If merging
46 * is not enabled, its value should be 0. This parameter is specified
47 * by the %[0-9]m specifier. For instance %2m enables merging using
48 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
49 * can only appear once at the end of the name pattern. */
50 unsigned MergePoolSize;
Xinliang David Li315e49d2016-05-24 21:29:18 +000051} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000052
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000053lprofFilename lprofCurFilename = {0, {0}, {0}, 0, 0, 0};
Xinliang David Li315e49d2016-05-24 21:29:18 +000054
55int getpid(void);
56static int getCurFilenameLength();
57static const char *getCurFilename(char *FilenameBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000058static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000059
Xinliang David Li2d5bf072015-11-21 04:16:42 +000060/* Return 1 if there is an error, otherwise return 0. */
61static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
62 void **WriterCtx) {
63 uint32_t I;
64 FILE *File = (FILE *)*WriterCtx;
65 for (I = 0; I < NumIOVecs; I++) {
66 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
67 IOVecs[I].NumElm)
68 return 1;
69 }
70 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000071}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000072
Xinliang David Licda3bc22015-12-29 23:54:41 +000073COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +000074lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +000075 FreeHook = &free;
76 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
77 VPBufferSize = BufferSz;
78 return lprofCreateBufferIO(fileWriter, File);
79}
80
81static void setupIOBuffer() {
82 const char *BufferSzStr = 0;
83 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
84 if (BufferSzStr && BufferSzStr[0]) {
85 VPBufferSize = atoi(BufferSzStr);
86 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
87 }
Xinliang David Licda3bc22015-12-29 23:54:41 +000088}
89
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000090/* Read profile data in \c ProfileFile and merge with in-memory
91 profile counters. Returns -1 if there is fatal error, otheriwse
92 0 is returned.
93*/
94static int doProfileMerging(FILE *ProfileFile) {
95 uint64_t ProfileFileSize;
96 char *ProfileBuffer;
97
98 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
99 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
100 strerror(errno));
101 return -1;
102 }
103 ProfileFileSize = ftell(ProfileFile);
104
105 /* Restore file offset. */
106 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
107 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
108 strerror(errno));
109 return -1;
110 }
111
112 /* Nothing to merge. */
113 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
114 if (ProfileFileSize)
115 PROF_WARN("Unable to merge profile data: %s\n",
116 "source profile file is too small.");
117 return 0;
118 }
119
120 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
121 fileno(ProfileFile), 0);
122 if (ProfileBuffer == MAP_FAILED) {
123 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
124 strerror(errno));
125 return -1;
126 }
127
128 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
129 (void)munmap(ProfileBuffer, ProfileFileSize);
130 PROF_WARN("Unable to merge profile data: %s\n",
131 "source profile file is not compatible.");
132 return 0;
133 }
134
135 /* Now start merging */
136 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
137 (void)munmap(ProfileBuffer, ProfileFileSize);
138
139 return 0;
140}
141
142/* Open the profile data for merging. It opens the file in r+b mode with
143 * file locking. If the file has content which is compatible with the
144 * current process, it also reads in the profile data in the file and merge
145 * it with in-memory counters. After the profile data is merged in memory,
146 * the original profile data is truncated and gets ready for the profile
147 * dumper. With profile merging enabled, each executable as well as any of
148 * its instrumented shared libraries dump profile data into their own data file.
149*/
150static FILE *openFileForMerging(const char *ProfileFileName) {
151 FILE *ProfileFile;
152 int rc;
153
154 ProfileFile = lprofOpenFileEx(ProfileFileName);
155 if (!ProfileFile)
156 return NULL;
157
158 rc = doProfileMerging(ProfileFile);
159 if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
160 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
161 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
162 strerror(errno));
163 fclose(ProfileFile);
164 return NULL;
165 }
166 fseek(ProfileFile, 0L, SEEK_SET);
167 return ProfileFile;
168}
169
Xinliang David Li315e49d2016-05-24 21:29:18 +0000170/* Write profile data to file \c OutputName. */
171static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000172 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000173 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000174
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000175 if (!doMerging())
176 OutputFile = fopen(OutputName, "ab");
177 else
178 OutputFile = openFileForMerging(OutputName);
179
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000180 if (!OutputFile)
181 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000182
Xinliang David Li315e49d2016-05-24 21:29:18 +0000183 FreeHook = &free;
184 setupIOBuffer();
185 RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000186
187 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000188 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000189}
190
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000191static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000192 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000193 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000194 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000195 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000196
Xinliang David Li315e49d2016-05-24 21:29:18 +0000197 Length = getCurFilenameLength();
198 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
199 Filename = getCurFilename(FilenameBuf);
200 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000201 return;
202
Diego Novilloeae95142015-07-09 17:21:52 +0000203 /* Create the directory holding the file, if needed. */
Sean Silvac2feac72016-03-28 21:32:46 +0000204 if (strchr(Filename, '/') || strchr(Filename, '\\')) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000205 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
206 strncpy(Copy, Filename, Length + 1);
Diego Novilloeae95142015-07-09 17:21:52 +0000207 __llvm_profile_recursive_mkdir(Copy);
208 }
209
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000210 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000211 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000212 if (!File)
213 return;
214 fclose(File);
215}
216
Xinliang David Li315e49d2016-05-24 21:29:18 +0000217static void resetFilenameToDefault(void) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000218 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
219 lprofCurFilename.FilenamePat = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000220}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000221
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000222static int containsMergeSpecifier(const char *FilenamePat, int I) {
223 return (FilenamePat[I] == 'm' ||
224 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
225 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
226 * to be in-bound as the string is null terminated. */
227 FilenamePat[I + 1] == 'm'));
228}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000229
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000230/* Parses the pattern string \p FilenamePat and stores the result to
231 * lprofcurFilename structure. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000232static int parseFilenamePattern(const char *FilenamePat) {
233 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000234 char *PidChars = &lprofCurFilename.PidChars[0];
235 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000236 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000237
Xinliang David Li7f08d122016-05-25 17:30:15 +0000238 lprofCurFilename.FilenamePat = FilenamePat;
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000239 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000240 for (I = 0; FilenamePat[I]; ++I)
241 if (FilenamePat[I] == '%') {
242 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000243 if (!NumPids++) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000244 if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
245 PROF_WARN(
246 "Unable to parse filename pattern %s. Using the default name.",
247 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000248 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000249 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000250 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000251 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000252 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000253 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
254 PROF_WARN(
255 "Unable to parse filename pattern %s. Using the default name.",
256 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000257 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000258 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000259 } else if (containsMergeSpecifier(FilenamePat, I)) {
260 if (MergingEnabled) {
261 PROF_WARN(
262 "%%m specifier can only be specified once at the end of %s.\n",
263 FilenamePat);
264 return -1;
265 }
266 MergingEnabled = 1;
267 if (FilenamePat[I] == 'm')
268 lprofCurFilename.MergePoolSize = 1;
269 else {
270 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
271 I++; /* advance to 'm' */
272 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000273 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000274 }
275
Xinliang David Li7f08d122016-05-25 17:30:15 +0000276 lprofCurFilename.NumPids = NumPids;
277 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000278 return 0;
279}
280
Xinliang David Li7f08d122016-05-25 17:30:15 +0000281static void parseAndSetFilename(const char *FilenamePat) {
282 int NewFile;
283 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
284
285 if (!FilenamePat || parseFilenamePattern(FilenamePat))
286 resetFilenameToDefault();
287
288 NewFile =
289 !OldFilenamePat || (strcmp(OldFilenamePat, lprofCurFilename.FilenamePat));
290
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000291 if (NewFile && !lprofCurFilename.MergePoolSize)
Xinliang David Li7f08d122016-05-25 17:30:15 +0000292 truncateCurrentFile();
293}
294
Xinliang David Li315e49d2016-05-24 21:29:18 +0000295/* Return buffer length that is required to store the current profile
296 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000297/* The length to hold uint64_t followed by 2 digit pool id including '_' */
298#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000299static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000300 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000301 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000302 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000303
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000304 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
305 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000306 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000307
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000308 Len = strlen(lprofCurFilename.FilenamePat) +
309 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
310 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
311 if (lprofCurFilename.MergePoolSize)
312 Len += SIGLEN;
313 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000314}
315
316/* Return the pointer to the current profile file name (after substituting
317 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
318 * to store the resulting filename. If no substitution is needed, the
319 * current filename pattern string is directly returned. */
320static const char *getCurFilename(char *FilenameBuf) {
321 int I, J, PidLength, HostNameLength;
322 const char *FilenamePat = lprofCurFilename.FilenamePat;
323
324 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
325 return 0;
326
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000327 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
328 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000329 return lprofCurFilename.FilenamePat;
330
331 PidLength = strlen(lprofCurFilename.PidChars);
332 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000333 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000334 for (I = 0, J = 0; FilenamePat[I]; ++I)
335 if (FilenamePat[I] == '%') {
336 if (FilenamePat[++I] == 'p') {
337 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000338 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000339 } else if (FilenamePat[I] == 'h') {
340 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000341 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000342 } else if (containsMergeSpecifier(FilenamePat, I)) {
343 char LoadModuleSignature[SIGLEN];
344 int S;
345 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
346 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
347 lprofGetLoadModuleSignature(), ProfilePoolId);
348 if (S == -1 || S > SIGLEN)
349 S = SIGLEN;
350 memcpy(FilenameBuf + J, LoadModuleSignature, S);
351 J += S;
352 if (FilenamePat[I] != 'm')
353 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000354 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000355 /* Drop any unknown substitutions. */
356 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000357 FilenameBuf[J++] = FilenamePat[I];
358 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000359
Xinliang David Li315e49d2016-05-24 21:29:18 +0000360 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000361}
362
Xinliang David Li315e49d2016-05-24 21:29:18 +0000363/* Returns the pointer to the environment variable
364 * string. Returns null if the env var is not set. */
365static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000366 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000367 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000368 return 0;
369 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000370}
371
Xinliang David Li51fe0022016-05-24 01:23:09 +0000372/* This method is invoked by the runtime initialization hook
373 * InstrProfilingRuntime.o if it is linked in. Both user specified
374 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
375 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000376COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000377void __llvm_profile_initialize_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000378 const char *FilenamePat;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000379 /* Check if the filename has been initialized. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000380 if (lprofCurFilename.FilenamePat)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000381 return;
382
383 /* Detect the filename and truncate. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000384 FilenamePat = getFilenamePatFromEnv();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000385 parseAndSetFilename(FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000386}
387
Xinliang David Li51fe0022016-05-24 01:23:09 +0000388/* This API is directly called by the user application code. It has the
389 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000390 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000391 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000392COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000393void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000394 parseAndSetFilename(FilenamePat);
Eric Christopherd641b532015-04-28 22:54:51 +0000395}
396
Xinliang David Li315e49d2016-05-24 21:29:18 +0000397/*
Xinliang David Li51fe0022016-05-24 01:23:09 +0000398 * This API is invoked by the global initializers emitted by Clang/LLVM when
399 * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate
400 * without an argument). This option has lower precedence than the
401 * LLVM_PROFILE_FILE environment variable.
402 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000403COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000404void __llvm_profile_override_default_filename(const char *FilenamePat) {
Eric Christopherd641b532015-04-28 22:54:51 +0000405 /* If the env var is set, skip setting filename from argument. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000406 const char *Env_Filename = getFilenamePatFromEnv();
Xinliang David Li51fe0022016-05-24 01:23:09 +0000407 if (Env_Filename)
Eric Christopherd641b532015-04-28 22:54:51 +0000408 return;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000409
410 parseAndSetFilename(FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000411}
412
Xinliang David Li315e49d2016-05-24 21:29:18 +0000413/* The public API for writing profile data into the file with name
414 * set by previous calls to __llvm_profile_set_filename or
415 * __llvm_profile_override_default_filename or
416 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000417COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000418int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000419 int rc, Length;
420 const char *Filename;
421 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000422
Xinliang David Li315e49d2016-05-24 21:29:18 +0000423 Length = getCurFilenameLength();
424 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
425 Filename = getCurFilename(FilenameBuf);
426
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000427 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000428 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000429 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000430 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000431 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000432
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000433 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000434 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000435 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000436 "expected %d, but get %d\n",
437 INSTR_PROF_RAW_VERSION,
438 (int)GET_VERSION(__llvm_profile_get_version()));
439 return -1;
440 }
441
Xinliang David Li315e49d2016-05-24 21:29:18 +0000442 /* Write profile data to the file. */
443 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000444 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000445 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Justin Bogner66fd5c92015-01-16 20:10:56 +0000446 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000447}
448
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000449static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000450
Xinliang David Liabfd5532015-12-16 03:29:15 +0000451COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000452int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000453 static int HasBeenRegistered = 0;
454
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000455 if (HasBeenRegistered)
456 return 0;
457
Xinliang David Li4617aa72016-05-18 22:34:05 +0000458 lprofSetupValueProfiler();
459
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000460 HasBeenRegistered = 1;
461 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000462}