2014-10-14 20:41:50 +02:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-03-21 18:05:05 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2014-11-14 14:20:57 -08:00
|
|
|
use std::cell::{RefCell, Cell};
|
2014-08-27 21:46:52 -04:00
|
|
|
use std::collections::HashMap;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::ffi::CString;
|
2015-01-20 15:45:07 -08:00
|
|
|
use std::fmt::Debug;
|
2016-01-21 10:28:39 -08:00
|
|
|
use std::hash::{Hash, BuildHasher};
|
2014-12-10 19:46:38 -08:00
|
|
|
use std::iter::repeat;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::path::Path;
|
2016-08-23 13:23:58 -04:00
|
|
|
use std::time::{Duration, Instant};
|
2014-11-10 12:27:56 -08:00
|
|
|
|
2015-01-10 11:54:15 -05:00
|
|
|
// The name of the associated type for `Fn` return types
|
|
|
|
pub const FN_OUTPUT_NAME: &'static str = "Output";
|
|
|
|
|
2014-11-07 16:26:26 -05:00
|
|
|
// Useful type to use with `Result<>` indicate that an error has already
|
|
|
|
// been reported to the user, so no need to continue checking.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2014-11-08 06:59:10 -05:00
|
|
|
pub struct ErrorReported;
|
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));
|
|
|
|
|
|
|
|
/// Read the current depth of `time()` calls. This is used to
|
|
|
|
/// encourage indentation across threads.
|
|
|
|
pub fn time_depth() -> usize {
|
|
|
|
TIME_DEPTH.with(|slot| slot.get())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the current depth of `time()` calls. The idea is to call
|
|
|
|
/// `set_time_depth()` with the result from `time_depth()` in the
|
|
|
|
/// parent thread.
|
|
|
|
pub fn set_time_depth(depth: usize) {
|
|
|
|
TIME_DEPTH.with(|slot| slot.set(depth));
|
|
|
|
}
|
|
|
|
|
2015-06-25 10:07:01 -07:00
|
|
|
pub fn time<T, F>(do_it: bool, what: &str, f: F) -> T where
|
|
|
|
F: FnOnce() -> T,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-06-25 10:07:01 -07:00
|
|
|
if !do_it { return f(); }
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
let old = TIME_DEPTH.with(|slot| {
|
2014-11-14 14:20:57 -08:00
|
|
|
let r = slot.get();
|
|
|
|
slot.set(r + 1);
|
|
|
|
r
|
|
|
|
});
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2015-12-02 17:31:49 -08:00
|
|
|
let start = Instant::now();
|
|
|
|
let rv = f();
|
|
|
|
let dur = start.elapsed();
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2015-06-27 18:37:33 -07:00
|
|
|
let mem_string = match get_resident() {
|
|
|
|
Some(n) => {
|
|
|
|
let mb = n as f64 / 1_000_000.0;
|
|
|
|
format!("; rss: {}MB", mb.round() as usize)
|
|
|
|
}
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
2016-08-23 13:23:58 -04:00
|
|
|
println!("{}time: {}{}\t{}",
|
|
|
|
repeat(" ").take(old).collect::<String>(),
|
|
|
|
duration_to_secs_str(dur),
|
|
|
|
mem_string,
|
|
|
|
what);
|
2015-05-15 10:55:23 +12:00
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
TIME_DEPTH.with(|slot| slot.set(old));
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2013-03-21 18:50:02 +09:00
|
|
|
rv
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2016-08-23 13:23:58 -04:00
|
|
|
// Hack up our own formatting for the duration to make it easier for scripts
|
|
|
|
// to parse (always use the same number of decimal places and the same unit).
|
|
|
|
pub fn duration_to_secs_str(dur: Duration) -> String {
|
|
|
|
const NANOS_PER_SEC: f64 = 1_000_000_000.0;
|
|
|
|
let secs = dur.as_secs() as f64 +
|
|
|
|
dur.subsec_nanos() as f64 / NANOS_PER_SEC;
|
|
|
|
|
|
|
|
format!("{:.3}", secs)
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:37:39 -04:00
|
|
|
pub fn to_readable_str(mut val: usize) -> String {
|
|
|
|
let mut groups = vec![];
|
|
|
|
loop {
|
|
|
|
let group = val % 1000;
|
|
|
|
|
|
|
|
val /= 1000;
|
|
|
|
|
|
|
|
if val == 0 {
|
|
|
|
groups.push(format!("{}", group));
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
groups.push(format!("{:03}", group));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groups.reverse();
|
|
|
|
|
|
|
|
groups.join("_")
|
|
|
|
}
|
|
|
|
|
2016-08-23 13:23:58 -04:00
|
|
|
pub fn record_time<T, F>(accu: &Cell<Duration>, f: F) -> T where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
{
|
|
|
|
let start = Instant::now();
|
|
|
|
let rv = f();
|
|
|
|
let duration = start.elapsed();
|
|
|
|
accu.set(duration + accu.get());
|
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
// Like std::macros::try!, but for Option<>.
|
|
|
|
macro_rules! option_try(
|
|
|
|
($e:expr) => (match $e { Some(e) => e, None => return None })
|
|
|
|
);
|
|
|
|
|
2015-06-27 18:37:33 -07:00
|
|
|
// Memory reporting
|
2015-07-08 08:29:41 -04:00
|
|
|
#[cfg(unix)]
|
2015-06-27 18:37:33 -07:00
|
|
|
fn get_resident() -> Option<usize> {
|
2015-08-11 17:27:05 -07:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2015-07-08 08:29:41 -04:00
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
let field = 1;
|
|
|
|
let mut f = option_try!(File::open("/proc/self/statm").ok());
|
|
|
|
let mut contents = String::new();
|
|
|
|
option_try!(f.read_to_string(&mut contents).ok());
|
|
|
|
let s = option_try!(contents.split_whitespace().nth(field));
|
|
|
|
let npages = option_try!(s.parse::<usize>().ok());
|
|
|
|
Some(npages * 4096)
|
2015-06-27 18:37:33 -07:00
|
|
|
}
|
|
|
|
|
2015-07-08 08:29:41 -04:00
|
|
|
#[cfg(windows)]
|
2015-08-11 17:27:05 -07:00
|
|
|
fn get_resident() -> Option<usize> {
|
2015-11-02 16:23:22 -08:00
|
|
|
type BOOL = i32;
|
|
|
|
type DWORD = u32;
|
|
|
|
type HANDLE = *mut u8;
|
|
|
|
use libc::size_t;
|
2015-07-08 08:29:41 -04:00
|
|
|
use std::mem;
|
|
|
|
#[repr(C)] #[allow(non_snake_case)]
|
|
|
|
struct PROCESS_MEMORY_COUNTERS {
|
|
|
|
cb: DWORD,
|
|
|
|
PageFaultCount: DWORD,
|
2015-11-02 16:23:22 -08:00
|
|
|
PeakWorkingSetSize: size_t,
|
|
|
|
WorkingSetSize: size_t,
|
|
|
|
QuotaPeakPagedPoolUsage: size_t,
|
|
|
|
QuotaPagedPoolUsage: size_t,
|
|
|
|
QuotaPeakNonPagedPoolUsage: size_t,
|
|
|
|
QuotaNonPagedPoolUsage: size_t,
|
|
|
|
PagefileUsage: size_t,
|
|
|
|
PeakPagefileUsage: size_t,
|
2015-07-08 08:29:41 -04:00
|
|
|
}
|
|
|
|
type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
|
|
|
|
#[link(name = "psapi")]
|
|
|
|
extern "system" {
|
2015-11-02 16:23:22 -08:00
|
|
|
fn GetCurrentProcess() -> HANDLE;
|
2015-07-08 08:29:41 -04:00
|
|
|
fn GetProcessMemoryInfo(Process: HANDLE,
|
|
|
|
ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
|
|
|
|
cb: DWORD) -> BOOL;
|
|
|
|
}
|
|
|
|
let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { mem::zeroed() };
|
|
|
|
pmc.cb = mem::size_of_val(&pmc) as DWORD;
|
|
|
|
match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
|
|
|
|
0 => None,
|
|
|
|
_ => Some(pmc.WorkingSetSize as usize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn indent<R, F>(op: F) -> R where
|
2015-01-20 15:45:07 -08:00
|
|
|
R: Debug,
|
2014-12-08 20:26:43 -05:00
|
|
|
F: FnOnce() -> R,
|
|
|
|
{
|
2012-04-05 20:59:07 -07:00
|
|
|
// Use in conjunction with the log post-processor like `src/etc/indenter`
|
|
|
|
// to make debug output more readable.
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2012-10-23 11:28:20 -07:00
|
|
|
let r = op();
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("<< (Result = {:?})", r);
|
2013-02-15 04:14:34 -05:00
|
|
|
r
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub struct Indenter {
|
|
|
|
_cannot_construct_outside_of_this_module: ()
|
2013-02-27 19:13:53 -05:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
impl Drop for Indenter {
|
2013-10-21 13:08:31 -07:00
|
|
|
fn drop(&mut self) { debug!("<<"); }
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub fn indenter() -> Indenter {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2014-06-06 15:51:42 +02:00
|
|
|
Indenter { _cannot_construct_outside_of_this_module: () }
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
pub trait MemoizationMap {
|
|
|
|
type Key: Clone;
|
|
|
|
type Value: Clone;
|
|
|
|
|
|
|
|
/// If `key` is present in the map, return the valuee,
|
|
|
|
/// otherwise invoke `op` and store the value in the map.
|
|
|
|
///
|
|
|
|
/// NB: if the receiver is a `DepTrackingMap`, special care is
|
|
|
|
/// needed in the `op` to ensure that the correct edges are
|
|
|
|
/// added into the dep graph. See the `DepTrackingMap` impl for
|
|
|
|
/// more details!
|
|
|
|
fn memoize<OP>(&self, key: Self::Key, op: OP) -> Self::Value
|
|
|
|
where OP: FnOnce() -> Self::Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V, S> MemoizationMap for RefCell<HashMap<K,V,S>>
|
2016-01-21 10:28:39 -08:00
|
|
|
where K: Hash+Eq+Clone, V: Clone, S: BuildHasher
|
2015-02-17 20:48:07 -08:00
|
|
|
{
|
2015-12-22 16:39:33 -05:00
|
|
|
type Key = K;
|
|
|
|
type Value = V;
|
|
|
|
|
|
|
|
fn memoize<OP>(&self, key: K, op: OP) -> V
|
|
|
|
where OP: FnOnce() -> V
|
|
|
|
{
|
|
|
|
let result = self.borrow().get(&key).cloned();
|
|
|
|
match result {
|
|
|
|
Some(result) => result,
|
|
|
|
None => {
|
|
|
|
let result = op();
|
|
|
|
self.borrow_mut().insert(key, result.clone());
|
|
|
|
result
|
|
|
|
}
|
2015-02-17 20:48:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 21:00:43 -08:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn path2cstr(p: &Path) -> CString {
|
|
|
|
use std::os::unix::prelude::*;
|
2015-03-30 11:00:05 -07:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
let p: &OsStr = p.as_ref();
|
|
|
|
CString::new(p.as_bytes()).unwrap()
|
2015-02-26 21:00:43 -08:00
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn path2cstr(p: &Path) -> CString {
|
|
|
|
CString::new(p.to_str().unwrap()).unwrap()
|
|
|
|
}
|
2016-11-04 11:37:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_readable_str() {
|
|
|
|
assert_eq!("0", to_readable_str(0));
|
|
|
|
assert_eq!("1", to_readable_str(1));
|
|
|
|
assert_eq!("99", to_readable_str(99));
|
|
|
|
assert_eq!("999", to_readable_str(999));
|
|
|
|
assert_eq!("1_000", to_readable_str(1_000));
|
|
|
|
assert_eq!("1_001", to_readable_str(1_001));
|
|
|
|
assert_eq!("999_999", to_readable_str(999_999));
|
|
|
|
assert_eq!("1_000_000", to_readable_str(1_000_000));
|
|
|
|
assert_eq!("1_234_567", to_readable_str(1_234_567));
|
|
|
|
}
|