blob: 99b138b2dd46ee4678281fc2b0eb34b236463bb5 [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|*
Chandler Carruth2946cd72019-01-19 08:50:56 +00003|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4|* See https://llvm.org/LICENSE.txt for license information.
5|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00006|*
7\*===----------------------------------------------------------------------===*/
8
Petr Hosek47e5fcb2018-07-25 03:01:35 +00009#if !defined(__Fuchsia__)
10
Joerg Sonnenbergeref24b412015-03-09 11:23:29 +000011#include <errno.h>
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000012#include <stdio.h>
13#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000014#include <string.h>
Xinliang David Li71eddbf2016-05-20 04:52:27 +000015#ifdef _MSC_VER
Xinliang David Li315e49d2016-05-24 21:29:18 +000016/* For _alloca. */
Xinliang David Li71eddbf2016-05-20 04:52:27 +000017#include <malloc.h>
Xinliang David Lifb320a12016-05-20 05:40:07 +000018#endif
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000019#if defined(_WIN32)
20#include "WindowsMMap.h"
21/* For _chsize_s */
22#include <io.h>
Reid Kleckner963aba32018-04-23 17:05:47 +000023#include <process.h>
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000024#else
25#include <sys/file.h>
26#include <sys/mman.h>
27#include <unistd.h>
28#if defined(__linux__)
29#include <sys/types.h>
30#endif
31#endif
Xinliang David Li71eddbf2016-05-20 04:52:27 +000032
Vedant Kumard7c93362017-12-14 19:01:04 +000033#include "InstrProfiling.h"
34#include "InstrProfilingInternal.h"
35#include "InstrProfilingUtil.h"
36
Xinliang David Li153e8b62016-06-10 20:35:01 +000037/* From where is profile name specified.
38 * The order the enumerators define their
39 * precedence. Re-order them may lead to
40 * runtime behavior change. */
41typedef enum ProfileNameSpecifier {
42 PNS_unknown = 0,
43 PNS_default,
44 PNS_command_line,
45 PNS_environment,
46 PNS_runtime_api
47} ProfileNameSpecifier;
48
49static const char *getPNSStr(ProfileNameSpecifier PNS) {
50 switch (PNS) {
51 case PNS_default:
52 return "default setting";
53 case PNS_command_line:
54 return "command line";
55 case PNS_environment:
56 return "environment variable";
57 case PNS_runtime_api:
58 return "runtime API";
59 default:
60 return "Unknown";
61 }
62}
63
Xinliang David Li315e49d2016-05-24 21:29:18 +000064#define MAX_PID_SIZE 16
Ying Yi2c614cf2016-08-10 10:48:02 +000065/* Data structure holding the result of parsed filename pattern. */
Xinliang David Li315e49d2016-05-24 21:29:18 +000066typedef struct lprofFilename {
67 /* File name string possibly with %p or %h specifiers. */
68 const char *FilenamePat;
Xinliang David Li14c91c42016-08-02 19:34:00 +000069 /* A flag indicating if FilenamePat's memory is allocated
70 * by runtime. */
71 unsigned OwnsFilenamePat;
Xinliang David Lieaf238d2016-07-20 04:26:09 +000072 const char *ProfilePathPrefix;
Teresa Johnson73053b22018-07-19 19:03:50 +000073 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +000074 char PidChars[MAX_PID_SIZE];
75 char Hostname[COMPILER_RT_MAX_HOSTLEN];
76 unsigned NumPids;
77 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000078 /* When in-process merging is enabled, this parameter specifies
79 * the total number of profile data files shared by all the processes
80 * spawned from the same binary. By default the value is 1. If merging
81 * is not enabled, its value should be 0. This parameter is specified
82 * by the %[0-9]m specifier. For instance %2m enables merging using
83 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
84 * can only appear once at the end of the name pattern. */
85 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000086 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000087} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000088
Teresa Johnson73053b22018-07-19 19:03:50 +000089COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0},
90 {0}, 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000091
Alex Lorenz341317f2017-08-31 15:51:23 +000092static int getCurFilenameLength();
Teresa Johnson73053b22018-07-19 19:03:50 +000093static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000094static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000095
Xinliang David Li2d5bf072015-11-21 04:16:42 +000096/* Return 1 if there is an error, otherwise return 0. */
Xinliang David Li967669f2017-06-27 17:28:01 +000097static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
98 uint32_t NumIOVecs) {
Xinliang David Li2d5bf072015-11-21 04:16:42 +000099 uint32_t I;
Xinliang David Li967669f2017-06-27 17:28:01 +0000100 FILE *File = (FILE *)This->WriterCtx;
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000101 for (I = 0; I < NumIOVecs; I++) {
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000102 if (IOVecs[I].Data) {
103 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
104 IOVecs[I].NumElm)
105 return 1;
106 } else {
107 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1)
108 return 1;
109 }
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000110 }
111 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000112}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000113
Xinliang David Li967669f2017-06-27 17:28:01 +0000114static void initFileWriter(ProfDataWriter *This, FILE *File) {
115 This->Write = fileWriter;
116 This->WriterCtx = File;
117}
118
Xinliang David Licda3bc22015-12-29 23:54:41 +0000119COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000120lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000121 FreeHook = &free;
122 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
123 VPBufferSize = BufferSz;
Xinliang David Li967669f2017-06-27 17:28:01 +0000124 ProfDataWriter *fileWriter =
125 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
126 initFileWriter(fileWriter, File);
127 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
128 IO->OwnFileWriter = 1;
129 return IO;
Xinliang David Li609fae32016-05-13 18:26:26 +0000130}
131
132static void setupIOBuffer() {
133 const char *BufferSzStr = 0;
134 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
135 if (BufferSzStr && BufferSzStr[0]) {
136 VPBufferSize = atoi(BufferSzStr);
137 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
138 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000139}
140
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000141/* Read profile data in \c ProfileFile and merge with in-memory
142 profile counters. Returns -1 if there is fatal error, otheriwse
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000143 0 is returned. Returning 0 does not mean merge is actually
144 performed. If merge is actually done, *MergeDone is set to 1.
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000145*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000146static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000147 uint64_t ProfileFileSize;
148 char *ProfileBuffer;
149
150 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
151 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
152 strerror(errno));
153 return -1;
154 }
155 ProfileFileSize = ftell(ProfileFile);
156
157 /* Restore file offset. */
158 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
159 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
160 strerror(errno));
161 return -1;
162 }
163
164 /* Nothing to merge. */
165 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
166 if (ProfileFileSize)
167 PROF_WARN("Unable to merge profile data: %s\n",
168 "source profile file is too small.");
169 return 0;
170 }
171
172 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
173 fileno(ProfileFile), 0);
174 if (ProfileBuffer == MAP_FAILED) {
175 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
176 strerror(errno));
177 return -1;
178 }
179
180 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
181 (void)munmap(ProfileBuffer, ProfileFileSize);
182 PROF_WARN("Unable to merge profile data: %s\n",
183 "source profile file is not compatible.");
184 return 0;
185 }
186
187 /* Now start merging */
188 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000189
Rong Xu95ab7582018-04-02 16:57:00 +0000190 // Truncate the file in case merging of value profile did not happend to
191 // prevent from leaving garbage data at the end of the profile file.
Reid Kleckner963aba32018-04-23 17:05:47 +0000192 COMPILER_RT_FTRUNCATE(ProfileFile, __llvm_profile_get_size_for_buffer());
Rong Xu95ab7582018-04-02 16:57:00 +0000193
194 (void)munmap(ProfileBuffer, ProfileFileSize);
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000195 *MergeDone = 1;
196
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000197 return 0;
198}
199
Xinliang David Lif2d94812017-02-14 21:39:55 +0000200/* Create the directory holding the file, if needed. */
201static void createProfileDir(const char *Filename) {
202 size_t Length = strlen(Filename);
203 if (lprofFindFirstDirSeparator(Filename)) {
204 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
205 strncpy(Copy, Filename, Length + 1);
206 __llvm_profile_recursive_mkdir(Copy);
207 }
208}
209
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000210/* Open the profile data for merging. It opens the file in r+b mode with
211 * file locking. If the file has content which is compatible with the
212 * current process, it also reads in the profile data in the file and merge
213 * it with in-memory counters. After the profile data is merged in memory,
214 * the original profile data is truncated and gets ready for the profile
215 * dumper. With profile merging enabled, each executable as well as any of
216 * its instrumented shared libraries dump profile data into their own data file.
217*/
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000218static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000219 FILE *ProfileFile;
220 int rc;
221
Xinliang David Lif2d94812017-02-14 21:39:55 +0000222 createProfileDir(ProfileFileName);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000223 ProfileFile = lprofOpenFileEx(ProfileFileName);
224 if (!ProfileFile)
225 return NULL;
226
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000227 rc = doProfileMerging(ProfileFile, MergeDone);
228 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) ||
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000229 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
230 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
231 strerror(errno));
232 fclose(ProfileFile);
233 return NULL;
234 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000235 return ProfileFile;
236}
237
Xinliang David Li315e49d2016-05-24 21:29:18 +0000238/* Write profile data to file \c OutputName. */
239static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000240 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000241 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000242
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000243 int MergeDone = 0;
Rong Xu95ab7582018-04-02 16:57:00 +0000244 VPMergeHook = &lprofMergeValueProfData;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000245 if (!doMerging())
246 OutputFile = fopen(OutputName, "ab");
247 else
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000248 OutputFile = openFileForMerging(OutputName, &MergeDone);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000249
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000250 if (!OutputFile)
251 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000252
Xinliang David Li315e49d2016-05-24 21:29:18 +0000253 FreeHook = &free;
254 setupIOBuffer();
Xinliang David Li967669f2017-06-27 17:28:01 +0000255 ProfDataWriter fileWriter;
256 initFileWriter(&fileWriter, OutputFile);
Xinliang David Lif50cc3e2017-06-28 16:46:06 +0000257 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000258
259 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000260 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000261}
262
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000263static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000264 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000265 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000266 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000267 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000268
Xinliang David Li315e49d2016-05-24 21:29:18 +0000269 Length = getCurFilenameLength();
270 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000271 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Li315e49d2016-05-24 21:29:18 +0000272 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000273 return;
274
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000275 /* By pass file truncation to allow online raw profile
276 * merging. */
277 if (lprofCurFilename.MergePoolSize)
278 return;
279
Xinliang David Lif2d94812017-02-14 21:39:55 +0000280 createProfileDir(Filename);
281
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000282 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000283 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000284 if (!File)
285 return;
286 fclose(File);
287}
288
Xinliang David Li153e8b62016-06-10 20:35:01 +0000289static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000290static void resetFilenameToDefault(void) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000291 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
292 free((void *)lprofCurFilename.FilenamePat);
293 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000294 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000295 lprofCurFilename.FilenamePat = DefaultProfileName;
296 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000297}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000298
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000299static int containsMergeSpecifier(const char *FilenamePat, int I) {
300 return (FilenamePat[I] == 'm' ||
301 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
302 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
303 * to be in-bound as the string is null terminated. */
304 FilenamePat[I + 1] == 'm'));
305}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000306
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000307/* Parses the pattern string \p FilenamePat and stores the result to
308 * lprofcurFilename structure. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000309static int parseFilenamePattern(const char *FilenamePat,
310 unsigned CopyFilenamePat) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000311 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000312 char *PidChars = &lprofCurFilename.PidChars[0];
313 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000314 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000315
Teresa Johnson73053b22018-07-19 19:03:50 +0000316 /* Clean up cached prefix and filename. */
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000317 if (lprofCurFilename.ProfilePathPrefix)
318 free((void *)lprofCurFilename.ProfilePathPrefix);
Teresa Johnson73053b22018-07-19 19:03:50 +0000319 if (lprofCurFilename.Filename)
320 free((void *)lprofCurFilename.Filename);
321
Xinliang David Li14c91c42016-08-02 19:34:00 +0000322 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
323 free((void *)lprofCurFilename.FilenamePat);
324 }
325
Igor Kudrin63600c72018-07-24 12:28:53 +0000326 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
327
Xinliang David Li14c91c42016-08-02 19:34:00 +0000328 if (!CopyFilenamePat)
329 lprofCurFilename.FilenamePat = FilenamePat;
330 else {
331 lprofCurFilename.FilenamePat = strdup(FilenamePat);
332 lprofCurFilename.OwnsFilenamePat = 1;
333 }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000334 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000335 for (I = 0; FilenamePat[I]; ++I)
336 if (FilenamePat[I] == '%') {
337 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000338 if (!NumPids++) {
Vedant Kumard7c93362017-12-14 19:01:04 +0000339 if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000340 PROF_WARN("Unable to get pid for filename pattern %s. Using the "
341 "default name.",
342 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000343 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000344 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000345 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000346 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000347 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000348 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000349 PROF_WARN("Unable to get hostname for filename pattern %s. Using "
350 "the default name.",
351 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000352 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000353 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000354 } else if (containsMergeSpecifier(FilenamePat, I)) {
355 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000356 PROF_WARN("%%m specifier can only be specified once in %s.\n",
357 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000358 return -1;
359 }
360 MergingEnabled = 1;
361 if (FilenamePat[I] == 'm')
362 lprofCurFilename.MergePoolSize = 1;
363 else {
364 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
365 I++; /* advance to 'm' */
366 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000367 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000368 }
369
Xinliang David Li7f08d122016-05-25 17:30:15 +0000370 lprofCurFilename.NumPids = NumPids;
371 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000372 return 0;
373}
374
Xinliang David Li153e8b62016-06-10 20:35:01 +0000375static void parseAndSetFilename(const char *FilenamePat,
Xinliang David Li14c91c42016-08-02 19:34:00 +0000376 ProfileNameSpecifier PNS,
377 unsigned CopyFilenamePat) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000378
Xinliang David Li153e8b62016-06-10 20:35:01 +0000379 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
380 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
381
382 if (PNS < OldPNS)
383 return;
384
385 if (!FilenamePat)
386 FilenamePat = DefaultProfileName;
387
Xinliang David Li153e8b62016-06-10 20:35:01 +0000388 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
389 lprofCurFilename.PNS = PNS;
390 return;
391 }
392
393 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000394 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
Xinliang David Li7f08d122016-05-25 17:30:15 +0000395 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000396 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000397
Xinliang David Li153e8b62016-06-10 20:35:01 +0000398 if (!OldFilenamePat) {
Xinliang David Lie68df592016-09-22 21:00:29 +0000399 if (getenv("LLVM_PROFILE_VERBOSE"))
400 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
401 lprofCurFilename.FilenamePat, getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000402 } else {
Xinliang David Lie68df592016-09-22 21:00:29 +0000403 if (getenv("LLVM_PROFILE_VERBOSE"))
404 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
405 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
406 getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000407 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000408
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000409 truncateCurrentFile();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000410}
411
Xinliang David Li315e49d2016-05-24 21:29:18 +0000412/* Return buffer length that is required to store the current profile
413 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000414/* The length to hold uint64_t followed by 2 digit pool id including '_' */
415#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000416static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000417 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000418 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000419 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000420
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000421 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
422 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000423 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000424
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000425 Len = strlen(lprofCurFilename.FilenamePat) +
426 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
427 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
428 if (lprofCurFilename.MergePoolSize)
429 Len += SIGLEN;
430 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000431}
432
433/* Return the pointer to the current profile file name (after substituting
434 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
435 * to store the resulting filename. If no substitution is needed, the
Teresa Johnson73053b22018-07-19 19:03:50 +0000436 * current filename pattern string is directly returned, unless ForceUseBuf
437 * is enabled. */
438static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
439 int I, J, PidLength, HostNameLength, FilenamePatLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000440 const char *FilenamePat = lprofCurFilename.FilenamePat;
441
442 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
443 return 0;
444
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000445 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
Teresa Johnson73053b22018-07-19 19:03:50 +0000446 lprofCurFilename.MergePoolSize)) {
447 if (!ForceUseBuf)
448 return lprofCurFilename.FilenamePat;
449
450 FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
451 memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
452 FilenameBuf[FilenamePatLength] = '\0';
453 return FilenameBuf;
454 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000455
456 PidLength = strlen(lprofCurFilename.PidChars);
457 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000458 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000459 for (I = 0, J = 0; FilenamePat[I]; ++I)
460 if (FilenamePat[I] == '%') {
461 if (FilenamePat[++I] == 'p') {
462 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000463 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000464 } else if (FilenamePat[I] == 'h') {
465 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000466 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000467 } else if (containsMergeSpecifier(FilenamePat, I)) {
468 char LoadModuleSignature[SIGLEN];
469 int S;
470 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
471 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
472 lprofGetLoadModuleSignature(), ProfilePoolId);
473 if (S == -1 || S > SIGLEN)
474 S = SIGLEN;
475 memcpy(FilenameBuf + J, LoadModuleSignature, S);
476 J += S;
477 if (FilenamePat[I] != 'm')
478 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000479 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000480 /* Drop any unknown substitutions. */
481 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000482 FilenameBuf[J++] = FilenamePat[I];
483 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000484
Xinliang David Li315e49d2016-05-24 21:29:18 +0000485 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000486}
487
Xinliang David Li315e49d2016-05-24 21:29:18 +0000488/* Returns the pointer to the environment variable
489 * string. Returns null if the env var is not set. */
490static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000491 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000492 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000493 return 0;
494 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000495}
496
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000497COMPILER_RT_VISIBILITY
498const char *__llvm_profile_get_path_prefix(void) {
499 int Length;
500 char *FilenameBuf, *Prefix;
501 const char *Filename, *PrefixEnd;
502
503 if (lprofCurFilename.ProfilePathPrefix)
504 return lprofCurFilename.ProfilePathPrefix;
505
506 Length = getCurFilenameLength();
507 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000508 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000509 if (!Filename)
510 return "\0";
511
512 PrefixEnd = lprofFindLastDirSeparator(Filename);
513 if (!PrefixEnd)
514 return "\0";
515
516 Length = PrefixEnd - Filename + 1;
517 Prefix = (char *)malloc(Length + 1);
518 if (!Prefix) {
519 PROF_ERR("Failed to %s\n", "allocate memory.");
520 return "\0";
521 }
522 memcpy(Prefix, Filename, Length);
523 Prefix[Length] = '\0';
524 lprofCurFilename.ProfilePathPrefix = Prefix;
525 return Prefix;
526}
527
Teresa Johnson73053b22018-07-19 19:03:50 +0000528COMPILER_RT_VISIBILITY
529const char *__llvm_profile_get_filename(void) {
530 int Length;
531 char *FilenameBuf;
532 const char *Filename;
533
534 if (lprofCurFilename.Filename)
535 return lprofCurFilename.Filename;
536
537 Length = getCurFilenameLength();
538 FilenameBuf = (char *)malloc(Length + 1);
539 if (!FilenameBuf) {
540 PROF_ERR("Failed to %s\n", "allocate memory.");
541 return "\0";
542 }
543 Filename = getCurFilename(FilenameBuf, 1);
544 if (!Filename)
545 return "\0";
546
547 lprofCurFilename.Filename = FilenameBuf;
548 return FilenameBuf;
549}
550
Xinliang David Li51fe0022016-05-24 01:23:09 +0000551/* This method is invoked by the runtime initialization hook
552 * InstrProfilingRuntime.o if it is linked in. Both user specified
553 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
554 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000555COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000556void __llvm_profile_initialize_file(void) {
Xinliang David Lie9539332016-07-21 23:19:18 +0000557 const char *EnvFilenamePat;
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000558 const char *SelectedPat = NULL;
559 ProfileNameSpecifier PNS = PNS_unknown;
Xinliang David Lie9539332016-07-21 23:19:18 +0000560 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000561
Xinliang David Lie9539332016-07-21 23:19:18 +0000562 EnvFilenamePat = getFilenamePatFromEnv();
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000563 if (EnvFilenamePat) {
Xinliang David Lic7c53032017-08-23 21:39:33 +0000564 /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
565 at the moment when __llvm_profile_write_file() gets executed. */
566 parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
567 return;
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000568 } else if (hasCommandLineOverrider) {
569 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
570 PNS = PNS_command_line;
Xinliang David Lie9539332016-07-21 23:19:18 +0000571 } else {
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000572 SelectedPat = NULL;
573 PNS = PNS_default;
Xinliang David Lie9539332016-07-21 23:19:18 +0000574 }
575
Xinliang David Li1c9320c2017-08-15 03:13:01 +0000576 parseAndSetFilename(SelectedPat, PNS, 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000577}
578
Xinliang David Li51fe0022016-05-24 01:23:09 +0000579/* This API is directly called by the user application code. It has the
580 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000581 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000582 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000583COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000584void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000585 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
Eric Christopherd641b532015-04-28 22:54:51 +0000586}
587
Xinliang David Li315e49d2016-05-24 21:29:18 +0000588/* The public API for writing profile data into the file with name
589 * set by previous calls to __llvm_profile_set_filename or
590 * __llvm_profile_override_default_filename or
591 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000592COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000593int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000594 int rc, Length;
595 const char *Filename;
596 char *FilenameBuf;
Rong Xucf1f6fb2017-03-17 18:41:33 +0000597 int PDeathSig = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000598
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000599 if (lprofProfileDumped()) {
600 PROF_NOTE("Profile data not written to file: %s.\n",
601 "already written");
602 return 0;
603 }
604
Xinliang David Li315e49d2016-05-24 21:29:18 +0000605 Length = getCurFilenameLength();
606 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
Teresa Johnson73053b22018-07-19 19:03:50 +0000607 Filename = getCurFilename(FilenameBuf, 0);
Xinliang David Li315e49d2016-05-24 21:29:18 +0000608
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000609 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000610 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000611 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000612 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000613 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000614
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000615 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000616 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000617 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000618 "expected %d, but get %d\n",
619 INSTR_PROF_RAW_VERSION,
620 (int)GET_VERSION(__llvm_profile_get_version()));
621 return -1;
622 }
623
Rong Xucf1f6fb2017-03-17 18:41:33 +0000624 // Temporarily suspend getting SIGKILL when the parent exits.
625 PDeathSig = lprofSuspendSigKill();
626
Xinliang David Li315e49d2016-05-24 21:29:18 +0000627 /* Write profile data to the file. */
628 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000629 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000630 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Rong Xucf1f6fb2017-03-17 18:41:33 +0000631
632 // Restore SIGKILL.
633 if (PDeathSig == 1)
634 lprofRestoreSigKill();
635
Justin Bogner66fd5c92015-01-16 20:10:56 +0000636 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000637}
638
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000639COMPILER_RT_VISIBILITY
640int __llvm_profile_dump(void) {
641 if (!doMerging())
642 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
Nico Weber81291a02016-09-08 01:46:52 +0000643 " of previously dumped profile data : %s. Either use %%m "
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000644 "in profile name or change profile name before dumping.\n",
645 "online profile merging is not on");
646 int rc = __llvm_profile_write_file();
647 lprofSetProfileDumped();
648 return rc;
649}
650
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000651static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000652
Xinliang David Liabfd5532015-12-16 03:29:15 +0000653COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000654int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000655 static int HasBeenRegistered = 0;
656
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000657 if (HasBeenRegistered)
658 return 0;
659
Xinliang David Li4617aa72016-05-18 22:34:05 +0000660 lprofSetupValueProfiler();
661
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000662 HasBeenRegistered = 1;
663 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000664}
Petr Hosek47e5fcb2018-07-25 03:01:35 +0000665
666#endif