@@ -120,6 +120,22 @@ function Base.show(io::IO, e::EventType)
120
120
end
121
121
end
122
122
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
+
123
139
function EventType (cat:: Symbol , event:: Symbol )
124
140
cat != = :cache || error (" cache events needs 3 arguments" )
125
141
for (cat_name, cat_id, events) in EVENT_TYPES
@@ -161,7 +177,7 @@ mutable struct EventGroup
161
177
my_types = EventType[]
162
178
group = new (- 1 , Cint[], EventType[])
163
179
164
- for (i,evt_type) in enumerate (types)
180
+ for (i, evt_type) in enumerate (types)
165
181
attr = perf_event_attr ()
166
182
attr. typ = evt_type. category
167
183
attr. size = sizeof (perf_event_attr)
@@ -177,9 +193,8 @@ mutable struct EventGroup
177
193
PERF_FORMAT_GROUP |
178
194
PERF_FORMAT_TOTAL_TIME_ENABLED |
179
195
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 )
183
198
if fd < 0
184
199
errno = Libc. errno ()
185
200
if errno in (Libc. EINVAL,Libc. ENOENT)
@@ -222,9 +237,8 @@ const PERF_EVENT_IOC_RESET = UInt64(0x2403)
222
237
223
238
function ioctl (group:: EventGroup , x)
224
239
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
228
242
end
229
243
230
244
enable! (g:: EventGroup ) = ioctl (g, PERF_EVENT_IOC_ENABLE)
0 commit comments