Add compile-time feature for profiling (#3370)

Co-authored-by: Przemyslaw Hugh Kaznowski <hugh@surrealdb.com>
Co-authored-by: Rushmore Mushambi <rushmore@surrealdb.com>
This commit is contained in:
Tobie Morgan Hitchcock 2024-02-28 15:46:00 +00:00 committed by GitHub
parent a1b4ee3a28
commit fba5aa875d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 226 additions and 13 deletions

79
Cargo.lock generated
View file

@ -3197,6 +3197,12 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "multimap"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a"
[[package]]
name = "nanoid"
version = "0.4.0"
@ -3504,7 +3510,7 @@ dependencies = [
"http 0.2.11",
"opentelemetry",
"opentelemetry-proto",
"prost",
"prost 0.11.9",
"thiserror",
"tokio",
"tonic 0.8.3",
@ -3519,7 +3525,7 @@ dependencies = [
"futures",
"futures-util",
"opentelemetry",
"prost",
"prost 0.11.9",
"tonic 0.8.3",
]
@ -3881,6 +3887,10 @@ dependencies = [
"nix 0.26.4",
"once_cell",
"parking_lot",
"prost 0.12.3",
"prost-build",
"prost-derive 0.12.3",
"sha2",
"smallvec",
"symbolic-demangle",
"tempfile",
@ -4009,7 +4019,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd"
dependencies = [
"bytes",
"prost-derive",
"prost-derive 0.11.9",
]
[[package]]
name = "prost"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a"
dependencies = [
"bytes",
"prost-derive 0.12.3",
]
[[package]]
name = "prost-build"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bdf592881d821b83d471f8af290226c8d51402259e9bb5be7f9f8bdebbb11ac"
dependencies = [
"bytes",
"heck",
"itertools 0.10.5",
"log",
"multimap",
"once_cell",
"petgraph",
"prettyplease",
"prost 0.12.3",
"prost-types",
"regex",
"syn 2.0.48",
"tempfile",
"which",
]
[[package]]
@ -4025,6 +4067,28 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "prost-derive"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e"
dependencies = [
"anyhow",
"itertools 0.10.5",
"proc-macro2",
"quote",
"syn 2.0.48",
]
[[package]]
name = "prost-types"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e081b29f63d83a4bc75cfc9f3fe424f9156cf92d8a4f0c9407cce9a1b67327cf"
dependencies = [
"prost 0.12.3",
]
[[package]]
name = "psl-types"
version = "2.0.11"
@ -5319,6 +5383,7 @@ dependencies = [
"opentelemetry-otlp",
"opentelemetry-proto",
"pin-project-lite",
"pprof",
"rand 0.8.5",
"rcgen",
"reqwest",
@ -5540,7 +5605,7 @@ dependencies = [
"log",
"pin-project",
"prometheus",
"prost",
"prost 0.11.9",
"rand 0.8.5",
"regex",
"semver",
@ -6036,8 +6101,8 @@ dependencies = [
"hyper-timeout",
"percent-encoding",
"pin-project",
"prost",
"prost-derive",
"prost 0.11.9",
"prost-derive 0.11.9",
"tokio",
"tokio-stream",
"tokio-util",
@ -6068,7 +6133,7 @@ dependencies = [
"hyper-timeout",
"percent-encoding",
"pin-project",
"prost",
"prost 0.11.9",
"rustls-pemfile",
"tokio",
"tokio-rustls",

View file

@ -23,6 +23,7 @@ ml2 = ["surrealdb/ml2"]
jwks = ["surrealdb/jwks"]
sql2 = ["surrealdb/sql2"]
parser2 = ["surrealdb/parser2"]
performance-profiler = ["dep:pprof"]
[workspace]
members = ["core", "lib", "lib/examples/actix", "lib/examples/axum"]
@ -61,6 +62,10 @@ once_cell = "1.18.0"
opentelemetry = { version = "0.19", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.12.0", features = ["metrics"] }
pin-project-lite = "0.2.13"
pprof = { version = "0.13.0", features = [
"flamegraph",
"prost-codec",
], optional = true }
rand = "0.8.5"
reqwest = { version = "0.11.22", default-features = false, features = [
"blocking",

View file

@ -79,7 +79,16 @@ enum Commands {
}
pub async fn init() -> ExitCode {
// Start a new CPU profiler
#[cfg(feature = "performance-profiler")]
let guard = pprof::ProfilerGuardBuilder::default()
.frequency(1000)
.blocklist(&["libc", "libgcc", "pthread", "vdso"])
.build()
.unwrap();
// Parse the CLI arguments
let args = Cli::parse();
// Run the respective command
let output = match args.command {
Commands::Start(args) => start::init(args).await,
Commands::Backup(args) => backup::init(args).await,
@ -92,6 +101,23 @@ pub async fn init() -> ExitCode {
Commands::IsReady(args) => isready::init(args).await,
Commands::Validate(args) => validate::init(args).await,
};
// Save the flamegraph and profile
#[cfg(feature = "performance-profiler")]
if let Ok(report) = guard.report().build() {
// Import necessary traits
use pprof::protos::Message;
use std::io::Write;
// Output a flamegraph
let file = std::fs::File::create("flamegraph.svg").unwrap();
report.flamegraph(file).unwrap();
// Output a pprof
let mut file = std::fs::File::create("profile.pb").unwrap();
let profile = report.pprof().unwrap();
let mut content = Vec::new();
profile.encode(&mut content).unwrap();
file.write_all(&content).unwrap();
};
// Error and exit the programme
if let Err(e) = output {
error!("{}", e);
ExitCode::FAILURE

View file

@ -331,6 +331,10 @@ criteria = "safe-to-deploy"
version = "0.1.11+1.0.8"
criteria = "safe-to-deploy"
[[exemptions.cast]]
version = "0.3.0"
criteria = "safe-to-deploy"
[[exemptions.cast]]
version = "0.3.0"
criteria = "safe-to-run"
@ -415,6 +419,10 @@ criteria = "safe-to-deploy"
version = "0.9.4"
criteria = "safe-to-deploy"
[[exemptions.cpp_demangle]]
version = "0.4.3"
criteria = "safe-to-deploy"
[[exemptions.cpp_demangle]]
version = "0.4.3"
criteria = "safe-to-run"
@ -427,10 +435,18 @@ criteria = "safe-to-deploy"
version = "1.3.2"
criteria = "safe-to-deploy"
[[exemptions.criterion]]
version = "0.5.1"
criteria = "safe-to-deploy"
[[exemptions.criterion]]
version = "0.5.1"
criteria = "safe-to-run"
[[exemptions.criterion-plot]]
version = "0.5.0"
criteria = "safe-to-deploy"
[[exemptions.criterion-plot]]
version = "0.5.0"
criteria = "safe-to-run"
@ -555,14 +571,14 @@ criteria = "safe-to-deploy"
version = "0.4.0"
criteria = "safe-to-deploy"
[[exemptions.fd-lock]]
version = "3.0.13"
criteria = "safe-to-deploy"
[[exemptions.filetime]]
version = "0.2.23"
criteria = "safe-to-deploy"
[[exemptions.findshlibs]]
version = "0.10.2"
criteria = "safe-to-deploy"
[[exemptions.findshlibs]]
version = "0.10.2"
criteria = "safe-to-run"
@ -807,6 +823,10 @@ criteria = "safe-to-deploy"
version = "0.2.3"
criteria = "safe-to-run"
[[exemptions.inferno]]
version = "0.11.19"
criteria = "safe-to-deploy"
[[exemptions.inferno]]
version = "0.11.19"
criteria = "safe-to-run"
@ -939,6 +959,10 @@ criteria = "safe-to-deploy"
version = "2.7.1"
criteria = "safe-to-deploy"
[[exemptions.memmap2]]
version = "0.9.4"
criteria = "safe-to-deploy"
[[exemptions.memmap2]]
version = "0.9.4"
criteria = "safe-to-run"
@ -975,6 +999,10 @@ criteria = "safe-to-deploy"
version = "0.8.10"
criteria = "safe-to-deploy"
[[exemptions.multimap]]
version = "0.8.3"
criteria = "safe-to-deploy"
[[exemptions.nanoid]]
version = "0.4.0"
criteria = "safe-to-deploy"
@ -1015,6 +1043,10 @@ criteria = "safe-to-deploy"
version = "0.1.0"
criteria = "safe-to-deploy"
[[exemptions.num-format]]
version = "0.4.4"
criteria = "safe-to-deploy"
[[exemptions.num-format]]
version = "0.4.4"
criteria = "safe-to-run"
@ -1035,6 +1067,10 @@ criteria = "safe-to-deploy"
version = "1.19.0"
criteria = "safe-to-deploy"
[[exemptions.oorandom]]
version = "11.1.3"
criteria = "safe-to-deploy"
[[exemptions.openssl]]
version = "0.10.63"
criteria = "safe-to-deploy"
@ -1151,14 +1187,26 @@ criteria = "safe-to-deploy"
version = "0.10.2"
criteria = "safe-to-deploy"
[[exemptions.plotters]]
version = "0.3.5"
criteria = "safe-to-deploy"
[[exemptions.plotters]]
version = "0.3.5"
criteria = "safe-to-run"
[[exemptions.plotters-backend]]
version = "0.3.5"
criteria = "safe-to-deploy"
[[exemptions.plotters-backend]]
version = "0.3.5"
criteria = "safe-to-run"
[[exemptions.plotters-svg]]
version = "0.3.5"
criteria = "safe-to-deploy"
[[exemptions.plotters-svg]]
version = "0.3.5"
criteria = "safe-to-run"
@ -1167,6 +1215,10 @@ criteria = "safe-to-run"
version = "0.2.0"
criteria = "safe-to-deploy"
[[exemptions.pprof]]
version = "0.13.0"
criteria = "safe-to-deploy"
[[exemptions.pprof]]
version = "0.13.0"
criteria = "safe-to-run"
@ -1207,10 +1259,26 @@ criteria = "safe-to-deploy"
version = "0.11.9"
criteria = "safe-to-deploy"
[[exemptions.prost]]
version = "0.12.3"
criteria = "safe-to-deploy"
[[exemptions.prost-build]]
version = "0.12.1"
criteria = "safe-to-deploy"
[[exemptions.prost-derive]]
version = "0.11.9"
criteria = "safe-to-deploy"
[[exemptions.prost-derive]]
version = "0.12.3"
criteria = "safe-to-deploy"
[[exemptions.prost-types]]
version = "0.12.1"
criteria = "safe-to-deploy"
[[exemptions.psm]]
version = "0.1.21"
criteria = "safe-to-deploy"
@ -1223,6 +1291,10 @@ criteria = "safe-to-deploy"
version = "0.1.4"
criteria = "safe-to-deploy"
[[exemptions.quick-xml]]
version = "0.26.0"
criteria = "safe-to-deploy"
[[exemptions.quick-xml]]
version = "0.26.0"
criteria = "safe-to-run"
@ -1327,6 +1399,10 @@ criteria = "safe-to-deploy"
version = "0.4.2"
criteria = "safe-to-deploy"
[[exemptions.rgb]]
version = "0.8.37"
criteria = "safe-to-deploy"
[[exemptions.rgb]]
version = "0.8.37"
criteria = "safe-to-run"
@ -1603,6 +1679,10 @@ criteria = "safe-to-deploy"
version = "1.0.6"
criteria = "safe-to-deploy"
[[exemptions.str_stack]]
version = "0.1.0"
criteria = "safe-to-deploy"
[[exemptions.str_stack]]
version = "0.1.0"
criteria = "safe-to-run"
@ -1619,10 +1699,18 @@ criteria = "safe-to-deploy"
version = "0.2.0-surreal.2"
criteria = "safe-to-deploy"
[[exemptions.symbolic-common]]
version = "12.8.0"
criteria = "safe-to-deploy"
[[exemptions.symbolic-common]]
version = "12.8.0"
criteria = "safe-to-run"
[[exemptions.symbolic-demangle]]
version = "12.8.0"
criteria = "safe-to-deploy"
[[exemptions.symbolic-demangle]]
version = "12.8.0"
criteria = "safe-to-run"
@ -1699,6 +1787,10 @@ criteria = "safe-to-deploy"
version = "2.0.2"
criteria = "safe-to-deploy"
[[exemptions.tinytemplate]]
version = "1.2.1"
criteria = "safe-to-deploy"
[[exemptions.tinytemplate]]
version = "1.2.1"
criteria = "safe-to-run"

View file

@ -143,8 +143,8 @@ user-login = "rushmorem"
user-name = "Rushmore Mushambi"
[[publisher.surrealkv]]
version = "0.1.2"
when = "2024-02-17"
version = "0.1.3"
when = "2024-02-22"
user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
@ -290,6 +290,24 @@ This update had a few doc updates but no otherwise-substantial source code
updates.
"""
[[audits.bytecode-alliance.audits.fd-lock]]
who = "Pat Hickey <phickey@fastly.com>"
criteria = "safe-to-deploy"
version = "3.0.9"
notes = "This crate uses unsafe to make Windows syscalls, to borrow an Fd with an appropriate lifetime, and to zero a windows API structure that appears to have a valid representation with zeroed memory."
[[audits.bytecode-alliance.audits.fd-lock]]
who = "Pat Hickey <phickey@fastly.com>"
criteria = "safe-to-deploy"
delta = "3.0.9 -> 3.0.10"
notes = "Just a dependency version bump"
[[audits.bytecode-alliance.audits.fd-lock]]
who = "Dan Gohman <dev@sunfishcode.online>"
criteria = "safe-to-deploy"
delta = "3.0.10 -> 3.0.12"
notes = "Just a dependency version bump"
[[audits.bytecode-alliance.audits.foreign-types]]
who = "Pat Hickey <phickey@fastly.com>"
criteria = "safe-to-deploy"
@ -1091,6 +1109,13 @@ criteria = "safe-to-deploy"
delta = "1.9.0 -> 2.0.0"
aggregated-from = "https://hg.mozilla.org/mozilla-central/raw-file/tip/supply-chain/audits.toml"
[[audits.mozilla.audits.fd-lock]]
who = "Jan-Erik Rediger <jrediger@mozilla.com>"
criteria = "safe-to-deploy"
delta = "3.0.12 -> 3.0.13"
notes = "Dependency updates only"
aggregated-from = "https://raw.githubusercontent.com/mozilla/glean/main/supply-chain/audits.toml"
[[audits.mozilla.audits.fnv]]
who = "Bobby Holley <bobbyholley@gmail.com>"
criteria = "safe-to-deploy"