Skip to content

Commit 1f56c43

Browse files
committed
Refactor syscall
1 parent c313da3 commit 1f56c43

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/LinuxPerf.jl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ function Base.show(io::IO, e::EventType)
120120
end
121121
end
122122

123+
const SYS_perf_event_open = 298
124+
125+
"""
126+
perf_event_open(attr::perf_event_attr, pid, cpu, fd, flags)
127+
"""
128+
function perf_event_open(attr::perf_event_attr, pid, cpu, leader_fd, flags)
129+
r_attr = Ref(attr)
130+
GC.@preserve r_attr begin
131+
# Have to do a manual conversion, since the ABI is a vararg call
132+
ptr = Base.unsafe_convert(Ptr{Cvoid}, Base.cconvert(Ptr{Cvoid}, r_attr))
133+
fd = ccall(:syscall, Cint, (Clong, Clong...), SYS_perf_event_open,
134+
ptr, pid, cpu, leader_fd, flags)
135+
end
136+
return fd
137+
end
138+
123139
function EventType(cat::Symbol, event::Symbol)
124140
cat !== :cache || error("cache events needs 3 arguments")
125141
for (cat_name, cat_id, events) in EVENT_TYPES
@@ -161,7 +177,7 @@ mutable struct EventGroup
161177
my_types = EventType[]
162178
group = new(-1, Cint[], EventType[])
163179

164-
for (i,evt_type) in enumerate(types)
180+
for (i, evt_type) in enumerate(types)
165181
attr = perf_event_attr()
166182
attr.typ = evt_type.category
167183
attr.size = sizeof(perf_event_attr)
@@ -177,9 +193,8 @@ mutable struct EventGroup
177193
PERF_FORMAT_GROUP |
178194
PERF_FORMAT_TOTAL_TIME_ENABLED |
179195
PERF_FORMAT_TOTAL_TIME_RUNNING
180-
fd = ccall(:syscall, Cint, (Clong, Clong...), SYS_perf_event_open,
181-
pointer_from_objref(attr),
182-
0, -1, group.leader_fd, 0)
196+
197+
fd = perf_event_open(attr, 0, -1, group.leader_fd, 0)
183198
if fd < 0
184199
errno = Libc.errno()
185200
if errno in (Libc.EINVAL,Libc.ENOENT)
@@ -222,9 +237,8 @@ const PERF_EVENT_IOC_RESET = UInt64(0x2403)
222237

223238
function ioctl(group::EventGroup, x)
224239
res = ccall(:ioctl, Cint, (Cint, Clong, Clong), group.leader_fd, x, 1)
225-
if res < 0
226-
error("ioctl error : $(Libc.strerror())")
227-
end
240+
Base.systemerror(:ioctl, res < 0)
241+
return nothing
228242
end
229243

230244
enable!(g::EventGroup) = ioctl(g, PERF_EVENT_IOC_ENABLE)

0 commit comments

Comments
 (0)