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 #[cfg(target_os="macos")]
99 #[allow(non_camel_case_types)]
100 mod darwin_fd_limit {
102 * darwin_fd_limit exists to work around an issue where launchctl on Mac OS X defaults the
103 * rlimit maxfiles to 256/unlimited. The default soft limit of 256 ends up being far too low
104 * for our multithreaded scheduler testing, depending on the number of cores available.
106 * This fixes issue #7772.
110 type rlim_t = libc::uint64_t;
117 // name probably doesn't need to be mut, but the C function doesn't specify const
118 fn sysctl(name: *mut libc::c_int, namelen: libc::c_uint,
119 oldp: *mut libc::c_void, oldlenp: *mut libc::size_t,
120 newp: *mut libc::c_void, newlen: libc::size_t) -> libc::c_int;
121 fn getrlimit(resource: libc::c_int, rlp: *mut rlimit) -> libc::c_int;
122 fn setrlimit(resource: libc::c_int, rlp: *const rlimit) -> libc::c_int;
124 static CTL_KERN: libc::c_int = 1;
125 static KERN_MAXFILESPERPROC: libc::c_int = 29;
126 static RLIMIT_NOFILE: libc::c_int = 8;
128 pub unsafe fn raise_fd_limit() {
129 // The strategy here is to fetch the current resource limits, read the kern.maxfilesperproc
130 // sysctl value, and bump the soft resource limit for maxfiles up to the sysctl value.
132 use mem::size_of_val;
133 use os::last_os_error;
135 // Fetch the kern.maxfilesperproc value
136 let mut mib: [libc::c_int, ..2] = [CTL_KERN, KERN_MAXFILESPERPROC];
137 let mut maxfiles: libc::c_int = 0;
138 let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
139 if sysctl(&mut mib[0], 2, &mut maxfiles as *mut libc::c_int as *mut libc::c_void, &mut size,
140 null_mut(), 0) != 0 {
141 let err = last_os_error();
142 panic!("raise_fd_limit: error calling sysctl: {}", err);
145 // Fetch the current resource limits
146 let mut rlim = rlimit{rlim_cur: 0, rlim_max: 0};
147 if getrlimit(RLIMIT_NOFILE, &mut rlim) != 0 {
148 let err = last_os_error();
149 panic!("raise_fd_limit: error calling getrlimit: {}", err);
152 // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard limit
153 rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max);
155 // Set our newly-increased resource limit
156 if setrlimit(RLIMIT_NOFILE, &rlim) != 0 {
157 let err = last_os_error();
158 panic!("raise_fd_limit: error calling setrlimit: {}", err);
163 #[cfg(not(target_os="macos"))]
164 mod darwin_fd_limit {
165 pub unsafe fn raise_fd_limit() {}