Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CPU profiling sections of code #3971

Merged
merged 24 commits into from May 6, 2019
Merged
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b156a05
Support profiling sections of code
breeswish Dec 24, 2018
9ddf5e9
Support Callgrind
breeswish Dec 24, 2018
2091eda
Make rustfmt and clippy happy
breeswish Dec 24, 2018
35eb12c
Fix doc test
breeswish Dec 25, 2018
213229e
start profiling without env variables
breeswish Jan 2, 2019
1827120
Merge remote-tracking branch 'origin/master' into __profiler
breeswish Jan 2, 2019
05f25ac
Fix lock
breeswish Jan 2, 2019
6003a20
Merge remote-tracking branch 'origin/master' into __profiler
breeswish Apr 10, 2019
27d4973
Merge remote-tracking branch 'origin/master' into __profiler
breeswish Apr 12, 2019
e59f569
Use tikv_alloc
breeswish Apr 12, 2019
0c7e44b
Merge branch 'master' into __profiler
breeswish Apr 15, 2019
8105a60
Merge branch 'master' into __profiler
breeswish Apr 15, 2019
d7e919f
Merge branch 'master' into __profiler
breeswish Apr 18, 2019
9c677dd
Merge branch 'master' into __profiler
breeswish Apr 18, 2019
f4c67e5
Merge branch 'master' into __profiler
breeswish Apr 25, 2019
3998965
Merge branch 'master' into __profiler
breeswish Apr 25, 2019
5bc7552
Address comments about the returning value
breeswish Apr 28, 2019
ed94239
Merge remote-tracking branch 'origin/master' into __profiler
breeswish Apr 28, 2019
e22fe77
Upgrade dependency
breeswish Apr 28, 2019
3fe352c
Merge branch 'master' into __profiler
breeswish Apr 29, 2019
dd57f1d
Merge branch 'master' into __profiler
breeswish Apr 29, 2019
0df8cc7
Merge branch 'master' into __profiler
ice1000 Apr 29, 2019
8db777f
Merge branch 'master' into __profiler
breeswish May 6, 2019
048008c
Merge branch 'master' into __profiler
brson May 6, 2019
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Some generated files are not rendered by default. Learn more.

@@ -15,6 +15,7 @@ jemalloc = ["tikv_alloc/jemalloc"]
portable = ["engine/portable"]
sse = ["engine/sse"]
mem-profiling = ["tikv_alloc/mem-profiling"]
profiling = ["profiler/profiling"]
no-fail = ["fail/no_fail"]

[lib]
@@ -115,6 +116,7 @@ hyper = { version = "0.12", default-features = false, features = ["runtime"] }
tokio-threadpool = "0.1.13"
vlog = "0.1.4"
twoway = "0.2.0"
profiler = { path = "components/profiler" }
cop_datatype = { path = "components/cop_datatype" }
panic_hook = { path = "components/panic_hook" }
tipb = { git = "https://github.com/pingcap/tipb.git" }
@@ -0,0 +1,21 @@
[package]
name = "profiler"
version = "0.0.1"
edition = "2018"
publish = false
This conversation was marked as resolved by breeswish

This comment has been minimized.

Copy link
@kennytm

kennytm Apr 12, 2019

Contributor

This new package should use edition = 2018?


[features]
profiling = ["lazy_static", "cpuprofiler", "callgrind", "valgrind_request"]

This comment has been minimized.

Copy link
@BusyJay

BusyJay Apr 26, 2019

Contributor

Can it compile on Windows without the feature enabled?

This comment has been minimized.

Copy link
@breeswish

breeswish Apr 28, 2019

Author Member

It should work though I don't have a Windows machine. I changed it to [target.'cfg(linux)'.dependencies] and it can compile on my MacOS.


[dependencies]
tikv_alloc = { path = "../tikv_alloc" }

[target.'cfg(unix)'.dependencies]
lazy_static = { version = "1.2.0", optional = true }
cpuprofiler = { version = "0.0.3", optional = true }
callgrind = { version = "1.1.0", optional = true }

This comment has been minimized.

Copy link
@BusyJay

BusyJay Apr 26, 2019

Contributor

Is valgrind a useful use case? I think most of the time only cpuprofiling is used.

This comment has been minimized.

Copy link
@breeswish

breeswish Apr 26, 2019

Author Member

valgrind is very useful for micro benchmark, which can report precise amount of function calls, as well as precise (emulated) cache hit.

This comment has been minimized.

Copy link
@brson

brson Apr 28, 2019

Contributor

I'm also hoping that this crate can be extended into a general purpose profiling module that can be published for the community, and ultimately fulfill the various requirements of the Go profiling tools. More profiler options in that case seems good.

valgrind_request = { version = "1.1.0", optional = true }

[[example]]
name = "prime"
required-features = ["profiling"]
@@ -0,0 +1,84 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

//! Profiling sample: Calculate prime numbers.
//!
//! ## Usage
//!
//! ### Build
//!
//! ```bash
//! cargo build --features "profiling" --example prime
//! ```
//!
//! (You may also want `--release` in real scenarios).
//!
//! ### Run using CPU Profiler
//!
//! ```bash
//! ../../target/debug/examples/prime
//! ```
//!
//! ### Run using Callgrind
//!
//! ```bash
//! valgrind --tool=callgrind --instr-atstart=no ../../target/debug/examples/prime
//! ```
//!
//! You must not run example via `valgrind cargo run ...`. The framework won't detect Callgrind!

#[inline(never)]
fn is_prime_number(v: usize, prime_numbers: &[usize]) -> bool {
if v < 10000 {
let r = prime_numbers.binary_search(&v);
return r.is_ok();
}

for n in prime_numbers {
if v % n == 0 {
return false;
}
}

true
}

#[inline(never)]
fn prepare_prime_numbers() -> Vec<usize> {
// bootstrap: Generate a prime table of 0..10000
let mut prime_number_table: [bool; 10000] = [true; 10000];
prime_number_table[0] = false;
prime_number_table[1] = false;
for i in 2..10000 {
if prime_number_table[i] {
let mut v = i * 2;
while v < 10000 {
prime_number_table[v] = false;
v += i;
}
}
}
let mut prime_numbers = vec![];
for i in 2..10000 {
if prime_number_table[i] {
prime_numbers.push(i);
}
}
prime_numbers
}

fn main() {
let prime_numbers = prepare_prime_numbers();

profiler::start("prime.profile");

let mut v = 0;
for i in 2..50000 {
if is_prime_number(i, &prime_numbers) {
v += 1;
}
}

profiler::stop();

println!("Prime numbers: {}", v);
}
@@ -0,0 +1,58 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

//! Profile a part of the code using CPU Profiler from gperftools or Callgrind.
//! Supports Linux and MacOS.
//!
//! ## Requirements
//!
//! 1. gperftools
//!
//! Linux:
//!
//! You can follow its [INSTALL manual](https://github.com/gperftools/gperftools/blob/master/INSTALL).
//! Roughly the instructions are the following:
//!
//! 1. Download packages from [release](https://github.com/gperftools/gperftools/releases)
//! 2. Run `./configure`
//! 3. Run `make install`
//!
//! MacOS:
//!
//! Simply `brew install gperftools`.
//!
//! ## Usage
//!
//! ```ignore
//! profiler::start("./app.profile");
//! some_complex_code();
//! profiler::stop();
//! ```
//!
//! Then, compile the code with `profiling` feature enabled.
//!
//! By default, a profile called `app.profile` will be generated by CPU Profiler.
//! You can then analyze the profile using [pprof](https://github.com/google/pprof).
//!
//! If the application is running in Callgrind, a Callgrind profile dump will be generated instead.
//! Notice that you should run Callgrind with command line option `--instr-atstart=no`, e.g.:
//!
//! ```bash
//! valgrind --tool=callgrind --instr-atstart=no ./my_example
//! ```
//!
//! Also see `examples/prime.rs`.

#[allow(unused_extern_crates)]
extern crate tikv_alloc;

This comment has been minimized.

Copy link
@BusyJay

BusyJay Apr 26, 2019

Contributor

Why use tikv_alloc?

This comment has been minimized.

Copy link
@brson

brson Apr 26, 2019

Contributor

tikv_alloc generally needs to be linked into every crate that doesn't link to tikv, so that tests and benches of that crate use tikv's allocator. I don't think this extern crate statement is needed though as long as the dependency exists.

This comment has been minimized.

Copy link
@breeswish

breeswish Apr 26, 2019

Author Member

CI will fail if alloc is not linked

This comment has been minimized.

Copy link
@brson

brson Apr 28, 2019

Contributor

Ah, right. There are tests that all binaries contain jemalloc.


#[cfg(all(unix, feature = "profiling"))]
mod profiler_linux;
This conversation was marked as resolved by breeswish

This comment has been minimized.

Copy link
@BusyJay

BusyJay Apr 26, 2019

Contributor

Should it be named as profiler_unix?


#[cfg(all(unix, feature = "profiling"))]
pub use profiler_linux::*;

#[cfg(not(all(unix, feature = "profiling")))]
mod profiler_dummy;

#[cfg(not(all(unix, feature = "profiling")))]
pub use profiler_dummy::*;
@@ -0,0 +1,11 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

#[inline]
pub fn start(_name: impl AsRef<str>) {
// Do nothing
}

#[inline]
pub fn stop() {
// Do nothing
}
@@ -0,0 +1,68 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::sync::Mutex;

use callgrind::CallgrindClientRequest;

#[derive(Debug, PartialEq)]
enum Profiler {
None,
GPerfTools,
CallGrind,
}

lazy_static::lazy_static! {
#[derive(Debug)]
static ref ACTIVE_PROFILER: Mutex<Profiler> = Mutex::new(Profiler::None);
}

/// Start profiling.
///
/// When `profiling` feature is not enabled, this function will do nothing and there is totally
/// zero cost.
///
/// When running in Callgrind, Callgrind instrumentation will be started
/// (`CALLGRIND_START_INSTRUMENTATION`). Otherwise, the CPU Profiler will be started and profile
/// will be generated to the file specified by `name`.
// TODO: Better multi-thread support.
#[inline]
pub fn start(name: impl AsRef<str>) {
let mut profiler = ACTIVE_PROFILER.lock().unwrap();

// Profiling in progress.
if *profiler != Profiler::None {
return;
This conversation was marked as resolved by breeswish

This comment has been minimized.

Copy link
@BusyJay

BusyJay Apr 26, 2019

Contributor

Should it return error instead? For example, what if two profiling requests are received at the same time?

}

if valgrind_request::running_on_valgrind() != 0 {
*profiler = Profiler::CallGrind;
CallgrindClientRequest::start();
} else {
*profiler = Profiler::GPerfTools;
cpuprofiler::PROFILER
.lock()
.unwrap()
.start(name.as_ref())
.unwrap();
}
}

/// Stop profiling if it is started previously.
///
/// When `profiling` feature is not enabled, this function will do nothing and there is totally
/// zero cost.
#[inline]
pub fn stop() {
let mut profiler = ACTIVE_PROFILER.lock().unwrap();
match *profiler {
Profiler::None => {}
Profiler::CallGrind => {
CallgrindClientRequest::stop(None);
*profiler = Profiler::None;
}
Profiler::GPerfTools => {
cpuprofiler::PROFILER.lock().unwrap().stop().unwrap();
*profiler = Profiler::None;
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.