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

perf: add jemalloc heap profile #316

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 120 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ log = "0.4.20"
lettre = { version = "0.11.7", features = ["tokio1", "tokio1-native-tls"] }
handlebars = "5.1.2"

# Memory profiling
[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = { version = "0.5.4", features = ["profiling", "unprefixed_malloc_on_supported_platforms"] }
jemalloc_pprof = "0.1.0"

[dev-dependencies]
once_cell = "1.19.0"
Expand Down
Binary file added heap.pb.gz
Binary file not shown.
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod file_storage;
pub mod metrics;
pub mod pprof;
pub mod user;
pub mod util;
pub mod workspace;
Expand Down
24 changes: 24 additions & 0 deletions src/api/pprof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use actix_web::{web, Scope};

pub fn pprof_scope() -> Scope {
web::scope("/debug/pprof")

// Usage:
// curl localhost:3000/debug/pprof/heap > heap.pb.gz
// pprof -http=:8080 heap.pb.gz
.service(web::resource("/heap").route(web::get().to(handle_get_heap)))
}

async fn handle_get_heap() -> Vec<u8> {
let mut prof_ctl = match jemalloc_pprof::PROF_CTL.as_ref() {
Some(x) => x.lock().await,
None => return "none: cannot get prof_ctl".into(),
};
if !prof_ctl.activated() {
return "heap profiling not activated".into();
}
match prof_ctl.dump_pprof() {
Ok(x) => x,
Err(e) => format!("error: {}", e).into(),
}
}
2 changes: 2 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::api::metrics::metrics_scope;

use crate::api::file_storage::file_storage_scope;
use crate::api::pprof::pprof_scope;
use crate::api::user::user_scope;
use crate::api::workspace::{collab_scope, workspace_scope};
use crate::api::ws::ws_scope;
Expand Down Expand Up @@ -136,6 +137,7 @@ pub async fn run_actix_server(
.service(ws_scope())
.service(file_storage_scope())
.service(metrics_scope())
.service(pprof_scope())
.app_data(Data::new(state.metrics.registry.clone()))
.app_data(Data::new(state.metrics.request_metrics.clone()))
.app_data(Data::new(state.metrics.realtime_metrics.clone()))
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ use appflowy_cloud::application::{init_state, Application};
use appflowy_cloud::config::config::get_configuration;
use appflowy_cloud::telemetry::init_subscriber;

// https://github.com/polarsignals/rust-jemalloc-pprof?tab=readme-ov-file#usage
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

#[allow(non_upper_case_globals)]
#[export_name = "malloc_conf"]
pub static malloc_conf: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:19\0";

#[actix_web::main]
async fn main() -> anyhow::Result<()> {
let level = std::env::var("RUST_LOG").unwrap_or("info".to_string());
Expand Down
Loading