Skip to content

Commit ed6b501

Browse files
authored
Merge pull request #1 from hycakir/master
Julia 1.0 compat
2 parents ac1e11f + 1e9a06d commit ed6b501

File tree

2 files changed

+82
-62
lines changed

2 files changed

+82
-62
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
julia 0.5
1+
julia 1.0

src/LinuxPerf.jl

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
module LinuxPerf
22

3+
using Printf
4+
5+
export make_bench, enable!, disable!, reset!, reasonable_defaults, counters
6+
7+
import Base: show, length, close
8+
39
const SYS_perf_event_open = 298
410

5-
type perf_event_attr
6-
typ :: UInt32
7-
size :: UInt32
8-
config :: UInt64
9-
sample_period_or_freq :: UInt64
10-
sample_type :: UInt64
11-
read_format :: UInt64
12-
flags :: UInt64
13-
wakeup_events_or_watermark :: UInt32
14-
bp_type :: UInt32
15-
bp_addr_or_config1 :: UInt64
16-
bp_len_or_config2 :: UInt64
17-
branch_sample_type :: UInt64
18-
19-
sample_regs_user :: UInt64
20-
sample_stack_user :: UInt32
21-
clockid :: Int32
22-
sample_regs_intr :: UInt64
23-
aux_watermark :: UInt32
24-
__reserved_2 :: UInt32
25-
26-
perf_event_attr() = new()
11+
mutable struct perf_event_attr
12+
typ::UInt32
13+
size::UInt32
14+
config::UInt64
15+
sample_period_or_freq::UInt64
16+
sample_type::UInt64
17+
read_format::UInt64
18+
flags::UInt64
19+
wakeup_events_or_watermark::UInt32
20+
bp_type::UInt32
21+
bp_addr_or_config1::UInt64
22+
bp_len_or_config2::UInt64
23+
branch_sample_type::UInt64
24+
25+
sample_regs_user::UInt64
26+
sample_stack_user::UInt32
27+
clockid::Int32
28+
sample_regs_intr::UInt64
29+
aux_watermark::UInt32
30+
__reserved_2::UInt32
31+
2732
end
2833

34+
perf_event_attr() = perf_event_attr(ntuple(x->0, fieldcount(perf_event_attr))...)
35+
2936
const EVENT_TYPES =
3037
[
3138
(:hw, 0, # PERF_TYPE_HARDWARE
@@ -70,9 +77,9 @@ const PERF_FORMAT_TOTAL_TIME_ENABLED = 1 << 0
7077
const PERF_FORMAT_TOTAL_TIME_RUNNING = 1 << 1
7178
const PERF_FORMAT_GROUP = 1 << 3
7279

73-
immutable EventType
74-
category :: UInt32
75-
event :: UInt64
80+
struct EventType
81+
category::UInt32
82+
event::UInt64
7683
end
7784

7885
function all_events()
@@ -141,17 +148,19 @@ function EventType(cat::Symbol, cache::Symbol, op::Symbol, evt::Symbol)
141148
cache_id | (op_id << 8) | (evt_id << 16))
142149
end
143150

144-
type EventGroup
145-
leader_fd :: Cint
146-
fds :: Vector{Cint}
147-
event_types :: Vector{EventType}
148-
leader_io :: IOStream
149-
function EventGroup(types :: Vector{EventType};
151+
mutable struct EventGroup
152+
leader_fd::Cint
153+
fds::Vector{Cint}
154+
event_types::Vector{EventType}
155+
leader_io::IOStream
156+
157+
function EventGroup(types::Vector{EventType};
150158
warn_unsupported = true,
151159
userspace_only = false
152160
)
153-
my_types = Array(EventType, 0)
154-
group = new(-1, Array(Cint, 0), Array(EventType, 0))
161+
my_types = EventType[]
162+
group = new(-1, Cint[], EventType[])
163+
155164
for (i,evt_type) in enumerate(types)
156165
attr = perf_event_attr()
157166
attr.typ = evt_type.category
@@ -175,14 +184,13 @@ type EventGroup
175184
errno = Libc.errno()
176185
if errno in (Libc.EINVAL,Libc.ENOENT)
177186
if warn_unsupported
178-
warn("$evt_type not supported, skipping")
187+
@warn("$evt_type not supported, skipping")
179188
end
180189
continue
181190
else
182191
if errno == Libc.EACCES && !userspace_only
183-
warn("try to adjust /proc/sys/kernel/perf_event_paranoid to a value <= 1 or use user-space only events")
192+
@warn("try to adjust /proc/sys/kernel/perf_event_paranoid to a value <= 1 or use user-space only events")
184193
end
185-
@show errno
186194
error("perf_event_open error : $(Libc.strerror(errno))")
187195
end
188196
end
@@ -218,48 +226,57 @@ function ioctl(group::EventGroup, x)
218226
error("ioctl error : $(Libc.strerror())")
219227
end
220228
end
229+
221230
enable!(g::EventGroup) = ioctl(g, PERF_EVENT_IOC_ENABLE)
222231
disable!(g::EventGroup) = ioctl(g, PERF_EVENT_IOC_DISABLE)
223232
reset!(g::EventGroup) = ioctl(g, PERF_EVENT_IOC_RESET)
233+
224234
function Base.close(g::EventGroup)
225235
for fd in g.fds
226236
ccall(:close, Cint, (Cint,), fd)
227237
end
228238
end
229239

230-
type PerfBench
231-
groups :: Vector{EventGroup}
240+
mutable struct PerfBench
241+
groups::Vector{EventGroup}
232242
end
233-
immutable Counter
234-
event :: EventType
235-
value :: UInt64
236-
enabled :: UInt64
237-
running :: UInt64
243+
244+
struct Counter
245+
event::EventType
246+
value::UInt64
247+
enabled::UInt64
248+
running::UInt64
238249
end
239-
immutable Counters
240-
counters :: Vector{Counter}
250+
251+
struct Counters
252+
counters::Vector{Counter}
241253
end
254+
242255
function Base.show(io::IO, c::Counters)
256+
println(io)
243257
for c in c.counters
244-
print(io, c.event, " : ")
258+
print(io, c.event, ": ")
245259
if c.enabled == 0
246260
print(io, "never enabled")
247261
elseif c.running == 0
248262
print(io, "did not run")
249263
else
250264
@printf(io, "\n\t%20d (%.1f %%)", Int64(c.value), 100*(c.running/c.enabled))
251265
end
252-
println()
266+
println(io)
253267
end
254268
end
269+
255270
enable!(b::PerfBench) = foreach(enable!, b.groups)
256271
disable!(b::PerfBench) = foreach(disable!, b.groups)
257272
reset!(b::PerfBench) = foreach(reset!, b.groups)
273+
258274
function counters(b::PerfBench)
259-
c = Array(Counter, 0)
275+
c = Counter[]
260276
for g in b.groups
261-
values = read(g.leader_io, UInt64, length(g)+1+2)
262-
@assert(length(g) == values[1])
277+
values = Vector{UInt64}(undef, length(g)+1+2)
278+
read!(g.leader_io, values)
279+
#?Ref@assert(length(g) == values[1])
263280
enabled, running = values[2], values[3]
264281
for i = 1:length(g)
265282
push!(c, Counter(g.event_types[i], values[3+i],
@@ -268,17 +285,6 @@ function counters(b::PerfBench)
268285
end
269286
Counters(c)
270287
end
271-
function make_bench(x)
272-
groups = Array(EventGroup, 0)
273-
for y in x
274-
if isa(y, EventType)
275-
push!(groups, EventGroup([y]))
276-
else
277-
push!(groups, EventGroup(y))
278-
end
279-
end
280-
PerfBench(groups)
281-
end
282288

283289
const reasonable_defaults =
284290
[EventType(:hw, :cycles),
@@ -297,4 +303,18 @@ const reasonable_defaults =
297303
[EventType(:cache, :L1_data, :write, :access),
298304
EventType(:cache, :L1_data, :write, :miss)]=#]
299305

306+
function make_bench(x)
307+
groups = EventGroup[]
308+
for y in x
309+
if isa(y, EventType)
310+
push!(groups, EventGroup([y]))
311+
else
312+
push!(groups, EventGroup(y))
313+
end
314+
end
315+
PerfBench(groups)
316+
end
317+
318+
make_bench() = make_bench(reasonable_defaults)
319+
300320
end

0 commit comments

Comments
 (0)