2014-03-21 18:05:05 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2019-09-25 21:02:50 -04:00
|
|
|
use rustc_data_structures::sync::Lock;
|
2018-04-01 08:17:25 +02:00
|
|
|
|
2019-09-25 21:02:50 -04:00
|
|
|
use std::cell::Cell;
|
2015-01-20 15:45:07 -08:00
|
|
|
use std::fmt::Debug;
|
2016-08-23 13:23:58 -04:00
|
|
|
use std::time::{Duration, Instant};
|
2014-11-10 12:27:56 -08:00
|
|
|
|
2019-05-17 15:21:31 +10:00
|
|
|
use syntax::symbol::{Symbol, sym};
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::session::Session;
|
2017-07-19 15:04:30 -06:00
|
|
|
|
2019-08-01 03:35:26 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// The name of the associated type for `Fn` return types.
|
2019-05-17 15:21:31 +10:00
|
|
|
pub const FN_OUTPUT_NAME: Symbol = sym::Output;
|
2015-01-10 11:54:15 -05:00
|
|
|
|
2019-11-24 16:59:56 -05:00
|
|
|
pub use errors::ErrorReported;
|
2014-11-08 06:59:10 -05:00
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));
|
|
|
|
|
|
2018-08-29 08:21:01 -05:00
|
|
|
#[allow(nonstandard_style)]
|
2018-04-19 02:33:24 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
pub struct QueryMsg {
|
|
|
|
|
pub query: &'static str,
|
|
|
|
|
pub msg: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
/// 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())
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Sets the current depth of `time()` calls. The idea is to call
|
2016-12-27 21:35:34 -05:00
|
|
|
/// `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));
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 14:21:23 +01:00
|
|
|
pub fn time<T, F>(sess: &Session, what: &str, f: F) -> T where
|
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
|
{
|
2019-10-03 21:29:25 -04:00
|
|
|
time_ext(sess.time_passes(), what, f)
|
2017-12-03 14:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-10-03 21:29:25 -04:00
|
|
|
pub fn time_ext<T, F>(do_it: bool, what: &str, f: F) -> T where
|
2015-06-25 10:07:01 -07:00
|
|
|
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
|
|
|
|
2019-06-25 13:52:07 +10:00
|
|
|
print_time_passes_entry(true, what, dur);
|
2017-07-31 14:51:47 +02:00
|
|
|
|
|
|
|
|
TIME_DEPTH.with(|slot| slot.set(old));
|
|
|
|
|
|
|
|
|
|
rv
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn print_time_passes_entry(do_it: bool, what: &str, dur: Duration) {
|
|
|
|
|
if !do_it {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let indentation = TIME_DEPTH.with(|slot| slot.get());
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2018-08-23 10:14:52 +02:00
|
|
|
None => String::new(),
|
2015-06-27 18:37:33 -07:00
|
|
|
};
|
2016-08-23 13:23:58 -04:00
|
|
|
println!("{}time: {}{}\t{}",
|
2018-04-01 13:48:15 +09:00
|
|
|
" ".repeat(indentation),
|
2016-08-23 13:23:58 -04:00
|
|
|
duration_to_secs_str(dur),
|
|
|
|
|
mem_string,
|
|
|
|
|
what);
|
2013-03-21 18:50:02 +09:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2019-11-12 08:30:40 -05:00
|
|
|
pub use rustc_session::utils::duration_to_secs_str;
|
2016-08-23 13:23:58 -04:00
|
|
|
|
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 {
|
2018-07-27 11:11:18 +02:00
|
|
|
groups.push(group.to_string());
|
2017-03-13 02:12:13 +02:00
|
|
|
break;
|
2016-11-04 11:37:39 -04:00
|
|
|
} else {
|
|
|
|
|
groups.push(format!("{:03}", group));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groups.reverse();
|
|
|
|
|
|
|
|
|
|
groups.join("_")
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-01 08:17:25 +02:00
|
|
|
pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T where
|
2016-08-23 13:23:58 -04:00
|
|
|
F: FnOnce() -> T,
|
|
|
|
|
{
|
|
|
|
|
let start = Instant::now();
|
|
|
|
|
let rv = f();
|
|
|
|
|
let duration = start.elapsed();
|
2018-04-01 08:17:25 +02:00
|
|
|
let mut accu = accu.lock();
|
|
|
|
|
*accu = *accu + duration;
|
2016-08-23 13:23:58 -04:00
|
|
|
rv
|
|
|
|
|
}
|
|
|
|
|
|
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> {
|
2018-01-10 08:58:39 -08:00
|
|
|
use std::fs;
|
2015-07-08 08:29:41 -04:00
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
let field = 1;
|
2018-03-30 09:48:18 -07:00
|
|
|
let contents = fs::read("/proc/self/statm").ok()?;
|
|
|
|
|
let contents = String::from_utf8(contents).ok()?;
|
2017-12-08 17:32:04 -08:00
|
|
|
let s = contents.split_whitespace().nth(field)?;
|
|
|
|
|
let npages = s.parse::<usize>().ok()?;
|
2015-08-11 17:27:05 -07:00
|
|
|
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;
|
2017-03-13 02:12:13 +02:00
|
|
|
#[repr(C)]
|
|
|
|
|
#[allow(non_snake_case)]
|
2015-07-08 08:29:41 -04:00
|
|
|
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 {
|
2017-03-13 02:12:13 +02:00
|
|
|
_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
|
|
|
}
|