blob: f82080c98aac4bbbc2a2f92a947d85fb1a9804fc [file] [log] [blame]
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +00001/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00002|*
3|* The LLVM Compiler Infrastructure
4|*
5|* This file is distributed under the University of Illinois Open Source
6|* License. See LICENSE.TXT for details.
7|*
8\*===----------------------------------------------------------------------===*/
9
10#include "InstrProfiling.h"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000011#include "InstrProfilingInternal.h"
Xinliang David Li6e557162015-11-18 21:11:46 +000012#include "InstrProfilingUtil.h"
Joerg Sonnenbergeref24b412015-03-09 11:23:29 +000013#include <errno.h>
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000014#include <stdio.h>
15#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000016#include <string.h>
Xinliang David Li71eddbf2016-05-20 04:52:27 +000017#ifdef _MSC_VER
Xinliang David Li315e49d2016-05-24 21:29:18 +000018/* For _alloca. */
Xinliang David Li71eddbf2016-05-20 04:52:27 +000019#include <malloc.h>
Xinliang David Lifb320a12016-05-20 05:40:07 +000020#endif
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000021#if defined(_WIN32)
22#include "WindowsMMap.h"
23/* For _chsize_s */
24#include <io.h>
25#else
26#include <sys/file.h>
27#include <sys/mman.h>
28#include <unistd.h>
29#if defined(__linux__)
30#include <sys/types.h>
31#endif
32#endif
Xinliang David Li71eddbf2016-05-20 04:52:27 +000033
Xinliang David Li153e8b62016-06-10 20:35:01 +000034/* From where is profile name specified.
35 * The order the enumerators define their
36 * precedence. Re-order them may lead to
37 * runtime behavior change. */
38typedef enum ProfileNameSpecifier {
39 PNS_unknown = 0,
40 PNS_default,
41 PNS_command_line,
42 PNS_environment,
43 PNS_runtime_api
44} ProfileNameSpecifier;
45
46static const char *getPNSStr(ProfileNameSpecifier PNS) {
47 switch (PNS) {
48 case PNS_default:
49 return "default setting";
50 case PNS_command_line:
51 return "command line";
52 case PNS_environment:
53 return "environment variable";
54 case PNS_runtime_api:
55 return "runtime API";
56 default:
57 return "Unknown";
58 }
59}
60
Xinliang David Li315e49d2016-05-24 21:29:18 +000061#define MAX_PID_SIZE 16
Ying Yi2c614cf2016-08-10 10:48:02 +000062/* Data structure holding the result of parsed filename pattern. */
Xinliang David Li315e49d2016-05-24 21:29:18 +000063typedef struct lprofFilename {
64 /* File name string possibly with %p or %h specifiers. */
65 const char *FilenamePat;
Xinliang David Li14c91c42016-08-02 19:34:00 +000066 /* A flag indicating if FilenamePat's memory is allocated
67 * by runtime. */
68 unsigned OwnsFilenamePat;
Xinliang David Lieaf238d2016-07-20 04:26:09 +000069 const char *ProfilePathPrefix;
Xinliang David Li315e49d2016-05-24 21:29:18 +000070 char PidChars[MAX_PID_SIZE];
71 char Hostname[COMPILER_RT_MAX_HOSTLEN];
72 unsigned NumPids;
73 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000074 /* When in-process merging is enabled, this parameter specifies
75 * the total number of profile data files shared by all the processes
76 * spawned from the same binary. By default the value is 1. If merging
77 * is not enabled, its value should be 0. This parameter is specified
78 * by the %[0-9]m specifier. For instance %2m enables merging using
79 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
80 * can only appear once at the end of the name pattern. */
81 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000082 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000083} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000084
Vedant Kumar6ff82bd2016-10-18 00:02:28 +000085COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0},
86 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000087
88int getpid(void);
89static int getCurFilenameLength();
90static const char *getCurFilename(char *FilenameBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000091static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000092
Xinliang David Li2d5bf072015-11-21 04:16:42 +000093/* Return 1 if there is an error, otherwise return 0. */
94static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
95 void **WriterCtx) {
96 uint32_t I;
97 FILE *File = (FILE *)*WriterCtx;
98 for (I = 0; I < NumIOVecs; I++) {
99 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
100 IOVecs[I].NumElm)
101 return 1;
102 }
103 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000104}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000105
Xinliang David Licda3bc22015-12-29 23:54:41 +0000106COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000107lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000108 FreeHook = &free;
109 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
110 VPBufferSize = BufferSz;
111 return lprofCreateBufferIO(fileWriter, File);
112}
113
114static void setupIOBuffer() {
115 const char *BufferSzStr = 0;
116 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
117 if (BufferSzStr && BufferSzStr[0]) {
118 VPBufferSize = atoi(BufferSzStr);
119 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
120 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000121}
122
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000123/* Read profile data in \c ProfileFile and merge with in-memory
124 profile counters. Returns -1 if there is fatal error, otheriwse
125 0 is returned.
126*/
127static int doProfileMerging(FILE *ProfileFile) {
128 uint64_t ProfileFileSize;
129 char *ProfileBuffer;
130
131 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
132 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
133 strerror(errno));
134 return -1;
135 }
136 ProfileFileSize = ftell(ProfileFile);
137
138 /* Restore file offset. */
139 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
140 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
141 strerror(errno));
142 return -1;
143 }
144
145 /* Nothing to merge. */
146 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
147 if (ProfileFileSize)
148 PROF_WARN("Unable to merge profile data: %s\n",
149 "source profile file is too small.");
150 return 0;
151 }
152
153 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
154 fileno(ProfileFile), 0);
155 if (ProfileBuffer == MAP_FAILED) {
156 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
157 strerror(errno));
158 return -1;
159 }
160
161 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
162 (void)munmap(ProfileBuffer, ProfileFileSize);
163 PROF_WARN("Unable to merge profile data: %s\n",
164 "source profile file is not compatible.");
165 return 0;
166 }
167
168 /* Now start merging */
169 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
170 (void)munmap(ProfileBuffer, ProfileFileSize);
171
172 return 0;
173}
174
175/* Open the profile data for merging. It opens the file in r+b mode with
176 * file locking. If the file has content which is compatible with the
177 * current process, it also reads in the profile data in the file and merge
178 * it with in-memory counters. After the profile data is merged in memory,
179 * the original profile data is truncated and gets ready for the profile
180 * dumper. With profile merging enabled, each executable as well as any of
181 * its instrumented shared libraries dump profile data into their own data file.
182*/
183static FILE *openFileForMerging(const char *ProfileFileName) {
184 FILE *ProfileFile;
185 int rc;
186
187 ProfileFile = lprofOpenFileEx(ProfileFileName);
188 if (!ProfileFile)
189 return NULL;
190
191 rc = doProfileMerging(ProfileFile);
192 if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
193 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
194 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
195 strerror(errno));
196 fclose(ProfileFile);
197 return NULL;
198 }
199 fseek(ProfileFile, 0L, SEEK_SET);
200 return ProfileFile;
201}
202
Xinliang David Li315e49d2016-05-24 21:29:18 +0000203/* Write profile data to file \c OutputName. */
204static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000205 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000206 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000207
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000208 if (!doMerging())
209 OutputFile = fopen(OutputName, "ab");
210 else
211 OutputFile = openFileForMerging(OutputName);
212
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000213 if (!OutputFile)
214 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000215
Xinliang David Li315e49d2016-05-24 21:29:18 +0000216 FreeHook = &free;
217 setupIOBuffer();
218 RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000219
220 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000221 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000222}
223
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000224static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000225 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000226 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000227 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000228 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000229
Xinliang David Li315e49d2016-05-24 21:29:18 +0000230 Length = getCurFilenameLength();
231 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
232 Filename = getCurFilename(FilenameBuf);
233 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000234 return;
235
Diego Novilloeae95142015-07-09 17:21:52 +0000236 /* Create the directory holding the file, if needed. */
Xinliang David Lib6d43b72016-07-19 20:48:00 +0000237 if (lprofFindFirstDirSeparator(Filename)) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000238 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
239 strncpy(Copy, Filename, Length + 1);
Diego Novilloeae95142015-07-09 17:21:52 +0000240 __llvm_profile_recursive_mkdir(Copy);
241 }
242
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000243 /* By pass file truncation to allow online raw profile
244 * merging. */
245 if (lprofCurFilename.MergePoolSize)
246 return;
247
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000248 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000249 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000250 if (!File)
251 return;
252 fclose(File);
253}
254
Xinliang David Li153e8b62016-06-10 20:35:01 +0000255static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000256static void resetFilenameToDefault(void) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000257 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
258 free((void *)lprofCurFilename.FilenamePat);
259 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000260 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000261 lprofCurFilename.FilenamePat = DefaultProfileName;
262 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000263}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000264
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000265static int containsMergeSpecifier(const char *FilenamePat, int I) {
266 return (FilenamePat[I] == 'm' ||
267 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
268 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
269 * to be in-bound as the string is null terminated. */
270 FilenamePat[I + 1] == 'm'));
271}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000272
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000273/* Parses the pattern string \p FilenamePat and stores the result to
274 * lprofcurFilename structure. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000275static int parseFilenamePattern(const char *FilenamePat,
276 unsigned CopyFilenamePat) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000277 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000278 char *PidChars = &lprofCurFilename.PidChars[0];
279 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000280 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000281
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000282 /* Clean up cached prefix. */
283 if (lprofCurFilename.ProfilePathPrefix)
284 free((void *)lprofCurFilename.ProfilePathPrefix);
285 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
286
Xinliang David Li14c91c42016-08-02 19:34:00 +0000287 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
288 free((void *)lprofCurFilename.FilenamePat);
289 }
290
291 if (!CopyFilenamePat)
292 lprofCurFilename.FilenamePat = FilenamePat;
293 else {
294 lprofCurFilename.FilenamePat = strdup(FilenamePat);
295 lprofCurFilename.OwnsFilenamePat = 1;
296 }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000297 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000298 for (I = 0; FilenamePat[I]; ++I)
299 if (FilenamePat[I] == '%') {
300 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000301 if (!NumPids++) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000302 if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000303 PROF_WARN("Unable to get pid for filename pattern %s. Using the "
304 "default name.",
305 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000306 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000307 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000308 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000309 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000310 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000311 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
Bob Haarman9ee65ac2016-11-29 15:24:00 +0000312 PROF_WARN("Unable to get hostname for filename pattern %s. Using "
313 "the default name.",
314 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000315 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000316 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000317 } else if (containsMergeSpecifier(FilenamePat, I)) {
318 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000319 PROF_WARN("%%m specifier can only be specified once in %s.\n",
320 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000321 return -1;
322 }
323 MergingEnabled = 1;
324 if (FilenamePat[I] == 'm')
325 lprofCurFilename.MergePoolSize = 1;
326 else {
327 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
328 I++; /* advance to 'm' */
329 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000330 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000331 }
332
Xinliang David Li7f08d122016-05-25 17:30:15 +0000333 lprofCurFilename.NumPids = NumPids;
334 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000335 return 0;
336}
337
Xinliang David Li153e8b62016-06-10 20:35:01 +0000338static void parseAndSetFilename(const char *FilenamePat,
Xinliang David Li14c91c42016-08-02 19:34:00 +0000339 ProfileNameSpecifier PNS,
340 unsigned CopyFilenamePat) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000341
Xinliang David Li153e8b62016-06-10 20:35:01 +0000342 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
343 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
344
345 if (PNS < OldPNS)
346 return;
347
348 if (!FilenamePat)
349 FilenamePat = DefaultProfileName;
350
Xinliang David Li153e8b62016-06-10 20:35:01 +0000351 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
352 lprofCurFilename.PNS = PNS;
353 return;
354 }
355
356 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000357 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
Xinliang David Li7f08d122016-05-25 17:30:15 +0000358 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000359 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000360
Xinliang David Li153e8b62016-06-10 20:35:01 +0000361 if (!OldFilenamePat) {
Xinliang David Lie68df592016-09-22 21:00:29 +0000362 if (getenv("LLVM_PROFILE_VERBOSE"))
363 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
364 lprofCurFilename.FilenamePat, getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000365 } else {
Xinliang David Lie68df592016-09-22 21:00:29 +0000366 if (getenv("LLVM_PROFILE_VERBOSE"))
367 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
368 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
369 getPNSStr(PNS));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000370 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000371
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000372 truncateCurrentFile();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000373}
374
Xinliang David Li315e49d2016-05-24 21:29:18 +0000375/* Return buffer length that is required to store the current profile
376 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000377/* The length to hold uint64_t followed by 2 digit pool id including '_' */
378#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000379static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000380 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000381 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000382 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000383
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000384 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
385 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000386 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000387
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000388 Len = strlen(lprofCurFilename.FilenamePat) +
389 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
390 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
391 if (lprofCurFilename.MergePoolSize)
392 Len += SIGLEN;
393 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000394}
395
396/* Return the pointer to the current profile file name (after substituting
397 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
398 * to store the resulting filename. If no substitution is needed, the
399 * current filename pattern string is directly returned. */
400static const char *getCurFilename(char *FilenameBuf) {
401 int I, J, PidLength, HostNameLength;
402 const char *FilenamePat = lprofCurFilename.FilenamePat;
403
404 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
405 return 0;
406
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000407 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
408 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000409 return lprofCurFilename.FilenamePat;
410
411 PidLength = strlen(lprofCurFilename.PidChars);
412 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000413 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000414 for (I = 0, J = 0; FilenamePat[I]; ++I)
415 if (FilenamePat[I] == '%') {
416 if (FilenamePat[++I] == 'p') {
417 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000418 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000419 } else if (FilenamePat[I] == 'h') {
420 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000421 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000422 } else if (containsMergeSpecifier(FilenamePat, I)) {
423 char LoadModuleSignature[SIGLEN];
424 int S;
425 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
426 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
427 lprofGetLoadModuleSignature(), ProfilePoolId);
428 if (S == -1 || S > SIGLEN)
429 S = SIGLEN;
430 memcpy(FilenameBuf + J, LoadModuleSignature, S);
431 J += S;
432 if (FilenamePat[I] != 'm')
433 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000434 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000435 /* Drop any unknown substitutions. */
436 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000437 FilenameBuf[J++] = FilenamePat[I];
438 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000439
Xinliang David Li315e49d2016-05-24 21:29:18 +0000440 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000441}
442
Xinliang David Li315e49d2016-05-24 21:29:18 +0000443/* Returns the pointer to the environment variable
444 * string. Returns null if the env var is not set. */
445static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000446 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000447 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000448 return 0;
449 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000450}
451
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000452COMPILER_RT_VISIBILITY
453const char *__llvm_profile_get_path_prefix(void) {
454 int Length;
455 char *FilenameBuf, *Prefix;
456 const char *Filename, *PrefixEnd;
457
458 if (lprofCurFilename.ProfilePathPrefix)
459 return lprofCurFilename.ProfilePathPrefix;
460
461 Length = getCurFilenameLength();
462 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
463 Filename = getCurFilename(FilenameBuf);
464 if (!Filename)
465 return "\0";
466
467 PrefixEnd = lprofFindLastDirSeparator(Filename);
468 if (!PrefixEnd)
469 return "\0";
470
471 Length = PrefixEnd - Filename + 1;
472 Prefix = (char *)malloc(Length + 1);
473 if (!Prefix) {
474 PROF_ERR("Failed to %s\n", "allocate memory.");
475 return "\0";
476 }
477 memcpy(Prefix, Filename, Length);
478 Prefix[Length] = '\0';
479 lprofCurFilename.ProfilePathPrefix = Prefix;
480 return Prefix;
481}
482
Xinliang David Li51fe0022016-05-24 01:23:09 +0000483/* This method is invoked by the runtime initialization hook
484 * InstrProfilingRuntime.o if it is linked in. Both user specified
485 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
486 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000487COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000488void __llvm_profile_initialize_file(void) {
Xinliang David Lie9539332016-07-21 23:19:18 +0000489 const char *EnvFilenamePat;
490 const char *SelectedPat = NULL;
491 ProfileNameSpecifier PNS = PNS_unknown;
492 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000493
Xinliang David Lie9539332016-07-21 23:19:18 +0000494 EnvFilenamePat = getFilenamePatFromEnv();
495 if (EnvFilenamePat) {
496 SelectedPat = EnvFilenamePat;
497 PNS = PNS_environment;
498 } else if (hasCommandLineOverrider) {
499 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
500 PNS = PNS_command_line;
501 } else {
502 SelectedPat = NULL;
503 PNS = PNS_default;
504 }
505
Xinliang David Li14c91c42016-08-02 19:34:00 +0000506 parseAndSetFilename(SelectedPat, PNS, 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000507}
508
Xinliang David Li51fe0022016-05-24 01:23:09 +0000509/* This API is directly called by the user application code. It has the
510 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000511 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000512 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000513COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000514void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000515 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
Eric Christopherd641b532015-04-28 22:54:51 +0000516}
517
Xinliang David Li315e49d2016-05-24 21:29:18 +0000518/* The public API for writing profile data into the file with name
519 * set by previous calls to __llvm_profile_set_filename or
520 * __llvm_profile_override_default_filename or
521 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000522COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000523int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000524 int rc, Length;
525 const char *Filename;
526 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000527
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000528 if (lprofProfileDumped()) {
529 PROF_NOTE("Profile data not written to file: %s.\n",
530 "already written");
531 return 0;
532 }
533
Xinliang David Li315e49d2016-05-24 21:29:18 +0000534 Length = getCurFilenameLength();
535 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
536 Filename = getCurFilename(FilenameBuf);
537
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000538 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000539 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000540 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000541 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000542 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000543
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000544 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000545 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000546 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000547 "expected %d, but get %d\n",
548 INSTR_PROF_RAW_VERSION,
549 (int)GET_VERSION(__llvm_profile_get_version()));
550 return -1;
551 }
552
Xinliang David Li315e49d2016-05-24 21:29:18 +0000553 /* Write profile data to the file. */
554 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000555 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000556 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Justin Bogner66fd5c92015-01-16 20:10:56 +0000557 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000558}
559
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000560COMPILER_RT_VISIBILITY
561int __llvm_profile_dump(void) {
562 if (!doMerging())
563 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
Nico Weber81291a02016-09-08 01:46:52 +0000564 " of previously dumped profile data : %s. Either use %%m "
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000565 "in profile name or change profile name before dumping.\n",
566 "online profile merging is not on");
567 int rc = __llvm_profile_write_file();
568 lprofSetProfileDumped();
569 return rc;
570}
571
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000572static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000573
Xinliang David Liabfd5532015-12-16 03:29:15 +0000574COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000575int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000576 static int HasBeenRegistered = 0;
577
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000578 if (HasBeenRegistered)
579 return 0;
580
Xinliang David Li4617aa72016-05-18 22:34:05 +0000581 lprofSetupValueProfiler();
582
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000583 HasBeenRegistered = 1;
584 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000585}