blob: d038bb9cb5b0f973aef1764a8b86d859264e4951 [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 Li153e8b62016-06-10 20:35:01 +000034/* From where is profile name specified.
35 * The order the enumerators define their
36 * precedence. Re-order them may lead to
37 * runtime behavior change. */
38typedef enum ProfileNameSpecifier {
39 PNS_unknown = 0,
40 PNS_default,
41 PNS_command_line,
42 PNS_environment,
43 PNS_runtime_api
44} ProfileNameSpecifier;
45
46static const char *getPNSStr(ProfileNameSpecifier PNS) {
47 switch (PNS) {
48 case PNS_default:
49 return "default setting";
50 case PNS_command_line:
51 return "command line";
52 case PNS_environment:
53 return "environment variable";
54 case PNS_runtime_api:
55 return "runtime API";
56 default:
57 return "Unknown";
58 }
59}
60
Xinliang David Li315e49d2016-05-24 21:29:18 +000061#define MAX_PID_SIZE 16
Ying Yi2c614cf2016-08-10 10:48:02 +000062/* Data structure holding the result of parsed filename pattern. */
Xinliang David Li315e49d2016-05-24 21:29:18 +000063typedef struct lprofFilename {
64 /* File name string possibly with %p or %h specifiers. */
65 const char *FilenamePat;
Xinliang David Li14c91c42016-08-02 19:34:00 +000066 /* A flag indicating if FilenamePat's memory is allocated
67 * by runtime. */
68 unsigned OwnsFilenamePat;
Xinliang David Lieaf238d2016-07-20 04:26:09 +000069 const char *ProfilePathPrefix;
Xinliang David Li315e49d2016-05-24 21:29:18 +000070 char PidChars[MAX_PID_SIZE];
71 char Hostname[COMPILER_RT_MAX_HOSTLEN];
72 unsigned NumPids;
73 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000074 /* When in-process merging is enabled, this parameter specifies
75 * the total number of profile data files shared by all the processes
76 * spawned from the same binary. By default the value is 1. If merging
77 * is not enabled, its value should be 0. This parameter is specified
78 * by the %[0-9]m specifier. For instance %2m enables merging using
79 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
80 * can only appear once at the end of the name pattern. */
81 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000082 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000083} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000084
Vedant Kumar6ff82bd2016-10-18 00:02:28 +000085COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0},
86 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000087
88int getpid(void);
89static int getCurFilenameLength();
90static const char *getCurFilename(char *FilenameBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000091static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000092
Xinliang David Li2d5bf072015-11-21 04:16:42 +000093/* Return 1 if there is an error, otherwise return 0. */
Xinliang David Li967669f2017-06-27 17:28:01 +000094static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
95 uint32_t NumIOVecs) {
Xinliang David Li2d5bf072015-11-21 04:16:42 +000096 uint32_t I;
Xinliang David Li967669f2017-06-27 17:28:01 +000097 FILE *File = (FILE *)This->WriterCtx;
Xinliang David Li2d5bf072015-11-21 04:16:42 +000098 for (I = 0; I < NumIOVecs; I++) {
Xinliang David Lif50cc3e2017-06-28 16:46:06 +000099 if (IOVecs[I].Data) {
100 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
101 IOVecs[I].NumElm)
102 return 1;
103 } else {
104 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1)
105 return 1;
106 }
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000107 }
108 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000109}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000110
Xinliang David Li967669f2017-06-27 17:28:01 +0000111static void initFileWriter(ProfDataWriter *This, FILE *File) {
112 This->Write = fileWriter;
113 This->WriterCtx = File;
114}
115
Xinliang David Licda3bc22015-12-29 23:54:41 +0000116COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000117lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000118 FreeHook = &free;
119 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
120 VPBufferSize = BufferSz;
Xinliang David Li967669f2017-06-27 17:28:01 +0000121 ProfDataWriter *fileWriter =
122 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
123 initFileWriter(fileWriter, File);
124 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
125 IO->OwnFileWriter = 1;
126 return IO;
Xinliang David Li609fae32016-05-13 18:26:26 +0000127}
128
129static void setupIOBuffer() {
130 const char *BufferSzStr = 0;
131 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
132 if (BufferSzStr && BufferSzStr[0]) {
133 VPBufferSize = atoi(BufferSzStr);
134 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
135 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000136}
137
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000138/* Read profile data in \c ProfileFile and merge with in-memory
139 profile counters. Returns -1 if there is fatal error, otheriwse
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000140 0 is returned. Returning 0 does not mean merge is actually
141 performed. If merge is actually done, *MergeDone is set to 1.
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000142*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000143static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000144 uint64_t ProfileFileSize;
145 char *ProfileBuffer;
146
147 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
148 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
149 strerror(errno));
150 return -1;
151 }
152 ProfileFileSize = ftell(ProfileFile);
153
154 /* Restore file offset. */
155 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
156 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
157 strerror(errno));
158 return -1;
159 }
160
161 /* Nothing to merge. */
162 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
163 if (ProfileFileSize)
164 PROF_WARN("Unable to merge profile data: %s\n",
165 "source profile file is too small.");
166 return 0;
167 }
168
169 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
170 fileno(ProfileFile), 0);
171 if (ProfileBuffer == MAP_FAILED) {
172 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
173 strerror(errno));
174 return -1;
175 }
176
177 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
178 (void)munmap(ProfileBuffer, ProfileFileSize);
179 PROF_WARN("Unable to merge profile data: %s\n",
180 "source profile file is not compatible.");
181 return 0;
182 }
183
184 /* Now start merging */
185 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
186 (void)munmap(ProfileBuffer, ProfileFileSize);
187
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000188 *MergeDone = 1;
189
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000190 return 0;
191}
192
Xinliang David Lif2d94812017-02-14 21:39:55 +0000193/* Create the directory holding the file, if needed. */
194static void createProfileDir(const char *Filename) {
195 size_t Length = strlen(Filename);
196 if (lprofFindFirstDirSeparator(Filename)) {
197 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
198 strncpy(Copy, Filename, Length + 1);
199 __llvm_profile_recursive_mkdir(Copy);
200 }
201}
202
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000203/* Open the profile data for merging. It opens the file in r+b mode with
204 * file locking. If the file has content which is compatible with the
205 * current process, it also reads in the profile data in the file and merge
206 * it with in-memory counters. After the profile data is merged in memory,
207 * the original profile data is truncated and gets ready for the profile
208 * dumper. With profile merging enabled, each executable as well as any of
209 * its instrumented shared libraries dump profile data into their own data file.
210*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000211static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000212 FILE *ProfileFile;
213 int rc;
214
Xinliang David Lif2d94812017-02-14 21:39:55 +0000215 createProfileDir(ProfileFileName);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000216 ProfileFile = lprofOpenFileEx(ProfileFileName);
217 if (!ProfileFile)
218 return NULL;
219
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000220 rc = doProfileMerging(ProfileFile, MergeDone);
221 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) ||
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000222 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
223 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
224 strerror(errno));
225 fclose(ProfileFile);
226 return NULL;
227 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000228 return ProfileFile;
229}
230
Xinliang David Li315e49d2016-05-24 21:29:18 +0000231/* Write profile data to file \c OutputName. */
232static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000233 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000234 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000235
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000236 int MergeDone = 0;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000237 if (!doMerging())
238 OutputFile = fopen(OutputName, "ab");
239 else
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000240 OutputFile = openFileForMerging(OutputName, &MergeDone);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000241
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000242 if (!OutputFile)
243 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000244
Xinliang David Li315e49d2016-05-24 21:29:18 +0000245 FreeHook = &free;
246 setupIOBuffer();
Xinliang David Li967669f2017-06-27 17:28:01 +0000247 ProfDataWriter fileWriter;
248 initFileWriter(&fileWriter, OutputFile);
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000249 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000250
251 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000252 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000253}
254
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000255static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000256 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000257 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000258 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000259 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000260
Xinliang David Li315e49d2016-05-24 21:29:18 +0000261 Length = getCurFilenameLength();
262 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
263 Filename = getCurFilename(FilenameBuf);
264 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000265 return;
266
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000267 /* By pass file truncation to allow online raw profile
268 * merging. */
269 if (lprofCurFilename.MergePoolSize)
270 return;
271
Xinliang David Lif2d94812017-02-14 21:39:55 +0000272 createProfileDir(Filename);
273
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000274 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000275 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000276 if (!File)
277 return;
278 fclose(File);
279}
280
Xinliang David Li153e8b62016-06-10 20:35:01 +0000281static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000282static void resetFilenameToDefault(void) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000283 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
284 free((void *)lprofCurFilename.FilenamePat);
285 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000286 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000287 lprofCurFilename.FilenamePat = DefaultProfileName;
288 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000289}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000290
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000291static int containsMergeSpecifier(const char *FilenamePat, int I) {
292 return (FilenamePat[I] == 'm' ||
293 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
294 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
295 * to be in-bound as the string is null terminated. */
296 FilenamePat[I + 1] == 'm'));
297}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000298
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000299/* Parses the pattern string \p FilenamePat and stores the result to
300 * lprofcurFilename structure. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000301static int parseFilenamePattern(const char *FilenamePat,
302 unsigned CopyFilenamePat) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000303 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000304 char *PidChars = &lprofCurFilename.PidChars[0];
305 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000306 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000307
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000308 /* Clean up cached prefix. */
309 if (lprofCurFilename.ProfilePathPrefix)
310 free((void *)lprofCurFilename.ProfilePathPrefix);
311 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
312
Xinliang David Li14c91c42016-08-02 19:34:00 +0000313 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
314 free((void *)lprofCurFilename.FilenamePat);
315 }
316
317 if (!CopyFilenamePat)
318 lprofCurFilename.FilenamePat = FilenamePat;
319 else {
320 lprofCurFilename.FilenamePat = strdup(FilenamePat);
321 lprofCurFilename.OwnsFilenamePat = 1;
322 }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000323 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000324 for (I = 0; FilenamePat[I]; ++I)
325 if (FilenamePat[I] == '%') {
326 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000327 if (!NumPids++) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000328 if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000329 PROF_WARN("Unable to get pid for filename pattern %s. Using the "
330 "default name.",
331 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000332 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000333 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000334 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000335 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000336 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000337 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000338 PROF_WARN("Unable to get hostname for filename pattern %s. Using "
339 "the default name.",
340 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000341 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000342 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000343 } else if (containsMergeSpecifier(FilenamePat, I)) {
344 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000345 PROF_WARN("%%m specifier can only be specified once in %s.\n",
346 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000347 return -1;
348 }
349 MergingEnabled = 1;
350 if (FilenamePat[I] == 'm')
351 lprofCurFilename.MergePoolSize = 1;
352 else {
353 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
354 I++; /* advance to 'm' */
355 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000356 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000357 }
358
Xinliang David Li7f08d122016-05-25 17:30:15 +0000359 lprofCurFilename.NumPids = NumPids;
360 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000361 return 0;
362}
363
Xinliang David Li153e8b62016-06-10 20:35:01 +0000364static void parseAndSetFilename(const char *FilenamePat,
Xinliang David Li14c91c42016-08-02 19:34:00 +0000365 ProfileNameSpecifier PNS,
366 unsigned CopyFilenamePat) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000367
Xinliang David Li153e8b62016-06-10 20:35:01 +0000368 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
369 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
370
371 if (PNS < OldPNS)
372 return;
373
374 if (!FilenamePat)
375 FilenamePat = DefaultProfileName;
376
Xinliang David Li153e8b62016-06-10 20:35:01 +0000377 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
378 lprofCurFilename.PNS = PNS;
379 return;
380 }
381
382 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000383 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
Xinliang David Li7f08d122016-05-25 17:30:15 +0000384 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000385 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000386
Xinliang David Li153e8b62016-06-10 20:35:01 +0000387 if (!OldFilenamePat) {
Xinliang David Lie68df592016-09-22 21:00:29 +0000388 if (getenv("LLVM_PROFILE_VERBOSE"))
389 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
390 lprofCurFilename.FilenamePat, getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000391 } else {
Xinliang David Lie68df592016-09-22 21:00:29 +0000392 if (getenv("LLVM_PROFILE_VERBOSE"))
393 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
394 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
395 getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000396 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000397
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000398 truncateCurrentFile();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000399}
400
Xinliang David Li315e49d2016-05-24 21:29:18 +0000401/* Return buffer length that is required to store the current profile
402 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000403/* The length to hold uint64_t followed by 2 digit pool id including '_' */
404#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000405static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000406 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000407 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000408 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000409
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000410 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
411 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000412 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000413
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000414 Len = strlen(lprofCurFilename.FilenamePat) +
415 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
416 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
417 if (lprofCurFilename.MergePoolSize)
418 Len += SIGLEN;
419 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000420}
421
422/* Return the pointer to the current profile file name (after substituting
423 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
424 * to store the resulting filename. If no substitution is needed, the
425 * current filename pattern string is directly returned. */
426static const char *getCurFilename(char *FilenameBuf) {
427 int I, J, PidLength, HostNameLength;
428 const char *FilenamePat = lprofCurFilename.FilenamePat;
429
430 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
431 return 0;
432
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000433 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
434 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000435 return lprofCurFilename.FilenamePat;
436
437 PidLength = strlen(lprofCurFilename.PidChars);
438 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000439 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000440 for (I = 0, J = 0; FilenamePat[I]; ++I)
441 if (FilenamePat[I] == '%') {
442 if (FilenamePat[++I] == 'p') {
443 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000444 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000445 } else if (FilenamePat[I] == 'h') {
446 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000447 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000448 } else if (containsMergeSpecifier(FilenamePat, I)) {
449 char LoadModuleSignature[SIGLEN];
450 int S;
451 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
452 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
453 lprofGetLoadModuleSignature(), ProfilePoolId);
454 if (S == -1 || S > SIGLEN)
455 S = SIGLEN;
456 memcpy(FilenameBuf + J, LoadModuleSignature, S);
457 J += S;
458 if (FilenamePat[I] != 'm')
459 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000460 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000461 /* Drop any unknown substitutions. */
462 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000463 FilenameBuf[J++] = FilenamePat[I];
464 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000465
Xinliang David Li315e49d2016-05-24 21:29:18 +0000466 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000467}
468
Xinliang David Li315e49d2016-05-24 21:29:18 +0000469/* Returns the pointer to the environment variable
470 * string. Returns null if the env var is not set. */
471static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000472 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000473 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000474 return 0;
475 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000476}
477
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000478COMPILER_RT_VISIBILITY
479const char *__llvm_profile_get_path_prefix(void) {
480 int Length;
481 char *FilenameBuf, *Prefix;
482 const char *Filename, *PrefixEnd;
483
484 if (lprofCurFilename.ProfilePathPrefix)
485 return lprofCurFilename.ProfilePathPrefix;
486
487 Length = getCurFilenameLength();
488 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
489 Filename = getCurFilename(FilenameBuf);
490 if (!Filename)
491 return "\0";
492
493 PrefixEnd = lprofFindLastDirSeparator(Filename);
494 if (!PrefixEnd)
495 return "\0";
496
497 Length = PrefixEnd - Filename + 1;
498 Prefix = (char *)malloc(Length + 1);
499 if (!Prefix) {
500 PROF_ERR("Failed to %s\n", "allocate memory.");
501 return "\0";
502 }
503 memcpy(Prefix, Filename, Length);
504 Prefix[Length] = '\0';
505 lprofCurFilename.ProfilePathPrefix = Prefix;
506 return Prefix;
507}
508
Xinliang David Li51fe0022016-05-24 01:23:09 +0000509/* This method is invoked by the runtime initialization hook
510 * InstrProfilingRuntime.o if it is linked in. Both user specified
511 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
512 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000513COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000514void __llvm_profile_initialize_file(void) {
Xinliang David Lie9539332016-07-21 23:19:18 +0000515 const char *EnvFilenamePat;
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000516 const char *SelectedPat = NULL;
517 ProfileNameSpecifier PNS = PNS_unknown;
Xinliang David Lie9539332016-07-21 23:19:18 +0000518 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000519
Xinliang David Lie9539332016-07-21 23:19:18 +0000520 EnvFilenamePat = getFilenamePatFromEnv();
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000521 if (EnvFilenamePat) {
522 SelectedPat = EnvFilenamePat;
523 PNS = PNS_environment;
524 } else if (hasCommandLineOverrider) {
525 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
526 PNS = PNS_command_line;
Xinliang David Lie9539332016-07-21 23:19:18 +0000527 } else {
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000528 SelectedPat = NULL;
529 PNS = PNS_default;
Xinliang David Lie9539332016-07-21 23:19:18 +0000530 }
531
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000532 parseAndSetFilename(SelectedPat, PNS, 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000533}
534
Xinliang David Li51fe0022016-05-24 01:23:09 +0000535/* This API is directly called by the user application code. It has the
536 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000537 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000538 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000539COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000540void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000541 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
Eric Christopherd641b532015-04-28 22:54:51 +0000542}
543
Xinliang David Li315e49d2016-05-24 21:29:18 +0000544/* The public API for writing profile data into the file with name
545 * set by previous calls to __llvm_profile_set_filename or
546 * __llvm_profile_override_default_filename or
547 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000548COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000549int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000550 int rc, Length;
551 const char *Filename;
552 char *FilenameBuf;
Rong Xucf1f6fb2017-03-17 18:41:33 +0000553 int PDeathSig = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000554
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000555 if (lprofProfileDumped()) {
556 PROF_NOTE("Profile data not written to file: %s.\n",
557 "already written");
558 return 0;
559 }
560
Xinliang David Li315e49d2016-05-24 21:29:18 +0000561 Length = getCurFilenameLength();
562 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
563 Filename = getCurFilename(FilenameBuf);
564
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000565 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000566 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000567 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000568 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000569 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000570
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000571 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000572 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000573 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000574 "expected %d, but get %d\n",
575 INSTR_PROF_RAW_VERSION,
576 (int)GET_VERSION(__llvm_profile_get_version()));
577 return -1;
578 }
579
Rong Xucf1f6fb2017-03-17 18:41:33 +0000580 // Temporarily suspend getting SIGKILL when the parent exits.
581 PDeathSig = lprofSuspendSigKill();
582
Xinliang David Li315e49d2016-05-24 21:29:18 +0000583 /* Write profile data to the file. */
584 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000585 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000586 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Rong Xucf1f6fb2017-03-17 18:41:33 +0000587
588 // Restore SIGKILL.
589 if (PDeathSig == 1)
590 lprofRestoreSigKill();
591
Justin Bogner66fd5c92015-01-16 20:10:56 +0000592 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000593}
594
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000595COMPILER_RT_VISIBILITY
596int __llvm_profile_dump(void) {
597 if (!doMerging())
598 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
Nico Weber81291a02016-09-08 01:46:52 +0000599 " of previously dumped profile data : %s. Either use %%m "
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000600 "in profile name or change profile name before dumping.\n",
601 "online profile merging is not on");
602 int rc = __llvm_profile_write_file();
603 lprofSetProfileDumped();
604 return rc;
605}
606
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000607static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000608
Xinliang David Liabfd5532015-12-16 03:29:15 +0000609COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000610int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000611 static int HasBeenRegistered = 0;
612
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000613 if (HasBeenRegistered)
614 return 0;
615
Xinliang David Li4617aa72016-05-18 22:34:05 +0000616 lprofSetupValueProfiler();
617
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000618 HasBeenRegistered = 1;
619 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000620}