llvm.org GIT mirror llvm / 9b25ff6
PGO: allows the profile data file name to be specified by the LLVMPROF_OUTPUT environment variable. This allows parallel make for profiling code, without it there are file collisions as each parallel run uses the default file name. There is already code in the runtime library to specify the output file name via the command line, but this only works for programs which already process argc/argv. This patch builds on that support. Patch by Alastair Murray. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167269 91177308-0d34-0410-b5e6-96231b3b80d8 Manman Ren 8 years ago
1 changed file(s) with 22 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2727
2828 static char *SavedArgs = 0;
2929 static unsigned SavedArgsLength = 0;
30 static const char *SavedEnvVar = 0;
3031
3132 static const char *OutputFilename = "llvmprof.out";
3233
34 /* check_environment_variable - Check to see if the LLVMPROF_OUTPUT environment
35 * variable is set. If it is then save it and set OutputFilename.
36 */
37 static void check_environment_variable(void) {
38 if (SavedEnvVar) return; /* Guarantee that we can't leak memory. */
39
40 const char *EnvVar = getenv("LLVMPROF_OUTPUT");
41 if (EnvVar) {
42 /* The string that getenv returns is allowed to be statically allocated,
43 * which means it may be changed by future calls to getenv, so copy it.
44 */
45 SavedEnvVar = strdup(EnvVar);
46 OutputFilename = SavedEnvVar;
47 }
48 }
49
3350 /* save_arguments - Save argc and argv as passed into the program for the file
3451 * we output.
52 * If either the LLVMPROF_OUTPUT environment variable or the -llvmprof-output
53 * command line argument are set then change OutputFilename to the provided
54 * value. The command line argument value overrides the environment variable.
3555 */
3656 int save_arguments(int argc, const char **argv) {
3757 unsigned Length, i;
58 if (!SavedEnvVar && !SavedArgs) check_environment_variable();
3859 if (SavedArgs || !argv) return argc; /* This can be called multiple times */
3960
4061 /* Check to see if there are any arguments passed into the program for the
5374 puts("-llvmprof-output requires a filename argument!");
5475 else {
5576 OutputFilename = strdup(argv[1]);
77 if (SavedEnvVar) { free((void *)SavedEnvVar); SavedEnvVar = 0; }
5678 memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
5779 --argc;
5880 }