1 /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 \*===----------------------------------------------------------------------===*/
19 #include "WindowsMMap.h"
26 #if defined(__linux__)
27 #include <sys/types.h>
31 #include "InstrProfiling.h"
32 #include "InstrProfilingInternal.h"
33 #include "InstrProfilingUtil.h"
35 /* From where is profile name specified.
36 * The order the enumerators define their
37 * precedence. Re-order them may lead to
38 * runtime behavior change. */
39 typedef enum ProfileNameSpecifier {
45 } ProfileNameSpecifier;
47 static const char *getPNSStr(ProfileNameSpecifier PNS) {
50 return "default setting";
51 case PNS_command_line:
52 return "command line";
54 return "environment variable";
62 #define MAX_PID_SIZE 16
63 /* Data structure holding the result of parsed filename pattern. */
64 typedef struct lprofFilename {
65 /* File name string possibly with %p or %h specifiers. */
66 const char *FilenamePat;
67 /* A flag indicating if FilenamePat's memory is allocated
69 unsigned OwnsFilenamePat;
70 const char *ProfilePathPrefix;
71 char PidChars[MAX_PID_SIZE];
72 char Hostname[COMPILER_RT_MAX_HOSTLEN];
75 /* When in-process merging is enabled, this parameter specifies
76 * the total number of profile data files shared by all the processes
77 * spawned from the same binary. By default the value is 1. If merging
78 * is not enabled, its value should be 0. This parameter is specified
79 * by the %[0-9]m specifier. For instance %2m enables merging using
80 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
81 * can only appear once at the end of the name pattern. */
82 unsigned MergePoolSize;
83 ProfileNameSpecifier PNS;
86 COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0},
87 0, 0, 0, PNS_unknown};
89 static int getCurFilenameLength();
90 static const char *getCurFilename(char *FilenameBuf);
91 static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
93 /* Return 1 if there is an error, otherwise return 0. */
94 static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
97 FILE *File = (FILE *)This->WriterCtx;
98 for (I = 0; I < NumIOVecs; I++) {
100 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
104 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1)
111 static void initFileWriter(ProfDataWriter *This, FILE *File) {
112 This->Write = fileWriter;
113 This->WriterCtx = File;
116 COMPILER_RT_VISIBILITY ProfBufferIO *
117 lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
119 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
120 VPBufferSize = BufferSz;
121 ProfDataWriter *fileWriter =
122 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
123 initFileWriter(fileWriter, File);
124 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
125 IO->OwnFileWriter = 1;
129 static void setupIOBuffer() {
130 const char *BufferSzStr = 0;
131 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
132 if (BufferSzStr && BufferSzStr[0]) {
133 VPBufferSize = atoi(BufferSzStr);
134 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
138 /* Read profile data in \c ProfileFile and merge with in-memory
139 profile counters. Returns -1 if there is fatal error, otheriwse
140 0 is returned. Returning 0 does not mean merge is actually
141 performed. If merge is actually done, *MergeDone is set to 1.
143 static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
144 uint64_t ProfileFileSize;
147 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
148 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
152 ProfileFileSize = ftell(ProfileFile);
154 /* Restore file offset. */
155 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
156 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
161 /* Nothing to merge. */
162 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
164 PROF_WARN("Unable to merge profile data: %s\n",
165 "source profile file is too small.");
169 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
170 fileno(ProfileFile), 0);
171 if (ProfileBuffer == MAP_FAILED) {
172 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
177 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
178 (void)munmap(ProfileBuffer, ProfileFileSize);
179 PROF_WARN("Unable to merge profile data: %s\n",
180 "source profile file is not compatible.");
184 /* Now start merging */
185 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
186 (void)munmap(ProfileBuffer, ProfileFileSize);
193 /* Create the directory holding the file, if needed. */
194 static void createProfileDir(const char *Filename) {
195 size_t Length = strlen(Filename);
196 if (lprofFindFirstDirSeparator(Filename)) {
197 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
198 strncpy(Copy, Filename, Length + 1);
199 __llvm_profile_recursive_mkdir(Copy);
203 /* Open the profile data for merging. It opens the file in r+b mode with
204 * file locking. If the file has content which is compatible with the
205 * current process, it also reads in the profile data in the file and merge
206 * it with in-memory counters. After the profile data is merged in memory,
207 * the original profile data is truncated and gets ready for the profile
208 * dumper. With profile merging enabled, each executable as well as any of
209 * its instrumented shared libraries dump profile data into their own data file.
211 static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
215 createProfileDir(ProfileFileName);
216 ProfileFile = lprofOpenFileEx(ProfileFileName);
220 rc = doProfileMerging(ProfileFile, MergeDone);
221 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) ||
222 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
223 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
231 /* Write profile data to file \c OutputName. */
232 static int writeFile(const char *OutputName) {
238 OutputFile = fopen(OutputName, "ab");
240 OutputFile = openFileForMerging(OutputName, &MergeDone);
247 ProfDataWriter fileWriter;
248 initFileWriter(&fileWriter, OutputFile);
249 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
255 static void truncateCurrentFile(void) {
256 const char *Filename;
261 Length = getCurFilenameLength();
262 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
263 Filename = getCurFilename(FilenameBuf);
267 /* By pass file truncation to allow online raw profile
269 if (lprofCurFilename.MergePoolSize)
272 createProfileDir(Filename);
274 /* Truncate the file. Later we'll reopen and append. */
275 File = fopen(Filename, "w");
281 static const char *DefaultProfileName = "default.profraw";
282 static void resetFilenameToDefault(void) {
283 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
284 free((void *)lprofCurFilename.FilenamePat);
286 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
287 lprofCurFilename.FilenamePat = DefaultProfileName;
288 lprofCurFilename.PNS = PNS_default;
291 static int containsMergeSpecifier(const char *FilenamePat, int I) {
292 return (FilenamePat[I] == 'm' ||
293 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
294 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
295 * to be in-bound as the string is null terminated. */
296 FilenamePat[I + 1] == 'm'));
299 /* Parses the pattern string \p FilenamePat and stores the result to
300 * lprofcurFilename structure. */
301 static int parseFilenamePattern(const char *FilenamePat,
302 unsigned CopyFilenamePat) {
303 int NumPids = 0, NumHosts = 0, I;
304 char *PidChars = &lprofCurFilename.PidChars[0];
305 char *Hostname = &lprofCurFilename.Hostname[0];
306 int MergingEnabled = 0;
308 /* Clean up cached prefix. */
309 if (lprofCurFilename.ProfilePathPrefix)
310 free((void *)lprofCurFilename.ProfilePathPrefix);
311 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
313 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
314 free((void *)lprofCurFilename.FilenamePat);
317 if (!CopyFilenamePat)
318 lprofCurFilename.FilenamePat = FilenamePat;
320 lprofCurFilename.FilenamePat = strdup(FilenamePat);
321 lprofCurFilename.OwnsFilenamePat = 1;
323 /* Check the filename for "%p", which indicates a pid-substitution. */
324 for (I = 0; FilenamePat[I]; ++I)
325 if (FilenamePat[I] == '%') {
326 if (FilenamePat[++I] == 'p') {
328 if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) {
329 PROF_WARN("Unable to get pid for filename pattern %s. Using the "
335 } else if (FilenamePat[I] == 'h') {
337 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
338 PROF_WARN("Unable to get hostname for filename pattern %s. Using "
343 } else if (containsMergeSpecifier(FilenamePat, I)) {
344 if (MergingEnabled) {
345 PROF_WARN("%%m specifier can only be specified once in %s.\n",
350 if (FilenamePat[I] == 'm')
351 lprofCurFilename.MergePoolSize = 1;
353 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
354 I++; /* advance to 'm' */
359 lprofCurFilename.NumPids = NumPids;
360 lprofCurFilename.NumHosts = NumHosts;
364 static void parseAndSetFilename(const char *FilenamePat,
365 ProfileNameSpecifier PNS,
366 unsigned CopyFilenamePat) {
368 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
369 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
375 FilenamePat = DefaultProfileName;
377 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
378 lprofCurFilename.PNS = PNS;
382 /* When PNS >= OldPNS, the last one wins. */
383 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
384 resetFilenameToDefault();
385 lprofCurFilename.PNS = PNS;
387 if (!OldFilenamePat) {
388 if (getenv("LLVM_PROFILE_VERBOSE"))
389 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
390 lprofCurFilename.FilenamePat, getPNSStr(PNS));
392 if (getenv("LLVM_PROFILE_VERBOSE"))
393 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
394 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
398 truncateCurrentFile();
401 /* Return buffer length that is required to store the current profile
402 * filename with PID and hostname substitutions. */
403 /* The length to hold uint64_t followed by 2 digit pool id including '_' */
405 static int getCurFilenameLength() {
407 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
410 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
411 lprofCurFilename.MergePoolSize))
412 return strlen(lprofCurFilename.FilenamePat);
414 Len = strlen(lprofCurFilename.FilenamePat) +
415 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
416 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
417 if (lprofCurFilename.MergePoolSize)
422 /* Return the pointer to the current profile file name (after substituting
423 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
424 * to store the resulting filename. If no substitution is needed, the
425 * current filename pattern string is directly returned. */
426 static const char *getCurFilename(char *FilenameBuf) {
427 int I, J, PidLength, HostNameLength;
428 const char *FilenamePat = lprofCurFilename.FilenamePat;
430 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
433 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
434 lprofCurFilename.MergePoolSize))
435 return lprofCurFilename.FilenamePat;
437 PidLength = strlen(lprofCurFilename.PidChars);
438 HostNameLength = strlen(lprofCurFilename.Hostname);
439 /* Construct the new filename. */
440 for (I = 0, J = 0; FilenamePat[I]; ++I)
441 if (FilenamePat[I] == '%') {
442 if (FilenamePat[++I] == 'p') {
443 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
445 } else if (FilenamePat[I] == 'h') {
446 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
448 } else if (containsMergeSpecifier(FilenamePat, I)) {
449 char LoadModuleSignature[SIGLEN];
451 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
452 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
453 lprofGetLoadModuleSignature(), ProfilePoolId);
454 if (S == -1 || S > SIGLEN)
456 memcpy(FilenameBuf + J, LoadModuleSignature, S);
458 if (FilenamePat[I] != 'm')
461 /* Drop any unknown substitutions. */
463 FilenameBuf[J++] = FilenamePat[I];
469 /* Returns the pointer to the environment variable
470 * string. Returns null if the env var is not set. */
471 static const char *getFilenamePatFromEnv(void) {
472 const char *Filename = getenv("LLVM_PROFILE_FILE");
473 if (!Filename || !Filename[0])
478 COMPILER_RT_VISIBILITY
479 const char *__llvm_profile_get_path_prefix(void) {
481 char *FilenameBuf, *Prefix;
482 const char *Filename, *PrefixEnd;
484 if (lprofCurFilename.ProfilePathPrefix)
485 return lprofCurFilename.ProfilePathPrefix;
487 Length = getCurFilenameLength();
488 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
489 Filename = getCurFilename(FilenameBuf);
493 PrefixEnd = lprofFindLastDirSeparator(Filename);
497 Length = PrefixEnd - Filename + 1;
498 Prefix = (char *)malloc(Length + 1);
500 PROF_ERR("Failed to %s\n", "allocate memory.");
503 memcpy(Prefix, Filename, Length);
504 Prefix[Length] = '\0';
505 lprofCurFilename.ProfilePathPrefix = Prefix;
509 /* This method is invoked by the runtime initialization hook
510 * InstrProfilingRuntime.o if it is linked in. Both user specified
511 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
512 * environment variable can override this default value. */
513 COMPILER_RT_VISIBILITY
514 void __llvm_profile_initialize_file(void) {
515 const char *EnvFilenamePat;
516 const char *SelectedPat = NULL;
517 ProfileNameSpecifier PNS = PNS_unknown;
518 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
520 EnvFilenamePat = getFilenamePatFromEnv();
521 if (EnvFilenamePat) {
522 /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
523 at the moment when __llvm_profile_write_file() gets executed. */
524 parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
526 } else if (hasCommandLineOverrider) {
527 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
528 PNS = PNS_command_line;
534 parseAndSetFilename(SelectedPat, PNS, 0);
537 /* This API is directly called by the user application code. It has the
538 * highest precedence compared with LLVM_PROFILE_FILE environment variable
539 * and command line option -fprofile-instr-generate=<profile_name>.
541 COMPILER_RT_VISIBILITY
542 void __llvm_profile_set_filename(const char *FilenamePat) {
543 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
546 /* The public API for writing profile data into the file with name
547 * set by previous calls to __llvm_profile_set_filename or
548 * __llvm_profile_override_default_filename or
549 * __llvm_profile_initialize_file. */
550 COMPILER_RT_VISIBILITY
551 int __llvm_profile_write_file(void) {
553 const char *Filename;
557 if (lprofProfileDumped()) {
558 PROF_NOTE("Profile data not written to file: %s.\n",
563 Length = getCurFilenameLength();
564 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
565 Filename = getCurFilename(FilenameBuf);
567 /* Check the filename. */
569 PROF_ERR("Failed to write file : %s\n", "Filename not set");
573 /* Check if there is llvm/runtime version mismatch. */
574 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
575 PROF_ERR("Runtime and instrumentation version mismatch : "
576 "expected %d, but get %d\n",
577 INSTR_PROF_RAW_VERSION,
578 (int)GET_VERSION(__llvm_profile_get_version()));
582 // Temporarily suspend getting SIGKILL when the parent exits.
583 PDeathSig = lprofSuspendSigKill();
585 /* Write profile data to the file. */
586 rc = writeFile(Filename);
588 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
592 lprofRestoreSigKill();
597 COMPILER_RT_VISIBILITY
598 int __llvm_profile_dump(void) {
600 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
601 " of previously dumped profile data : %s. Either use %%m "
602 "in profile name or change profile name before dumping.\n",
603 "online profile merging is not on");
604 int rc = __llvm_profile_write_file();
605 lprofSetProfileDumped();
609 static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
611 COMPILER_RT_VISIBILITY
612 int __llvm_profile_register_write_file_atexit(void) {
613 static int HasBeenRegistered = 0;
615 if (HasBeenRegistered)
618 lprofSetupValueProfiler();
620 HasBeenRegistered = 1;
621 return atexit(writeFileWithoutReturn);