blob: f6013ad2299b72a5eaceec28fff282946914f07f [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
Joerg Sonnenbergeref24b412015-03-09 11:23:29 +000010#include <errno.h>
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000011#include <stdio.h>
12#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000013#include <string.h>
Xinliang David Li71eddbf2016-05-20 04:52:27 +000014#ifdef _MSC_VER
Xinliang David Li315e49d2016-05-24 21:29:18 +000015/* For _alloca. */
Xinliang David Li71eddbf2016-05-20 04:52:27 +000016#include <malloc.h>
Xinliang David Lifb320a12016-05-20 05:40:07 +000017#endif
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000018#if defined(_WIN32)
19#include "WindowsMMap.h"
20/* For _chsize_s */
21#include <io.h>
Reid Kleckner963aba32018-04-23 17:05:47 +000022#include <process.h>
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000023#else
24#include <sys/file.h>
25#include <sys/mman.h>
26#include <unistd.h>
27#if defined(__linux__)
28#include <sys/types.h>
29#endif
30#endif
Xinliang David Li71eddbf2016-05-20 04:52:27 +000031
Vedant Kumard7c93362017-12-14 19:01:04 +000032#include "InstrProfiling.h"
33#include "InstrProfilingInternal.h"
34#include "InstrProfilingUtil.h"
35
Xinliang David Li153e8b62016-06-10 20:35:01 +000036/* From where is profile name specified.
37 * The order the enumerators define their
38 * precedence. Re-order them may lead to
39 * runtime behavior change. */
40typedef enum ProfileNameSpecifier {
41 PNS_unknown = 0,
42 PNS_default,
43 PNS_command_line,
44 PNS_environment,
45 PNS_runtime_api
46} ProfileNameSpecifier;
47
48static const char *getPNSStr(ProfileNameSpecifier PNS) {
49 switch (PNS) {
50 case PNS_default:
51 return "default setting";
52 case PNS_command_line:
53 return "command line";
54 case PNS_environment:
55 return "environment variable";
56 case PNS_runtime_api:
57 return "runtime API";
58 default:
59 return "Unknown";
60 }
61}
62
Xinliang David Li315e49d2016-05-24 21:29:18 +000063#define MAX_PID_SIZE 16
Ying Yi2c614cf2016-08-10 10:48:02 +000064/* Data structure holding the result of parsed filename pattern. */
Xinliang David Li315e49d2016-05-24 21:29:18 +000065typedef struct lprofFilename {
66 /* File name string possibly with %p or %h specifiers. */
67 const char *FilenamePat;
Xinliang David Li14c91c42016-08-02 19:34:00 +000068 /* A flag indicating if FilenamePat's memory is allocated
69 * by runtime. */
70 unsigned OwnsFilenamePat;
Xinliang David Lieaf238d2016-07-20 04:26:09 +000071 const char *ProfilePathPrefix;
Teresa Johnson73053b22018-07-19 19:03:50 +000072 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +000073 char PidChars[MAX_PID_SIZE];
74 char Hostname[COMPILER_RT_MAX_HOSTLEN];
75 unsigned NumPids;
76 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000077 /* When in-process merging is enabled, this parameter specifies
78 * the total number of profile data files shared by all the processes
79 * spawned from the same binary. By default the value is 1. If merging
80 * is not enabled, its value should be 0. This parameter is specified
81 * by the %[0-9]m specifier. For instance %2m enables merging using
82 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
83 * can only appear once at the end of the name pattern. */
84 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000085 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000086} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000087
Teresa Johnson73053b22018-07-19 19:03:50 +000088COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0},
89 {0}, 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000090
Alex Lorenz341317f2017-08-31 15:51:23 +000091static int getCurFilenameLength();
Teresa Johnson73053b22018-07-19 19:03:50 +000092static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000093static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000094
Xinliang David Li2d5bf072015-11-21 04:16:42 +000095/* Return 1 if there is an error, otherwise return 0. */
Xinliang David Li967669f2017-06-27 17:28:01 +000096static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
97 uint32_t NumIOVecs) {
Xinliang David Li2d5bf072015-11-21 04:16:42 +000098 uint32_t I;
Xinliang David Li967669f2017-06-27 17:28:01 +000099 FILE *File = (FILE *)This->WriterCtx;
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000100 for (I = 0; I < NumIOVecs; I++) {
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000101 if (IOVecs[I].Data) {
102 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
103 IOVecs[I].NumElm)
104 return 1;
105 } else {
106 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1)
107 return 1;
108 }
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000109 }
110 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000111}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000112
Xinliang David Li967669f2017-06-27 17:28:01 +0000113static void initFileWriter(ProfDataWriter *This, FILE *File) {
114 This->Write = fileWriter;
115 This->WriterCtx = File;
116}
117
Xinliang David Licda3bc22015-12-29 23:54:41 +0000118COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000119lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000120 FreeHook = &free;
121 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
122 VPBufferSize = BufferSz;
Xinliang David Li967669f2017-06-27 17:28:01 +0000123 ProfDataWriter *fileWriter =
124 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
125 initFileWriter(fileWriter, File);
126 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
127 IO->OwnFileWriter = 1;
128 return IO;
Xinliang David Li609fae32016-05-13 18:26:26 +0000129}
130
131static void setupIOBuffer() {
132 const char *BufferSzStr = 0;
133 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
134 if (BufferSzStr && BufferSzStr[0]) {
135 VPBufferSize = atoi(BufferSzStr);
136 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
137 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000138}
139
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000140/* Read profile data in \c ProfileFile and merge with in-memory
141 profile counters. Returns -1 if there is fatal error, otheriwse
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000142 0 is returned. Returning 0 does not mean merge is actually
143 performed. If merge is actually done, *MergeDone is set to 1.
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000144*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000145static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000146 uint64_t ProfileFileSize;
147 char *ProfileBuffer;
148
149 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
150 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
151 strerror(errno));
152 return -1;
153 }
154 ProfileFileSize = ftell(ProfileFile);
155
156 /* Restore file offset. */
157 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
158 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
159 strerror(errno));
160 return -1;
161 }
162
163 /* Nothing to merge. */
164 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
165 if (ProfileFileSize)
166 PROF_WARN("Unable to merge profile data: %s\n",
167 "source profile file is too small.");
168 return 0;
169 }
170
171 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
172 fileno(ProfileFile), 0);
173 if (ProfileBuffer == MAP_FAILED) {
174 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
175 strerror(errno));
176 return -1;
177 }
178
179 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
180 (void)munmap(ProfileBuffer, ProfileFileSize);
181 PROF_WARN("Unable to merge profile data: %s\n",
182 "source profile file is not compatible.");
183 return 0;
184 }
185
186 /* Now start merging */
187 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000188
Rong Xu95ab7582018-04-02 16:57:00 +0000189 // Truncate the file in case merging of value profile did not happend to
190 // prevent from leaving garbage data at the end of the profile file.
Reid Kleckner963aba32018-04-23 17:05:47 +0000191 COMPILER_RT_FTRUNCATE(ProfileFile, __llvm_profile_get_size_for_buffer());
Rong Xu95ab7582018-04-02 16:57:00 +0000192
193 (void)munmap(ProfileBuffer, ProfileFileSize);
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000194 *MergeDone = 1;
195
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000196 return 0;
197}
198
Xinliang David Lif2d94812017-02-14 21:39:55 +0000199/* Create the directory holding the file, if needed. */
200static void createProfileDir(const char *Filename) {
201 size_t Length = strlen(Filename);
202 if (lprofFindFirstDirSeparator(Filename)) {
203 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
204 strncpy(Copy, Filename, Length + 1);
205 __llvm_profile_recursive_mkdir(Copy);
206 }
207}
208
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000209/* Open the profile data for merging. It opens the file in r+b mode with
210 * file locking. If the file has content which is compatible with the
211 * current process, it also reads in the profile data in the file and merge
212 * it with in-memory counters. After the profile data is merged in memory,
213 * the original profile data is truncated and gets ready for the profile
214 * dumper. With profile merging enabled, each executable as well as any of
215 * its instrumented shared libraries dump profile data into their own data file.
216*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000217static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000218 FILE *ProfileFile;
219 int rc;
220
Xinliang David Lif2d94812017-02-14 21:39:55 +0000221 createProfileDir(ProfileFileName);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000222 ProfileFile = lprofOpenFileEx(ProfileFileName);
223 if (!ProfileFile)
224 return NULL;
225
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000226 rc = doProfileMerging(ProfileFile, MergeDone);
227 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) ||
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000228 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
229 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
230 strerror(errno));
231 fclose(ProfileFile);
232 return NULL;
233 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000234 return ProfileFile;
235}
236
Xinliang David Li315e49d2016-05-24 21:29:18 +0000237/* Write profile data to file \c OutputName. */
238static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000239 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000240 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000241
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000242 int MergeDone = 0;
Rong Xu95ab7582018-04-02 16:57:00 +0000243 VPMergeHook = &lprofMergeValueProfData;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000244 if (!doMerging())
245 OutputFile = fopen(OutputName, "ab");
246 else
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000247 OutputFile = openFileForMerging(OutputName, &MergeDone);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000248
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000249 if (!OutputFile)
250 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000251
Xinliang David Li315e49d2016-05-24 21:29:18 +0000252 FreeHook = &free;
253 setupIOBuffer();
Xinliang David Li967669f2017-06-27 17:28:01 +0000254 ProfDataWriter fileWriter;
255 initFileWriter(&fileWriter, OutputFile);
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000256 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000257
258 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000259 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000260}
261
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000262static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000263 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000264 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000265 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000266 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000267
Xinliang David Li315e49d2016-05-24 21:29:18 +0000268 Length = getCurFilenameLength();
269 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000270 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Li315e49d2016-05-24 21:29:18 +0000271 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000272 return;
273
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000274 /* By pass file truncation to allow online raw profile
275 * merging. */
276 if (lprofCurFilename.MergePoolSize)
277 return;
278
Xinliang David Lif2d94812017-02-14 21:39:55 +0000279 createProfileDir(Filename);
280
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000281 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000282 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000283 if (!File)
284 return;
285 fclose(File);
286}
287
Xinliang David Li153e8b62016-06-10 20:35:01 +0000288static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000289static void resetFilenameToDefault(void) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000290 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
291 free((void *)lprofCurFilename.FilenamePat);
292 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000293 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000294 lprofCurFilename.FilenamePat = DefaultProfileName;
295 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000296}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000297
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000298static int containsMergeSpecifier(const char *FilenamePat, int I) {
299 return (FilenamePat[I] == 'm' ||
300 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
301 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
302 * to be in-bound as the string is null terminated. */
303 FilenamePat[I + 1] == 'm'));
304}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000305
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000306/* Parses the pattern string \p FilenamePat and stores the result to
307 * lprofcurFilename structure. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000308static int parseFilenamePattern(const char *FilenamePat,
309 unsigned CopyFilenamePat) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000310 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000311 char *PidChars = &lprofCurFilename.PidChars[0];
312 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000313 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000314
Teresa Johnson73053b22018-07-19 19:03:50 +0000315 /* Clean up cached prefix and filename. */
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000316 if (lprofCurFilename.ProfilePathPrefix)
317 free((void *)lprofCurFilename.ProfilePathPrefix);
Teresa Johnson73053b22018-07-19 19:03:50 +0000318 if (lprofCurFilename.Filename)
319 free((void *)lprofCurFilename.Filename);
320
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000321 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
322
Xinliang David Li14c91c42016-08-02 19:34:00 +0000323 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
324 free((void *)lprofCurFilename.FilenamePat);
325 }
326
327 if (!CopyFilenamePat)
328 lprofCurFilename.FilenamePat = FilenamePat;
329 else {
330 lprofCurFilename.FilenamePat = strdup(FilenamePat);
331 lprofCurFilename.OwnsFilenamePat = 1;
332 }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000333 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000334 for (I = 0; FilenamePat[I]; ++I)
335 if (FilenamePat[I] == '%') {
336 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000337 if (!NumPids++) {
Vedant Kumard7c93362017-12-14 19:01:04 +0000338 if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000339 PROF_WARN("Unable to get pid for filename pattern %s. Using the "
340 "default name.",
341 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000342 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000343 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000344 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000345 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000346 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000347 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000348 PROF_WARN("Unable to get hostname for filename pattern %s. Using "
349 "the default name.",
350 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000351 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000352 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000353 } else if (containsMergeSpecifier(FilenamePat, I)) {
354 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000355 PROF_WARN("%%m specifier can only be specified once in %s.\n",
356 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000357 return -1;
358 }
359 MergingEnabled = 1;
360 if (FilenamePat[I] == 'm')
361 lprofCurFilename.MergePoolSize = 1;
362 else {
363 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
364 I++; /* advance to 'm' */
365 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000366 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000367 }
368
Xinliang David Li7f08d122016-05-25 17:30:15 +0000369 lprofCurFilename.NumPids = NumPids;
370 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000371 return 0;
372}
373
Xinliang David Li153e8b62016-06-10 20:35:01 +0000374static void parseAndSetFilename(const char *FilenamePat,
Xinliang David Li14c91c42016-08-02 19:34:00 +0000375 ProfileNameSpecifier PNS,
376 unsigned CopyFilenamePat) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000377
Xinliang David Li153e8b62016-06-10 20:35:01 +0000378 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
379 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
380
381 if (PNS < OldPNS)
382 return;
383
384 if (!FilenamePat)
385 FilenamePat = DefaultProfileName;
386
Xinliang David Li153e8b62016-06-10 20:35:01 +0000387 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
388 lprofCurFilename.PNS = PNS;
389 return;
390 }
391
392 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000393 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
Xinliang David Li7f08d122016-05-25 17:30:15 +0000394 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000395 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000396
Xinliang David Li153e8b62016-06-10 20:35:01 +0000397 if (!OldFilenamePat) {
Xinliang David Lie68df592016-09-22 21:00:29 +0000398 if (getenv("LLVM_PROFILE_VERBOSE"))
399 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
400 lprofCurFilename.FilenamePat, getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000401 } else {
Xinliang David Lie68df592016-09-22 21:00:29 +0000402 if (getenv("LLVM_PROFILE_VERBOSE"))
403 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
404 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
405 getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000406 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000407
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000408 truncateCurrentFile();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000409}
410
Xinliang David Li315e49d2016-05-24 21:29:18 +0000411/* Return buffer length that is required to store the current profile
412 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000413/* The length to hold uint64_t followed by 2 digit pool id including '_' */
414#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000415static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000416 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000417 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000418 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000419
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000420 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
421 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000422 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000423
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000424 Len = strlen(lprofCurFilename.FilenamePat) +
425 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
426 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
427 if (lprofCurFilename.MergePoolSize)
428 Len += SIGLEN;
429 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000430}
431
432/* Return the pointer to the current profile file name (after substituting
433 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
434 * to store the resulting filename. If no substitution is needed, the
Teresa Johnson73053b22018-07-19 19:03:50 +0000435 * current filename pattern string is directly returned, unless ForceUseBuf
436 * is enabled. */
437static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
438 int I, J, PidLength, HostNameLength, FilenamePatLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000439 const char *FilenamePat = lprofCurFilename.FilenamePat;
440
441 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
442 return 0;
443
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000444 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
Teresa Johnson73053b22018-07-19 19:03:50 +0000445 lprofCurFilename.MergePoolSize)) {
446 if (!ForceUseBuf)
447 return lprofCurFilename.FilenamePat;
448
449 FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
450 memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
451 FilenameBuf[FilenamePatLength] = '\0';
452 return FilenameBuf;
453 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000454
455 PidLength = strlen(lprofCurFilename.PidChars);
456 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000457 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000458 for (I = 0, J = 0; FilenamePat[I]; ++I)
459 if (FilenamePat[I] == '%') {
460 if (FilenamePat[++I] == 'p') {
461 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000462 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000463 } else if (FilenamePat[I] == 'h') {
464 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000465 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000466 } else if (containsMergeSpecifier(FilenamePat, I)) {
467 char LoadModuleSignature[SIGLEN];
468 int S;
469 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
470 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
471 lprofGetLoadModuleSignature(), ProfilePoolId);
472 if (S == -1 || S > SIGLEN)
473 S = SIGLEN;
474 memcpy(FilenameBuf + J, LoadModuleSignature, S);
475 J += S;
476 if (FilenamePat[I] != 'm')
477 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000478 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000479 /* Drop any unknown substitutions. */
480 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000481 FilenameBuf[J++] = FilenamePat[I];
482 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000483
Xinliang David Li315e49d2016-05-24 21:29:18 +0000484 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000485}
486
Xinliang David Li315e49d2016-05-24 21:29:18 +0000487/* Returns the pointer to the environment variable
488 * string. Returns null if the env var is not set. */
489static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000490 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000491 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000492 return 0;
493 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000494}
495
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000496COMPILER_RT_VISIBILITY
497const char *__llvm_profile_get_path_prefix(void) {
498 int Length;
499 char *FilenameBuf, *Prefix;
500 const char *Filename, *PrefixEnd;
501
502 if (lprofCurFilename.ProfilePathPrefix)
503 return lprofCurFilename.ProfilePathPrefix;
504
505 Length = getCurFilenameLength();
506 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000507 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000508 if (!Filename)
509 return "\0";
510
511 PrefixEnd = lprofFindLastDirSeparator(Filename);
512 if (!PrefixEnd)
513 return "\0";
514
515 Length = PrefixEnd - Filename + 1;
516 Prefix = (char *)malloc(Length + 1);
517 if (!Prefix) {
518 PROF_ERR("Failed to %s\n", "allocate memory.");
519 return "\0";
520 }
521 memcpy(Prefix, Filename, Length);
522 Prefix[Length] = '\0';
523 lprofCurFilename.ProfilePathPrefix = Prefix;
524 return Prefix;
525}
526
Teresa Johnson73053b22018-07-19 19:03:50 +0000527COMPILER_RT_VISIBILITY
528const char *__llvm_profile_get_filename(void) {
529 int Length;
530 char *FilenameBuf;
531 const char *Filename;
532
533 if (lprofCurFilename.Filename)
534 return lprofCurFilename.Filename;
535
536 Length = getCurFilenameLength();
537 FilenameBuf = (char *)malloc(Length + 1);
538 if (!FilenameBuf) {
539 PROF_ERR("Failed to %s\n", "allocate memory.");
540 return "\0";
541 }
542 Filename = getCurFilename(FilenameBuf, 1);
543 if (!Filename)
544 return "\0";
545
546 lprofCurFilename.Filename = FilenameBuf;
547 return FilenameBuf;
548}
549
Xinliang David Li51fe0022016-05-24 01:23:09 +0000550/* This method is invoked by the runtime initialization hook
551 * InstrProfilingRuntime.o if it is linked in. Both user specified
552 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
553 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000554COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000555void __llvm_profile_initialize_file(void) {
Xinliang David Lie9539332016-07-21 23:19:18 +0000556 const char *EnvFilenamePat;
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000557 const char *SelectedPat = NULL;
558 ProfileNameSpecifier PNS = PNS_unknown;
Xinliang David Lie9539332016-07-21 23:19:18 +0000559 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000560
Xinliang David Lie9539332016-07-21 23:19:18 +0000561 EnvFilenamePat = getFilenamePatFromEnv();
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000562 if (EnvFilenamePat) {
Xinliang David Lic7c53032017-08-23 21:39:33 +0000563 /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
564 at the moment when __llvm_profile_write_file() gets executed. */
565 parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
566 return;
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000567 } else if (hasCommandLineOverrider) {
568 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
569 PNS = PNS_command_line;
Xinliang David Lie9539332016-07-21 23:19:18 +0000570 } else {
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000571 SelectedPat = NULL;
572 PNS = PNS_default;
Xinliang David Lie9539332016-07-21 23:19:18 +0000573 }
574
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000575 parseAndSetFilename(SelectedPat, PNS, 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000576}
577
Xinliang David Li51fe0022016-05-24 01:23:09 +0000578/* This API is directly called by the user application code. It has the
579 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000580 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000581 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000582COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000583void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000584 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
Eric Christopherd641b532015-04-28 22:54:51 +0000585}
586
Xinliang David Li315e49d2016-05-24 21:29:18 +0000587/* The public API for writing profile data into the file with name
588 * set by previous calls to __llvm_profile_set_filename or
589 * __llvm_profile_override_default_filename or
590 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000591COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000592int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000593 int rc, Length;
594 const char *Filename;
595 char *FilenameBuf;
Rong Xucf1f6fb2017-03-17 18:41:33 +0000596 int PDeathSig = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000597
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000598 if (lprofProfileDumped()) {
599 PROF_NOTE("Profile data not written to file: %s.\n",
600 "already written");
601 return 0;
602 }
603
Xinliang David Li315e49d2016-05-24 21:29:18 +0000604 Length = getCurFilenameLength();
605 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000606 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Li315e49d2016-05-24 21:29:18 +0000607
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000608 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000609 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000610 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000611 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000612 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000613
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000614 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000615 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000616 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000617 "expected %d, but get %d\n",
618 INSTR_PROF_RAW_VERSION,
619 (int)GET_VERSION(__llvm_profile_get_version()));
620 return -1;
621 }
622
Rong Xucf1f6fb2017-03-17 18:41:33 +0000623 // Temporarily suspend getting SIGKILL when the parent exits.
624 PDeathSig = lprofSuspendSigKill();
625
Xinliang David Li315e49d2016-05-24 21:29:18 +0000626 /* Write profile data to the file. */
627 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000628 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000629 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Rong Xucf1f6fb2017-03-17 18:41:33 +0000630
631 // Restore SIGKILL.
632 if (PDeathSig == 1)
633 lprofRestoreSigKill();
634
Justin Bogner66fd5c92015-01-16 20:10:56 +0000635 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000636}
637
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000638COMPILER_RT_VISIBILITY
639int __llvm_profile_dump(void) {
640 if (!doMerging())
641 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
Nico Weber81291a02016-09-08 01:46:52 +0000642 " of previously dumped profile data : %s. Either use %%m "
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000643 "in profile name or change profile name before dumping.\n",
644 "online profile merging is not on");
645 int rc = __llvm_profile_write_file();
646 lprofSetProfileDumped();
647 return rc;
648}
649
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000650static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000651
Xinliang David Liabfd5532015-12-16 03:29:15 +0000652COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000653int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000654 static int HasBeenRegistered = 0;
655
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000656 if (HasBeenRegistered)
657 return 0;
658
Xinliang David Li4617aa72016-05-18 22:34:05 +0000659 lprofSetupValueProfiler();
660
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000661 HasBeenRegistered = 1;
662 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000663}