File: util.c

package info (click to toggle)
lz4 1.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,496 kB
  • sloc: ansic: 19,102; makefile: 1,060; python: 812; sh: 486; cpp: 173
file content (165 lines) | stat: -rw-r--r-- 4,103 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
    util.h - utility functions
    Copyright (C) 2023, Yann Collet

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#if defined (__cplusplus)
extern "C" {
#endif


/*-****************************************
*  Dependencies
******************************************/
#include "util.h"   /* note : ensure that platform.h is included first ! */

/*-****************************************
*  count the number of cores
******************************************/

#if defined(_WIN32)

#include <windows.h>

int UTIL_countCores(void)
{
    static int numCores = 0;
    if (numCores != 0) return numCores;

    {   SYSTEM_INFO sysinfo;
        GetSystemInfo(&sysinfo);
        numCores = sysinfo.dwNumberOfProcessors;
    }

    if (numCores == 0) {
        /* Unexpected result, fall back on 1 */
        return numCores = 1;
    }

    return numCores;
}

#elif defined(__APPLE__)

#include <sys/sysctl.h>

/* Use apple-provided syscall
 * see: man 3 sysctl */
int UTIL_countCores(void)
{
    static S32 numCores = 0; /* apple specifies int32_t */
    if (numCores != 0) return (int)numCores;

    {   size_t size = sizeof(S32);
        int const ret = sysctlbyname("hw.logicalcpu", &numCores, &size, NULL, 0);
        if (ret != 0) {
            /* error: fall back on 1 */
            numCores = 1;
        }
    }
    return (int)numCores;
}

#elif defined(__linux__)

int UTIL_countCores(void)
{
    static int numCores = 0;

    if (numCores != 0) return numCores;

    numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
    if (numCores == -1) {
        /* value not queryable, fall back on 1 */
        return numCores = 1;
    }

    return numCores;
}

#elif defined(__FreeBSD__)

#include <stdio.h>  /* perror */
#include <errno.h>
#include <sys/param.h>
#include <sys/sysctl.h>

/* Use physical core sysctl when available
 * see: man 4 smp, man 3 sysctl */
int UTIL_countCores(void)
{
    static int numCores = 0; /* freebsd sysctl is native int sized */
    if (numCores != 0) return numCores;

#if __FreeBSD_version >= 1300008
    {   size_t size = sizeof(numCores);
        int ret = sysctlbyname("kern.smp.cores", &numCores, &size, NULL, 0);
        if (ret == 0) {
            int perCore = 1;
            ret = sysctlbyname("kern.smp.threads_per_core", &perCore, &size, NULL, 0);
            /* default to physical cores if logical cannot be read */
            if (ret != 0) /* error */
                return numCores;
            numCores *= perCore;
            return numCores;
        }
        if (errno != ENOENT) {
            perror("lz4: can't get number of cpus");
            exit(1);
        }
        /* sysctl not present, fall through to older sysconf method */
    }
#endif

    numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
    if (numCores == -1) {
        /* value not queryable, fall back on 1 */
        numCores = 1;
    }
    return numCores;
}

#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)

/* Use POSIX sysconf
 * see: man 3 sysconf */
int UTIL_countCores(void)
{
    static int numCores = 0;
    if (numCores != 0) return numCores;

    numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
    if (numCores == -1) {
        /* value not queryable, fall back on 1 */
        numCores = 1;
    }
    return numCores;
}

#else

int UTIL_countCores(void)
{
    /* no clue */
    return 1;
}

#endif

#if defined (__cplusplus)
}
#endif