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};
21 /// Get a port number, starting at 9600, for use in tests
22 pub fn next_test_port() -> u16 {
23 static NEXT_OFFSET: AtomicUint = INIT_ATOMIC_UINT;
24 base_port() + NEXT_OFFSET.fetch_add(1, Relaxed) as u16
27 /// Get a temporary path which could be the location of a unix socket
28 pub fn next_test_unix() -> Path {
29 static COUNT: AtomicUint = INIT_ATOMIC_UINT;
30 // base port and pid are an attempt to be unique between multiple
31 // test-runners of different configurations running on one
32 // buildbot, the count is to be unique within this executable.
33 let string = format!("rust-test-unix-path-{}-{}-{}",
35 unsafe {libc::getpid()},
36 COUNT.fetch_add(1, Relaxed));
38 os::tmpdir().join(string)
40 Path::new(format!("{}{}", r"\\.\pipe\", string))
44 /// Get a unique IPv4 localhost:port pair starting at 9600
45 pub fn next_test_ip4() -> SocketAddr {
46 SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: next_test_port() }
49 /// Get a unique IPv6 localhost:port pair starting at 9600
50 pub fn next_test_ip6() -> SocketAddr {
51 SocketAddr { ip: Ipv6Addr(0, 0, 0, 0, 0, 0, 0, 1), port: next_test_port() }
55 XXX: Welcome to MegaHack City.
57 The bots run multiple builds at the same time, and these builds
58 all want to use ports. This function figures out which workspace
59 it is running in and assigns a port range based on it.
61 fn base_port() -> u16 {
67 ("32-opt", base + range * 1),
68 ("32-nopt", base + range * 2),
69 ("64-opt", base + range * 3),
70 ("64-nopt", base + range * 4),
71 ("64-opt-vg", base + range * 5),
72 ("all-opt", base + range * 6),
73 ("snap3", base + range * 7),
74 ("dist", base + range * 8)
77 // FIXME (#9639): This needs to handle non-utf8 paths
78 let path = os::getcwd().unwrap();
79 let path_s = path.as_str().unwrap();
81 let mut final_base = base;
83 for &(dir, base) in bases.iter() {
84 if path_s.contains(dir) {
93 /// Raises the file descriptor limit when running tests if necessary
94 pub fn raise_fd_limit() {
95 unsafe { darwin_fd_limit::raise_fd_limit() }
98 /// darwin_fd_limit exists to work around an issue where launchctl on Mac OS X defaults the rlimit
99 /// maxfiles to 256/unlimited. The default soft limit of 256 ends up being far too low for our
100 /// multithreaded scheduler testing, depending on the number of cores available.
102 /// This fixes issue #7772.
103 #[cfg(target_os="macos")]
104 #[allow(non_camel_case_types)]
105 mod darwin_fd_limit {
107 type rlim_t = libc::uint64_t;
114 // name probably doesn't need to be mut, but the C function doesn't specify const
115 fn sysctl(name: *mut libc::c_int, namelen: libc::c_uint,
116 oldp: *mut libc::c_void, oldlenp: *mut libc::size_t,
117 newp: *mut libc::c_void, newlen: libc::size_t) -> libc::c_int;
118 fn getrlimit(resource: libc::c_int, rlp: *mut rlimit) -> libc::c_int;
119 fn setrlimit(resource: libc::c_int, rlp: *const rlimit) -> libc::c_int;
121 static CTL_KERN: libc::c_int = 1;
122 static KERN_MAXFILESPERPROC: libc::c_int = 29;
123 static RLIMIT_NOFILE: libc::c_int = 8;
125 pub unsafe fn raise_fd_limit() {
126 // The strategy here is to fetch the current resource limits, read the kern.maxfilesperproc
127 // sysctl value, and bump the soft resource limit for maxfiles up to the sysctl value.
129 use mem::size_of_val;
130 use os::last_os_error;
132 // Fetch the kern.maxfilesperproc value
133 let mut mib: [libc::c_int, ..2] = [CTL_KERN, KERN_MAXFILESPERPROC];
134 let mut maxfiles: libc::c_int = 0;
135 let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
136 if sysctl(&mut mib[0], 2, &mut maxfiles as *mut libc::c_int as *mut libc::c_void, &mut size,
137 null_mut(), 0) != 0 {
138 let err = last_os_error();
139 panic!("raise_fd_limit: error calling sysctl: {}", err);
142 // Fetch the current resource limits
143 let mut rlim = rlimit{rlim_cur: 0, rlim_max: 0};
144 if getrlimit(RLIMIT_NOFILE, &mut rlim) != 0 {
145 let err = last_os_error();
146 panic!("raise_fd_limit: error calling getrlimit: {}", err);
149 // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard limit
150 rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max);
152 // Set our newly-increased resource limit
153 if setrlimit(RLIMIT_NOFILE, &rlim) != 0 {
154 let err = last_os_error();
155 panic!("raise_fd_limit: error calling setrlimit: {}", err);
160 #[cfg(not(target_os="macos"))]
161 mod darwin_fd_limit {
162 pub unsafe fn raise_fd_limit() {}