Gather a bit more information from sysinfo()
[hiphop-php.git] / hphp / hack / src / utils / sys / sysinfo.c
blob063958313fb5f164e1e2ed008bf7e0bd406b4f8f
1 /**
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 */
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>
16 #include <assert.h>
17 #ifdef __linux__
18 #include <sys/sysinfo.h>
19 #endif
21 #ifdef _WIN32
22 #include <windows.h>
23 #else
24 #include <unistd.h>
25 #endif
27 // This function returns an option of a 9-int-member struct
28 value hh_sysinfo(void) {
29 CAMLparam0();
30 #ifdef __linux__
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);
48 CAMLreturn(some);
49 #else
50 CAMLreturn(Val_int(0)); // None
51 #endif
54 CAMLprim value hh_sysinfo_is_apple_os(void) {
55 CAMLparam0();
56 #ifdef __APPLE__
57 return Val_bool(1);
58 #else
59 return Val_bool(0);
60 #endif
63 value hh_nproc(void) {
64 CAMLparam0();
65 CAMLlocal1(result);
66 #ifdef _WIN32
67 SYSTEM_INFO sysinfo;
68 GetSystemInfo(&sysinfo);
69 result = Val_long(sysinfo.dwNumberOfProcessors);
70 #else
71 result = Val_long(sysconf(_SC_NPROCESSORS_ONLN));
72 #endif
73 CAMLreturn(result);
76 /**
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
80 * process handle.
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) {
92 CAMLparam1(handle);
93 #ifdef _WIN32
94 CAMLreturn(Val_int(GetProcessId((HANDLE)Long_val(handle))));
95 #else
96 CAMLreturn(handle);
97 #endif
100 value handle_of_pid_for_termination(value pid) {
101 CAMLparam1(pid);
102 #ifdef _WIN32
103 CAMLreturn(Val_int(OpenProcess(PROCESS_TERMINATE, FALSE, Int_val(pid))));
104 #else
105 CAMLreturn(pid);
106 #endif