Skip to content

Commit dfa7e21

Browse files
committed
Auto merge of #42433 - marco-c:profiling, r=alexcrichton
Build instruction profiler runtime as part of compiler-rt r? @alexcrichton This is #38608 with some fixes. Still missing: - [x] testing with profiler enabled on some builders (on which ones? Should I add the option to some of the already existing configurations, or create a new configuration?); - [x] enabling distribution (on which builders?); - [x] documentation.
2 parents e40ef96 + 5c084fd commit dfa7e21

File tree

38 files changed

+319
-17
lines changed

38 files changed

+319
-17
lines changed

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ matrix:
5454
# version that we're using, 8.2, cannot compile LLVM for OSX 10.7.
5555
- env: >
5656
RUST_CHECK_TARGET=check
57-
RUST_CONFIGURE_ARGS="--build=x86_64-apple-darwin --enable-sanitizers"
57+
RUST_CONFIGURE_ARGS="--build=x86_64-apple-darwin --enable-sanitizers --enable-profiler"
5858
SRC=.
5959
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
6060
SCCACHE_ERROR_LOG=/tmp/sccache.log
@@ -87,7 +87,7 @@ matrix:
8787
# OSX 10.7 and `xcode7` is the latest Xcode able to compile LLVM for 10.7.
8888
- env: >
8989
RUST_CHECK_TARGET=dist
90-
RUST_CONFIGURE_ARGS="--build=i686-apple-darwin --enable-extended"
90+
RUST_CONFIGURE_ARGS="--build=i686-apple-darwin --enable-extended --enable-profiler"
9191
SRC=.
9292
DEPLOY=1
9393
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
@@ -101,7 +101,7 @@ matrix:
101101
- *osx_install_sccache
102102
- env: >
103103
RUST_CHECK_TARGET=dist
104-
RUST_CONFIGURE_ARGS="--target=aarch64-apple-ios,armv7-apple-ios,armv7s-apple-ios,i386-apple-ios,x86_64-apple-ios --enable-extended --enable-sanitizers"
104+
RUST_CONFIGURE_ARGS="--target=aarch64-apple-ios,armv7-apple-ios,armv7s-apple-ios,i386-apple-ios,x86_64-apple-ios --enable-extended --enable-sanitizers --enable-profiler"
105105
SRC=.
106106
DEPLOY=1
107107
RUSTC_RETRY_LINKER_ON_SEGFAULT=1

appveyor.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ environment:
77
matrix:
88
# 32/64 bit MSVC tests
99
- MSYS_BITS: 64
10-
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-msvc
10+
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-msvc --enable-profiler
1111
SCRIPT: python x.py test
1212
- MSYS_BITS: 32
1313
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-msvc --target=i586-pc-windows-msvc
@@ -48,12 +48,14 @@ environment:
4848
- RUST_CONFIGURE_ARGS: >
4949
--build=x86_64-pc-windows-msvc
5050
--enable-extended
51+
--enable-profiler
5152
SCRIPT: python x.py dist
5253
DEPLOY: 1
5354
- RUST_CONFIGURE_ARGS: >
5455
--build=i686-pc-windows-msvc
5556
--target=i586-pc-windows-msvc
5657
--enable-extended
58+
--enable-profiler
5759
SCRIPT: python x.py dist
5860
DEPLOY: 1
5961
- MSYS_BITS: 32

configure

+1
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ opt vendor 0 "enable usage of vendored Rust crates"
452452
opt sanitizers 0 "build the sanitizer runtimes (asan, lsan, msan, tsan)"
453453
opt dist-src 1 "when building tarballs enables building a source tarball"
454454
opt cargo-openssl-static 0 "static openssl in cargo"
455+
opt profiler 0 "build the profiler runtime"
455456

456457
# Optimization and debugging options. These may be overridden by the release channel, etc.
457458
opt_nosave optimize 1 "build optimized rust code"

src/Cargo.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bootstrap/check.rs

+4
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ pub fn compiletest(build: &Build,
299299
cmd.env("SANITIZER_SUPPORT", "1");
300300
}
301301

302+
if build.config.profiler {
303+
cmd.env("PROFILER_SUPPORT", "1");
304+
}
305+
302306
cmd.arg("--adb-path").arg("adb");
303307
cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
304308
if target.contains("android") {

src/bootstrap/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub struct Config {
5050
pub full_bootstrap: bool,
5151
pub extended: bool,
5252
pub sanitizers: bool,
53+
pub profiler: bool,
5354

5455
// llvm codegen options
5556
pub llvm_assertions: bool,
@@ -162,6 +163,7 @@ struct Build {
162163
extended: Option<bool>,
163164
verbose: Option<usize>,
164165
sanitizers: Option<bool>,
166+
profiler: Option<bool>,
165167
openssl_static: Option<bool>,
166168
}
167169

@@ -318,6 +320,7 @@ impl Config {
318320
set(&mut config.extended, build.extended);
319321
set(&mut config.verbose, build.verbose);
320322
set(&mut config.sanitizers, build.sanitizers);
323+
set(&mut config.profiler, build.profiler);
321324
set(&mut config.openssl_static, build.openssl_static);
322325

323326
if let Some(ref install) = toml.install {
@@ -471,6 +474,7 @@ impl Config {
471474
("FULL_BOOTSTRAP", self.full_bootstrap),
472475
("EXTENDED", self.extended),
473476
("SANITIZERS", self.sanitizers),
477+
("PROFILER", self.profiler),
474478
("DIST_SRC", self.rust_dist_src),
475479
("CARGO_OPENSSL_STATIC", self.openssl_static),
476480
}

src/bootstrap/config.toml.example

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
# Build the sanitizer runtimes
148148
#sanitizers = false
149149

150+
# Build the profiler runtime
151+
#profiler = false
152+
150153
# Indicates whether the OpenSSL linked into Cargo will be statically linked or
151154
# not. If static linkage is specified then the build system will download a
152155
# known-good version of OpenSSL, compile it, and link it to Cargo.

src/bootstrap/dist.rs

+1
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ pub fn rust_src(build: &Build) {
570570
"src/libgetopts",
571571
"src/compiler-rt",
572572
"src/jemalloc",
573+
"src/libprofiler_builtins",
573574
];
574575
let std_src_dirs_exclude = [
575576
"src/compiler-rt/test",

src/bootstrap/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ impl Build {
594594
if self.config.backtrace {
595595
features.push_str(" backtrace");
596596
}
597+
if self.config.profiler {
598+
features.push_str(" profiler");
599+
}
597600
return features
598601
}
599602

src/ci/docker/dist-i686-linux/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ ENV HOSTS=i686-unknown-linux-gnu
9090
ENV RUST_CONFIGURE_ARGS \
9191
--host=$HOSTS \
9292
--enable-extended \
93-
--enable-sanitizers
93+
--enable-sanitizers \
94+
--enable-profiler
9495
ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS
9596

9697
# This is the only builder which will create source tarballs

src/ci/docker/dist-x86_64-linux/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ ENV HOSTS=x86_64-unknown-linux-gnu
9090
ENV RUST_CONFIGURE_ARGS \
9191
--host=$HOSTS \
9292
--enable-extended \
93-
--enable-sanitizers
93+
--enable-sanitizers \
94+
--enable-profiler
9495
ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS
9596

9697
# This is the only builder which will create source tarballs

src/ci/docker/x86_64-gnu/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ RUN curl -OL https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-ini
2222
rm dumb-init_*.deb
2323
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
2424

25-
ENV RUST_CONFIGURE_ARGS --build=x86_64-unknown-linux-gnu --enable-sanitizers
25+
ENV RUST_CONFIGURE_ARGS --build=x86_64-unknown-linux-gnu --enable-sanitizers --enable-profiler
2626
ENV SCRIPT python2.7 ../x.py test

src/doc/unstable-book/src/SUMMARY.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [Compiler flags](compiler-flags.md)
44
- [linker_flavor](compiler-flags/linker-flavor.md)
5+
- [profile](compiler-flags/profile.md)
56
- [remap_path_prefix](compiler-flags/remap-path-prefix.md)
67
- [Language features](language-features.md)
78
- [abi_msp430_interrupt](language-features/abi-msp430-interrupt.md)
@@ -70,6 +71,7 @@
7071
- [plugin_registrar](language-features/plugin-registrar.md)
7172
- [prelude_import](language-features/prelude-import.md)
7273
- [proc_macro](language-features/proc-macro.md)
74+
- [profiler_runtime](language-features/profiler-runtime.md)
7375
- [quote](language-features/quote.md)
7476
- [repr_align](language-features/repr-align.md)
7577
- [repr_simd](language-features/repr-simd.md)
@@ -178,6 +180,7 @@
178180
- [placement_new_protocol](library-features/placement-new-protocol.md)
179181
- [print_internals](library-features/print-internals.md)
180182
- [proc_macro_internals](library-features/proc-macro-internals.md)
183+
- [profiler_runtime_lib](library-features/sanitizer-runtime-lib.md)
181184
- [question_mark_carrier](library-features/question-mark-carrier.md)
182185
- [rand](library-features/rand.md)
183186
- [range_contains](library-features/range-contains.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# `profile`
2+
3+
The tracking issue for this feature is: [#42524](https://github.com/rust-lang/rust/issues/42524).
4+
5+
------------------------
6+
7+
This feature allows the generation of code coverage reports.
8+
9+
Set the `-Zprofile` compiler flag in order to enable gcov profiling.
10+
11+
For example:
12+
```Bash
13+
cargo new testgcov --bin
14+
cd testgcov
15+
export RUSTFLAGS="-Zprofile"
16+
cargo build
17+
cargo run
18+
```
19+
20+
Once you've built and run your program, files with the `gcno` (after build) and `gcda` (after execution) extensions will be created.
21+
You can parse them with [llvm-cov gcov](http://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-gcov) or [grcov](https://github.com/marco-c/grcov).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `profiler_runtime`
2+
3+
The tracking issue for this feature is: [#42524](https://github.com/rust-lang/rust/issues/42524).
4+
5+
------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `profiler_runtime_lib`
2+
3+
This feature is internal to the Rust compiler and is not intended for general use.
4+
5+
------------------------

src/libprofiler_builtins/Cargo.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
build = "build.rs"
4+
name = "profiler_builtins"
5+
version = "0.0.0"
6+
7+
[lib]
8+
name = "profiler_builtins"
9+
path = "lib.rs"
10+
test = false
11+
bench = false
12+
doc = false
13+
14+
[dependencies]
15+
core = { path = "../libcore" }
16+
17+
[build-dependencies]
18+
gcc = "0.3.50"

src/libprofiler_builtins/build.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2017 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.
4+
//
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.
10+
11+
//! Compiles the profiler part of the `compiler-rt` library.
12+
//!
13+
//! See the build.rs for libcompiler_builtins crate for details.
14+
15+
extern crate gcc;
16+
17+
use std::env;
18+
use std::path::Path;
19+
20+
fn main() {
21+
let target = env::var("TARGET").expect("TARGET was not set");
22+
let cfg = &mut gcc::Config::new();
23+
24+
let mut profile_sources = vec!["GCDAProfiling.c",
25+
"InstrProfiling.c",
26+
"InstrProfilingBuffer.c",
27+
"InstrProfilingFile.c",
28+
"InstrProfilingMerge.c",
29+
"InstrProfilingMergeFile.c",
30+
"InstrProfilingPlatformDarwin.c",
31+
"InstrProfilingPlatformLinux.c",
32+
"InstrProfilingPlatformOther.c",
33+
"InstrProfilingRuntime.cc",
34+
"InstrProfilingUtil.c",
35+
"InstrProfilingValue.c",
36+
"InstrProfilingWriter.c"];
37+
38+
if target.contains("msvc") {
39+
// Don't pull in extra libraries on MSVC
40+
cfg.flag("/Zl");
41+
profile_sources.push("WindowsMMap.c");
42+
cfg.define("strdup", Some("_strdup"));
43+
cfg.define("open", Some("_open"));
44+
cfg.define("fdopen", Some("_fdopen"));
45+
} else {
46+
// Turn off various features of gcc and such, mostly copying
47+
// compiler-rt's build system already
48+
cfg.flag("-fno-builtin");
49+
cfg.flag("-fvisibility=hidden");
50+
cfg.flag("-fomit-frame-pointer");
51+
cfg.flag("-ffreestanding");
52+
cfg.define("VISIBILITY_HIDDEN", None);
53+
}
54+
55+
for src in profile_sources {
56+
cfg.file(Path::new("../compiler-rt/lib/profile").join(src));
57+
}
58+
59+
cfg.compile("libprofiler-rt.a");
60+
}

src/libprofiler_builtins/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 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.
4+
//
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.
10+
11+
#![no_std]
12+
#![cfg_attr(not(stage0), feature(profiler_runtime))]
13+
#![cfg_attr(not(stage0), profiler_runtime)]
14+
#![unstable(feature = "profiler_runtime_lib",
15+
reason = "internal implementation detail of rustc right now",
16+
issue = "0")]
17+
#![crate_name = "profiler_builtins"]
18+
#![crate_type = "rlib"]
19+
#![allow(unused_features)]
20+
#![feature(staged_api)]

src/librustc/middle/cstore.rs

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ pub trait CrateStore {
258258
fn is_panic_runtime(&self, cnum: CrateNum) -> bool;
259259
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool;
260260
fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool;
261+
fn is_profiler_runtime(&self, cnum: CrateNum) -> bool;
261262
fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy;
262263
fn extern_crate(&self, cnum: CrateNum) -> Option<ExternCrate>;
263264
/// The name of the crate as it is referred to in source code of the current
@@ -381,6 +382,7 @@ impl CrateStore for DummyCrateStore {
381382
fn is_allocator(&self, cnum: CrateNum) -> bool { bug!("is_allocator") }
382383
fn is_panic_runtime(&self, cnum: CrateNum) -> bool { bug!("is_panic_runtime") }
383384
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool { bug!("is_compiler_builtins") }
385+
fn is_profiler_runtime(&self, cnum: CrateNum) -> bool { bug!("is_profiler_runtime") }
384386
fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool { bug!("is_sanitizer_runtime") }
385387
fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy {
386388
bug!("panic_strategy")

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10331033
"a single extra argument to prepend the linker invocation (can be used several times)"),
10341034
pre_link_args: Option<Vec<String>> = (None, parse_opt_list, [UNTRACKED],
10351035
"extra arguments to prepend to the linker invocation (space separated)"),
1036+
profile: bool = (false, parse_bool, [TRACKED],
1037+
"insert profiling code"),
10361038
}
10371039

10381040
pub fn default_lib_output() -> CrateType {

src/librustc_driver/driver.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ pub fn compile_input(sess: &Session,
204204
println!("Pre-trans");
205205
tcx.print_debug_stats();
206206
}
207-
let trans = phase_4_translate_to_llvm(tcx, analysis, &incremental_hashes_map);
207+
let trans = phase_4_translate_to_llvm(tcx, analysis, &incremental_hashes_map,
208+
&outputs);
208209

209210
if log_enabled!(::log::LogLevel::Info) {
210211
println!("Post-trans");
@@ -1042,7 +1043,8 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
10421043
/// be discarded.
10431044
pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10441045
analysis: ty::CrateAnalysis,
1045-
incremental_hashes_map: &IncrementalHashesMap)
1046+
incremental_hashes_map: &IncrementalHashesMap,
1047+
output_filenames: &OutputFilenames)
10461048
-> trans::CrateTranslation {
10471049
let time_passes = tcx.sess.time_passes();
10481050

@@ -1053,7 +1055,7 @@ pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10531055
let translation =
10541056
time(time_passes,
10551057
"translation",
1056-
move || trans::trans_crate(tcx, analysis, &incremental_hashes_map));
1058+
move || trans::trans_crate(tcx, analysis, &incremental_hashes_map, output_filenames));
10571059

10581060
time(time_passes,
10591061
"assert dep graph",

0 commit comments

Comments
 (0)