blob: c4cf3ccd70c29a6f033f863c0a0d4d00a8227ff4 [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
Petr Hosek47e5fcb2018-07-25 03:01:35 +000010#if !defined(__Fuchsia__)
11
Joerg Sonnenbergeref24b412015-03-09 11:23:29 +000012#include <errno.h>
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000013#include <stdio.h>
14#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000015#include <string.h>
Xinliang David Li71eddbf2016-05-20 04:52:27 +000016#ifdef _MSC_VER
Xinliang David Li315e49d2016-05-24 21:29:18 +000017/* For _alloca. */
Xinliang David Li71eddbf2016-05-20 04:52:27 +000018#include <malloc.h>
Xinliang David Lifb320a12016-05-20 05:40:07 +000019#endif
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000020#if defined(_WIN32)
21#include "WindowsMMap.h"
22/* For _chsize_s */
23#include <io.h>
Reid Kleckner963aba32018-04-23 17:05:47 +000024#include <process.h>
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000025#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
Vedant Kumard7c93362017-12-14 19:01:04 +000034#include "InstrProfiling.h"
35#include "InstrProfilingInternal.h"
36#include "InstrProfilingUtil.h"
37
Xinliang David Li153e8b62016-06-10 20:35:01 +000038/* From where is profile name specified.
39 * The order the enumerators define their
40 * precedence. Re-order them may lead to
41 * runtime behavior change. */
42typedef enum ProfileNameSpecifier {
43 PNS_unknown = 0,
44 PNS_default,
45 PNS_command_line,
46 PNS_environment,
47 PNS_runtime_api
48} ProfileNameSpecifier;
49
50static const char *getPNSStr(ProfileNameSpecifier PNS) {
51 switch (PNS) {
52 case PNS_default:
53 return "default setting";
54 case PNS_command_line:
55 return "command line";
56 case PNS_environment:
57 return "environment variable";
58 case PNS_runtime_api:
59 return "runtime API";
60 default:
61 return "Unknown";
62 }
63}
64
Xinliang David Li315e49d2016-05-24 21:29:18 +000065#define MAX_PID_SIZE 16
Ying Yi2c614cf2016-08-10 10:48:02 +000066/* Data structure holding the result of parsed filename pattern. */
Xinliang David Li315e49d2016-05-24 21:29:18 +000067typedef struct lprofFilename {
68 /* File name string possibly with %p or %h specifiers. */
69 const char *FilenamePat;
Xinliang David Li14c91c42016-08-02 19:34:00 +000070 /* A flag indicating if FilenamePat's memory is allocated
71 * by runtime. */
72 unsigned OwnsFilenamePat;
Xinliang David Lieaf238d2016-07-20 04:26:09 +000073 const char *ProfilePathPrefix;
Teresa Johnson73053b22018-07-19 19:03:50 +000074 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +000075 char PidChars[MAX_PID_SIZE];
76 char Hostname[COMPILER_RT_MAX_HOSTLEN];
77 unsigned NumPids;
78 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000079 /* When in-process merging is enabled, this parameter specifies
80 * the total number of profile data files shared by all the processes
81 * spawned from the same binary. By default the value is 1. If merging
82 * is not enabled, its value should be 0. This parameter is specified
83 * by the %[0-9]m specifier. For instance %2m enables merging using
84 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
85 * can only appear once at the end of the name pattern. */
86 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000087 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000088} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000089
Teresa Johnson73053b22018-07-19 19:03:50 +000090COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0},
91 {0}, 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000092
Alex Lorenz341317f2017-08-31 15:51:23 +000093static int getCurFilenameLength();
Teresa Johnson73053b22018-07-19 19:03:50 +000094static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000095static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000096
Xinliang David Li2d5bf072015-11-21 04:16:42 +000097/* Return 1 if there is an error, otherwise return 0. */
Xinliang David Li967669f2017-06-27 17:28:01 +000098static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
99 uint32_t NumIOVecs) {
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000100 uint32_t I;
Xinliang David Li967669f2017-06-27 17:28:01 +0000101 FILE *File = (FILE *)This->WriterCtx;
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000102 for (I = 0; I < NumIOVecs; I++) {
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000103 if (IOVecs[I].Data) {
104 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
105 IOVecs[I].NumElm)
106 return 1;
107 } else {
108 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1)
109 return 1;
110 }
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000111 }
112 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000113}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000114
Xinliang David Li967669f2017-06-27 17:28:01 +0000115static void initFileWriter(ProfDataWriter *This, FILE *File) {
116 This->Write = fileWriter;
117 This->WriterCtx = File;
118}
119
Xinliang David Licda3bc22015-12-29 23:54:41 +0000120COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000121lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000122 FreeHook = &free;
123 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
124 VPBufferSize = BufferSz;
Xinliang David Li967669f2017-06-27 17:28:01 +0000125 ProfDataWriter *fileWriter =
126 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
127 initFileWriter(fileWriter, File);
128 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
129 IO->OwnFileWriter = 1;
130 return IO;
Xinliang David Li609fae32016-05-13 18:26:26 +0000131}
132
133static void setupIOBuffer() {
134 const char *BufferSzStr = 0;
135 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
136 if (BufferSzStr && BufferSzStr[0]) {
137 VPBufferSize = atoi(BufferSzStr);
138 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
139 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000140}
141
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000142/* Read profile data in \c ProfileFile and merge with in-memory
143 profile counters. Returns -1 if there is fatal error, otheriwse
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000144 0 is returned. Returning 0 does not mean merge is actually
145 performed. If merge is actually done, *MergeDone is set to 1.
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000146*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000147static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000148 uint64_t ProfileFileSize;
149 char *ProfileBuffer;
150
151 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
152 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
153 strerror(errno));
154 return -1;
155 }
156 ProfileFileSize = ftell(ProfileFile);
157
158 /* Restore file offset. */
159 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
160 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
161 strerror(errno));
162 return -1;
163 }
164
165 /* Nothing to merge. */
166 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
167 if (ProfileFileSize)
168 PROF_WARN("Unable to merge profile data: %s\n",
169 "source profile file is too small.");
170 return 0;
171 }
172
173 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
174 fileno(ProfileFile), 0);
175 if (ProfileBuffer == MAP_FAILED) {
176 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
177 strerror(errno));
178 return -1;
179 }
180
181 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
182 (void)munmap(ProfileBuffer, ProfileFileSize);
183 PROF_WARN("Unable to merge profile data: %s\n",
184 "source profile file is not compatible.");
185 return 0;
186 }
187
188 /* Now start merging */
189 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000190
Rong Xu95ab7582018-04-02 16:57:00 +0000191 // Truncate the file in case merging of value profile did not happend to
192 // prevent from leaving garbage data at the end of the profile file.
Reid Kleckner963aba32018-04-23 17:05:47 +0000193 COMPILER_RT_FTRUNCATE(ProfileFile, __llvm_profile_get_size_for_buffer());
Rong Xu95ab7582018-04-02 16:57:00 +0000194
195 (void)munmap(ProfileBuffer, ProfileFileSize);
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000196 *MergeDone = 1;
197
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000198 return 0;
199}
200
Xinliang David Lif2d94812017-02-14 21:39:55 +0000201/* Create the directory holding the file, if needed. */
202static void createProfileDir(const char *Filename) {
203 size_t Length = strlen(Filename);
204 if (lprofFindFirstDirSeparator(Filename)) {
205 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
206 strncpy(Copy, Filename, Length + 1);
207 __llvm_profile_recursive_mkdir(Copy);
208 }
209}
210
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000211/* Open the profile data for merging. It opens the file in r+b mode with
212 * file locking. If the file has content which is compatible with the
213 * current process, it also reads in the profile data in the file and merge
214 * it with in-memory counters. After the profile data is merged in memory,
215 * the original profile data is truncated and gets ready for the profile
216 * dumper. With profile merging enabled, each executable as well as any of
217 * its instrumented shared libraries dump profile data into their own data file.
218*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000219static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000220 FILE *ProfileFile;
221 int rc;
222
Xinliang David Lif2d94812017-02-14 21:39:55 +0000223 createProfileDir(ProfileFileName);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000224 ProfileFile = lprofOpenFileEx(ProfileFileName);
225 if (!ProfileFile)
226 return NULL;
227
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000228 rc = doProfileMerging(ProfileFile, MergeDone);
229 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) ||
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000230 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
231 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
232 strerror(errno));
233 fclose(ProfileFile);
234 return NULL;
235 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000236 return ProfileFile;
237}
238
Xinliang David Li315e49d2016-05-24 21:29:18 +0000239/* Write profile data to file \c OutputName. */
240static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000241 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000242 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000243
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000244 int MergeDone = 0;
Rong Xu95ab7582018-04-02 16:57:00 +0000245 VPMergeHook = &lprofMergeValueProfData;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000246 if (!doMerging())
247 OutputFile = fopen(OutputName, "ab");
248 else
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000249 OutputFile = openFileForMerging(OutputName, &MergeDone);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000250
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000251 if (!OutputFile)
252 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000253
Xinliang David Li315e49d2016-05-24 21:29:18 +0000254 FreeHook = &free;
255 setupIOBuffer();
Xinliang David Li967669f2017-06-27 17:28:01 +0000256 ProfDataWriter fileWriter;
257 initFileWriter(&fileWriter, OutputFile);
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000258 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000259
260 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000261 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000262}
263
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000264static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000265 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000266 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000267 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000268 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000269
Xinliang David Li315e49d2016-05-24 21:29:18 +0000270 Length = getCurFilenameLength();
271 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000272 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Li315e49d2016-05-24 21:29:18 +0000273 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000274 return;
275
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000276 /* By pass file truncation to allow online raw profile
277 * merging. */
278 if (lprofCurFilename.MergePoolSize)
279 return;
280
Xinliang David Lif2d94812017-02-14 21:39:55 +0000281 createProfileDir(Filename);
282
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000283 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000284 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000285 if (!File)
286 return;
287 fclose(File);
288}
289
Xinliang David Li153e8b62016-06-10 20:35:01 +0000290static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000291static void resetFilenameToDefault(void) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000292 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
293 free((void *)lprofCurFilename.FilenamePat);
294 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000295 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000296 lprofCurFilename.FilenamePat = DefaultProfileName;
297 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000298}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000299
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000300static int containsMergeSpecifier(const char *FilenamePat, int I) {
301 return (FilenamePat[I] == 'm' ||
302 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
303 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
304 * to be in-bound as the string is null terminated. */
305 FilenamePat[I + 1] == 'm'));
306}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000307
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000308/* Parses the pattern string \p FilenamePat and stores the result to
309 * lprofcurFilename structure. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000310static int parseFilenamePattern(const char *FilenamePat,
311 unsigned CopyFilenamePat) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000312 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000313 char *PidChars = &lprofCurFilename.PidChars[0];
314 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000315 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000316
Teresa Johnson73053b22018-07-19 19:03:50 +0000317 /* Clean up cached prefix and filename. */
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000318 if (lprofCurFilename.ProfilePathPrefix)
319 free((void *)lprofCurFilename.ProfilePathPrefix);
Teresa Johnson73053b22018-07-19 19:03:50 +0000320 if (lprofCurFilename.Filename)
321 free((void *)lprofCurFilename.Filename);
322
Xinliang David Li14c91c42016-08-02 19:34:00 +0000323 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
324 free((void *)lprofCurFilename.FilenamePat);
325 }
326
Igor Kudrin63600c72018-07-24 12:28:53 +0000327 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
328
Xinliang David Li14c91c42016-08-02 19:34:00 +0000329 if (!CopyFilenamePat)
330 lprofCurFilename.FilenamePat = FilenamePat;
331 else {
332 lprofCurFilename.FilenamePat = strdup(FilenamePat);
333 lprofCurFilename.OwnsFilenamePat = 1;
334 }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000335 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000336 for (I = 0; FilenamePat[I]; ++I)
337 if (FilenamePat[I] == '%') {
338 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000339 if (!NumPids++) {
Vedant Kumard7c93362017-12-14 19:01:04 +0000340 if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000341 PROF_WARN("Unable to get pid for filename pattern %s. Using the "
342 "default name.",
343 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000344 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000345 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000346 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000347 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000348 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000349 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000350 PROF_WARN("Unable to get hostname for filename pattern %s. Using "
351 "the default name.",
352 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000353 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000354 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000355 } else if (containsMergeSpecifier(FilenamePat, I)) {
356 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000357 PROF_WARN("%%m specifier can only be specified once in %s.\n",
358 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000359 return -1;
360 }
361 MergingEnabled = 1;
362 if (FilenamePat[I] == 'm')
363 lprofCurFilename.MergePoolSize = 1;
364 else {
365 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
366 I++; /* advance to 'm' */
367 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000368 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000369 }
370
Xinliang David Li7f08d122016-05-25 17:30:15 +0000371 lprofCurFilename.NumPids = NumPids;
372 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000373 return 0;
374}
375
Xinliang David Li153e8b62016-06-10 20:35:01 +0000376static void parseAndSetFilename(const char *FilenamePat,
Xinliang David Li14c91c42016-08-02 19:34:00 +0000377 ProfileNameSpecifier PNS,
378 unsigned CopyFilenamePat) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000379
Xinliang David Li153e8b62016-06-10 20:35:01 +0000380 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
381 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
382
383 if (PNS < OldPNS)
384 return;
385
386 if (!FilenamePat)
387 FilenamePat = DefaultProfileName;
388
Xinliang David Li153e8b62016-06-10 20:35:01 +0000389 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
390 lprofCurFilename.PNS = PNS;
391 return;
392 }
393
394 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000395 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
Xinliang David Li7f08d122016-05-25 17:30:15 +0000396 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000397 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000398
Xinliang David Li153e8b62016-06-10 20:35:01 +0000399 if (!OldFilenamePat) {
Xinliang David Lie68df592016-09-22 21:00:29 +0000400 if (getenv("LLVM_PROFILE_VERBOSE"))
401 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
402 lprofCurFilename.FilenamePat, getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000403 } else {
Xinliang David Lie68df592016-09-22 21:00:29 +0000404 if (getenv("LLVM_PROFILE_VERBOSE"))
405 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
406 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
407 getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000408 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000409
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000410 truncateCurrentFile();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000411}
412
Xinliang David Li315e49d2016-05-24 21:29:18 +0000413/* Return buffer length that is required to store the current profile
414 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000415/* The length to hold uint64_t followed by 2 digit pool id including '_' */
416#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000417static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000418 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000419 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000420 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000421
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000422 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
423 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000424 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000425
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000426 Len = strlen(lprofCurFilename.FilenamePat) +
427 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
428 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
429 if (lprofCurFilename.MergePoolSize)
430 Len += SIGLEN;
431 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000432}
433
434/* Return the pointer to the current profile file name (after substituting
435 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
436 * to store the resulting filename. If no substitution is needed, the
Teresa Johnson73053b22018-07-19 19:03:50 +0000437 * current filename pattern string is directly returned, unless ForceUseBuf
438 * is enabled. */
439static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
440 int I, J, PidLength, HostNameLength, FilenamePatLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000441 const char *FilenamePat = lprofCurFilename.FilenamePat;
442
443 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
444 return 0;
445
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000446 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
Teresa Johnson73053b22018-07-19 19:03:50 +0000447 lprofCurFilename.MergePoolSize)) {
448 if (!ForceUseBuf)
449 return lprofCurFilename.FilenamePat;
450
451 FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
452 memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
453 FilenameBuf[FilenamePatLength] = '\0';
454 return FilenameBuf;
455 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000456
457 PidLength = strlen(lprofCurFilename.PidChars);
458 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000459 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000460 for (I = 0, J = 0; FilenamePat[I]; ++I)
461 if (FilenamePat[I] == '%') {
462 if (FilenamePat[++I] == 'p') {
463 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000464 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000465 } else if (FilenamePat[I] == 'h') {
466 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000467 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000468 } else if (containsMergeSpecifier(FilenamePat, I)) {
469 char LoadModuleSignature[SIGLEN];
470 int S;
471 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
472 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
473 lprofGetLoadModuleSignature(), ProfilePoolId);
474 if (S == -1 || S > SIGLEN)
475 S = SIGLEN;
476 memcpy(FilenameBuf + J, LoadModuleSignature, S);
477 J += S;
478 if (FilenamePat[I] != 'm')
479 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000480 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000481 /* Drop any unknown substitutions. */
482 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000483 FilenameBuf[J++] = FilenamePat[I];
484 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000485
Xinliang David Li315e49d2016-05-24 21:29:18 +0000486 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000487}
488
Xinliang David Li315e49d2016-05-24 21:29:18 +0000489/* Returns the pointer to the environment variable
490 * string. Returns null if the env var is not set. */
491static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000492 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000493 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000494 return 0;
495 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000496}
497
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000498COMPILER_RT_VISIBILITY
499const char *__llvm_profile_get_path_prefix(void) {
500 int Length;
501 char *FilenameBuf, *Prefix;
502 const char *Filename, *PrefixEnd;
503
504 if (lprofCurFilename.ProfilePathPrefix)
505 return lprofCurFilename.ProfilePathPrefix;
506
507 Length = getCurFilenameLength();
508 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000509 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000510 if (!Filename)
511 return "\0";
512
513 PrefixEnd = lprofFindLastDirSeparator(Filename);
514 if (!PrefixEnd)
515 return "\0";
516
517 Length = PrefixEnd - Filename + 1;
518 Prefix = (char *)malloc(Length + 1);
519 if (!Prefix) {
520 PROF_ERR("Failed to %s\n", "allocate memory.");
521 return "\0";
522 }
523 memcpy(Prefix, Filename, Length);
524 Prefix[Length] = '\0';
525 lprofCurFilename.ProfilePathPrefix = Prefix;
526 return Prefix;
527}
528
Teresa Johnson73053b22018-07-19 19:03:50 +0000529COMPILER_RT_VISIBILITY
530const char *__llvm_profile_get_filename(void) {
531 int Length;
532 char *FilenameBuf;
533 const char *Filename;
534
535 if (lprofCurFilename.Filename)
536 return lprofCurFilename.Filename;
537
538 Length = getCurFilenameLength();
539 FilenameBuf = (char *)malloc(Length + 1);
540 if (!FilenameBuf) {
541 PROF_ERR("Failed to %s\n", "allocate memory.");
542 return "\0";
543 }
544 Filename = getCurFilename(FilenameBuf, 1);
545 if (!Filename)
546 return "\0";
547
548 lprofCurFilename.Filename = FilenameBuf;
549 return FilenameBuf;
550}
551
Xinliang David Li51fe0022016-05-24 01:23:09 +0000552/* This method is invoked by the runtime initialization hook
553 * InstrProfilingRuntime.o if it is linked in. Both user specified
554 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
555 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000556COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000557void __llvm_profile_initialize_file(void) {
Xinliang David Lie9539332016-07-21 23:19:18 +0000558 const char *EnvFilenamePat;
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000559 const char *SelectedPat = NULL;
560 ProfileNameSpecifier PNS = PNS_unknown;
Xinliang David Lie9539332016-07-21 23:19:18 +0000561 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000562
Xinliang David Lie9539332016-07-21 23:19:18 +0000563 EnvFilenamePat = getFilenamePatFromEnv();
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000564 if (EnvFilenamePat) {
Xinliang David Lic7c53032017-08-23 21:39:33 +0000565 /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
566 at the moment when __llvm_profile_write_file() gets executed. */
567 parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
568 return;
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000569 } else if (hasCommandLineOverrider) {
570 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
571 PNS = PNS_command_line;
Xinliang David Lie9539332016-07-21 23:19:18 +0000572 } else {
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000573 SelectedPat = NULL;
574 PNS = PNS_default;
Xinliang David Lie9539332016-07-21 23:19:18 +0000575 }
576
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000577 parseAndSetFilename(SelectedPat, PNS, 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000578}
579
Xinliang David Li51fe0022016-05-24 01:23:09 +0000580/* This API is directly called by the user application code. It has the
581 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000582 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000583 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000584COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000585void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000586 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
Eric Christopherd641b532015-04-28 22:54:51 +0000587}
588
Xinliang David Li315e49d2016-05-24 21:29:18 +0000589/* The public API for writing profile data into the file with name
590 * set by previous calls to __llvm_profile_set_filename or
591 * __llvm_profile_override_default_filename or
592 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000593COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000594int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000595 int rc, Length;
596 const char *Filename;
597 char *FilenameBuf;
Rong Xucf1f6fb2017-03-17 18:41:33 +0000598 int PDeathSig = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000599
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000600 if (lprofProfileDumped()) {
601 PROF_NOTE("Profile data not written to file: %s.\n",
602 "already written");
603 return 0;
604 }
605
Xinliang David Li315e49d2016-05-24 21:29:18 +0000606 Length = getCurFilenameLength();
607 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000608 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Li315e49d2016-05-24 21:29:18 +0000609
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000610 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000611 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000612 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000613 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000614 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000615
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000616 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000617 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000618 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000619 "expected %d, but get %d\n",
620 INSTR_PROF_RAW_VERSION,
621 (int)GET_VERSION(__llvm_profile_get_version()));
622 return -1;
623 }
624
Rong Xucf1f6fb2017-03-17 18:41:33 +0000625 // Temporarily suspend getting SIGKILL when the parent exits.
626 PDeathSig = lprofSuspendSigKill();
627
Xinliang David Li315e49d2016-05-24 21:29:18 +0000628 /* Write profile data to the file. */
629 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000630 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000631 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Rong Xucf1f6fb2017-03-17 18:41:33 +0000632
633 // Restore SIGKILL.
634 if (PDeathSig == 1)
635 lprofRestoreSigKill();
636
Justin Bogner66fd5c92015-01-16 20:10:56 +0000637 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000638}
639
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000640COMPILER_RT_VISIBILITY
641int __llvm_profile_dump(void) {
642 if (!doMerging())
643 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
Nico Weber81291a02016-09-08 01:46:52 +0000644 " of previously dumped profile data : %s. Either use %%m "
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000645 "in profile name or change profile name before dumping.\n",
646 "online profile merging is not on");
647 int rc = __llvm_profile_write_file();
648 lprofSetProfileDumped();
649 return rc;
650}
651
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000652static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000653
Xinliang David Liabfd5532015-12-16 03:29:15 +0000654COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000655int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000656 static int HasBeenRegistered = 0;
657
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000658 if (HasBeenRegistered)
659 return 0;
660
Xinliang David Li4617aa72016-05-18 22:34:05 +0000661 lprofSetupValueProfiler();
662
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000663 HasBeenRegistered = 1;
664 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000665}
Petr Hosek47e5fcb2018-07-25 03:01:35 +0000666
667#endif