@@ -123,27 +123,37 @@ end
123123
124124@static if ! (isdefined(Profile, :Allocs) && isdefined(PProf, :Allocs))
125125
126- function allocations_profile_endpoint(:: HTTP.Request )
127- return HTTP. Response(501 , " You must use a build of Julia (1.8+) and PProf that support Allocations profiling." )
126+ for f in (:allocations_profile_endpoint, :allocations_start_endpoint, :allocations_stop_endpoint)
127+ @eval function $ f(:: HTTP.Request )
128+ return HTTP. Response(501 , " You must use a build of Julia (1.8+) and PProf that support Allocations profiling." )
129+ end
128130end
129131
130132else
131133
132134function allocations_profile_endpoint(req:: HTTP.Request )
133-
134135 uri = HTTP. URI(req. target)
135136 qp = HTTP. queryparams(uri)
136137 if isempty(qp)
137138 @info " TODO: interactive HTML input page"
138139 return HTTP. Response(400 , allocs_profile_error_message())
139140 end
140-
141141 sample_rate = convert(Float64, parse(Float64, get(qp, " sample_rate" , default_alloc_sample_rate())))
142142 duration = parse(Float64, get(qp, " duration" , default_duration()))
143-
144143 return _do_alloc_profile(duration, sample_rate)
145144end
146145
146+ function allocations_start_endpoint(req:: HTTP.Request )
147+ uri = HTTP. URI(req. target)
148+ qp = HTTP. queryparams(uri)
149+ sample_rate = convert(Float64, parse(Float64, get(qp, " sample_rate" , default_alloc_sample_rate())))
150+ return _start_alloc_profile(sample_rate)
151+ end
152+
153+ function allocations_stop_endpoint(req:: HTTP.Request )
154+ return _stop_alloc_profile()
155+ end
156+
147157function _do_alloc_profile(duration, sample_rate)
148158 @info " Starting allocation Profiling from PerformanceProfilingHttpEndpoints with configuration:" duration sample_rate
149159
@@ -158,13 +168,30 @@ function _do_alloc_profile(duration, sample_rate)
158168 " allocs_profile-duration=$duration &sample_rate=$sample_rate .pb.gz" )
159169end
160170
171+ function _start_alloc_profile(sample_rate)
172+ @info " Starting allocation Profiling from PerformanceProfilingHttpEndpoints with configuration:" sample_rate
173+ Profile. Allocs. clear()
174+ Profile. Allocs. start(; sample_rate)
175+ return HTTP. Response(200 , " Allocation profiling started." )
176+ end
177+
178+ function _stop_alloc_profile()
179+ Profile. Allocs. stop()
180+ prof_name = tempname()
181+ PProf. Allocs. pprof(out= prof_name, web= false )
182+ prof_name = " $prof_name .pb.gz"
183+ return _http_response(read(prof_name), " allocs_profile.pb.gz" )
184+ end
185+
161186end # if isdefined
162187
163188function serve_profiling_server(;addr= " 127.0.0.1" , port= 16825 , verbose= false , kw... )
164189 verbose >= 0 && @info " Starting HTTP profiling server on port $port "
165190 router = HTTP. Router()
166191 HTTP. register!(router, " /profile" , cpu_profile_endpoint)
167192 HTTP. register!(router, " /allocs_profile" , allocations_profile_endpoint)
193+ HTTP. register!(router, " /allocs_profile_start" , allocations_start_endpoint)
194+ HTTP. register!(router, " /allocs_profile_stop" , allocations_stop_endpoint)
168195 # HTTP.serve! returns listening/serving server object
169196 return HTTP. serve!(router, addr, port; verbose, kw... )
170197end
@@ -176,8 +203,12 @@ function __init__()
176203 precompile(cpu_profile_endpoint, (HTTP. Request,)) || error(" precompilation of package functions is not supposed to fail" )
177204 precompile(_do_cpu_profile, (Int,Float64,Float64,Bool)) || error(" precompilation of package functions is not supposed to fail" )
178205 precompile(allocations_profile_endpoint, (HTTP. Request,)) || error(" precompilation of package functions is not supposed to fail" )
206+ precompile(allocations_start_endpoint, (HTTP. Request,)) || error(" precompilation of package functions is not supposed to fail" )
207+ precompile(allocations_stop_endpoint, (HTTP. Request,)) || error(" precompilation of package functions is not supposed to fail" )
179208 if isdefined(Profile, :Allocs) && isdefined(PProf, :Allocs)
180209 precompile(_do_alloc_profile, (Float64,Float64,)) || error(" precompilation of package functions is not supposed to fail" )
210+ precompile(_start_alloc_profile, (Float64,)) || error(" precompilation of package functions is not supposed to fail" )
211+ precompile(_stop_alloc_profile, ()) || error(" precompilation of package functions is not supposed to fail" )
181212 end
182213end
183214
0 commit comments