Skip to content

Conversation

@Heath123
Copy link
Contributor

Fixes #164581.

This PR adds support for interpreting perf profiles containing multiple processes running concurrently. Previously, profiling multiple processes worked only if a single instance was running at any given time, since the base address of the binary would be overwritten; see the linked issue for more detail. This is addressed by using a map which stores the base address for each binary's PID.

Since including the PID of the relevant process in perf samples changes the output format, the feature has been gated behind a flag, to prevent breaking existing perf script files.

To compliment the support for multiple processes, the PID filter may now also take multiple processes.

@llvmbot llvmbot added the PGO Profile Guided Optimizations label Nov 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2025

@llvm/pr-subscribers-pgo

Author: circuit10 (Heath123)

Changes

Fixes #164581.

This PR adds support for interpreting perf profiles containing multiple processes running concurrently. Previously, profiling multiple processes worked only if a single instance was running at any given time, since the base address of the binary would be overwritten; see the linked issue for more detail. This is addressed by using a map which stores the base address for each binary's PID.

Since including the PID of the relevant process in perf samples changes the output format, the feature has been gated behind a flag, to prevent breaking existing perf script files.

To compliment the support for multiple processes, the PID filter may now also take multiple processes.


Patch is 47.36 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/169353.diff

13 Files Affected:

  • (added) llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfbin ()
  • (added) llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfscript (+30)
  • (added) llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfbin ()
  • (added) llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfscript (+12)
  • (added) llvm/test/tools/llvm-profgen/Inputs/multi-process-warning.perfscript (+12)
  • (added) llvm/test/tools/llvm-profgen/multi-pid-filter.test (+39)
  • (added) llvm/test/tools/llvm-profgen/multi-process-cs-probe.test (+46)
  • (added) llvm/test/tools/llvm-profgen/multi-process-nocs-noprobe.test (+40)
  • (added) llvm/test/tools/llvm-profgen/multi-process-warning.test (+10)
  • (modified) llvm/tools/llvm-profgen/PerfReader.cpp (+137-35)
  • (modified) llvm/tools/llvm-profgen/PerfReader.h (+15-18)
  • (modified) llvm/tools/llvm-profgen/ProfiledBinary.h (+23-5)
  • (modified) llvm/tools/llvm-profgen/llvm-profgen.cpp (+10-7)
diff --git a/llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfbin b/llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfbin
new file mode 100755
index 0000000000000..6e98e86d236a4
Binary files /dev/null and b/llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfbin differ
diff --git a/llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfscript b/llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfscript
new file mode 100644
index 0000000000000..1e23ed3f81fde
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/Inputs/multi-process-cs-probe.perfscript
@@ -0,0 +1,30 @@
+; Load first process
+ 262511 PERF_RECORD_MMAP2 262511/262511: [0x61f3acfa8000(0x1000) @ 0x1000 103:02 13931513 3957944494]: r-xp /home/multi-process-cs-probe.perfbin
+
+; Samples from first process
+ 262511 
+	    61f3acfa8155
+	    61f3acfa81e9
+	    78d7ee62a1ca
+	    78d7ee62a28b
+	    61f3acfa8065
+ 0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/- 
+
+; Load second process
+ 262512 PERF_RECORD_MMAP2 262512/262512: [0x5b3c4c631000(0x1000) @ 0x1000 103:02 13931513 3957944494]: r-xp /home/multi-process-cs-probe.perfbin
+
+; Samples from both processes
+ 262512 
+	    5b3c4c631195
+	    5b3c4c63120c
+	    73bf70e2a1ca
+	    73bf70e2a28b
+	    5b3c4c631065
+ 0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/5/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/4/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/5/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/5/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/5/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/5/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/5/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/-  0x5b3c4c631190/0x5b3c4c631195/P/-/-/1/UNCOND/-  0x5b3c4c6311a1/0x5b3c4c63117c/P/-/-/1/UNCOND/- 
+ 262511 
+	    61f3acfa813c
+	    61f3acfa81e9
+	    78d7ee62a1ca
+	    78d7ee62a28b
+	    61f3acfa8065
+ 0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/4/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/-  0x61f3acfa8161/0x61f3acfa813c/P/-/-/1/UNCOND/-  0x61f3acfa8150/0x61f3acfa8155/P/-/-/1/UNCOND/- 
diff --git a/llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfbin b/llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfbin
new file mode 100755
index 0000000000000..b7859ec8d6794
Binary files /dev/null and b/llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfbin differ
diff --git a/llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfscript b/llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfscript
new file mode 100644
index 0000000000000..2ec2975cf07af
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/Inputs/multi-process-nocs-noprobe.perfscript
@@ -0,0 +1,12 @@
+; Load first process
+ 149754 PERF_RECORD_MMAP2 149754/149754: [0x5ffeb3637000(0x1000) @ 0x1000 103:02 16291238 2733047042]: r-xp /home/multi-process-nocs-noprobe.perfbin
+
+; Samples from first process
+ 149754      5ffeb3637150 0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/- 
+
+; Load second process
+ 149755 PERF_RECORD_MMAP2 149755/149755: [0x5caa46c41000(0x1000) @ 0x1000 103:02 16291238 2733047042]: r-xp /home/multi-process-nocs-noprobe.perfbin
+
+; Samples from both processes
+ 149755      5caa46c41190 0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/- 
+ 149754      5ffeb3637161 0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/- 
diff --git a/llvm/test/tools/llvm-profgen/Inputs/multi-process-warning.perfscript b/llvm/test/tools/llvm-profgen/Inputs/multi-process-warning.perfscript
new file mode 100644
index 0000000000000..bfca95dd26307
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/Inputs/multi-process-warning.perfscript
@@ -0,0 +1,12 @@
+; Load first process
+PERF_RECORD_MMAP2 149754/149754: [0x5ffeb3637000(0x1000) @ 0x1000 103:02 16291238 2733047042]: r-xp /home/multi-process-nocs-noprobe.perfbin
+
+; Samples from first process
+     5ffeb3637150 0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/- 
+
+; Load second process
+PERF_RECORD_MMAP2 149755/149755: [0x5caa46c41000(0x1000) @ 0x1000 103:02 16291238 2733047042]: r-xp /home/multi-process-nocs-noprobe.perfbin
+
+; Samples from both processes
+     5caa46c41190 0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/-  0x5caa46c41190/0x5caa46c41195/P/-/-/1/UNCOND/-  0x5caa46c411a1/0x5caa46c4117c/P/-/-/1/UNCOND/- 
+     5ffeb3637161 0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/-  0x5ffeb3637161/0x5ffeb363713c/P/-/-/1/UNCOND/-  0x5ffeb3637150/0x5ffeb3637155/P/-/-/1/UNCOND/- 
diff --git a/llvm/test/tools/llvm-profgen/multi-pid-filter.test b/llvm/test/tools/llvm-profgen/multi-pid-filter.test
new file mode 100644
index 0000000000000..0802a6b545555
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/multi-pid-filter.test
@@ -0,0 +1,39 @@
+; Test that the --pid argument works, including when passing multiple PIDs
+
+; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/multi-process-nocs-noprobe.perfscript --binary=%S/Inputs/multi-process-nocs-noprobe.perfbin --output=%t --profile-summary-cold-count=0 --multi-process-profile --pid=149754
+; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-FIRST-ONLY
+
+CHECK-FIRST-ONLY-NOT: loop2
+
+CHECK-FIRST-ONLY: loop1:1302:0
+CHECK-FIRST-ONLY-NEXT: 0: 0
+CHECK-FIRST-ONLY-NEXT: 1: 31
+CHECK-FIRST-ONLY-NEXT: 2: 0
+
+CHECK-FIRST-ONLY-NOT: loop2
+
+; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/multi-process-nocs-noprobe.perfscript --binary=%S/Inputs/multi-process-nocs-noprobe.perfbin --output=%t --profile-summary-cold-count=0 --multi-process-profile --pid=149755
+; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-SECOND-ONLY
+
+CHECK-SECOND-ONLY-NOT: loop1
+
+CHECK-SECOND-ONLY:loop2:655:0
+CHECK-SECOND-ONLY-NEXT: 0: 0
+CHECK-SECOND-ONLY-NEXT: 1: 16
+CHECK-SECOND-ONLY-NEXT: 2: 0
+
+CHECK-SECOND-ONLY-NOT: loop1
+
+; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/multi-process-nocs-noprobe.perfscript --binary=%S/Inputs/multi-process-nocs-noprobe.perfbin --output=%t --profile-summary-cold-count=0 --multi-process-profile --pid=149754,149755
+; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-BOTH
+
+CHECK-BOTH: loop1:1302:0
+CHECK-BOTH-NEXT: 0: 0
+CHECK-BOTH-NEXT: 1: 31
+CHECK-BOTH-NEXT: 2: 0
+CHECK-BOTH-NEXT:loop2:655:0
+CHECK-BOTH-NEXT: 0: 0
+CHECK-BOTH-NEXT: 1: 16
+CHECK-BOTH-NEXT: 2: 0
+
+; original code can be found in multi-process-nocs-noprobe.test
diff --git a/llvm/test/tools/llvm-profgen/multi-process-cs-probe.test b/llvm/test/tools/llvm-profgen/multi-process-cs-probe.test
new file mode 100644
index 0000000000000..adc28040349ff
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/multi-process-cs-probe.test
@@ -0,0 +1,46 @@
+; Check that we can handle profiles with multiple processes, using pseudo probes
+; and a context-senstive profile
+
+; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/multi-process-cs-probe.perfscript --binary=%S/Inputs/multi-process-cs-probe.perfbin --output=%t --profile-summary-cold-count=0 --multi-process-profile
+; RUN: FileCheck %s --input-file %t
+
+CHECK: loop1:93:0
+CHECK-NEXT: 1: 0
+CHECK-NEXT: 2: 31
+CHECK-NEXT: 3: 31
+CHECK-NEXT: 4: 31
+CHECK-NEXT: 5: 0
+CHECK-NEXT: !CFGChecksum: 88680961901
+CHECK-NEXT:loop2:47:0
+CHECK-NEXT: 1: 0
+CHECK-NEXT: 2: 16
+CHECK-NEXT: 3: 16
+CHECK-NEXT: 4: 15
+CHECK-NEXT: 5: 0
+CHECK-NEXT: !CFGChecksum: 88680961901
+
+; original code:
+; clang -g -fpseudo-probe-for-profiling -fno-omit-frame-pointer test.c -o multi-process-cs-probe.perfbin
+#include <stdint.h>
+
+// Loop for a while so we see samples in the function (compiled at -O0)
+void loop1() {
+  for (uint64_t i = 0; i < 10000000000; i++) {}
+}
+
+// Slightly modified to make sure there isn't any kind of linker merging
+void loop2() {
+  for (uint64_t i = 1; i < 10000000001; i++) {}
+}
+
+int main(int argc, char *argv[]) {
+  // Use CLI argument to choose which loop to run, so we can distinguish which
+  // process...
[truncated]

@github-actions
Copy link

github-actions bot commented Nov 24, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@snehasish
Copy link

snehasish commented Nov 26, 2025

Thanks, fixing this will be a nice improvement. It is a bit onerous for the user to have to enumerate all the relevant pids to be processed. I wonder if instead we can read the build id from the binary specified in the command line (--binary) and match it to the build ids collected by perf? This should get you all the pids (for that build id) so that the base address is computed correctly. What do you think?

@Heath123
Copy link
Contributor Author

Thanks, fixing this will be a nice improvement. It is a bit onerous for the user to have to enumerate all the relevant pids to be processed. I wonder if instead we can read the build id from the binary specified in the command line (--binary) and match it to the build ids collected by perf? This should get you all the pids (for that build id) so that the base address is computed correctly. What do you think?

Thanks for the comments.

Passing the PIDs of the relevant processes isn't actually required; if the PID filter is empty, profgen will accept all PIDs, and use the name of the binary for this:

StringRef BinaryName = filename(MMap.BinaryPath, Binary->isCOFF());
if (Binary->isKernel()) {
return Binary->isKernelImageName(BinaryName);
}
return Binary->getName() == BinaryName;

The PID filter is for if the user needs to ignore certain processes in the output which would otherwise pass this check.

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.

[llvm-profgen] [SPGO] Samples incorrectly discarded when multiple processes of target binary run concurrently

3 participants