forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarwin.c
63 lines (56 loc) · 2.03 KB
/
darwin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <mach/mach.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include "platform/platform.h"
struct cpu_usage get_cpu_usage() {
host_cpu_load_info_data_t load;
mach_msg_type_number_t fuck = HOST_CPU_LOAD_INFO_COUNT;
host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t) &load, &fuck);
struct cpu_usage usage;
usage.user_ticks = load.cpu_ticks[CPU_STATE_USER];
usage.system_ticks = load.cpu_ticks[CPU_STATE_SYSTEM];
usage.idle_ticks = load.cpu_ticks[CPU_STATE_IDLE];
usage.nice_ticks = load.cpu_ticks[CPU_STATE_NICE];
return usage;
}
struct mem_usage get_mem_usage() {
host_basic_info_data_t basic = {};
mach_msg_type_number_t fuck = HOST_BASIC_INFO_COUNT;
kern_return_t status = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &basic, &fuck);
vm_statistics64_data_t vm = {};
fuck = HOST_VM_INFO64_COUNT;
status = host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t) &vm, &fuck);
struct mem_usage usage;
usage.total = basic.max_mem;
usage.free = vm.free_count * vm_page_size;
usage.active = vm.active_count * vm_page_size;
usage.inactive = vm.inactive_count * vm_page_size;
return usage;
}
struct uptime_info get_uptime() {
uint64_t kern_boottime[2];
size_t size = sizeof(kern_boottime);
sysctlbyname("kern.boottime", &kern_boottime, &size, NULL, 0);
struct timeval now;
gettimeofday(&now, NULL);
struct {
uint32_t ldavg[3];
long scale;
} vm_loadavg;
size = sizeof(vm_loadavg);
sysctlbyname("vm.loadavg", &vm_loadavg, &size, NULL, 0);
// linux wants the scale to be 16 bits
for (int i = 0; i < 3; i++) {
if (FSHIFT < 16)
vm_loadavg.ldavg[i] <<= 16 - FSHIFT;
else
vm_loadavg.ldavg[i] >>= FSHIFT - 16;
}
struct uptime_info uptime = {
.uptime_ticks = now.tv_sec - kern_boottime[0],
.load_1m = vm_loadavg.ldavg[0],
.load_5m = vm_loadavg.ldavg[1],
.load_15m = vm_loadavg.ldavg[2],
};
return uptime;
}