Skip to content

Add some copy methods #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/LinuxPerf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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