|
| 1 | +using Profile: Profile |
| 2 | + |
| 3 | +struct ProfileProgressCaller <: RequestCaller |
| 4 | + trigger_path::String |
| 5 | + token::ProgressToken |
| 6 | +end |
| 7 | +cancellable_token(caller::ProfileProgressCaller) = caller.token |
| 8 | + |
| 9 | +function trigger_profile!(server::Server, trigger_path::String) |
| 10 | + if supports(server, :window, :workDoneProgress) |
| 11 | + id = String(gensym(:WorkDoneProgressCreateRequest_profile)) |
| 12 | + token = String(gensym(:ProfileProgress)) |
| 13 | + addrequest!(server, id => ProfileProgressCaller(trigger_path, token)) |
| 14 | + params = WorkDoneProgressCreateParams(; token) |
| 15 | + send(server, WorkDoneProgressCreateRequest(; id, params)) |
| 16 | + else |
| 17 | + do_profile(server, trigger_path) |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +function handle_profile_progress_response( |
| 22 | + server::Server, msg::Dict{Symbol,Any}, request_caller::ProfileProgressCaller |
| 23 | + ) |
| 24 | + if handle_response_error(server, msg, "create work done progress") |
| 25 | + return |
| 26 | + end |
| 27 | + (; trigger_path, token) = request_caller |
| 28 | + do_profile_with_progress(server, trigger_path, token) |
| 29 | +end |
| 30 | + |
| 31 | +function do_profile_with_progress(server::Server, trigger_path::String, token::ProgressToken) |
| 32 | + send_progress(server, token, |
| 33 | + WorkDoneProgressBegin(; title="Taking heap snapshot")) |
| 34 | + completed = false |
| 35 | + try |
| 36 | + do_profile(server, trigger_path) |
| 37 | + completed = true |
| 38 | + finally |
| 39 | + send_progress(server, token, |
| 40 | + WorkDoneProgressEnd(; |
| 41 | + message = "Heap snapshot " * (completed ? "completed" : "failed"))) |
| 42 | + end |
| 43 | +end |
| 44 | + |
| 45 | +function do_profile(server::Server, trigger_path::String) |
| 46 | + root_path = server.state.root_path |
| 47 | + timestamp = Libc.strftime("%Y%m%d_%H%M%S", time()) |
| 48 | + output_path = joinpath(root_path, "JETLS_$timestamp") |
| 49 | + |
| 50 | + assembled_path = output_path * ".heapsnapshot" |
| 51 | + try |
| 52 | + Profile.take_heap_snapshot(output_path; streaming=true) |
| 53 | + Profile.HeapSnapshot.assemble_snapshot(output_path, assembled_path) |
| 54 | + show_info_message(server, "Heap snapshot saved to: $assembled_path") |
| 55 | + catch e |
| 56 | + show_error_message(server, "Failed to take heap snapshot: $(sprint(showerror, e))") |
| 57 | + finally |
| 58 | + cleanup_streaming_files(output_path) |
| 59 | + end |
| 60 | + |
| 61 | + request_delete_file(server, filepath2uri(trigger_path)) |
| 62 | +end |
| 63 | + |
| 64 | +function cleanup_streaming_files(base_path::String) |
| 65 | + for suffix in (".nodes", ".edges", ".strings", ".metadata.json") |
| 66 | + path = base_path * suffix |
| 67 | + isfile(path) && rm(path) |
| 68 | + end |
| 69 | +end |
0 commit comments