Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 14fc9b2

Browse files
committed
add flag to specify the number of cpus
1 parent 6671f83 commit 14fc9b2

File tree

7 files changed

+28
-6
lines changed

7 files changed

+28
-6
lines changed

src/tools/miri/src/bin/miri.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,13 @@ fn main() {
550550
} else {
551551
show_error!("-Zmiri-extern-so-file `{}` does not exist", filename);
552552
}
553+
} else if let Some(param) = arg.strip_prefix("-Zmiri-num-cpus=") {
554+
let num_cpus = match param.parse::<u32>() {
555+
Ok(i) => i,
556+
Err(err) => show_error!("-Zmiri-num-cpus requires a `u32`: {}", err),
557+
};
558+
559+
miri_config.num_cpus = num_cpus;
553560
} else {
554561
// Forward to rustc.
555562
rustc_args.push(arg);

src/tools/miri/src/eval.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ pub struct MiriConfig {
132132
pub external_so_file: Option<PathBuf>,
133133
/// Run a garbage collector for SbTags every N basic blocks.
134134
pub gc_interval: u32,
135+
/// FIXME: add docs.
136+
pub num_cpus: u32,
135137
}
136138

137139
impl Default for MiriConfig {
@@ -164,6 +166,7 @@ impl Default for MiriConfig {
164166
retag_fields: false,
165167
external_so_file: None,
166168
gc_interval: 10_000,
169+
num_cpus: 1,
167170
}
168171
}
169172
}

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub use crate::helpers::{CurrentSpan, EvalContextExt as HelpersEvalContextExt};
104104
pub use crate::intptrcast::ProvenanceMode;
105105
pub use crate::machine::{
106106
AllocExtra, FrameData, MiriInterpCx, MiriInterpCxExt, MiriMachine, MiriMemoryKind, Provenance,
107-
ProvenanceExtra, NUM_CPUS, PAGE_SIZE, STACK_ADDR, STACK_SIZE,
107+
ProvenanceExtra, PAGE_SIZE, STACK_ADDR, STACK_SIZE,
108108
};
109109
pub use crate::mono_hash_map::MonoHashMap;
110110
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;

src/tools/miri/src/machine.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use crate::{
3535
pub const PAGE_SIZE: u64 = 4 * 1024; // FIXME: adjust to target architecture
3636
pub const STACK_ADDR: u64 = 32 * PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
3737
pub const STACK_SIZE: u64 = 16 * PAGE_SIZE; // whatever
38-
pub const NUM_CPUS: u64 = 1;
3938

4039
/// Extra data stored with each stack frame
4140
pub struct FrameData<'tcx> {
@@ -407,6 +406,8 @@ pub struct MiriMachine<'mir, 'tcx> {
407406
pub(crate) gc_interval: u32,
408407
/// The number of blocks that passed since the last SbTag GC pass.
409408
pub(crate) since_gc: u32,
409+
/// FIXME: docs,
410+
pub(crate) num_cpus: u32,
410411
}
411412

412413
impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
@@ -486,6 +487,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
486487
}),
487488
gc_interval: config.gc_interval,
488489
since_gc: 0,
490+
num_cpus: config.num_cpus,
489491
}
490492
}
491493

src/tools/miri/src/shims/unix/foreign_items.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
225225
"sysconf" => {
226226
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
227227
let name = this.read_scalar(name)?.to_i32()?;
228-
229228
// FIXME: Which of these are POSIX, and which are GNU/Linux?
230229
// At least the names seem to all also exist on macOS.
231230
let sysconfs: &[(&str, fn(&MiriInterpCx<'_, '_>) -> Scalar<Provenance>)] = &[
232231
("_SC_PAGESIZE", |this| Scalar::from_int(PAGE_SIZE, this.pointer_size())),
233-
("_SC_NPROCESSORS_CONF", |this| Scalar::from_int(NUM_CPUS, this.pointer_size())),
234-
("_SC_NPROCESSORS_ONLN", |this| Scalar::from_int(NUM_CPUS, this.pointer_size())),
232+
("_SC_NPROCESSORS_CONF", |this| Scalar::from_int(this.machine.num_cpus, this.pointer_size())),
233+
("_SC_NPROCESSORS_ONLN", |this| Scalar::from_int(this.machine.num_cpus, this.pointer_size())),
235234
// 512 seems to be a reasonable default. The value is not critical, in
236235
// the sense that getpwuid_r takes and checks the buffer length.
237236
("_SC_GETPW_R_SIZE_MAX", |this| Scalar::from_int(512, this.pointer_size()))

src/tools/miri/src/shims/windows/foreign_items.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
163163
)?;
164164
// Set number of processors.
165165
let num_cpus = system_info.offset(field_offsets[6], dword_layout, &this.tcx)?;
166-
this.write_scalar(Scalar::from_int(NUM_CPUS, dword_layout.size), &num_cpus.into())?;
166+
this.write_scalar(
167+
Scalar::from_int(this.machine.num_cpus, dword_layout.size),
168+
&num_cpus.into(),
169+
)?;
167170
}
168171

169172
// Thread-local storage
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@compile-flags: -Zmiri-num-cpus=1024
2+
3+
use std::num::NonZeroUsize;
4+
use std::thread::available_parallelism;
5+
6+
fn main() {
7+
assert_eq!(available_parallelism().unwrap(), NonZeroUsize::new(1024).unwrap());
8+
}

0 commit comments

Comments
 (0)