-
Notifications
You must be signed in to change notification settings - Fork 89
/
platform.cpp
56 lines (47 loc) · 1.06 KB
/
platform.cpp
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
#include "platform.hpp"
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/resource.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#endif
#include "errors.hpp"
long get_num_avail_cpus() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
long get_page_size() {
return sysconf(_SC_PAGESIZE);
}
size_t calc_memsize() {
size_t mem;
#ifdef __APPLE__
int64_t hw_memsize;
size_t len = sizeof(int64_t);
if (sysctlbyname("hw.memsize", &hw_memsize, &len, NULL, 0) < 0) {
perror("sysctl hw.memsize");
exit(EXIT_MEMORY);
}
mem = hw_memsize;
#else
long long pagesize = sysconf(_SC_PAGESIZE);
long long pages = sysconf(_SC_PHYS_PAGES);
if (pages < 0 || pagesize < 0) {
perror("sysconf _SC_PAGESIZE or _SC_PHYS_PAGES");
exit(EXIT_MEMORY);
}
mem = (long long) pages * pagesize;
#endif
return mem;
}
size_t get_max_open_files() {
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
perror("getrlimit");
exit(EXIT_PTHREAD);
}
return rl.rlim_cur;
}