Skip to content

Commit 5e75d8c

Browse files
committed
Add Serialization of LinuxPerf types
This commits type piracy unfortunately.
1 parent b118a4a commit 5e75d8c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/BenchmarkTools.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ using Random: Random
1414

1515
const BENCHMARKTOOLS_VERSION = v"1.0.0"
1616

17+
const LINUXPERF_VERSION = if VERSION >= v"1.9"
18+
pkgversion(LinuxPerf)
19+
else
20+
v"0.3.6"
21+
end
22+
1723
##############
1824
# Parameters #
1925
##############

src/serialization.jl

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const VERSIONS = Dict(
2-
"Julia" => string(VERSION), "BenchmarkTools" => string(BENCHMARKTOOLS_VERSION)
2+
"Julia" => string(VERSION),
3+
"BenchmarkTools" => string(BENCHMARKTOOLS_VERSION),
4+
"LinuxPerf" => string(LINUXPERF_VERSION),
35
)
46

57
# TODO: Add any new types as they're added
@@ -29,6 +31,24 @@ function JSON.lower(x::Union{values(SUPPORTED_TYPES)...})
2931
return [string(nameof(typeof(x))), d]
3032
end
3133

34+
# Should this be in LinuxPerf?
35+
JSON.lower(::typeof(LinuxPerf.parse_groups)) = "LinuxPerf.parse_groups"
36+
37+
# Should this be in LinuxPerf?
38+
# Need this to deserialize LinuxPerf.Stats
39+
Base.convert(::Type{LinuxPerf.Stats}, d::Dict{String}) = LinuxPerf.Stats(d["threads"])
40+
function Base.convert(::Type{LinuxPerf.ThreadStats}, d::Dict{String})
41+
return LinuxPerf.ThreadStats(d["pid"], d["groups"])
42+
end
43+
function Base.convert(::Type{LinuxPerf.EventType}, d::Dict{String})
44+
return LinuxPerf.EventType(d["category"], d["event"])
45+
end
46+
function Base.convert(::Type{LinuxPerf.Counter}, d::Dict{String})
47+
return LinuxPerf.Counter(
48+
convert(LinuxPerf.EventType, d["event"]), d["running"], d["enabled"], d["value"]
49+
)
50+
end
51+
3252
# a minimal 'eval' function, mirroring KeyTypes, but being slightly more lenient
3353
safeeval(@nospecialize x) = x
3454
safeeval(x::QuoteNode) = x.value
@@ -50,8 +70,27 @@ function recover(x::Vector)
5070
for i in 1:fc
5171
ft = fieldtype(T, i)
5272
fn = String(fieldname(T, i))
53-
if ft <: get(SUPPORTED_TYPES, nameof(ft), Union{})
73+
if ft == Union{Nothing,LinuxPerf.Stats}
74+
xsi = if isnothing(fields[fn])
75+
nothing
76+
else
77+
convert(ft, fields[fn])
78+
end
79+
elseif ft <: get(SUPPORTED_TYPES, nameof(ft), Union{})
5480
xsi = recover(fields[fn])
81+
elseif fn == "linux_perf_options"
82+
field_val = fields[fn]
83+
xsi = (
84+
events=Expr(
85+
Symbol(field_val["events"]["head"]),
86+
LinuxPerf.parse_groups,
87+
field_val["events"]["args"][2],
88+
),
89+
spaces=Expr(
90+
Symbol(field_val["spaces"]["head"]), field_val["spaces"]["args"]...
91+
),
92+
threads=field_val["threads"],
93+
)
5594
else
5695
xsi = convert(ft, fields[fn])
5796
end

0 commit comments

Comments
 (0)