]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/util.rs
rollup merge of #19577: aidancully/master
[rust.git] / src / libstd / rt / util.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use libc::uintptr_t;
12 use option::Option;
13 use option::Option::{Some, None};
14 use os;
15 use str::{FromStr, from_str, Str};
16 use sync::atomic;
17
18 /// Dynamically inquire about whether we're running under V.
19 /// You should usually not use this unless your test definitely
20 /// can't run correctly un-altered. Valgrind is there to help
21 /// you notice weirdness in normal, un-doctored code paths!
22 pub fn running_on_valgrind() -> bool {
23     extern {
24         fn rust_running_on_valgrind() -> uintptr_t;
25     }
26     unsafe { rust_running_on_valgrind() != 0 }
27 }
28
29 /// Valgrind has a fixed-sized array (size around 2000) of segment descriptors
30 /// wired into it; this is a hard limit and requires rebuilding valgrind if you
31 /// want to go beyond it. Normally this is not a problem, but in some tests, we
32 /// produce a lot of threads casually.  Making lots of threads alone might not
33 /// be a problem _either_, except on OSX, the segments produced for new threads
34 /// _take a while_ to get reclaimed by the OS. Combined with the fact that libuv
35 /// schedulers fork off a separate thread for polling fsevents on OSX, we get a
36 /// perfect storm of creating "too many mappings" for valgrind to handle when
37 /// running certain stress tests in the runtime.
38 pub fn limit_thread_creation_due_to_osx_and_valgrind() -> bool {
39     (cfg!(target_os="macos")) && running_on_valgrind()
40 }
41
42 pub fn min_stack() -> uint {
43     static MIN: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
44     match MIN.load(atomic::SeqCst) {
45         0 => {}
46         n => return n - 1,
47     }
48     let amt = os::getenv("RUST_MIN_STACK").and_then(|s| from_str(s.as_slice()));
49     let amt = amt.unwrap_or(2 * 1024 * 1024);
50     // 0 is our sentinel value, so ensure that we'll never see 0 after
51     // initialization has run
52     MIN.store(amt + 1, atomic::SeqCst);
53     return amt;
54 }
55
56 /// Get's the number of scheduler threads requested by the environment
57 /// either `RUST_THREADS` or `num_cpus`.
58 pub fn default_sched_threads() -> uint {
59     match os::getenv("RUST_THREADS") {
60         Some(nstr) => {
61             let opt_n: Option<uint> = FromStr::from_str(nstr.as_slice());
62             match opt_n {
63                 Some(n) if n > 0 => n,
64                 _ => panic!("`RUST_THREADS` is `{}`, should be a positive integer", nstr)
65             }
66         }
67         None => {
68             if limit_thread_creation_due_to_osx_and_valgrind() {
69                 1
70             } else {
71                 os::num_cpus()
72             }
73         }
74     }
75 }