Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llvm-profgen: Fix race condition #83489

Merged
merged 2 commits into from
Feb 29, 2024
Merged

llvm-profgen: Fix race condition #83489

merged 2 commits into from
Feb 29, 2024

Conversation

MatzeB
Copy link
Contributor

@MatzeB MatzeB commented Feb 29, 2024

Fix race condition when multiple instances of llvm-progen read from the same
inputs.

@MatzeB MatzeB marked this pull request as ready for review February 29, 2024 21:58
@llvmbot llvmbot added the PGO Profile Guided Optimizations label Feb 29, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 29, 2024

@llvm/pr-subscribers-pgo

Author: Matthias Braun (MatzeB)

Changes

Fix race condition when multiple instances of llvm-progen read from the same
inputs.


Full diff: https://github.com/llvm/llvm-project/pull/83489.diff

1 Files Affected:

  • (modified) llvm/tools/llvm-profgen/PerfReader.cpp (+8-3)
diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp
index 313d40483a2591..df952734fbf939 100644
--- a/llvm/tools/llvm-profgen/PerfReader.cpp
+++ b/llvm/tools/llvm-profgen/PerfReader.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 #include "PerfReader.h"
 #include "ProfileGenerator.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Process.h"
@@ -361,8 +362,11 @@ PerfScriptReader::convertPerfDataToTrace(ProfiledBinary *Binary,
     exitWithError("Perf not found.");
   }
   std::string PerfPath = *PerfExecutable;
-  std::string PerfTraceFile = PerfData.str() + ".script.tmp";
-  std::string ErrorFile = PerfData.str() + ".script.err.tmp";
+
+  SmallString<128> PerfTraceFile;
+  sys::fs::createUniquePath("perf-script-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.tmp",
+                            PerfTraceFile, /*MakeAbsolute=*/true);
+  std::string ErrorFile = std::string(PerfTraceFile) + ".script.err.tmp";
   StringRef ScriptMMapArgs[] = {PerfPath, "script",   "--show-mmap-events",
                                 "-F",     "comm,pid", "-i",
                                 PerfData};
@@ -400,7 +404,8 @@ PerfScriptReader::convertPerfDataToTrace(ProfiledBinary *Binary,
                                   PIDs,     "-i",         PerfData};
   sys::ExecuteAndWait(PerfPath, ScriptSampleArgs, std::nullopt, Redirects);
 
-  return {PerfTraceFile, PerfFormat::PerfScript, PerfContent::UnknownContent};
+  return {std::string(PerfTraceFile), PerfFormat::PerfScript,
+          PerfContent::UnknownContent};
 }
 
 void PerfScriptReader::updateBinaryAddress(const MMapEvent &Event) {

Copy link
Contributor

@wlei-llvm wlei-llvm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@MatzeB
Copy link
Contributor Author

MatzeB commented Feb 29, 2024

test failure on windows bot seems unrelated, merging

@MatzeB MatzeB merged commit 8466ab9 into llvm:main Feb 29, 2024
3 of 4 checks passed
@MatzeB MatzeB deleted the fix-profgen branch February 29, 2024 22:53
@HaohaiWen
Copy link
Contributor

@MatzeB , why do we need to create a uniq temp perf-script for each llvm-profgen read?

@MatzeB
Copy link
Contributor Author

MatzeB commented Mar 25, 2024

@MatzeB , why do we need to create a uniq temp perf-script for each llvm-profgen read?

Without this change, when you have two llvm-profgen instances running on the same input at the same time they will overwrite each others intermediate files and break (i.e. first caller starts conversion; at the same time 2nd caller starts conversion writing to the same file; then if first caller starts reading it may read a half-finished conversion from the 2nd caller).
The previous code also had downsides like needing the directory containing the profile file to be writable which isn't necessarily true or expected by the user.

@MatzeB
Copy link
Contributor Author

MatzeB commented Mar 25, 2024

Are you having trouble with temporary files accumulating and you running out of disk space? You could probably submit a patch then that the temporary file is deleted after it was read...

@HaohaiWen
Copy link
Contributor

Are you having trouble with temporary files accumulating and you running out of disk space? You could probably submit a patch then that the temporary file is deleted after it was read...

Yeah, I run out of disk for large project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants