blob: 2c1ca9568275245f3d81dc2614ae4eb98e7c9d8a [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
Xinliang David Li14c91c42016-08-02 19:34:00 +000085lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0}, 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000086
87int getpid(void);
88static int getCurFilenameLength();
89static const char *getCurFilename(char *FilenameBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000090static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000091
Xinliang David Li2d5bf072015-11-21 04:16:42 +000092/* Return 1 if there is an error, otherwise return 0. */
93static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
94 void **WriterCtx) {
95 uint32_t I;
96 FILE *File = (FILE *)*WriterCtx;
97 for (I = 0; I < NumIOVecs; I++) {
98 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
99 IOVecs[I].NumElm)
100 return 1;
101 }
102 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000103}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000104
Xinliang David Licda3bc22015-12-29 23:54:41 +0000105COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000106lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000107 FreeHook = &free;
108 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
109 VPBufferSize = BufferSz;
110 return lprofCreateBufferIO(fileWriter, File);
111}
112
113static void setupIOBuffer() {
114 const char *BufferSzStr = 0;
115 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
116 if (BufferSzStr && BufferSzStr[0]) {
117 VPBufferSize = atoi(BufferSzStr);
118 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
119 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000120}
121
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000122/* Read profile data in \c ProfileFile and merge with in-memory
123 profile counters. Returns -1 if there is fatal error, otheriwse
124 0 is returned.
125*/
126static int doProfileMerging(FILE *ProfileFile) {
127 uint64_t ProfileFileSize;
128 char *ProfileBuffer;
129
130 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
131 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
132 strerror(errno));
133 return -1;
134 }
135 ProfileFileSize = ftell(ProfileFile);
136
137 /* Restore file offset. */
138 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
139 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
140 strerror(errno));
141 return -1;
142 }
143
144 /* Nothing to merge. */
145 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
146 if (ProfileFileSize)
147 PROF_WARN("Unable to merge profile data: %s\n",
148 "source profile file is too small.");
149 return 0;
150 }
151
152 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
153 fileno(ProfileFile), 0);
154 if (ProfileBuffer == MAP_FAILED) {
155 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
156 strerror(errno));
157 return -1;
158 }
159
160 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
161 (void)munmap(ProfileBuffer, ProfileFileSize);
162 PROF_WARN("Unable to merge profile data: %s\n",
163 "source profile file is not compatible.");
164 return 0;
165 }
166
167 /* Now start merging */
168 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
169 (void)munmap(ProfileBuffer, ProfileFileSize);
170
171 return 0;
172}
173
174/* Open the profile data for merging. It opens the file in r+b mode with
175 * file locking. If the file has content which is compatible with the
176 * current process, it also reads in the profile data in the file and merge
177 * it with in-memory counters. After the profile data is merged in memory,
178 * the original profile data is truncated and gets ready for the profile
179 * dumper. With profile merging enabled, each executable as well as any of
180 * its instrumented shared libraries dump profile data into their own data file.
181*/
182static FILE *openFileForMerging(const char *ProfileFileName) {
183 FILE *ProfileFile;
184 int rc;
185
186 ProfileFile = lprofOpenFileEx(ProfileFileName);
187 if (!ProfileFile)
188 return NULL;
189
190 rc = doProfileMerging(ProfileFile);
191 if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
192 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
193 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
194 strerror(errno));
195 fclose(ProfileFile);
196 return NULL;
197 }
198 fseek(ProfileFile, 0L, SEEK_SET);
199 return ProfileFile;
200}
201
Xinliang David Li315e49d2016-05-24 21:29:18 +0000202/* Write profile data to file \c OutputName. */
203static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000204 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000205 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000206
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000207 if (!doMerging())
208 OutputFile = fopen(OutputName, "ab");
209 else
210 OutputFile = openFileForMerging(OutputName);
211
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000212 if (!OutputFile)
213 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000214
Xinliang David Li315e49d2016-05-24 21:29:18 +0000215 FreeHook = &free;
216 setupIOBuffer();
217 RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000218
219 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000220 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000221}
222
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000223static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000224 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000225 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000226 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000227 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000228
Xinliang David Li315e49d2016-05-24 21:29:18 +0000229 Length = getCurFilenameLength();
230 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
231 Filename = getCurFilename(FilenameBuf);
232 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000233 return;
234
Diego Novilloeae95142015-07-09 17:21:52 +0000235 /* Create the directory holding the file, if needed. */
Xinliang David Lib6d43b72016-07-19 20:48:00 +0000236 if (lprofFindFirstDirSeparator(Filename)) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000237 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
238 strncpy(Copy, Filename, Length + 1);
Diego Novilloeae95142015-07-09 17:21:52 +0000239 __llvm_profile_recursive_mkdir(Copy);
240 }
241
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000242 /* By pass file truncation to allow online raw profile
243 * merging. */
244 if (lprofCurFilename.MergePoolSize)
245 return;
246
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000247 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000248 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000249 if (!File)
250 return;
251 fclose(File);
252}
253
Xinliang David Li153e8b62016-06-10 20:35:01 +0000254static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000255static void resetFilenameToDefault(void) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000256 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
257 free((void *)lprofCurFilename.FilenamePat);
258 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000259 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000260 lprofCurFilename.FilenamePat = DefaultProfileName;
261 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000262}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000263
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000264static int containsMergeSpecifier(const char *FilenamePat, int I) {
265 return (FilenamePat[I] == 'm' ||
266 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
267 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
268 * to be in-bound as the string is null terminated. */
269 FilenamePat[I + 1] == 'm'));
270}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000271
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000272/* Parses the pattern string \p FilenamePat and stores the result to
273 * lprofcurFilename structure. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000274static int parseFilenamePattern(const char *FilenamePat,
275 unsigned CopyFilenamePat) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000276 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000277 char *PidChars = &lprofCurFilename.PidChars[0];
278 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000279 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000280
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000281 /* Clean up cached prefix. */
282 if (lprofCurFilename.ProfilePathPrefix)
283 free((void *)lprofCurFilename.ProfilePathPrefix);
284 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
285
Xinliang David Li14c91c42016-08-02 19:34:00 +0000286 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
287 free((void *)lprofCurFilename.FilenamePat);
288 }
289
290 if (!CopyFilenamePat)
291 lprofCurFilename.FilenamePat = FilenamePat;
292 else {
293 lprofCurFilename.FilenamePat = strdup(FilenamePat);
294 lprofCurFilename.OwnsFilenamePat = 1;
295 }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000296 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000297 for (I = 0; FilenamePat[I]; ++I)
298 if (FilenamePat[I] == '%') {
299 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000300 if (!NumPids++) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000301 if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
302 PROF_WARN(
303 "Unable to parse filename pattern %s. Using the default name.",
304 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000305 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000306 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000307 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000308 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000309 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000310 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
311 PROF_WARN(
312 "Unable to parse filename pattern %s. Using the default name.",
313 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000314 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000315 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000316 } else if (containsMergeSpecifier(FilenamePat, I)) {
317 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000318 PROF_WARN("%%m specifier can only be specified once in %s.\n",
319 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000320 return -1;
321 }
322 MergingEnabled = 1;
323 if (FilenamePat[I] == 'm')
324 lprofCurFilename.MergePoolSize = 1;
325 else {
326 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
327 I++; /* advance to 'm' */
328 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000329 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000330 }
331
Xinliang David Li7f08d122016-05-25 17:30:15 +0000332 lprofCurFilename.NumPids = NumPids;
333 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000334 return 0;
335}
336
Xinliang David Li153e8b62016-06-10 20:35:01 +0000337static void parseAndSetFilename(const char *FilenamePat,
Xinliang David Li14c91c42016-08-02 19:34:00 +0000338 ProfileNameSpecifier PNS,
339 unsigned CopyFilenamePat) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000340
Xinliang David Li153e8b62016-06-10 20:35:01 +0000341 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
342 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
343
344 if (PNS < OldPNS)
345 return;
346
347 if (!FilenamePat)
348 FilenamePat = DefaultProfileName;
349
Xinliang David Li153e8b62016-06-10 20:35:01 +0000350 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
351 lprofCurFilename.PNS = PNS;
352 return;
353 }
354
355 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li14c91c42016-08-02 19:34:00 +0000356 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
Xinliang David Li7f08d122016-05-25 17:30:15 +0000357 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000358 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000359
Xinliang David Li153e8b62016-06-10 20:35:01 +0000360 if (!OldFilenamePat) {
361 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
362 lprofCurFilename.FilenamePat, getPNSStr(PNS));
363 } else {
364 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
365 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
366 getPNSStr(PNS));
367 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000368
Xinliang David Lie3fc4d02016-07-21 03:38:07 +0000369 truncateCurrentFile();
Xinliang David Li7f08d122016-05-25 17:30:15 +0000370}
371
Xinliang David Li315e49d2016-05-24 21:29:18 +0000372/* Return buffer length that is required to store the current profile
373 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000374/* The length to hold uint64_t followed by 2 digit pool id including '_' */
375#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000376static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000377 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000378 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000379 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000380
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000381 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
382 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000383 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000384
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000385 Len = strlen(lprofCurFilename.FilenamePat) +
386 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
387 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
388 if (lprofCurFilename.MergePoolSize)
389 Len += SIGLEN;
390 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000391}
392
393/* Return the pointer to the current profile file name (after substituting
394 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
395 * to store the resulting filename. If no substitution is needed, the
396 * current filename pattern string is directly returned. */
397static const char *getCurFilename(char *FilenameBuf) {
398 int I, J, PidLength, HostNameLength;
399 const char *FilenamePat = lprofCurFilename.FilenamePat;
400
401 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
402 return 0;
403
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000404 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
405 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000406 return lprofCurFilename.FilenamePat;
407
408 PidLength = strlen(lprofCurFilename.PidChars);
409 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000410 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000411 for (I = 0, J = 0; FilenamePat[I]; ++I)
412 if (FilenamePat[I] == '%') {
413 if (FilenamePat[++I] == 'p') {
414 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000415 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000416 } else if (FilenamePat[I] == 'h') {
417 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000418 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000419 } else if (containsMergeSpecifier(FilenamePat, I)) {
420 char LoadModuleSignature[SIGLEN];
421 int S;
422 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
423 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
424 lprofGetLoadModuleSignature(), ProfilePoolId);
425 if (S == -1 || S > SIGLEN)
426 S = SIGLEN;
427 memcpy(FilenameBuf + J, LoadModuleSignature, S);
428 J += S;
429 if (FilenamePat[I] != 'm')
430 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000431 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000432 /* Drop any unknown substitutions. */
433 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000434 FilenameBuf[J++] = FilenamePat[I];
435 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000436
Xinliang David Li315e49d2016-05-24 21:29:18 +0000437 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000438}
439
Xinliang David Li315e49d2016-05-24 21:29:18 +0000440/* Returns the pointer to the environment variable
441 * string. Returns null if the env var is not set. */
442static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000443 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000444 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000445 return 0;
446 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000447}
448
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000449COMPILER_RT_VISIBILITY
450const char *__llvm_profile_get_path_prefix(void) {
451 int Length;
452 char *FilenameBuf, *Prefix;
453 const char *Filename, *PrefixEnd;
454
455 if (lprofCurFilename.ProfilePathPrefix)
456 return lprofCurFilename.ProfilePathPrefix;
457
458 Length = getCurFilenameLength();
459 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
460 Filename = getCurFilename(FilenameBuf);
461 if (!Filename)
462 return "\0";
463
464 PrefixEnd = lprofFindLastDirSeparator(Filename);
465 if (!PrefixEnd)
466 return "\0";
467
468 Length = PrefixEnd - Filename + 1;
469 Prefix = (char *)malloc(Length + 1);
470 if (!Prefix) {
471 PROF_ERR("Failed to %s\n", "allocate memory.");
472 return "\0";
473 }
474 memcpy(Prefix, Filename, Length);
475 Prefix[Length] = '\0';
476 lprofCurFilename.ProfilePathPrefix = Prefix;
477 return Prefix;
478}
479
Xinliang David Li51fe0022016-05-24 01:23:09 +0000480/* This method is invoked by the runtime initialization hook
481 * InstrProfilingRuntime.o if it is linked in. Both user specified
482 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
483 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000484COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000485void __llvm_profile_initialize_file(void) {
Xinliang David Lie9539332016-07-21 23:19:18 +0000486 const char *EnvFilenamePat;
487 const char *SelectedPat = NULL;
488 ProfileNameSpecifier PNS = PNS_unknown;
489 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000490
Xinliang David Lie9539332016-07-21 23:19:18 +0000491 EnvFilenamePat = getFilenamePatFromEnv();
492 if (EnvFilenamePat) {
493 SelectedPat = EnvFilenamePat;
494 PNS = PNS_environment;
495 } else if (hasCommandLineOverrider) {
496 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
497 PNS = PNS_command_line;
498 } else {
499 SelectedPat = NULL;
500 PNS = PNS_default;
501 }
502
Xinliang David Li14c91c42016-08-02 19:34:00 +0000503 parseAndSetFilename(SelectedPat, PNS, 0);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000504}
505
Xinliang David Li51fe0022016-05-24 01:23:09 +0000506/* This API is directly called by the user application code. It has the
507 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000508 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000509 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000510COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000511void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li14c91c42016-08-02 19:34:00 +0000512 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
Eric Christopherd641b532015-04-28 22:54:51 +0000513}
514
Xinliang David Li315e49d2016-05-24 21:29:18 +0000515/* The public API for writing profile data into the file with name
516 * set by previous calls to __llvm_profile_set_filename or
517 * __llvm_profile_override_default_filename or
518 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000519COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000520int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000521 int rc, Length;
522 const char *Filename;
523 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000524
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000525 if (lprofProfileDumped()) {
526 PROF_NOTE("Profile data not written to file: %s.\n",
527 "already written");
528 return 0;
529 }
530
Xinliang David Li315e49d2016-05-24 21:29:18 +0000531 Length = getCurFilenameLength();
532 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
533 Filename = getCurFilename(FilenameBuf);
534
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000535 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000536 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000537 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000538 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000539 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000540
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000541 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000542 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000543 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000544 "expected %d, but get %d\n",
545 INSTR_PROF_RAW_VERSION,
546 (int)GET_VERSION(__llvm_profile_get_version()));
547 return -1;
548 }
549
Xinliang David Li315e49d2016-05-24 21:29:18 +0000550 /* Write profile data to the file. */
551 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000552 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000553 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Justin Bogner66fd5c92015-01-16 20:10:56 +0000554 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000555}
556
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000557COMPILER_RT_VISIBILITY
558int __llvm_profile_dump(void) {
559 if (!doMerging())
560 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
Nico Weber81291a02016-09-08 01:46:52 +0000561 " of previously dumped profile data : %s. Either use %%m "
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000562 "in profile name or change profile name before dumping.\n",
563 "online profile merging is not on");
564 int rc = __llvm_profile_write_file();
565 lprofSetProfileDumped();
566 return rc;
567}
568
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000569static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000570
Xinliang David Liabfd5532015-12-16 03:29:15 +0000571COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000572int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000573 static int HasBeenRegistered = 0;
574
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000575 if (HasBeenRegistered)
576 return 0;
577
Xinliang David Li4617aa72016-05-18 22:34:05 +0000578 lprofSetupValueProfiler();
579
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000580 HasBeenRegistered = 1;
581 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000582}