1*7c3d14c8STreehugger Robot /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
2*7c3d14c8STreehugger Robot |*
3*7c3d14c8STreehugger Robot |* The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot |*
5*7c3d14c8STreehugger Robot |* This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot |* License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot |*
8*7c3d14c8STreehugger Robot \*===----------------------------------------------------------------------===*/
9*7c3d14c8STreehugger Robot
10*7c3d14c8STreehugger Robot #include "InstrProfiling.h"
11*7c3d14c8STreehugger Robot #include "InstrProfilingInternal.h"
12*7c3d14c8STreehugger Robot #include "InstrProfilingUtil.h"
13*7c3d14c8STreehugger Robot #include <errno.h>
14*7c3d14c8STreehugger Robot #include <stdio.h>
15*7c3d14c8STreehugger Robot #include <stdlib.h>
16*7c3d14c8STreehugger Robot #include <string.h>
17*7c3d14c8STreehugger Robot #ifdef _MSC_VER
18*7c3d14c8STreehugger Robot /* For _alloca. */
19*7c3d14c8STreehugger Robot #include <malloc.h>
20*7c3d14c8STreehugger Robot #endif
21*7c3d14c8STreehugger Robot #if defined(_WIN32)
22*7c3d14c8STreehugger Robot #include "WindowsMMap.h"
23*7c3d14c8STreehugger Robot /* For _chsize_s */
24*7c3d14c8STreehugger Robot #include <io.h>
25*7c3d14c8STreehugger Robot #else
26*7c3d14c8STreehugger Robot #include <sys/file.h>
27*7c3d14c8STreehugger Robot #include <sys/mman.h>
28*7c3d14c8STreehugger Robot #include <unistd.h>
29*7c3d14c8STreehugger Robot #if defined(__linux__)
30*7c3d14c8STreehugger Robot #include <sys/types.h>
31*7c3d14c8STreehugger Robot #endif
32*7c3d14c8STreehugger Robot #endif
33*7c3d14c8STreehugger Robot
34*7c3d14c8STreehugger Robot /* From where is profile name specified.
35*7c3d14c8STreehugger Robot * The order the enumerators define their
36*7c3d14c8STreehugger Robot * precedence. Re-order them may lead to
37*7c3d14c8STreehugger Robot * runtime behavior change. */
38*7c3d14c8STreehugger Robot typedef enum ProfileNameSpecifier {
39*7c3d14c8STreehugger Robot PNS_unknown = 0,
40*7c3d14c8STreehugger Robot PNS_default,
41*7c3d14c8STreehugger Robot PNS_command_line,
42*7c3d14c8STreehugger Robot PNS_environment,
43*7c3d14c8STreehugger Robot PNS_runtime_api
44*7c3d14c8STreehugger Robot } ProfileNameSpecifier;
45*7c3d14c8STreehugger Robot
getPNSStr(ProfileNameSpecifier PNS)46*7c3d14c8STreehugger Robot static const char *getPNSStr(ProfileNameSpecifier PNS) {
47*7c3d14c8STreehugger Robot switch (PNS) {
48*7c3d14c8STreehugger Robot case PNS_default:
49*7c3d14c8STreehugger Robot return "default setting";
50*7c3d14c8STreehugger Robot case PNS_command_line:
51*7c3d14c8STreehugger Robot return "command line";
52*7c3d14c8STreehugger Robot case PNS_environment:
53*7c3d14c8STreehugger Robot return "environment variable";
54*7c3d14c8STreehugger Robot case PNS_runtime_api:
55*7c3d14c8STreehugger Robot return "runtime API";
56*7c3d14c8STreehugger Robot default:
57*7c3d14c8STreehugger Robot return "Unknown";
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot }
60*7c3d14c8STreehugger Robot
61*7c3d14c8STreehugger Robot #define MAX_PID_SIZE 16
62*7c3d14c8STreehugger Robot /* Data structure holding the result of parsed filename pattern. */
63*7c3d14c8STreehugger Robot typedef struct lprofFilename {
64*7c3d14c8STreehugger Robot /* File name string possibly with %p or %h specifiers. */
65*7c3d14c8STreehugger Robot const char *FilenamePat;
66*7c3d14c8STreehugger Robot char PidChars[MAX_PID_SIZE];
67*7c3d14c8STreehugger Robot char Hostname[COMPILER_RT_MAX_HOSTLEN];
68*7c3d14c8STreehugger Robot unsigned NumPids;
69*7c3d14c8STreehugger Robot unsigned NumHosts;
70*7c3d14c8STreehugger Robot /* When in-process merging is enabled, this parameter specifies
71*7c3d14c8STreehugger Robot * the total number of profile data files shared by all the processes
72*7c3d14c8STreehugger Robot * spawned from the same binary. By default the value is 1. If merging
73*7c3d14c8STreehugger Robot * is not enabled, its value should be 0. This parameter is specified
74*7c3d14c8STreehugger Robot * by the %[0-9]m specifier. For instance %2m enables merging using
75*7c3d14c8STreehugger Robot * 2 profile data files. %1m is equivalent to %m. Also %m specifier
76*7c3d14c8STreehugger Robot * can only appear once at the end of the name pattern. */
77*7c3d14c8STreehugger Robot unsigned MergePoolSize;
78*7c3d14c8STreehugger Robot ProfileNameSpecifier PNS;
79*7c3d14c8STreehugger Robot } lprofFilename;
80*7c3d14c8STreehugger Robot
81*7c3d14c8STreehugger Robot lprofFilename lprofCurFilename = {0, {0}, {0}, 0, 0, 0, PNS_unknown};
82*7c3d14c8STreehugger Robot
83*7c3d14c8STreehugger Robot int getpid(void);
84*7c3d14c8STreehugger Robot static int getCurFilenameLength();
85*7c3d14c8STreehugger Robot static const char *getCurFilename(char *FilenameBuf);
doMerging()86*7c3d14c8STreehugger Robot static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
87*7c3d14c8STreehugger Robot
88*7c3d14c8STreehugger Robot /* Return 1 if there is an error, otherwise return 0. */
fileWriter(ProfDataIOVec * IOVecs,uint32_t NumIOVecs,void ** WriterCtx)89*7c3d14c8STreehugger Robot static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
90*7c3d14c8STreehugger Robot void **WriterCtx) {
91*7c3d14c8STreehugger Robot uint32_t I;
92*7c3d14c8STreehugger Robot FILE *File = (FILE *)*WriterCtx;
93*7c3d14c8STreehugger Robot for (I = 0; I < NumIOVecs; I++) {
94*7c3d14c8STreehugger Robot if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
95*7c3d14c8STreehugger Robot IOVecs[I].NumElm)
96*7c3d14c8STreehugger Robot return 1;
97*7c3d14c8STreehugger Robot }
98*7c3d14c8STreehugger Robot return 0;
99*7c3d14c8STreehugger Robot }
100*7c3d14c8STreehugger Robot
101*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY ProfBufferIO *
lprofCreateBufferIOInternal(void * File,uint32_t BufferSz)102*7c3d14c8STreehugger Robot lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
103*7c3d14c8STreehugger Robot FreeHook = &free;
104*7c3d14c8STreehugger Robot DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
105*7c3d14c8STreehugger Robot VPBufferSize = BufferSz;
106*7c3d14c8STreehugger Robot return lprofCreateBufferIO(fileWriter, File);
107*7c3d14c8STreehugger Robot }
108*7c3d14c8STreehugger Robot
setupIOBuffer()109*7c3d14c8STreehugger Robot static void setupIOBuffer() {
110*7c3d14c8STreehugger Robot const char *BufferSzStr = 0;
111*7c3d14c8STreehugger Robot BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
112*7c3d14c8STreehugger Robot if (BufferSzStr && BufferSzStr[0]) {
113*7c3d14c8STreehugger Robot VPBufferSize = atoi(BufferSzStr);
114*7c3d14c8STreehugger Robot DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
115*7c3d14c8STreehugger Robot }
116*7c3d14c8STreehugger Robot }
117*7c3d14c8STreehugger Robot
118*7c3d14c8STreehugger Robot /* Read profile data in \c ProfileFile and merge with in-memory
119*7c3d14c8STreehugger Robot profile counters. Returns -1 if there is fatal error, otheriwse
120*7c3d14c8STreehugger Robot 0 is returned.
121*7c3d14c8STreehugger Robot */
doProfileMerging(FILE * ProfileFile)122*7c3d14c8STreehugger Robot static int doProfileMerging(FILE *ProfileFile) {
123*7c3d14c8STreehugger Robot uint64_t ProfileFileSize;
124*7c3d14c8STreehugger Robot char *ProfileBuffer;
125*7c3d14c8STreehugger Robot
126*7c3d14c8STreehugger Robot if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
127*7c3d14c8STreehugger Robot PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
128*7c3d14c8STreehugger Robot strerror(errno));
129*7c3d14c8STreehugger Robot return -1;
130*7c3d14c8STreehugger Robot }
131*7c3d14c8STreehugger Robot ProfileFileSize = ftell(ProfileFile);
132*7c3d14c8STreehugger Robot
133*7c3d14c8STreehugger Robot /* Restore file offset. */
134*7c3d14c8STreehugger Robot if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
135*7c3d14c8STreehugger Robot PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
136*7c3d14c8STreehugger Robot strerror(errno));
137*7c3d14c8STreehugger Robot return -1;
138*7c3d14c8STreehugger Robot }
139*7c3d14c8STreehugger Robot
140*7c3d14c8STreehugger Robot /* Nothing to merge. */
141*7c3d14c8STreehugger Robot if (ProfileFileSize < sizeof(__llvm_profile_header)) {
142*7c3d14c8STreehugger Robot if (ProfileFileSize)
143*7c3d14c8STreehugger Robot PROF_WARN("Unable to merge profile data: %s\n",
144*7c3d14c8STreehugger Robot "source profile file is too small.");
145*7c3d14c8STreehugger Robot return 0;
146*7c3d14c8STreehugger Robot }
147*7c3d14c8STreehugger Robot
148*7c3d14c8STreehugger Robot ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
149*7c3d14c8STreehugger Robot fileno(ProfileFile), 0);
150*7c3d14c8STreehugger Robot if (ProfileBuffer == MAP_FAILED) {
151*7c3d14c8STreehugger Robot PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
152*7c3d14c8STreehugger Robot strerror(errno));
153*7c3d14c8STreehugger Robot return -1;
154*7c3d14c8STreehugger Robot }
155*7c3d14c8STreehugger Robot
156*7c3d14c8STreehugger Robot if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
157*7c3d14c8STreehugger Robot (void)munmap(ProfileBuffer, ProfileFileSize);
158*7c3d14c8STreehugger Robot PROF_WARN("Unable to merge profile data: %s\n",
159*7c3d14c8STreehugger Robot "source profile file is not compatible.");
160*7c3d14c8STreehugger Robot return 0;
161*7c3d14c8STreehugger Robot }
162*7c3d14c8STreehugger Robot
163*7c3d14c8STreehugger Robot /* Now start merging */
164*7c3d14c8STreehugger Robot __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
165*7c3d14c8STreehugger Robot (void)munmap(ProfileBuffer, ProfileFileSize);
166*7c3d14c8STreehugger Robot
167*7c3d14c8STreehugger Robot return 0;
168*7c3d14c8STreehugger Robot }
169*7c3d14c8STreehugger Robot
170*7c3d14c8STreehugger Robot /* Open the profile data for merging. It opens the file in r+b mode with
171*7c3d14c8STreehugger Robot * file locking. If the file has content which is compatible with the
172*7c3d14c8STreehugger Robot * current process, it also reads in the profile data in the file and merge
173*7c3d14c8STreehugger Robot * it with in-memory counters. After the profile data is merged in memory,
174*7c3d14c8STreehugger Robot * the original profile data is truncated and gets ready for the profile
175*7c3d14c8STreehugger Robot * dumper. With profile merging enabled, each executable as well as any of
176*7c3d14c8STreehugger Robot * its instrumented shared libraries dump profile data into their own data file.
177*7c3d14c8STreehugger Robot */
openFileForMerging(const char * ProfileFileName)178*7c3d14c8STreehugger Robot static FILE *openFileForMerging(const char *ProfileFileName) {
179*7c3d14c8STreehugger Robot FILE *ProfileFile;
180*7c3d14c8STreehugger Robot int rc;
181*7c3d14c8STreehugger Robot
182*7c3d14c8STreehugger Robot ProfileFile = lprofOpenFileEx(ProfileFileName);
183*7c3d14c8STreehugger Robot if (!ProfileFile)
184*7c3d14c8STreehugger Robot return NULL;
185*7c3d14c8STreehugger Robot
186*7c3d14c8STreehugger Robot rc = doProfileMerging(ProfileFile);
187*7c3d14c8STreehugger Robot if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
188*7c3d14c8STreehugger Robot fseek(ProfileFile, 0L, SEEK_SET) == -1) {
189*7c3d14c8STreehugger Robot PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
190*7c3d14c8STreehugger Robot strerror(errno));
191*7c3d14c8STreehugger Robot fclose(ProfileFile);
192*7c3d14c8STreehugger Robot return NULL;
193*7c3d14c8STreehugger Robot }
194*7c3d14c8STreehugger Robot fseek(ProfileFile, 0L, SEEK_SET);
195*7c3d14c8STreehugger Robot return ProfileFile;
196*7c3d14c8STreehugger Robot }
197*7c3d14c8STreehugger Robot
198*7c3d14c8STreehugger Robot /* Write profile data to file \c OutputName. */
writeFile(const char * OutputName)199*7c3d14c8STreehugger Robot static int writeFile(const char *OutputName) {
200*7c3d14c8STreehugger Robot int RetVal;
201*7c3d14c8STreehugger Robot FILE *OutputFile;
202*7c3d14c8STreehugger Robot
203*7c3d14c8STreehugger Robot if (!doMerging())
204*7c3d14c8STreehugger Robot OutputFile = fopen(OutputName, "ab");
205*7c3d14c8STreehugger Robot else
206*7c3d14c8STreehugger Robot OutputFile = openFileForMerging(OutputName);
207*7c3d14c8STreehugger Robot
208*7c3d14c8STreehugger Robot if (!OutputFile)
209*7c3d14c8STreehugger Robot return -1;
210*7c3d14c8STreehugger Robot
211*7c3d14c8STreehugger Robot FreeHook = &free;
212*7c3d14c8STreehugger Robot setupIOBuffer();
213*7c3d14c8STreehugger Robot RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
214*7c3d14c8STreehugger Robot
215*7c3d14c8STreehugger Robot fclose(OutputFile);
216*7c3d14c8STreehugger Robot return RetVal;
217*7c3d14c8STreehugger Robot }
218*7c3d14c8STreehugger Robot
truncateCurrentFile(void)219*7c3d14c8STreehugger Robot static void truncateCurrentFile(void) {
220*7c3d14c8STreehugger Robot const char *Filename;
221*7c3d14c8STreehugger Robot char *FilenameBuf;
222*7c3d14c8STreehugger Robot FILE *File;
223*7c3d14c8STreehugger Robot int Length;
224*7c3d14c8STreehugger Robot
225*7c3d14c8STreehugger Robot Length = getCurFilenameLength();
226*7c3d14c8STreehugger Robot FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
227*7c3d14c8STreehugger Robot Filename = getCurFilename(FilenameBuf);
228*7c3d14c8STreehugger Robot if (!Filename)
229*7c3d14c8STreehugger Robot return;
230*7c3d14c8STreehugger Robot
231*7c3d14c8STreehugger Robot /* Create the directory holding the file, if needed. */
232*7c3d14c8STreehugger Robot if (strchr(Filename, '/') || strchr(Filename, '\\')) {
233*7c3d14c8STreehugger Robot char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
234*7c3d14c8STreehugger Robot strncpy(Copy, Filename, Length + 1);
235*7c3d14c8STreehugger Robot __llvm_profile_recursive_mkdir(Copy);
236*7c3d14c8STreehugger Robot }
237*7c3d14c8STreehugger Robot
238*7c3d14c8STreehugger Robot /* Truncate the file. Later we'll reopen and append. */
239*7c3d14c8STreehugger Robot File = fopen(Filename, "w");
240*7c3d14c8STreehugger Robot if (!File)
241*7c3d14c8STreehugger Robot return;
242*7c3d14c8STreehugger Robot fclose(File);
243*7c3d14c8STreehugger Robot }
244*7c3d14c8STreehugger Robot
245*7c3d14c8STreehugger Robot static const char *DefaultProfileName = "default.profraw";
resetFilenameToDefault(void)246*7c3d14c8STreehugger Robot static void resetFilenameToDefault(void) {
247*7c3d14c8STreehugger Robot memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
248*7c3d14c8STreehugger Robot lprofCurFilename.FilenamePat = DefaultProfileName;
249*7c3d14c8STreehugger Robot lprofCurFilename.PNS = PNS_default;
250*7c3d14c8STreehugger Robot }
251*7c3d14c8STreehugger Robot
containsMergeSpecifier(const char * FilenamePat,int I)252*7c3d14c8STreehugger Robot static int containsMergeSpecifier(const char *FilenamePat, int I) {
253*7c3d14c8STreehugger Robot return (FilenamePat[I] == 'm' ||
254*7c3d14c8STreehugger Robot (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
255*7c3d14c8STreehugger Robot /* If FilenamePat[I] is not '\0', the next byte is guaranteed
256*7c3d14c8STreehugger Robot * to be in-bound as the string is null terminated. */
257*7c3d14c8STreehugger Robot FilenamePat[I + 1] == 'm'));
258*7c3d14c8STreehugger Robot }
259*7c3d14c8STreehugger Robot
260*7c3d14c8STreehugger Robot /* Parses the pattern string \p FilenamePat and stores the result to
261*7c3d14c8STreehugger Robot * lprofcurFilename structure. */
parseFilenamePattern(const char * FilenamePat)262*7c3d14c8STreehugger Robot static int parseFilenamePattern(const char *FilenamePat) {
263*7c3d14c8STreehugger Robot int NumPids = 0, NumHosts = 0, I;
264*7c3d14c8STreehugger Robot char *PidChars = &lprofCurFilename.PidChars[0];
265*7c3d14c8STreehugger Robot char *Hostname = &lprofCurFilename.Hostname[0];
266*7c3d14c8STreehugger Robot int MergingEnabled = 0;
267*7c3d14c8STreehugger Robot
268*7c3d14c8STreehugger Robot lprofCurFilename.FilenamePat = FilenamePat;
269*7c3d14c8STreehugger Robot /* Check the filename for "%p", which indicates a pid-substitution. */
270*7c3d14c8STreehugger Robot for (I = 0; FilenamePat[I]; ++I)
271*7c3d14c8STreehugger Robot if (FilenamePat[I] == '%') {
272*7c3d14c8STreehugger Robot if (FilenamePat[++I] == 'p') {
273*7c3d14c8STreehugger Robot if (!NumPids++) {
274*7c3d14c8STreehugger Robot if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
275*7c3d14c8STreehugger Robot PROF_WARN(
276*7c3d14c8STreehugger Robot "Unable to parse filename pattern %s. Using the default name.",
277*7c3d14c8STreehugger Robot FilenamePat);
278*7c3d14c8STreehugger Robot return -1;
279*7c3d14c8STreehugger Robot }
280*7c3d14c8STreehugger Robot }
281*7c3d14c8STreehugger Robot } else if (FilenamePat[I] == 'h') {
282*7c3d14c8STreehugger Robot if (!NumHosts++)
283*7c3d14c8STreehugger Robot if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
284*7c3d14c8STreehugger Robot PROF_WARN(
285*7c3d14c8STreehugger Robot "Unable to parse filename pattern %s. Using the default name.",
286*7c3d14c8STreehugger Robot FilenamePat);
287*7c3d14c8STreehugger Robot return -1;
288*7c3d14c8STreehugger Robot }
289*7c3d14c8STreehugger Robot } else if (containsMergeSpecifier(FilenamePat, I)) {
290*7c3d14c8STreehugger Robot if (MergingEnabled) {
291*7c3d14c8STreehugger Robot PROF_WARN("%%m specifier can only be specified once in %s.\n",
292*7c3d14c8STreehugger Robot FilenamePat);
293*7c3d14c8STreehugger Robot return -1;
294*7c3d14c8STreehugger Robot }
295*7c3d14c8STreehugger Robot MergingEnabled = 1;
296*7c3d14c8STreehugger Robot if (FilenamePat[I] == 'm')
297*7c3d14c8STreehugger Robot lprofCurFilename.MergePoolSize = 1;
298*7c3d14c8STreehugger Robot else {
299*7c3d14c8STreehugger Robot lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
300*7c3d14c8STreehugger Robot I++; /* advance to 'm' */
301*7c3d14c8STreehugger Robot }
302*7c3d14c8STreehugger Robot }
303*7c3d14c8STreehugger Robot }
304*7c3d14c8STreehugger Robot
305*7c3d14c8STreehugger Robot lprofCurFilename.NumPids = NumPids;
306*7c3d14c8STreehugger Robot lprofCurFilename.NumHosts = NumHosts;
307*7c3d14c8STreehugger Robot return 0;
308*7c3d14c8STreehugger Robot }
309*7c3d14c8STreehugger Robot
parseAndSetFilename(const char * FilenamePat,ProfileNameSpecifier PNS)310*7c3d14c8STreehugger Robot static void parseAndSetFilename(const char *FilenamePat,
311*7c3d14c8STreehugger Robot ProfileNameSpecifier PNS) {
312*7c3d14c8STreehugger Robot
313*7c3d14c8STreehugger Robot const char *OldFilenamePat = lprofCurFilename.FilenamePat;
314*7c3d14c8STreehugger Robot ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
315*7c3d14c8STreehugger Robot
316*7c3d14c8STreehugger Robot if (PNS < OldPNS)
317*7c3d14c8STreehugger Robot return;
318*7c3d14c8STreehugger Robot
319*7c3d14c8STreehugger Robot if (!FilenamePat)
320*7c3d14c8STreehugger Robot FilenamePat = DefaultProfileName;
321*7c3d14c8STreehugger Robot
322*7c3d14c8STreehugger Robot /* When -fprofile-instr-generate=<path> is specified on the
323*7c3d14c8STreehugger Robot * command line, each module will be instrumented with runtime
324*7c3d14c8STreehugger Robot * init call to __llvm_profile_init function which calls
325*7c3d14c8STreehugger Robot * __llvm_profile_override_default_filename. In most of the cases,
326*7c3d14c8STreehugger Robot * the path will be identical, so bypass the parsing completely.
327*7c3d14c8STreehugger Robot */
328*7c3d14c8STreehugger Robot if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
329*7c3d14c8STreehugger Robot lprofCurFilename.PNS = PNS;
330*7c3d14c8STreehugger Robot return;
331*7c3d14c8STreehugger Robot }
332*7c3d14c8STreehugger Robot
333*7c3d14c8STreehugger Robot /* When PNS >= OldPNS, the last one wins. */
334*7c3d14c8STreehugger Robot if (!FilenamePat || parseFilenamePattern(FilenamePat))
335*7c3d14c8STreehugger Robot resetFilenameToDefault();
336*7c3d14c8STreehugger Robot lprofCurFilename.PNS = PNS;
337*7c3d14c8STreehugger Robot
338*7c3d14c8STreehugger Robot if (!OldFilenamePat) {
339*7c3d14c8STreehugger Robot PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
340*7c3d14c8STreehugger Robot lprofCurFilename.FilenamePat, getPNSStr(PNS));
341*7c3d14c8STreehugger Robot } else {
342*7c3d14c8STreehugger Robot PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
343*7c3d14c8STreehugger Robot OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
344*7c3d14c8STreehugger Robot getPNSStr(PNS));
345*7c3d14c8STreehugger Robot }
346*7c3d14c8STreehugger Robot
347*7c3d14c8STreehugger Robot if (!lprofCurFilename.MergePoolSize)
348*7c3d14c8STreehugger Robot truncateCurrentFile();
349*7c3d14c8STreehugger Robot }
350*7c3d14c8STreehugger Robot
351*7c3d14c8STreehugger Robot /* Return buffer length that is required to store the current profile
352*7c3d14c8STreehugger Robot * filename with PID and hostname substitutions. */
353*7c3d14c8STreehugger Robot /* The length to hold uint64_t followed by 2 digit pool id including '_' */
354*7c3d14c8STreehugger Robot #define SIGLEN 24
getCurFilenameLength()355*7c3d14c8STreehugger Robot static int getCurFilenameLength() {
356*7c3d14c8STreehugger Robot int Len;
357*7c3d14c8STreehugger Robot if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
358*7c3d14c8STreehugger Robot return 0;
359*7c3d14c8STreehugger Robot
360*7c3d14c8STreehugger Robot if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
361*7c3d14c8STreehugger Robot lprofCurFilename.MergePoolSize))
362*7c3d14c8STreehugger Robot return strlen(lprofCurFilename.FilenamePat);
363*7c3d14c8STreehugger Robot
364*7c3d14c8STreehugger Robot Len = strlen(lprofCurFilename.FilenamePat) +
365*7c3d14c8STreehugger Robot lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
366*7c3d14c8STreehugger Robot lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
367*7c3d14c8STreehugger Robot if (lprofCurFilename.MergePoolSize)
368*7c3d14c8STreehugger Robot Len += SIGLEN;
369*7c3d14c8STreehugger Robot return Len;
370*7c3d14c8STreehugger Robot }
371*7c3d14c8STreehugger Robot
372*7c3d14c8STreehugger Robot /* Return the pointer to the current profile file name (after substituting
373*7c3d14c8STreehugger Robot * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
374*7c3d14c8STreehugger Robot * to store the resulting filename. If no substitution is needed, the
375*7c3d14c8STreehugger Robot * current filename pattern string is directly returned. */
getCurFilename(char * FilenameBuf)376*7c3d14c8STreehugger Robot static const char *getCurFilename(char *FilenameBuf) {
377*7c3d14c8STreehugger Robot int I, J, PidLength, HostNameLength;
378*7c3d14c8STreehugger Robot const char *FilenamePat = lprofCurFilename.FilenamePat;
379*7c3d14c8STreehugger Robot
380*7c3d14c8STreehugger Robot if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
381*7c3d14c8STreehugger Robot return 0;
382*7c3d14c8STreehugger Robot
383*7c3d14c8STreehugger Robot if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
384*7c3d14c8STreehugger Robot lprofCurFilename.MergePoolSize))
385*7c3d14c8STreehugger Robot return lprofCurFilename.FilenamePat;
386*7c3d14c8STreehugger Robot
387*7c3d14c8STreehugger Robot PidLength = strlen(lprofCurFilename.PidChars);
388*7c3d14c8STreehugger Robot HostNameLength = strlen(lprofCurFilename.Hostname);
389*7c3d14c8STreehugger Robot /* Construct the new filename. */
390*7c3d14c8STreehugger Robot for (I = 0, J = 0; FilenamePat[I]; ++I)
391*7c3d14c8STreehugger Robot if (FilenamePat[I] == '%') {
392*7c3d14c8STreehugger Robot if (FilenamePat[++I] == 'p') {
393*7c3d14c8STreehugger Robot memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
394*7c3d14c8STreehugger Robot J += PidLength;
395*7c3d14c8STreehugger Robot } else if (FilenamePat[I] == 'h') {
396*7c3d14c8STreehugger Robot memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
397*7c3d14c8STreehugger Robot J += HostNameLength;
398*7c3d14c8STreehugger Robot } else if (containsMergeSpecifier(FilenamePat, I)) {
399*7c3d14c8STreehugger Robot char LoadModuleSignature[SIGLEN];
400*7c3d14c8STreehugger Robot int S;
401*7c3d14c8STreehugger Robot int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
402*7c3d14c8STreehugger Robot S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
403*7c3d14c8STreehugger Robot lprofGetLoadModuleSignature(), ProfilePoolId);
404*7c3d14c8STreehugger Robot if (S == -1 || S > SIGLEN)
405*7c3d14c8STreehugger Robot S = SIGLEN;
406*7c3d14c8STreehugger Robot memcpy(FilenameBuf + J, LoadModuleSignature, S);
407*7c3d14c8STreehugger Robot J += S;
408*7c3d14c8STreehugger Robot if (FilenamePat[I] != 'm')
409*7c3d14c8STreehugger Robot I++;
410*7c3d14c8STreehugger Robot }
411*7c3d14c8STreehugger Robot /* Drop any unknown substitutions. */
412*7c3d14c8STreehugger Robot } else
413*7c3d14c8STreehugger Robot FilenameBuf[J++] = FilenamePat[I];
414*7c3d14c8STreehugger Robot FilenameBuf[J] = 0;
415*7c3d14c8STreehugger Robot
416*7c3d14c8STreehugger Robot return FilenameBuf;
417*7c3d14c8STreehugger Robot }
418*7c3d14c8STreehugger Robot
419*7c3d14c8STreehugger Robot /* Returns the pointer to the environment variable
420*7c3d14c8STreehugger Robot * string. Returns null if the env var is not set. */
getFilenamePatFromEnv(void)421*7c3d14c8STreehugger Robot static const char *getFilenamePatFromEnv(void) {
422*7c3d14c8STreehugger Robot const char *Filename = getenv("LLVM_PROFILE_FILE");
423*7c3d14c8STreehugger Robot if (!Filename || !Filename[0])
424*7c3d14c8STreehugger Robot return 0;
425*7c3d14c8STreehugger Robot return Filename;
426*7c3d14c8STreehugger Robot }
427*7c3d14c8STreehugger Robot
428*7c3d14c8STreehugger Robot /* This method is invoked by the runtime initialization hook
429*7c3d14c8STreehugger Robot * InstrProfilingRuntime.o if it is linked in. Both user specified
430*7c3d14c8STreehugger Robot * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
431*7c3d14c8STreehugger Robot * environment variable can override this default value. */
432*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
__llvm_profile_initialize_file(void)433*7c3d14c8STreehugger Robot void __llvm_profile_initialize_file(void) {
434*7c3d14c8STreehugger Robot const char *FilenamePat;
435*7c3d14c8STreehugger Robot
436*7c3d14c8STreehugger Robot FilenamePat = getFilenamePatFromEnv();
437*7c3d14c8STreehugger Robot parseAndSetFilename(FilenamePat, FilenamePat ? PNS_environment : PNS_default);
438*7c3d14c8STreehugger Robot }
439*7c3d14c8STreehugger Robot
440*7c3d14c8STreehugger Robot /* This API is directly called by the user application code. It has the
441*7c3d14c8STreehugger Robot * highest precedence compared with LLVM_PROFILE_FILE environment variable
442*7c3d14c8STreehugger Robot * and command line option -fprofile-instr-generate=<profile_name>.
443*7c3d14c8STreehugger Robot */
444*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
__llvm_profile_set_filename(const char * FilenamePat)445*7c3d14c8STreehugger Robot void __llvm_profile_set_filename(const char *FilenamePat) {
446*7c3d14c8STreehugger Robot parseAndSetFilename(FilenamePat, PNS_runtime_api);
447*7c3d14c8STreehugger Robot }
448*7c3d14c8STreehugger Robot
449*7c3d14c8STreehugger Robot /*
450*7c3d14c8STreehugger Robot * This API is invoked by the global initializers emitted by Clang/LLVM when
451*7c3d14c8STreehugger Robot * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate
452*7c3d14c8STreehugger Robot * without an argument). This option has lower precedence than the
453*7c3d14c8STreehugger Robot * LLVM_PROFILE_FILE environment variable.
454*7c3d14c8STreehugger Robot */
455*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
__llvm_profile_override_default_filename(const char * FilenamePat)456*7c3d14c8STreehugger Robot void __llvm_profile_override_default_filename(const char *FilenamePat) {
457*7c3d14c8STreehugger Robot parseAndSetFilename(FilenamePat, PNS_command_line);
458*7c3d14c8STreehugger Robot }
459*7c3d14c8STreehugger Robot
460*7c3d14c8STreehugger Robot /* The public API for writing profile data into the file with name
461*7c3d14c8STreehugger Robot * set by previous calls to __llvm_profile_set_filename or
462*7c3d14c8STreehugger Robot * __llvm_profile_override_default_filename or
463*7c3d14c8STreehugger Robot * __llvm_profile_initialize_file. */
464*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
__llvm_profile_write_file(void)465*7c3d14c8STreehugger Robot int __llvm_profile_write_file(void) {
466*7c3d14c8STreehugger Robot int rc, Length;
467*7c3d14c8STreehugger Robot const char *Filename;
468*7c3d14c8STreehugger Robot char *FilenameBuf;
469*7c3d14c8STreehugger Robot
470*7c3d14c8STreehugger Robot Length = getCurFilenameLength();
471*7c3d14c8STreehugger Robot FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
472*7c3d14c8STreehugger Robot Filename = getCurFilename(FilenameBuf);
473*7c3d14c8STreehugger Robot
474*7c3d14c8STreehugger Robot /* Check the filename. */
475*7c3d14c8STreehugger Robot if (!Filename) {
476*7c3d14c8STreehugger Robot PROF_ERR("Failed to write file : %s\n", "Filename not set");
477*7c3d14c8STreehugger Robot return -1;
478*7c3d14c8STreehugger Robot }
479*7c3d14c8STreehugger Robot
480*7c3d14c8STreehugger Robot /* Check if there is llvm/runtime version mismatch. */
481*7c3d14c8STreehugger Robot if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
482*7c3d14c8STreehugger Robot PROF_ERR("Runtime and instrumentation version mismatch : "
483*7c3d14c8STreehugger Robot "expected %d, but get %d\n",
484*7c3d14c8STreehugger Robot INSTR_PROF_RAW_VERSION,
485*7c3d14c8STreehugger Robot (int)GET_VERSION(__llvm_profile_get_version()));
486*7c3d14c8STreehugger Robot return -1;
487*7c3d14c8STreehugger Robot }
488*7c3d14c8STreehugger Robot
489*7c3d14c8STreehugger Robot /* Write profile data to the file. */
490*7c3d14c8STreehugger Robot rc = writeFile(Filename);
491*7c3d14c8STreehugger Robot if (rc)
492*7c3d14c8STreehugger Robot PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
493*7c3d14c8STreehugger Robot return rc;
494*7c3d14c8STreehugger Robot }
495*7c3d14c8STreehugger Robot
writeFileWithoutReturn(void)496*7c3d14c8STreehugger Robot static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
497*7c3d14c8STreehugger Robot
498*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
__llvm_profile_register_write_file_atexit(void)499*7c3d14c8STreehugger Robot int __llvm_profile_register_write_file_atexit(void) {
500*7c3d14c8STreehugger Robot static int HasBeenRegistered = 0;
501*7c3d14c8STreehugger Robot
502*7c3d14c8STreehugger Robot if (HasBeenRegistered)
503*7c3d14c8STreehugger Robot return 0;
504*7c3d14c8STreehugger Robot
505*7c3d14c8STreehugger Robot lprofSetupValueProfiler();
506*7c3d14c8STreehugger Robot
507*7c3d14c8STreehugger Robot HasBeenRegistered = 1;
508*7c3d14c8STreehugger Robot return atexit(writeFileWithoutReturn);
509*7c3d14c8STreehugger Robot }
510