this repo has no description
at main 1.0 kB view raw
1open Libbpf 2open Ctypes 3 4module RingBuffer = struct 5 type t = [ `Ring_buffer ] structure ptr 6 7 type callback = 8 unit Ctypes_static.ptr -> unit Ctypes_static.ptr -> Unsigned.size_t -> int 9 10 let init bpf_map ~callback f = 11 (* Coerce it to the static_funptr so it can be passed to the C function *) 12 let callback_c = 13 coerce 14 (Foreign.funptr ~runtime_lock:true ~check_errno:true 15 (ptr void @-> ptr void @-> size_t @-> returning int)) 16 C.Types.ring_buffer_sample_fn callback 17 in 18 let rb = 19 match 20 C.Functions.ring_buffer__new bpf_map.fd callback_c null 21 (from_voidp C.Types.ring_buffer_opts null) 22 with 23 | None -> failwith "Failed to create ring buffer\n" 24 | Some rb -> rb 25 in 26 try f rb 27 with e -> 28 C.Functions.ring_buffer__free rb; 29 raise e 30 31 let poll t ~timeout = C.Functions.ring_buffer__poll t timeout 32 let consume t = C.Functions.ring_buffer__consume t 33 let get_epoll_fd t = C.Functions.ring_buffer__epoll_fd t 34end