diff --git a/src/LinuxPerf.jl b/src/LinuxPerf.jl index e330e92..aef2e37 100644 --- a/src/LinuxPerf.jl +++ b/src/LinuxPerf.jl @@ -118,6 +118,8 @@ struct EventType event::UInt64 end +Base.copy(event::EventType) = EventType(event.category, event.event) + function all_events() evts = EventType[] for (cat_name, cat_id, events) in EVENT_TYPES @@ -356,6 +358,12 @@ struct Counter running::UInt64 end +function Base.copy(counter::Counter) + return Counter( + copy(counter.event), counter.value, counter.enabled, counter.running + ) +end + struct Counters counters::Vector{Counter} end @@ -734,6 +742,10 @@ struct ThreadStats groups::Vector{Vector{Counter}} end +function Base.copy(thread_stats::ThreadStats) + return ThreadStats(thread_stats.pid, copy(thread_stats.groups)) +end + function ThreadStats(b::PerfBench) groups = Vector{Counter}[] for g in b.groups @@ -775,6 +787,8 @@ struct Stats threads::Vector{ThreadStats} end +Base.copy(stats::Stats) = Stats(copy(stats.threads)) + Stats(b::PerfBenchThreaded) = Stats(map(ThreadStats, b.data)) Base.show(io::IO, stats::Stats) = printsummary(io, stats) diff --git a/test/runtests.jl b/test/runtests.jl index ec96e18..6c3fee8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,7 +1,7 @@ using LinuxPerf using Test -using LinuxPerf: make_bench, enable!, disable!, reset!, reasonable_defaults, counters, EventType, EventTypeExt, parse_groups +using LinuxPerf: make_bench, enable!, disable!, reset!, reasonable_defaults, counters, EventType, EventTypeExt, parse_groups, Counter, ThreadStats, Stats @testset "LinuxPerf" begin @@ -97,5 +97,27 @@ end @pstats "cpu-cycles,(instructions,branch-instructions,branch-misses),(task-clock,context-switches,cpu-migrations,page-faults),(L1-dcache-load-misses,L1-dcache-loads,L1-icache-load-misses),(dTLB-load-misses,dTLB-loads)" foo!(dest, a, b, c) end +@testset "copy structs" begin + event = EventType(:hw, :cycles) + @test_nowarn copy(event) + @test copy(event) === event + + counter = Counter(event, 1, 2, 3) + @test_nowarn copy(counter) + @test copy(counter) === counter + + thread_stats = ThreadStats(0, [[counter, counter]]) + @test_nowarn copy(thread_stats) + copied_thread_stats = copy(thread_stats) + @test copied_thread_stats.pid == thread_stats.pid + @test copied_thread_stats.groups == thread_stats.groups + @test copied_thread_stats.groups !== thread_stats.groups + + stats = Stats([thread_stats]) + @test_nowarn copy(stats) + copied_stats = copy(stats) + @test copied_stats.threads == stats.threads + @test copied_stats.threads !== stats.threads +end end \ No newline at end of file