Coverage Report

Created: 2017-05-25 16:17

/home/travis/build/MoarVM/MoarVM/src/platform/posix/sys.c
Line
Count
Source (jump to first uncovered line)
1
#define _GNU_SOURCE
2
#include "moar.h"
3
#include "platform/sys.h"
4
5
#if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 6) || defined(__FreeBSD_kernel__)
6
7
#include <unistd.h>
8
9
#if defined _SC_NPROCESSORS_ONLN
10
#  define SYSCONF_ARG _SC_NPROCESSORS_ONLN
11
#elif defined _SC_NPROC_ONLN
12
#  define SYSCONF_ARG _SC_NPROC_ONLN
13
#else
14
#  include <sys/sysctl.h>
15
#  if defined HW_AVAILCPU
16
#    define SYSCTL_ARG HW_AVAILCPU
17
#  elif defined HW_NCPU
18
#    define SYSCTL_ARG HW_NCPU
19
#  endif
20
#endif
21
22
#if defined SYSCONF_ARG
23
24
MVMuint32 MVM_platform_cpu_count(void) {
25
    long count = sysconf(SYSCONF_ARG);
26
    if (count < 0)
27
        return 0;
28
29
    return (MVMuint32)count;
30
}
31
32
#elif defined SYSCTL_ARG
33
34
MVMuint32 MVM_platform_cpu_count(void) {
35
    int mib[2] = { CTL_HW, SYSCTL_ARG };
36
    int count;
37
    int size = sizeof count;
38
39
    if (sysctl(mib, 2, &count, &size, NULL, 0) != 0)
40
        return 0;
41
42
    return (MVMuint32)count;
43
}
44
45
#else
46
47
#error "Unsupported platform"
48
49
#endif
50
51
#else
52
53
0
MVMuint32 MVM_platform_cpu_count(void) {
54
0
    cpu_set_t set;
55
0
56
0
    if (pthread_getaffinity_np(pthread_self(), sizeof set, &set) != 0)
57
0
        return 0;
58
0
59
0
    return CPU_COUNT(&set);
60
0
}
61
62
#endif