Skip to content

Commit

Permalink
address @palainp review comment, and close the vcpu on teardown
Browse files Browse the repository at this point in the history
This pushes a vmctx * vcpu pair through to the OCaml layer.
  • Loading branch information
hannesm committed Jan 10, 2024
1 parent c974250 commit 057463f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
14 changes: 8 additions & 6 deletions stats/albatross_stats_pure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ external sysctl_ifdata : int -> Stats.ifdata = "vmmanage_sysctl_ifdata"

type vmctx

external vmmapi_open : string -> vmctx = "vmmanage_vmmapi_open"
external vmmapi_close : vmctx -> unit = "vmmanage_vmmapi_close"
external vmmapi_stats : vmctx -> (string * int64) list = "vmmanage_vmmapi_stats"
type vcpu

external vmmapi_open : string -> (vmctx * vcpu) = "vmmanage_vmmapi_open"
external vmmapi_close : vmctx -> vcpu -> unit = "vmmanage_vmmapi_close"
external vmmapi_stats : vmctx -> vcpu -> (string * int64) list = "vmmanage_vmmapi_stats"

type 'a t = {
pid_nic : ((vmctx, int) result * string * (string * int * string) list) IM.t ;
pid_nic : ((vmctx * vcpu, int) result * string * (string * int * string) list) IM.t ;
vmid_pid : int Vmm_trie.t ;
name_sockets : 'a Vmm_trie.t ;
}
Expand Down Expand Up @@ -51,7 +53,7 @@ let remove_vmid t vmid =
| Some pid ->
Logs.info (fun m -> m "removing pid %d" pid) ;
(match IM.find_opt pid t.pid_nic with
| Some (Ok vmctx, _, _) -> ignore (wrap vmmapi_close vmctx) ; vmmapi `Close
| Some (Ok (vmctx, vcpu), _, _) -> ignore (wrap (vmmapi_close vmctx) vcpu) ; vmmapi `Close
| _ -> ()) ;
let pid_nic = IM.remove pid t.pid_nic
and vmid_pid = Vmm_trie.remove vmid t.vmid_pid
Expand Down Expand Up @@ -204,7 +206,7 @@ let gather pid vmctx nics =
ru, mem,
(match vmctx with
| Error _ -> None
| Ok vmctx -> wrap vmmapi_stats vmctx),
| Ok (vmctx, vcpu) -> wrap (vmmapi_stats vmctx) vcpu),
List.fold_left (fun ifd (bridge, nic, nname) ->
match wrap sysctl_ifdata nic with
| None ->
Expand Down
24 changes: 19 additions & 5 deletions stats/albatross_stats_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,48 @@ CAMLprim value vmmanage_vmmapi_open (value name) {
CAMLparam1(name);
struct vmctx *ctx;
const char *devname;
CAMLlocal1(res);

if (! caml_string_is_c_safe(name)) caml_raise_not_found();

devname = String_val(name);
ctx = vm_open(devname);
if (ctx == NULL) uerror("vm_open", Nothing);
CAMLreturn((value)ctx);
struct vcpu *vcpu;
#if __FreeBSD_version >= 1400000
vcpu = vm_vcpu_open(ctx, 0);
#else
vcpu = NULL;
#endif
res = caml_alloc(2, 0);
Store_field (res, 1, (value)ctx);
Store_field (res, 0, (value)vcpu);
CAMLreturn(res);
}

CAMLprim value vmmanage_vmmapi_close (value octx) {
CAMLprim value vmmanage_vmmapi_close (value octx, value ovcpu) {
struct vmctx *ctx = (struct vmctx*)octx;
struct vcpu *vcpu = (struct vcpu*)ovcpu;

#if __FreeBSD_version >= 1400000
vm_vcpu_close(vcpu);
#endif

close(vm_get_device_fd(ctx));
free(ctx);
return Val_unit;
}

CAMLprim value vmmanage_vmmapi_stats (value octx) {
CAMLprim value vmmanage_vmmapi_stats (value octx, value ovcpu) {
CAMLparam0();
CAMLlocal3(res, tmp, pair);
int i, num_stats;
uint64_t *stats;
const char *desc;
struct vmctx *ctx = (struct vmctx*)octx;
struct vcpu *vcpu = (struct vcpu*)ovcpu;

#if __FreeBSD_version >= 1400000
struct vcpu *vcpu;
vcpu = vm_vcpu_open(ctx, 0);
stats = vm_get_stats(vcpu, NULL, &num_stats);
#else
stats = vm_get_stats(ctx, 0, NULL, &num_stats);
Expand Down

0 comments on commit 057463f

Please sign in to comment.