about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-05-10 19:03:17 +0000
committerbors <bors@rust-lang.org>2023-05-10 19:03:17 +0000
commitd2ea42fad3baf71ea5bcc6e3c986b3b8b9f465dd (patch)
tree39a2d4008fbcb701b720ac96eb8771cdc213fce3
parent7a41eacf170ed234e059608515115e94fbe721fe (diff)
parentf9a42139682aff545e41b9ed3566fafbafdd09f1 (diff)
downloadrust-d2ea42fad3baf71ea5bcc6e3c986b3b8b9f465dd.tar.gz
rust-d2ea42fad3baf71ea5bcc6e3c986b3b8b9f465dd.zip
Auto merge of #2879 - saethlin:measureme, r=RalfJung
Include the current Crate name in the measureme output name

See https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/measureme.20flamegraph.20panics/near/356367013
cc `@andjo403`

Currently, attempting to use `MIRIFLAGS=-Zmiri-measureme=miri cargo miri test` on a crate with multiple test targets (which is very common) will produce a corrupted measureme output file, because the various interpreter processes will stomp each other's output.

This change does not entirely prevent this, but the various test targets seem to always have different crate names, so if nothing else this will make the broken measureme files much harder to encounter by accident, while also making it clear what they are all for.
-rw-r--r--src/tools/miri/README.md2
-rw-r--r--src/tools/miri/src/machine.rs18
2 files changed, 18 insertions, 2 deletions
diff --git a/src/tools/miri/README.md b/src/tools/miri/README.md
index 640a953dac9..79b0daf9e82 100644
--- a/src/tools/miri/README.md
+++ b/src/tools/miri/README.md
@@ -389,7 +389,7 @@ to Miri failing to detect cases of undefined behavior in a program.
   Follow [the discussion on supporting other types](https://github.com/rust-lang/miri/issues/2365).
 * `-Zmiri-measureme=<name>` enables `measureme` profiling for the interpreted program.
    This can be used to find which parts of your program are executing slowly under Miri.
-   The profile is written out to a file with the prefix `<name>`, and can be processed
+   The profile is written out to a file inside a directory called `<name>`, and can be processed
    using the tools in the repository https://github.com/rust-lang/measureme.
 * `-Zmiri-mute-stdout-stderr` silently ignores all writes to stdout and stderr,
   but reports to the program that it did actually write. This is useful when you
diff --git a/src/tools/miri/src/machine.rs b/src/tools/miri/src/machine.rs
index 21c5a9c1b70..32717a0d28b 100644
--- a/src/tools/miri/src/machine.rs
+++ b/src/tools/miri/src/machine.rs
@@ -4,6 +4,8 @@
 use std::borrow::Cow;
 use std::cell::RefCell;
 use std::fmt;
+use std::path::Path;
+use std::process;
 
 use rand::rngs::StdRng;
 use rand::SeedableRng;
@@ -498,7 +500,21 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
         let layouts =
             PrimitiveLayouts::new(layout_cx).expect("Couldn't get layouts of primitive types");
         let profiler = config.measureme_out.as_ref().map(|out| {
-            measureme::Profiler::new(out).expect("Couldn't create `measureme` profiler")
+            let crate_name = layout_cx
+                .tcx
+                .sess
+                .opts
+                .crate_name
+                .clone()
+                .unwrap_or_else(|| "unknown-crate".to_string());
+            let pid = process::id();
+            // We adopt the same naming scheme for the profiler output that rustc uses. In rustc,
+            // the PID is padded so that the nondeterministic value of the PID does not spread
+            // nondeterminisim to the allocator. In Miri we are not aiming for such performance
+            // control, we just pad for consistency with rustc.
+            let filename = format!("{crate_name}-{pid:07}");
+            let path = Path::new(out).join(filename);
+            measureme::Profiler::new(path).expect("Couldn't create `measureme` profiler")
         });
         let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
         let borrow_tracker = config.borrow_tracker.map(|bt| bt.instantiate_global_state(config));