Skip to content

Commit c4f833b

Browse files
committed
Add IO output
1 parent 27aa784 commit c4f833b

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/PProf.jl

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,35 @@ using Base.StackTraces: StackFrame
4444
# TODO:
4545
# - Mappings
4646

47+
function pprof(data::Union{Nothing, Vector{UInt}} = nothing;
48+
web::Bool = true,
49+
webhost::AbstractString = "localhost",
50+
webport::Integer = 57599,
51+
out::AbstractString = "profile.pb.gz",
52+
ui_relative_percentages::Bool = true,
53+
kwargs...)
54+
@assert !isempty(basename(out)) "`out=` must specify a file path to write to. Got unexpected: '$out'"
55+
if !endswith(out, ".pb.gz")
56+
out = "$out.pb.gz"
57+
@info "Writing output to $out"
58+
end
59+
60+
# Write to disk
61+
io = open(out, "w")
62+
try
63+
pprof(io, data; kwargs...)
64+
finally
65+
close(io)
66+
end
67+
68+
if web
69+
refresh(webhost = webhost, webport = webport, file = out,
70+
ui_relative_percentages = ui_relative_percentages)
71+
end
72+
73+
return out
74+
end
75+
4776
"""
4877
pprof([data, [lidict]];
4978
web = true, webhost = "localhost", webport = 57599,
@@ -85,18 +114,14 @@ You can also use `PProf.refresh(file="...")` to open a new file in the server.
85114
- `ui_relative_percentages`: Passes `-relative_percentages` to pprof. Causes nodes
86115
ignored/hidden through the web UI to be ignored from totals when computing percentages.
87116
"""
88-
function pprof(data::Union{Nothing, Vector{UInt}} = nothing,
117+
function pprof(io,
118+
data::Union{Nothing, Vector{UInt}} = nothing,
89119
lidict::Union{Nothing, Dict} = nothing;
90120
sampling_delay::Union{Nothing, UInt64} = nothing,
91-
web::Bool = true,
92-
webhost::AbstractString = "localhost",
93-
webport::Integer = 57599,
94-
out::AbstractString = "profile.pb.gz",
95121
from_c::Bool = true,
96122
full_signatures::Bool = true,
97123
drop_frames::Union{Nothing, AbstractString} = nothing,
98124
keep_frames::Union{Nothing, AbstractString} = nothing,
99-
ui_relative_percentages::Bool = true,
100125
)
101126
has_meta = false
102127
if data === nothing
@@ -116,11 +141,6 @@ function pprof(data::Union{Nothing, Vector{UInt}} = nothing,
116141
if sampling_delay === nothing
117142
sampling_delay = ccall(:jl_profile_delay_nsec, UInt64, ())
118143
end
119-
@assert !isempty(basename(out)) "`out=` must specify a file path to write to. Got unexpected: '$out'"
120-
if !endswith(out, ".pb.gz")
121-
out = "$out.pb.gz"
122-
@info "Writing output to $out"
123-
end
124144

125145
string_table = OrderedDict{AbstractString, Int64}()
126146
enter!(string) = _enter!(string_table, string)
@@ -323,20 +343,8 @@ function pprof(data::Union{Nothing, Vector{UInt}} = nothing,
323343
default_sample_type = 1, # events
324344
)
325345

326-
# Write to disk
327-
io = GzipCompressorStream(open(out, "w"))
328-
try
329-
ProtoBuf.encode(ProtoBuf.ProtoEncoder(io), prof)
330-
finally
331-
close(io)
332-
end
333-
334-
if web
335-
refresh(webhost = webhost, webport = webport, file = out,
336-
ui_relative_percentages = ui_relative_percentages)
337-
end
338-
339-
out
346+
ProtoBuf.encode(ProtoBuf.ProtoEncoder(GzipCompressorStream(io)), prof)
347+
return nothing
340348
end
341349

342350
function _escape_name_for_pprof(name)

0 commit comments

Comments
 (0)