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