-
Notifications
You must be signed in to change notification settings - Fork 3
Add cpu profiling start/stop endpoints #11
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
01e4cb7
Add cpu profiling start/stop endpoints
nickrobinson251 299f763
Group related code together
nickrobinson251 17d1d47
fixup! Add cpu profiling start/stop endpoints
nickrobinson251 4f5edc6
Move work to be outside of profiling
nickrobinson251 e36fce7
Move more work to be outside of profiling
nickrobinson251 310e3e7
Test `pprof=true` param
nickrobinson251 16f113f
Fix comment typo in test/runtests.jl
nickrobinson251 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,11 +21,21 @@ using Serialization: serialize | |
| # | ||
| #---------------------------------------------------------- | ||
|
|
||
| function _http_response(binary_data, filename) | ||
| return HTTP.Response(200, [ | ||
| "Content-Type" => "application/octet-stream" | ||
| "Content-Disposition" => "attachment; filename=$(repr(filename))" | ||
| ], body = binary_data) | ||
| end | ||
|
|
||
| ### | ||
| ### CPU | ||
| ### | ||
|
|
||
| default_n() = "1e8" | ||
| default_delay() = "0.01" | ||
| default_duration() = "10.0" | ||
| default_pprof() = "true" | ||
| default_alloc_sample_rate() = "0.0001" | ||
|
|
||
| cpu_profile_error_message() = """Need to provide query params: | ||
| - duration=$(default_duration()) | ||
|
|
@@ -50,24 +60,6 @@ controlled by `n=`. If you assume an average stack depth of 100, and you were ai | |
|
|
||
| The default `n` is 1e8, which should be big enough for most profiles. | ||
| """ | ||
| allocs_profile_error_message() = """Need to provide query params: | ||
| - duration=$(default_duration()) | ||
| - sample_rate=$(default_alloc_sample_rate()) | ||
|
|
||
| Hint: A good goal is to shoot for around 1,000 to 10,000 samples. So if you know what | ||
| duration you want to profile for, and you *already have an expectation for how much your | ||
| program will allocate,* you can pick a sample_rate via `sample_rate = 1,000 / expected_allocations`. | ||
|
|
||
| For example, if you expect your program will actually perform 1 million allocations: | ||
| 1_000 / 1_000_000 = 0.001 | ||
| for `duration=30&sample_rate=0.001` | ||
|
|
||
| Note that if your sample_rate gets too large, you can really slow down the program you're | ||
| profiling, and thus end up with an inaccurate profile. | ||
|
|
||
| Finally, if you think your program only allocates a small amount, you can capture *all* | ||
| allocations by passing sample_rate=1. | ||
| """ | ||
|
|
||
| function cpu_profile_endpoint(req::HTTP.Request) | ||
| uri = HTTP.URI(req.target) | ||
|
|
@@ -82,45 +74,95 @@ function cpu_profile_endpoint(req::HTTP.Request) | |
| delay = parse(Float64, get(qp, "delay", default_delay())) | ||
| duration = parse(Float64, get(qp, "duration", default_duration())) | ||
| with_pprof = parse(Bool, get(qp, "pprof", default_pprof())) | ||
|
|
||
| return _do_cpu_profile(n, delay, duration, with_pprof) | ||
| end | ||
|
|
||
| function cpu_profile_start_endpoint(req::HTTP.Request) | ||
| uri = HTTP.URI(req.target) | ||
| qp = HTTP.queryparams(uri) | ||
|
|
||
| # Run the profile | ||
| n = convert(Int, parse(Float64, get(qp, "n", default_n()))) | ||
| delay = parse(Float64, get(qp, "delay", default_delay())) | ||
| return _start_cpu_profile(n, delay) | ||
| end | ||
|
|
||
| function cpu_profile_stop_endpoint(req::HTTP.Request) | ||
| uri = HTTP.URI(req.target) | ||
| qp = HTTP.queryparams(uri) | ||
| with_pprof = parse(Bool, get(qp, "pprof", default_pprof())) | ||
| return _stop_cpu_profile(with_pprof) | ||
| end | ||
|
|
||
| function _do_cpu_profile(n, delay, duration, with_pprof) | ||
| @info "Starting CPU Profiling from PerformanceProfilingHttpEndpoints with configuration:" n delay duration | ||
|
|
||
| Profile.clear() | ||
|
|
||
| Profile.init(n, delay) | ||
|
|
||
| Profile.@profile sleep(duration) | ||
| data = Profile.retrieve() | ||
| filename = "cpu_profile-duration=$duration&delay=$delay&n=$n" | ||
| return _cpu_profile_response(data, filename; with_pprof) | ||
| end | ||
|
|
||
| function _start_cpu_profile(n, delay) | ||
| @info "Starting CPU Profiling from PerformanceProfilingHttpEndpoints with configuration:" n delay | ||
| resp = HTTP.Response(200, "CPU profiling started.") | ||
| Profile.clear() | ||
| Profile.init(n, delay) | ||
| Profile.start_timer() | ||
| return resp | ||
| end | ||
|
|
||
| function _stop_cpu_profile(with_pprof) | ||
| Profile.stop_timer() | ||
| @info "Stopping CPU Profiling from PerformanceProfilingHttpEndpoints" | ||
| data = Profile.retrieve() | ||
| filename = "cpu_profile" | ||
| return _cpu_profile_response(data, filename; with_pprof) | ||
| end | ||
|
|
||
| function _cpu_profile_response(data, filename; with_pprof::Bool) | ||
| if with_pprof | ||
| prof_name = tempname() | ||
| PProf.pprof(out=prof_name, web=false) | ||
| prof_name = "$prof_name.pb.gz" | ||
| return _http_response(read(prof_name), | ||
| "cpu_profile-duration=$duration&delay=$delay&n=$n.pb.gz") | ||
| return _http_response(read(prof_name), "$filename.pb.gz") | ||
| else | ||
| iobuf = IOBuffer() | ||
| serialize(iobuf, data) | ||
| return _http_response(take!(iobuf), | ||
| "cpu_profile&duration=$duration&delay=$delay&n=$n.prof.bin") | ||
| return _http_response(take!(iobuf), "$filename.prof.bin") | ||
| end | ||
| end | ||
|
|
||
| function _http_response(binary_data, filename) | ||
| return HTTP.Response(200, [ | ||
| "Content-Type" => "application/octet-stream" | ||
| "Content-Disposition" => "attachment; filename=$(repr(filename))" | ||
| ], body = binary_data) | ||
| end | ||
| ### | ||
| ### Allocs | ||
| ### | ||
|
|
||
| function heap_snapshot_endpoint(req::HTTP.Request) | ||
| # TODO: implement this once https://github.com/JuliaLang/julia/pull/42286 is merged | ||
| end | ||
|
|
||
| default_alloc_sample_rate() = "0.0001" | ||
|
|
||
| allocs_profile_error_message() = """Need to provide query params: | ||
| - duration=$(default_duration()) | ||
| - sample_rate=$(default_alloc_sample_rate()) | ||
|
|
||
| Hint: A good goal is to shoot for around 1,000 to 10,000 samples. So if you know what | ||
| duration you want to profile for, and you *already have an expectation for how much your | ||
| program will allocate,* you can pick a sample_rate via `sample_rate = 1,000 / expected_allocations`. | ||
|
|
||
| For example, if you expect your program will actually perform 1 million allocations: | ||
| 1_000 / 1_000_000 = 0.001 | ||
| for `duration=30&sample_rate=0.001` | ||
|
|
||
| Note that if your sample_rate gets too large, you can really slow down the program you're | ||
| profiling, and thus end up with an inaccurate profile. | ||
|
|
||
| Finally, if you think your program only allocates a small amount, you can capture *all* | ||
| allocations by passing sample_rate=1. | ||
| """ | ||
|
|
||
| @static if !(isdefined(Profile, :Allocs) && isdefined(PProf, :Allocs)) | ||
|
|
||
| for f in (:allocations_profile_endpoint, :allocations_start_endpoint, :allocations_stop_endpoint) | ||
|
|
@@ -170,9 +212,10 @@ end | |
|
|
||
| function _start_alloc_profile(sample_rate) | ||
| @info "Starting allocation Profiling from PerformanceProfilingHttpEndpoints with configuration:" sample_rate | ||
| resp = HTTP.Response(200, "Allocation profiling started.") | ||
| Profile.Allocs.clear() | ||
| Profile.Allocs.start(; sample_rate) | ||
| return HTTP.Response(200, "Allocation profiling started.") | ||
| return resp | ||
|
Comment on lines
+210
to
+213
Member
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. genius |
||
| end | ||
|
|
||
| function _stop_alloc_profile() | ||
|
|
@@ -185,10 +228,16 @@ end | |
|
|
||
| end # if isdefined | ||
|
|
||
| ### | ||
| ### Server | ||
| ### | ||
|
|
||
| function serve_profiling_server(;addr="127.0.0.1", port=16825, verbose=false, kw...) | ||
| verbose >= 0 && @info "Starting HTTP profiling server on port $port" | ||
| router = HTTP.Router() | ||
| HTTP.register!(router, "/profile", cpu_profile_endpoint) | ||
| HTTP.register!(router, "/profile_start", cpu_profile_start_endpoint) | ||
| HTTP.register!(router, "/profile_stop", cpu_profile_stop_endpoint) | ||
| HTTP.register!(router, "/allocs_profile", allocations_profile_endpoint) | ||
| HTTP.register!(router, "/allocs_profile_start", allocations_start_endpoint) | ||
| HTTP.register!(router, "/allocs_profile_stop", allocations_stop_endpoint) | ||
|
|
@@ -200,8 +249,14 @@ end | |
| # up profiling compilation! | ||
| function __init__() | ||
| precompile(serve_profiling_server, ()) || error("precompilation of package functions is not supposed to fail") | ||
|
|
||
| precompile(cpu_profile_endpoint, (HTTP.Request,)) || error("precompilation of package functions is not supposed to fail") | ||
| precompile(cpu_profile_start_endpoint, (HTTP.Request,)) || error("precompilation of package functions is not supposed to fail") | ||
| precompile(cpu_profile_stop_endpoint, (HTTP.Request,)) || error("precompilation of package functions is not supposed to fail") | ||
| precompile(_do_cpu_profile, (Int,Float64,Float64,Bool)) || error("precompilation of package functions is not supposed to fail") | ||
| precompile(_start_cpu_profile, (Int,Float64,)) || error("precompilation of package functions is not supposed to fail") | ||
| precompile(_stop_cpu_profile, (Bool,)) || error("precompilation of package functions is not supposed to fail") | ||
|
|
||
| precompile(allocations_profile_endpoint, (HTTP.Request,)) || error("precompilation of package functions is not supposed to fail") | ||
| precompile(allocations_start_endpoint, (HTTP.Request,)) || error("precompilation of package functions is not supposed to fail") | ||
| precompile(allocations_stop_endpoint, (HTTP.Request,)) || error("precompilation of package functions is not supposed to fail") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
+1 to @quinnj's suggestion to stop the profile as soon as possible, and then do the rest of this work after it's stopped.