Skip to content

Allow printing task backtraces via profiling peek mechanism #56043

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@ function profile_printing_listener()
profile = @something(profile, require(PkgId(UUID("9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"), "Profile")))

invokelatest(profile.peek_report[])
if Base.get_bool_env("JULIA_PROFILE_PEEK_TASK_BACKTRACES", true) === true
print(stderr, "Printing Julia task backtraces...\n")
flush(stderr)
ccall(:jl_print_task_backtraces, Cvoid, ())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prints to a different abstraction over stderr than the other println calls here, so may need to explicitly call flush around it to ensure the output doesn't get jumbled

Aside, it can also be better to use print(stderr, "<text>\n") instead of println where reliable async behavior is desired, since it ensures the whole text prints in the same syscall

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very helpful, thanks!

end
if Base.get_bool_env("JULIA_PROFILE_PEEK_HEAP_SNAPSHOT", false) === true
println(stderr, "Saving heap snapshot...")
fname = invokelatest(profile.take_heap_snapshot)
Expand Down
7 changes: 6 additions & 1 deletion doc/src/manual/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,12 @@ Enable debug logging for a file or module, see [`Logging`](@ref man-logging) for

### [`JULIA_PROFILE_PEEK_HEAP_SNAPSHOT`](@id JULIA_PROFILE_PEEK_HEAP_SNAPSHOT)

Enable collecting of a heap snapshot during execution via the profiling peek mechanism.
Enable collecting of a heap snapshot during execution via the profiling peek mechanism. Set to `1` to enable.
See [Triggered During Execution](@ref).

### [`JULIA_PROFILE_PEEK_TASK_BACKTRACES`](@id JULIA_PROFILE_PEEK_TASK_BACKTRACES)

Disable printing of task backtraces via the profiling peek mechanism. Set to `0` to disable.
See [Triggered During Execution](@ref).

### [`JULIA_TIMING_SUBSYSTEMS`](@id JULIA_TIMING_SUBSYSTEMS)
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Profile/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ First, a single stack trace at the instant that the signal was thrown is shown,
followed by the profile report at the next yield point, which may be at task completion for code without yield points
e.g. tight loops.

In addition to the profile, all task backtraces will be printed, unless the environment variable
[`JULIA_PROFILE_PEEK_TASK_BACKTRACES`](@ref JULIA_PROFILE_PEEK_TASK_BACKTRACES) is set to `0`.

Optionally set environment variable [`JULIA_PROFILE_PEEK_HEAP_SNAPSHOT`](@ref JULIA_PROFILE_PEEK_HEAP_SNAPSHOT) to `1` to also automatically collect a
[heap snapshot](@ref Heap-Snapshots).

Expand Down