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.
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.
11 /*! Various utility functions useful for writing I/O tests */
18 use std::io::net::ip::*;
19 use sync::atomic::{AtomicUint, INIT_ATOMIC_UINT, Relaxed};
22 { fn $name:ident() $b:block $(#[$a:meta])* } => (
24 #![allow(unused_imports)]
40 use rt::running_on_valgrind;
46 $(#[$a])* #[test] fn green() { f() }
47 $(#[$a])* #[test] fn native() {
49 let (tx, rx) = channel();
50 native::task::spawn(proc() { tx.send(f()) });
57 /// Get a port number, starting at 9600, for use in tests
58 pub fn next_test_port() -> u16 {
59 static mut next_offset: AtomicUint = INIT_ATOMIC_UINT;
61 base_port() + next_offset.fetch_add(1, Relaxed) as u16
65 /// Get a temporary path which could be the location of a unix socket
66 pub fn next_test_unix() -> Path {
67 static mut COUNT: AtomicUint = INIT_ATOMIC_UINT;
68 // base port and pid are an attempt to be unique between multiple
69 // test-runners of different configurations running on one
70 // buildbot, the count is to be unique within this executable.
71 let string = format!("rust-test-unix-path-{}-{}-{}",
73 unsafe {libc::getpid()},
74 unsafe {COUNT.fetch_add(1, Relaxed)});
76 os::tmpdir().join(string)
78 Path::new(format!("{}{}", r"\\.\pipe\", string))
82 /// Get a unique IPv4 localhost:port pair starting at 9600
83 pub fn next_test_ip4() -> SocketAddr {
84 SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: next_test_port() }
87 /// Get a unique IPv6 localhost:port pair starting at 9600
88 pub fn next_test_ip6() -> SocketAddr {
89 SocketAddr { ip: Ipv6Addr(0, 0, 0, 0, 0, 0, 0, 1), port: next_test_port() }
93 XXX: Welcome to MegaHack City.
95 The bots run multiple builds at the same time, and these builds
96 all want to use ports. This function figures out which workspace
97 it is running in and assigns a port range based on it.
99 fn base_port() -> u16 {
105 ("32-opt", base + range * 1),
106 ("32-nopt", base + range * 2),
107 ("64-opt", base + range * 3),
108 ("64-nopt", base + range * 4),
109 ("64-opt-vg", base + range * 5),
110 ("all-opt", base + range * 6),
111 ("snap3", base + range * 7),
112 ("dist", base + range * 8)
115 // FIXME (#9639): This needs to handle non-utf8 paths
116 let path = os::getcwd();
117 let path_s = path.as_str().unwrap();
119 let mut final_base = base;
121 for &(dir, base) in bases.iter() {
122 if path_s.contains(dir) {
131 /// Raises the file descriptor limit when running tests if necessary
132 pub fn raise_fd_limit() {
133 unsafe { darwin_fd_limit::raise_fd_limit() }
136 #[cfg(target_os="macos")]
137 #[allow(non_camel_case_types)]
138 mod darwin_fd_limit {
140 * darwin_fd_limit exists to work around an issue where launchctl on Mac OS X defaults the
141 * rlimit maxfiles to 256/unlimited. The default soft limit of 256 ends up being far too low
142 * for our multithreaded scheduler testing, depending on the number of cores available.
144 * This fixes issue #7772.
148 type rlim_t = libc::uint64_t;
155 // name probably doesn't need to be mut, but the C function doesn't specify const
156 fn sysctl(name: *mut libc::c_int, namelen: libc::c_uint,
157 oldp: *mut libc::c_void, oldlenp: *mut libc::size_t,
158 newp: *mut libc::c_void, newlen: libc::size_t) -> libc::c_int;
159 fn getrlimit(resource: libc::c_int, rlp: *mut rlimit) -> libc::c_int;
160 fn setrlimit(resource: libc::c_int, rlp: *const rlimit) -> libc::c_int;
162 static CTL_KERN: libc::c_int = 1;
163 static KERN_MAXFILESPERPROC: libc::c_int = 29;
164 static RLIMIT_NOFILE: libc::c_int = 8;
166 pub unsafe fn raise_fd_limit() {
167 // The strategy here is to fetch the current resource limits, read the kern.maxfilesperproc
168 // sysctl value, and bump the soft resource limit for maxfiles up to the sysctl value.
170 use mem::size_of_val;
171 use os::last_os_error;
173 // Fetch the kern.maxfilesperproc value
174 let mut mib: [libc::c_int, ..2] = [CTL_KERN, KERN_MAXFILESPERPROC];
175 let mut maxfiles: libc::c_int = 0;
176 let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
177 if sysctl(&mut mib[0], 2, &mut maxfiles as *mut libc::c_int as *mut libc::c_void, &mut size,
178 mut_null(), 0) != 0 {
179 let err = last_os_error();
180 fail!("raise_fd_limit: error calling sysctl: {}", err);
183 // Fetch the current resource limits
184 let mut rlim = rlimit{rlim_cur: 0, rlim_max: 0};
185 if getrlimit(RLIMIT_NOFILE, &mut rlim) != 0 {
186 let err = last_os_error();
187 fail!("raise_fd_limit: error calling getrlimit: {}", err);
190 // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard limit
191 rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max);
193 // Set our newly-increased resource limit
194 if setrlimit(RLIMIT_NOFILE, &rlim) != 0 {
195 let err = last_os_error();
196 fail!("raise_fd_limit: error calling setrlimit: {}", err);
201 #[cfg(not(target_os="macos"))]
202 mod darwin_fd_limit {
203 pub unsafe fn raise_fd_limit() {}