Skip to content

Commit

Permalink
add a branch in the debug super-endpoint to remove profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
d-netto committed Jun 7, 2024
1 parent ffd6913 commit 96d2721
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ProfileEndpoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ function handle_task_backtraces(stage_path = nothing)
else
backtrace_file = tempname(stage_path; cleanup=false)
end
backtrace_file *= ".task_backtraces"
open(backtrace_file, "w") do io
redirect_stderr(io) do
ccall(:jl_print_task_backtraces, Cvoid, ())
Expand All @@ -315,6 +316,28 @@ function handle_task_backtraces(stage_path = nothing)
return HTTP.Response(200, backtrace_file)
end

###
### Profile removal
###

function has_profile_extension(file)
return endswith(file, ".profile") || endswith(file, ".pb.gz") || endswith(file, ".task_backtraces")
end

function handle_profile_removal(profile_file, stage_path = nothing)
if stage_path !== nothing
profile_file = joinpath(stage_path, profile_file)
end
if !has_profile_extension(profile_file)
return HTTP.Response(400, "Profile file must have a `.profile`, `.pb.gz`, or `.task_backtraces` extension")
end
if !isfile(profile_file)
return HTTP.Response(400, "Profile file not found")
end
rm(profile_file)
return HTTP.Response(200, "All profiles removed")
end

###
### Debug super-endpoint
###
Expand Down Expand Up @@ -358,6 +381,12 @@ function debug_profile_endpoint_with_stage_path(stage_path = nothing)
)
elseif profile_type == "task_backtraces"
return handle_task_backtraces(stage_path)
elseif profile_type == "remove_profile"
if !haskey(body, "profile_file")
return HTTP.Response(400, "Need to provide a `profile_file` argument in the JSON body")
end
profile_file = body["profile_file"]
return handle_profile_removal(profile_file, stage_path)
else
return HTTP.Response(400, "Unknown profile_type: $profile_type")
end
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ const url = "http://127.0.0.1:$port"
@test isfile(fname)
end
end

@testset "Debug endpoint CPU profile and remove" begin
headers = ["Content-Type" => "application/json"]
payload = JSON3.write(Dict("profile_type" => "cpu_profile"))
req = HTTP.post("$url/debug_engine", headers, payload)
@test req.status == 200
fname = read(IOBuffer(req.body), String)
@info "filename: $fname"
@test isfile(fname)
headers = ["Content-Type" => "application/json"]
payload = JSON3.write(Dict("profile_type" => "remove_profile", "profile_file" => fname))
req = HTTP.post("$url/debug_engine", headers, payload)
@test req.status == 200
end
end

@testset "Heap snapshot $query" for query in ("", "?all_one=true")
Expand Down

0 comments on commit 96d2721

Please sign in to comment.