blob: 1b49d6f42e32fdb0ce462e19d4132efa32a4ebd9 [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
62/* Data structure holding the result of parsed filename pattern. */
63typedef struct lprofFilename {
64 /* File name string possibly with %p or %h specifiers. */
65 const char *FilenamePat;
Xinliang David Lieaf238d2016-07-20 04:26:09 +000066 const char *ProfilePathPrefix;
Xinliang David Li315e49d2016-05-24 21:29:18 +000067 char PidChars[MAX_PID_SIZE];
68 char Hostname[COMPILER_RT_MAX_HOSTLEN];
69 unsigned NumPids;
70 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000071 /* When in-process merging is enabled, this parameter specifies
72 * the total number of profile data files shared by all the processes
73 * spawned from the same binary. By default the value is 1. If merging
74 * is not enabled, its value should be 0. This parameter is specified
75 * by the %[0-9]m specifier. For instance %2m enables merging using
76 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
77 * can only appear once at the end of the name pattern. */
78 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000079 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000080} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000081
Xinliang David Lieaf238d2016-07-20 04:26:09 +000082lprofFilename lprofCurFilename = {0, 0, {0}, {0}, 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000083
84int getpid(void);
85static int getCurFilenameLength();
86static const char *getCurFilename(char *FilenameBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000087static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000088
Xinliang David Li2d5bf072015-11-21 04:16:42 +000089/* Return 1 if there is an error, otherwise return 0. */
90static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
91 void **WriterCtx) {
92 uint32_t I;
93 FILE *File = (FILE *)*WriterCtx;
94 for (I = 0; I < NumIOVecs; I++) {
95 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
96 IOVecs[I].NumElm)
97 return 1;
98 }
99 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000100}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000101
Xinliang David Licda3bc22015-12-29 23:54:41 +0000102COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000103lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000104 FreeHook = &free;
105 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
106 VPBufferSize = BufferSz;
107 return lprofCreateBufferIO(fileWriter, File);
108}
109
110static void setupIOBuffer() {
111 const char *BufferSzStr = 0;
112 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
113 if (BufferSzStr && BufferSzStr[0]) {
114 VPBufferSize = atoi(BufferSzStr);
115 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
116 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000117}
118
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000119/* Read profile data in \c ProfileFile and merge with in-memory
120 profile counters. Returns -1 if there is fatal error, otheriwse
121 0 is returned.
122*/
123static int doProfileMerging(FILE *ProfileFile) {
124 uint64_t ProfileFileSize;
125 char *ProfileBuffer;
126
127 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
128 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
129 strerror(errno));
130 return -1;
131 }
132 ProfileFileSize = ftell(ProfileFile);
133
134 /* Restore file offset. */
135 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
136 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
137 strerror(errno));
138 return -1;
139 }
140
141 /* Nothing to merge. */
142 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
143 if (ProfileFileSize)
144 PROF_WARN("Unable to merge profile data: %s\n",
145 "source profile file is too small.");
146 return 0;
147 }
148
149 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
150 fileno(ProfileFile), 0);
151 if (ProfileBuffer == MAP_FAILED) {
152 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
153 strerror(errno));
154 return -1;
155 }
156
157 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
158 (void)munmap(ProfileBuffer, ProfileFileSize);
159 PROF_WARN("Unable to merge profile data: %s\n",
160 "source profile file is not compatible.");
161 return 0;
162 }
163
164 /* Now start merging */
165 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
166 (void)munmap(ProfileBuffer, ProfileFileSize);
167
168 return 0;
169}
170
171/* Open the profile data for merging. It opens the file in r+b mode with
172 * file locking. If the file has content which is compatible with the
173 * current process, it also reads in the profile data in the file and merge
174 * it with in-memory counters. After the profile data is merged in memory,
175 * the original profile data is truncated and gets ready for the profile
176 * dumper. With profile merging enabled, each executable as well as any of
177 * its instrumented shared libraries dump profile data into their own data file.
178*/
179static FILE *openFileForMerging(const char *ProfileFileName) {
180 FILE *ProfileFile;
181 int rc;
182
183 ProfileFile = lprofOpenFileEx(ProfileFileName);
184 if (!ProfileFile)
185 return NULL;
186
187 rc = doProfileMerging(ProfileFile);
188 if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
189 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
190 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
191 strerror(errno));
192 fclose(ProfileFile);
193 return NULL;
194 }
195 fseek(ProfileFile, 0L, SEEK_SET);
196 return ProfileFile;
197}
198
Xinliang David Li315e49d2016-05-24 21:29:18 +0000199/* Write profile data to file \c OutputName. */
200static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000201 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000202 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000203
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000204 if (!doMerging())
205 OutputFile = fopen(OutputName, "ab");
206 else
207 OutputFile = openFileForMerging(OutputName);
208
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000209 if (!OutputFile)
210 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000211
Xinliang David Li315e49d2016-05-24 21:29:18 +0000212 FreeHook = &free;
213 setupIOBuffer();
214 RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000215
216 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000217 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000218}
219
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000220static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000221 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000222 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000223 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000224 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000225
Xinliang David Li315e49d2016-05-24 21:29:18 +0000226 Length = getCurFilenameLength();
227 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
228 Filename = getCurFilename(FilenameBuf);
229 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000230 return;
231
Diego Novilloeae95142015-07-09 17:21:52 +0000232 /* Create the directory holding the file, if needed. */
Xinliang David Lib6d43b72016-07-19 20:48:00 +0000233 if (lprofFindFirstDirSeparator(Filename)) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000234 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
235 strncpy(Copy, Filename, Length + 1);
Diego Novilloeae95142015-07-09 17:21:52 +0000236 __llvm_profile_recursive_mkdir(Copy);
237 }
238
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000239 /* By pass file truncation to allow online raw profile
240 * merging. */
241 if (lprofCurFilename.MergePoolSize)
242 return;
243
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000244 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000245 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000246 if (!File)
247 return;
248 fclose(File);
249}
250
Xinliang David Li153e8b62016-06-10 20:35:01 +0000251static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000252static void resetFilenameToDefault(void) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000253 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000254 lprofCurFilename.FilenamePat = DefaultProfileName;
255 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000256}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000257
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000258static int containsMergeSpecifier(const char *FilenamePat, int I) {
259 return (FilenamePat[I] == 'm' ||
260 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
261 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
262 * to be in-bound as the string is null terminated. */
263 FilenamePat[I + 1] == 'm'));
264}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000265
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000266/* Parses the pattern string \p FilenamePat and stores the result to
267 * lprofcurFilename structure. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000268static int parseFilenamePattern(const char *FilenamePat) {
269 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000270 char *PidChars = &lprofCurFilename.PidChars[0];
271 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000272 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000273
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000274 /* Clean up cached prefix. */
275 if (lprofCurFilename.ProfilePathPrefix)
276 free((void *)lprofCurFilename.ProfilePathPrefix);
277 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
278
Xinliang David Li7f08d122016-05-25 17:30:15 +0000279 lprofCurFilename.FilenamePat = FilenamePat;
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000280 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000281 for (I = 0; FilenamePat[I]; ++I)
282 if (FilenamePat[I] == '%') {
283 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000284 if (!NumPids++) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000285 if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
286 PROF_WARN(
287 "Unable to parse filename pattern %s. Using the default name.",
288 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000289 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000290 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000291 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000292 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000293 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000294 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
295 PROF_WARN(
296 "Unable to parse filename pattern %s. Using the default name.",
297 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000298 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000299 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000300 } else if (containsMergeSpecifier(FilenamePat, I)) {
301 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000302 PROF_WARN("%%m specifier can only be specified once in %s.\n",
303 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000304 return -1;
305 }
306 MergingEnabled = 1;
307 if (FilenamePat[I] == 'm')
308 lprofCurFilename.MergePoolSize = 1;
309 else {
310 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
311 I++; /* advance to 'm' */
312 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000313 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000314 }
315
Xinliang David Li7f08d122016-05-25 17:30:15 +0000316 lprofCurFilename.NumPids = NumPids;
317 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000318 return 0;
319}
320
Xinliang David Li153e8b62016-06-10 20:35:01 +0000321static void parseAndSetFilename(const char *FilenamePat,
322 ProfileNameSpecifier PNS) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000323
Xinliang David Li153e8b62016-06-10 20:35:01 +0000324 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
325 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
326
327 if (PNS < OldPNS)
328 return;
329
330 if (!FilenamePat)
331 FilenamePat = DefaultProfileName;
332
333 /* When -fprofile-instr-generate=<path> is specified on the
334 * command line, each module will be instrumented with runtime
335 * init call to __llvm_profile_init function which calls
336 * __llvm_profile_override_default_filename. In most of the cases,
337 * the path will be identical, so bypass the parsing completely.
338 */
339 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
340 lprofCurFilename.PNS = PNS;
341 return;
342 }
343
344 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li7f08d122016-05-25 17:30:15 +0000345 if (!FilenamePat || parseFilenamePattern(FilenamePat))
346 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000347 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000348
Xinliang David Li153e8b62016-06-10 20:35:01 +0000349 if (!OldFilenamePat) {
350 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
351 lprofCurFilename.FilenamePat, getPNSStr(PNS));
352 } else {
353 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
354 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
355 getPNSStr(PNS));
356 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000357
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000358 truncateCurrentFile();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000359}
360
Xinliang David Li315e49d2016-05-24 21:29:18 +0000361/* Return buffer length that is required to store the current profile
362 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000363/* The length to hold uint64_t followed by 2 digit pool id including '_' */
364#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000365static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000366 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000367 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000368 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000369
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000370 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
371 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000372 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000373
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000374 Len = strlen(lprofCurFilename.FilenamePat) +
375 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
376 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
377 if (lprofCurFilename.MergePoolSize)
378 Len += SIGLEN;
379 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000380}
381
382/* Return the pointer to the current profile file name (after substituting
383 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
384 * to store the resulting filename. If no substitution is needed, the
385 * current filename pattern string is directly returned. */
386static const char *getCurFilename(char *FilenameBuf) {
387 int I, J, PidLength, HostNameLength;
388 const char *FilenamePat = lprofCurFilename.FilenamePat;
389
390 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
391 return 0;
392
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000393 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
394 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000395 return lprofCurFilename.FilenamePat;
396
397 PidLength = strlen(lprofCurFilename.PidChars);
398 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000399 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000400 for (I = 0, J = 0; FilenamePat[I]; ++I)
401 if (FilenamePat[I] == '%') {
402 if (FilenamePat[++I] == 'p') {
403 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000404 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000405 } else if (FilenamePat[I] == 'h') {
406 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000407 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000408 } else if (containsMergeSpecifier(FilenamePat, I)) {
409 char LoadModuleSignature[SIGLEN];
410 int S;
411 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
412 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
413 lprofGetLoadModuleSignature(), ProfilePoolId);
414 if (S == -1 || S > SIGLEN)
415 S = SIGLEN;
416 memcpy(FilenameBuf + J, LoadModuleSignature, S);
417 J += S;
418 if (FilenamePat[I] != 'm')
419 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000420 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000421 /* Drop any unknown substitutions. */
422 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000423 FilenameBuf[J++] = FilenamePat[I];
424 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000425
Xinliang David Li315e49d2016-05-24 21:29:18 +0000426 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000427}
428
Xinliang David Li315e49d2016-05-24 21:29:18 +0000429/* Returns the pointer to the environment variable
430 * string. Returns null if the env var is not set. */
431static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000432 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000433 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000434 return 0;
435 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000436}
437
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000438COMPILER_RT_VISIBILITY
439const char *__llvm_profile_get_path_prefix(void) {
440 int Length;
441 char *FilenameBuf, *Prefix;
442 const char *Filename, *PrefixEnd;
443
444 if (lprofCurFilename.ProfilePathPrefix)
445 return lprofCurFilename.ProfilePathPrefix;
446
447 Length = getCurFilenameLength();
448 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
449 Filename = getCurFilename(FilenameBuf);
450 if (!Filename)
451 return "\0";
452
453 PrefixEnd = lprofFindLastDirSeparator(Filename);
454 if (!PrefixEnd)
455 return "\0";
456
457 Length = PrefixEnd - Filename + 1;
458 Prefix = (char *)malloc(Length + 1);
459 if (!Prefix) {
460 PROF_ERR("Failed to %s\n", "allocate memory.");
461 return "\0";
462 }
463 memcpy(Prefix, Filename, Length);
464 Prefix[Length] = '\0';
465 lprofCurFilename.ProfilePathPrefix = Prefix;
466 return Prefix;
467}
468
Xinliang David Li51fe0022016-05-24 01:23:09 +0000469/* This method is invoked by the runtime initialization hook
470 * InstrProfilingRuntime.o if it is linked in. Both user specified
471 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
472 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000473COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000474void __llvm_profile_initialize_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000475 const char *FilenamePat;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000476
Xinliang David Li315e49d2016-05-24 21:29:18 +0000477 FilenamePat = getFilenamePatFromEnv();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000478 parseAndSetFilename(FilenamePat, FilenamePat ? PNS_environment : PNS_default);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000479}
480
Xinliang David Li51fe0022016-05-24 01:23:09 +0000481/* This API is directly called by the user application code. It has the
482 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000483 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000484 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000485COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000486void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li153e8b62016-06-10 20:35:01 +0000487 parseAndSetFilename(FilenamePat, PNS_runtime_api);
Eric Christopherd641b532015-04-28 22:54:51 +0000488}
489
Xinliang David Li315e49d2016-05-24 21:29:18 +0000490/*
Xinliang David Li51fe0022016-05-24 01:23:09 +0000491 * This API is invoked by the global initializers emitted by Clang/LLVM when
492 * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate
493 * without an argument). This option has lower precedence than the
494 * LLVM_PROFILE_FILE environment variable.
495 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000496COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000497void __llvm_profile_override_default_filename(const char *FilenamePat) {
Xinliang David Li153e8b62016-06-10 20:35:01 +0000498 parseAndSetFilename(FilenamePat, PNS_command_line);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000499}
500
Xinliang David Li315e49d2016-05-24 21:29:18 +0000501/* The public API for writing profile data into the file with name
502 * set by previous calls to __llvm_profile_set_filename or
503 * __llvm_profile_override_default_filename or
504 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000505COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000506int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000507 int rc, Length;
508 const char *Filename;
509 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000510
Xinliang David Li315e49d2016-05-24 21:29:18 +0000511 Length = getCurFilenameLength();
512 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
513 Filename = getCurFilename(FilenameBuf);
514
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000515 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000516 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000517 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000518 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000519 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000520
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000521 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000522 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000523 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000524 "expected %d, but get %d\n",
525 INSTR_PROF_RAW_VERSION,
526 (int)GET_VERSION(__llvm_profile_get_version()));
527 return -1;
528 }
529
Xinliang David Li315e49d2016-05-24 21:29:18 +0000530 /* Write profile data to the file. */
531 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000532 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000533 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Justin Bogner66fd5c92015-01-16 20:10:56 +0000534 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000535}
536
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000537static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000538
Xinliang David Liabfd5532015-12-16 03:29:15 +0000539COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000540int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000541 static int HasBeenRegistered = 0;
542
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000543 if (HasBeenRegistered)
544 return 0;
545
Xinliang David Li4617aa72016-05-18 22:34:05 +0000546 lprofSetupValueProfiler();
547
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000548 HasBeenRegistered = 1;
549 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000550}