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 8 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.

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

@@ -121,6 +122,7 @@ tipb = { git = "https://github.com/pingcap/tipb.git" }
kvproto = { git = "https://github.com/pingcap/kvproto.git" }
log_wrappers = { path = "components/log_wrappers" }
engine = { path = "components/engine" }
profiler = { path = "components/profiler" }

[dependencies.murmur3]
git = "https://github.com/pingcap/murmur3.git"
@@ -0,0 +1,13 @@
[package]
name = "profiler"
version = "0.0.1"
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 = []

[dependencies]
lazy_static = "1.2.0"
cpuprofiler = "0.0.3"
callgrind = "1.1.0"
valgrind_request = "1.1.0"
@@ -0,0 +1,97 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

//! 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!

extern crate profiler;

#[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,67 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

//! Profile a part of the code using CPU Profiler from gperftools or Callgrind.
//!
//! ## Requirements
//!
//! 1. Linux
//!
//! Other OS may also work however, not tested.
//!
//! 2. gperftools
//!
//! 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`
//!
//! ## 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`.

#[cfg(all(target_os = "linux", feature = "profiling"))]
#[macro_use]
extern crate lazy_static;

#[cfg(all(target_os = "linux", 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(target_os = "linux", feature = "profiling"))]
pub use profiler_linux::*;

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

#[cfg(not(all(target_os = "linux", feature = "profiling")))]
pub use profiler_dummy::*;
@@ -0,0 +1,22 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

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

#[inline]
pub fn stop() {
// Do nothing
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.