2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
10 #define CAML_NAME_SPACE
11 #include <caml/alloc.h>
12 #include <caml/fail.h>
13 #include <caml/memory.h>
14 #include <caml/mlvalues.h>
18 #include <sys/sysinfo.h>
27 // This function returns an option of a 9-int-member struct
28 value
hh_sysinfo(void) {
31 CAMLlocal2(result
, some
);
32 result
= caml_alloc_tuple(9);
33 struct sysinfo info
= {0}; // this initializes all members to 0
34 if (sysinfo(&info
) != 0) {
35 caml_failwith("Failed to sysinfo()");
37 Store_field(result
, 0, Val_long(info
.uptime
));
38 Store_field(result
, 1, Val_long(info
.totalram
* info
.mem_unit
));
39 Store_field(result
, 2, Val_long(info
.freeram
* info
.mem_unit
));
40 Store_field(result
, 3, Val_long(info
.sharedram
* info
.mem_unit
));
41 Store_field(result
, 4, Val_long(info
.bufferram
* info
.mem_unit
));
42 Store_field(result
, 5, Val_long(info
.totalswap
* info
.mem_unit
));
43 Store_field(result
, 6, Val_long(info
.freeswap
* info
.mem_unit
));
44 Store_field(result
, 7, Val_long(info
.totalhigh
* info
.mem_unit
));
45 Store_field(result
, 8, Val_long(info
.freehigh
* info
.mem_unit
));
46 some
= caml_alloc(1, 0);
47 Store_field(some
, 0, result
);
50 CAMLreturn(Val_int(0)); // None
54 CAMLprim value
hh_sysinfo_is_apple_os(void) {
63 value
hh_nproc(void) {
68 GetSystemInfo(&sysinfo
);
69 result
= Val_long(sysinfo
.dwNumberOfProcessors
);
71 result
= Val_long(sysconf(_SC_NPROCESSORS_ONLN
));
77 * There are a bunch of functions that you expect to return a pid,
78 * like Unix.getpid() and Unix.create_process(). However, on
79 * Windows, instead of returning the process ID, they return a
82 * Process handles seem act like pointers to a process. You can have
83 * more than one handle that points to a single process (unlike
84 * pids, where there is a single pid for a process).
86 * This isn't a problem normally, since functons like Unix.waitpid()
87 * will take the process handle on Windows. But if you want to print
88 * or log the pid, then you need to dereference the handle and get
89 * the pid. And that's what this function does.
91 value
pid_of_handle(value handle
) {
94 CAMLreturn(Val_int(GetProcessId((HANDLE
)Long_val(handle
))));
100 value
handle_of_pid_for_termination(value pid
) {
103 CAMLreturn(Val_int(OpenProcess(PROCESS_TERMINATE
, FALSE
, Int_val(pid
))));