From 14f861b9458b081ef1cfded5c8499de47182fb1d Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Mon, 30 Mar 2020 18:42:50 +0200 Subject: [PATCH] stats: instead of executing the sysctl kinfo_proc twice (for retrieving kinfo_mem and rusage), only execute it once --- stats/albatross_stat_client.ml | 3 +- stats/albatross_stats_pure.ml | 12 ++-- stats/albatross_stats_stubs.c | 116 ++++++++++++++------------------- 3 files changed, 57 insertions(+), 74 deletions(-) diff --git a/stats/albatross_stat_client.ml b/stats/albatross_stat_client.ml index 16232c3..da77942 100644 --- a/stats/albatross_stat_client.ml +++ b/stats/albatross_stat_client.ml @@ -4,9 +4,8 @@ open Albatross_stats_pure let old_rt = ref 0L let timer pid vmmapi interval = - let rusage = sysctl_rusage pid in + let rusage, kinfo_mem = sysctl_kinfo_proc pid in Logs.app (fun m -> m "sysctl rusage: %a" Stats.pp_rusage_mem rusage) ; - let kinfo_mem = sysctl_kinfo_mem pid in Logs.app (fun m -> m "kinfo mem: %a" Stats.pp_kinfo_mem kinfo_mem) ; let delta, pct = if !old_rt = 0L then diff --git a/stats/albatross_stats_pure.ml b/stats/albatross_stats_pure.ml index e702ba8..0673604 100644 --- a/stats/albatross_stats_pure.ml +++ b/stats/albatross_stats_pure.ml @@ -5,8 +5,8 @@ open Rresult.R.Infix open Vmm_core -external sysctl_rusage : int -> Stats.rusage = "vmmanage_sysctl_rusage" -external sysctl_kinfo_mem : int -> Stats.kinfo_mem = "vmmanage_sysctl_kinfo_mem" +external sysctl_kinfo_proc : int -> Stats.rusage * Stats.kinfo_mem = + "vmmanage_sysctl_kinfo_proc" external sysctl_ifcount : unit -> int = "vmmanage_sysctl_ifcount" external sysctl_ifdata : int -> Stats.ifdata = "vmmanage_sysctl_ifdata" @@ -100,8 +100,12 @@ let try_open_vmmapi pid_nic = pid_nic IM.empty let gather pid vmctx nics = - wrap sysctl_rusage pid, - wrap sysctl_kinfo_mem pid, + let ru, mem = + match wrap sysctl_kinfo_proc pid with + | None -> None, None + | Some (mem, ru) -> Some mem, Some ru + in + ru, mem, (match vmctx with | Error _ -> None | Ok vmctx -> wrap vmmapi_stats vmctx), diff --git a/stats/albatross_stats_stubs.c b/stats/albatross_stats_stubs.c index 9c1d8d7..c75a462 100644 --- a/stats/albatross_stats_stubs.c +++ b/stats/albatross_stats_stubs.c @@ -21,43 +21,9 @@ #include #include -CAMLprim value vmmanage_sysctl_kinfo_mem (value pid_r) { +CAMLprim value vmmanage_sysctl_kinfo_proc (value pid_r) { CAMLparam1(pid_r); - CAMLlocal2(res, start); - int name[4]; - int error; - size_t len; - struct kinfo_proc p; - - len = sizeof(p); - name[0] = CTL_KERN; - name[1] = KERN_PROC; - name[2] = KERN_PROC_PID; - name[3] = Int_val(pid_r); - - error = sysctl(name, nitems(name), &p, &len, NULL, 0); - if (error < 0) - uerror("sysctl", Nothing); - - res = caml_alloc(8, 0); - Store_field (res, 0, Val64(p.ki_size)); - Store_field (res, 1, Val64(p.ki_rssize)); - Store_field (res, 2, Val64(p.ki_tsize)); - Store_field (res, 3, Val64(p.ki_dsize)); - Store_field (res, 4, Val64(p.ki_ssize)); - Store_field (res, 5, Val64(p.ki_runtime)); - Store_field (res, 6, Val_int(p.ki_cow)); - start = caml_alloc(2, 0); - Store_field (start, 0, Val64(p.ki_start.tv_sec)); - Store_field (start, 1, Val_int(p.ki_start.tv_usec)); - Store_field (res, 7, start); - - CAMLreturn(res); -} - -CAMLprim value vmmanage_sysctl_rusage (value pid_r) { - CAMLparam1(pid_r); - CAMLlocal3(res, utime, stime); + CAMLlocal4(res, res1, res2, time); int name[4]; int error; size_t len; @@ -72,36 +38,55 @@ CAMLprim value vmmanage_sysctl_rusage (value pid_r) { error = sysctl(name, nitems(name), &p, &len, NULL, 0); if (error < 0) - uerror("sysctl", Nothing); + uerror("sysctl ctl_kern.kern_proc.kern_proc_pid", Nothing); + if (p.ki_start.tv_usec < 0 || p.ki_start.tv_usec > 999999999) + uerror("sysctl ctl_kern.kern_proc.kern_proc_pid", Nothing); + + res1 = caml_alloc(8, 0); + Store_field (res1, 0, Val64(p.ki_size)); + Store_field (res1, 1, Val64(p.ki_rssize)); + Store_field (res1, 2, Val64(p.ki_tsize)); + Store_field (res1, 3, Val64(p.ki_dsize)); + Store_field (res1, 4, Val64(p.ki_ssize)); + Store_field (res1, 5, Val64(p.ki_runtime)); + Store_field (res1, 6, Val_int(p.ki_cow)); + time = caml_alloc(2, 0); + Store_field (time, 0, Val64(p.ki_start.tv_sec)); + Store_field (time, 1, Val_int(p.ki_start.tv_usec)); + Store_field (res1, 7, time); ru = p.ki_rusage; if (ru.ru_utime.tv_usec < 0 || ru.ru_utime.tv_usec > 999999999 || ru.ru_stime.tv_usec < 0 || ru.ru_stime.tv_usec > 999999999) - uerror("sysctl", Nothing); + uerror("sysctl ctl_kern.kern_proc.kern_proc_pid", Nothing); - utime = caml_alloc(2, 0); - Store_field (utime, 0, Val64(ru.ru_utime.tv_sec)); - Store_field (utime, 1, Val_int(ru.ru_utime.tv_usec)); - stime = caml_alloc(2, 0); - Store_field (stime, 0, Val64(ru.ru_stime.tv_sec)); - Store_field (stime, 1, Val_int(ru.ru_stime.tv_usec)); - res = caml_alloc(16, 0); - Store_field (res, 0, utime); - Store_field (res, 1, stime); - Store_field (res, 2, Val64(ru.ru_maxrss)); - Store_field (res, 3, Val64(ru.ru_ixrss)); - Store_field (res, 4, Val64(ru.ru_idrss)); - Store_field (res, 5, Val64(ru.ru_isrss)); - Store_field (res, 6, Val64(ru.ru_minflt)); - Store_field (res, 7, Val64(ru.ru_majflt)); - Store_field (res, 8, Val64(ru.ru_nswap)); - Store_field (res, 9, Val64(ru.ru_inblock)); - Store_field (res, 10, Val64(ru.ru_oublock)); - Store_field (res, 11, Val64(ru.ru_msgsnd)); - Store_field (res, 12, Val64(ru.ru_msgrcv)); - Store_field (res, 13, Val64(ru.ru_nsignals)); - Store_field (res, 14, Val64(ru.ru_nvcsw)); - Store_field (res, 15, Val64(ru.ru_nivcsw)); + res2 = caml_alloc(16, 0); + time = caml_alloc(2, 0); + Store_field (time, 0, Val64(ru.ru_utime.tv_sec)); + Store_field (time, 1, Val_int(ru.ru_utime.tv_usec)); + Store_field (res2, 0, time); + time = caml_alloc(2, 0); + Store_field (time, 0, Val64(ru.ru_stime.tv_sec)); + Store_field (time, 1, Val_int(ru.ru_stime.tv_usec)); + Store_field (res2, 1, time); + Store_field (res2, 2, Val64(ru.ru_maxrss)); + Store_field (res2, 3, Val64(ru.ru_ixrss)); + Store_field (res2, 4, Val64(ru.ru_idrss)); + Store_field (res2, 5, Val64(ru.ru_isrss)); + Store_field (res2, 6, Val64(ru.ru_minflt)); + Store_field (res2, 7, Val64(ru.ru_majflt)); + Store_field (res2, 8, Val64(ru.ru_nswap)); + Store_field (res2, 9, Val64(ru.ru_inblock)); + Store_field (res2, 10, Val64(ru.ru_oublock)); + Store_field (res2, 11, Val64(ru.ru_msgsnd)); + Store_field (res2, 12, Val64(ru.ru_msgrcv)); + Store_field (res2, 13, Val64(ru.ru_nsignals)); + Store_field (res2, 14, Val64(ru.ru_nvcsw)); + Store_field (res2, 15, Val64(ru.ru_nivcsw)); + + res = caml_alloc(2, 0); + Store_field (res, 1, res1); + Store_field (res, 0, res2); CAMLreturn(res); } @@ -230,14 +215,9 @@ CAMLprim value vmmanage_sysctl_ifdata (value num) { /* stub symbols for OS currently not supported */ -CAMLprim value vmmanage_sysctl_rusage (value pid_r) { +CAMLprim value vmmanage_sysctl_kinfo_proc (value pid_r) { CAMLparam1(pid_r); - uerror("sysctl_rusage", Nothing); -} - -CAMLprim value vmmanage_sysctl_kinfo_mem (value pid_r) { - CAMLparam1(pid_r); - uerror("sysctl_kinfo_mem", Nothing); + uerror("sysctl_kinfo_proc", Nothing); } CAMLprim value vmmanage_sysctl_ifcount (value unit) {