From 1e913489ce899810af972e1b6b00fbbeda4b8c1d Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Wed, 15 Dec 2021 05:27:11 -0600 Subject: [PATCH] Support saving from graph format (#184) This is useful for the flamegraphs produced by SnoopCompile. Fixes #173 Closes #183 --- Project.toml | 4 ++-- src/ProfileView.jl | 20 ++++++++++++++------ test/runtests.jl | 9 +++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index 3882715..391818c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ProfileView" uuid = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7" author = ["Tim Holy "] -version = "1.0.0" +version = "1.0.1" [deps] Cairo = "159f3aea-2a34-519c-b102-8c37f9878175" @@ -21,7 +21,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" Cairo = "0.6, 0.8, 1" Colors = "0.9, 0.10, 0.11, 0.12" FileIO = "1.6" -FlameGraphs = "0.2" +FlameGraphs = "0.2.8" Graphics = "0.4, 1" Gtk = "0.18, 1" GtkObservables = "1" diff --git a/src/ProfileView.jl b/src/ProfileView.jl index 4b60e6b..e239c0e 100644 --- a/src/ProfileView.jl +++ b/src/ProfileView.jl @@ -129,7 +129,7 @@ function viewgui(fcolor, g::Node{NodeData}; data=nothing, lidict=nothing, window push!(tb, tb_save_as) # FIXME: likely have to do `allkwargs` in the two below (add in C, combine, recur) signal_connect(open_cb, tb_open, "clicked", Nothing, (), false, (widget(c),gsig,kwargs)) - signal_connect(save_as_cb, tb_save_as, "clicked", Nothing, (), false, (widget(c),data,lidict)) + signal_connect(save_as_cb, tb_save_as, "clicked", Nothing, (), false, (widget(c),data,lidict,g)) win = Window(windowname, 800, 600) push!(win, bx) GtkObservables.gc_preserve(win, c) @@ -258,20 +258,28 @@ end end function _open(gsig, selection; kwargs...) - data, lidict = load(selection)::Tuple{Vector{UInt64},Profile.LineInfoDict} - push!(gsig, flamegraph(data; lidict=lidict, kwargs...)) + ret = load(selection) + if isa(ret, Node{NodeData}) + gsig[] = ret + else + data, lidict = ret::Tuple{Vector{UInt64},Profile.LineInfoDict} + gsig[] = flamegraph(data; lidict=lidict, kwargs...) + end return nothing end @guarded function save_as_cb(::Ptr, profdata::Tuple) - c, data, lidict = profdata + c, data, lidict, g = profdata selection = save_dialog("Save profile data as *.jlprof file", toplevel(c), ("*.jlprof",)) isempty(selection) && return nothing + if data === nothing && lidict === nothing + return _save(selection, g) + end return _save(selection, data, lidict) end -function _save(selection, data, lidict) - FileIO.save(File{format"JLPROF"}(selection), data, lidict) +function _save(selection, args...) + FileIO.save(File{format"JLPROF"}(selection), args...) return nothing end diff --git a/test/runtests.jl b/test/runtests.jl index e136943..417653c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -99,10 +99,15 @@ end end fn = tempname() try + tmp = Observable{typeof(g)}() ProfileView._save(fn, backtraces, lidict) - tmp = [] ProfileView._open(tmp, fn) - for (gn, sn) in zip(PreOrderDFS(g), PreOrderDFS(tmp[1])) + for (gn, sn) in zip(PreOrderDFS(g), PreOrderDFS(tmp[])) + @test gn.data == sn.data + end + ProfileView._save(fn, g) + ProfileView._open(tmp, fn) + for (gn, sn) in zip(PreOrderDFS(g), PreOrderDFS(tmp[])) @test gn.data == sn.data end finally