1 #define _XOPEN_SOURCE 700
4 #include <caml/alloc.h>
5 #include <caml/memory.h>
6 #include <caml/custom.h>
7 #include <caml/signals.h>
8 #include <caml/mlvalues.h>
9 #include <caml/bigarray.h>
17 #include <sys/sysinfo.h>
22 static void failwith_fmt (const char *fmt
, ...) Noreturn
;
23 static void failwith_fmt (const char *fmt
, ...)
29 vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
36 CAMLprim value
ml_waitalrm (value unit_v
)
43 sigaddset (&set
, SIGALRM
);
44 if (sigwait (&set
, &signr
)) {
45 failwith_fmt ("sigwait: %s", strerror (errno
));
47 CAMLreturn (Val_unit
);
50 CAMLprim value
ml_sysinfo (value unit_v
)
53 CAMLlocal2 (res_v
, loads_v
);
57 failwith_fmt ("sysinfo: %s", strerror (errno
));
60 loads_v
= caml_alloc_tuple (3);
61 Store_field (loads_v
, 0, caml_copy_int64 (si
.loads
[0]));
62 Store_field (loads_v
, 1, caml_copy_int64 (si
.loads
[1]));
63 Store_field (loads_v
, 2, caml_copy_int64 (si
.loads
[2]));
65 res_v
= caml_alloc_tuple (9);
66 Store_field (res_v
, 0, caml_copy_int64 (si
.uptime
));
67 Store_field (res_v
, 1, loads_v
);
68 Store_field (res_v
, 2, caml_copy_int64 (si
.totalram
));
69 Store_field (res_v
, 3, caml_copy_int64 (si
.freeram
));
70 Store_field (res_v
, 4, caml_copy_int64 (si
.sharedram
));
71 Store_field (res_v
, 5, caml_copy_int64 (si
.bufferram
));
72 Store_field (res_v
, 6, caml_copy_int64 (si
.totalswap
));
73 Store_field (res_v
, 7, caml_copy_int64 (si
.freeswap
));
74 Store_field (res_v
, 8, caml_copy_int64 (si
.procs
));
79 CAMLprim value
ml_get_nprocs (value unit_v
)
84 nprocs
= get_nprocs ();
86 failwith_fmt ("get_nprocs: %s", strerror (errno
));
89 CAMLreturn (Val_int (nprocs
));
92 CAMLprim value
ml_idletimeofday (value fd_v
, value nprocs_v
)
94 CAMLparam2 (fd_v
, nprocs_v
);
97 int fd
= Int_val (fd_v
);
98 int nprocs
= Int_val (nprocs_v
);
99 size_t n
= nprocs
* sizeof (tv
);
106 failwith_fmt ("alloca: %s", strerror (errno
));
109 m
= read (fd
, buf
, n
);
111 failwith_fmt ("read [n=%zu, m=%zi]: %s", n
, m
, strerror (errno
));
114 res_v
= caml_alloc (nprocs
* Double_wosize
, Double_array_tag
);
115 for (i
= 0; i
< nprocs
; ++i
) {
116 double d
= buf
[i
].tv_sec
+ buf
[i
].tv_usec
* 1e-6;
118 Store_double_field (res_v
, i
, d
);
123 CAMLprim value
ml_get_hz (value unit_v
)
126 CAMLreturn (Val_int (sysconf (_SC_CLK_TCK
)));
129 CAMLprim value
ml_nice (value nice_v
)
132 int niceval
= Int_val (nice_v
);
134 if (!nice (niceval
)) {
135 failwith_fmt ("nice %d: %s", niceval
, strerror (errno
));
138 CAMLreturn (Val_unit
);