-
Notifications
You must be signed in to change notification settings - Fork 203
Add jemalloc heap profiling to moq-relay #1194
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
Changes from all commits
d82cd27
cd77803
d4027e0
d42b23e
d47d0c0
0ee2142
e851415
2018ae2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||||||||||||||
| use tikv_jemalloc_ctl::raw; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Activate jemalloc heap profiling and listen for SIGUSR1 to dump profiles. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// The dump path is controlled by `MALLOC_CONF=prof_prefix:<path>`. | ||||||||||||||||||
| /// Returns `Ok(())` if profiling is not available (i.e. MALLOC_CONF=prof:true was not set). | ||||||||||||||||||
| pub async fn run() -> anyhow::Result<()> { | ||||||||||||||||||
| let prof_active = b"prof.active\0"; | ||||||||||||||||||
|
|
||||||||||||||||||
| match unsafe { raw::read::<bool>(prof_active) } { | ||||||||||||||||||
| Ok(true) => tracing::info!("jemalloc heap profiling is active"), | ||||||||||||||||||
| Ok(false) => { | ||||||||||||||||||
| tracing::info!("jemalloc profiling compiled in; activating"); | ||||||||||||||||||
| unsafe { raw::write(prof_active, true) } | ||||||||||||||||||
| .map_err(|err| anyhow::anyhow!("failed to activate jemalloc profiling: {err}"))?; | ||||||||||||||||||
| } | ||||||||||||||||||
|
kixelated marked this conversation as resolved.
|
||||||||||||||||||
| Err(err) => { | ||||||||||||||||||
| tracing::debug!(%err, "jemalloc profiling not available — set MALLOC_CONF=prof:true to enable"); | ||||||||||||||||||
| return Ok(()); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| let mut sig = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1())?; | ||||||||||||||||||
|
|
||||||||||||||||||
| loop { | ||||||||||||||||||
| sig.recv().await; | ||||||||||||||||||
|
|
||||||||||||||||||
| // Empty path tells jemalloc to use prof_prefix from MALLOC_CONF. | ||||||||||||||||||
| match unsafe { raw::write(b"prof.dump\0", b"\0" as *const u8) } { | ||||||||||||||||||
| Ok(()) => tracing::info!("heap profile dumped"), | ||||||||||||||||||
| Err(err) => tracing::error!(%err, "failed to dump heap profile"), | ||||||||||||||||||
|
Comment on lines
+28
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: Yes, passing NULL to the newp parameter of mallctl("prof.dump", NULL, NULL, NULL, 0) uses the prof_prefix to generate the filename according to the pattern <prefix><pid><seq>.m<mseq>.heap, where <prefix> comes from opt.prof_prefix or prof.prefix. No sources explicitly describe passing an empty string (i.e., mallctl("prof.dump", NULL, NULL, "", strlen(""))), but based on the man page description treating only NULL specially to use prof_prefix while non-NULL specifies a filename, an empty string would likely be treated as a filename "", which may fail to open or behave differently (e.g., error or empty file in current directory), not falling back to prof_prefix. Citations:
The jemalloc manual specifies that Suggested fix- // Empty path tells jemalloc to use prof_prefix from MALLOC_CONF.
- match unsafe { raw::write(b"prof.dump\0", b"\0" as *const u8) } {
+ // NULL tells jemalloc to use prof_prefix from MALLOC_CONF.
+ match unsafe { raw::write(b"prof.dump\0", std::ptr::null::<std::ffi::c_char>()) } {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
In systemd'ssystemd.exec(5), what doesPrivateTmp=truedo to a service's/tmp, and are files written there visible at the host path/tmp/...?💡 Result:
When PrivateTmp=true is set for a systemd service, systemd creates a new file system namespace for the service's processes and mounts private /tmp and /var/tmp directories inside it. These are bind-mounted over the service's view of /tmp and /var/tmp, using subdirectories of the host's /tmp and /var/tmp (typically named like /tmp/systemd-private--service-). From the service's perspective, /tmp behaves normally but is isolated. Files written by the service to its /tmp are not visible at the host's /tmp/... paths, nor shared with other services or the host unless they join the same namespace via JoinsNamespaceOf=.
Citations:
/tmpis the wrong default dump location withPrivateTmpenabled.Line 148 sets the default heap dump prefix to
/tmp/moq-relay.heap, but the unit enablesPrivateTmp = trueon line 206. With this setting, systemd creates an isolated file system namespace where the service's/tmpis private—files written there are not visible at the host's/tmp/...paths. The profile will not appear where the option description implies. Default this tocfg.stateDirinstead, which is already allowlisted inReadWritePathson line 209, or disablePrivateTmpif host-visible dumps are required.Suggested fix
Also applies to: 216-217
🤖 Prompt for AI Agents