xref: /freebsd/sys/contrib/zstd/programs/util.h (revision 0c16b53773565120a8f80a31a0af2ef56ccd368e)
1*0c16b537SWarner Losh /*
2*0c16b537SWarner Losh  * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
3*0c16b537SWarner Losh  * All rights reserved.
4*0c16b537SWarner Losh  *
5*0c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
6*0c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*0c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
8*0c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
9*0c16b537SWarner Losh  */
10*0c16b537SWarner Losh 
11*0c16b537SWarner Losh #ifndef UTIL_H_MODULE
12*0c16b537SWarner Losh #define UTIL_H_MODULE
13*0c16b537SWarner Losh 
14*0c16b537SWarner Losh #if defined (__cplusplus)
15*0c16b537SWarner Losh extern "C" {
16*0c16b537SWarner Losh #endif
17*0c16b537SWarner Losh 
18*0c16b537SWarner Losh 
19*0c16b537SWarner Losh 
20*0c16b537SWarner Losh /*-****************************************
21*0c16b537SWarner Losh *  Dependencies
22*0c16b537SWarner Losh ******************************************/
23*0c16b537SWarner Losh #include "platform.h"     /* PLATFORM_POSIX_VERSION */
24*0c16b537SWarner Losh #include <stdlib.h>       /* malloc */
25*0c16b537SWarner Losh #include <stddef.h>       /* size_t, ptrdiff_t */
26*0c16b537SWarner Losh #include <stdio.h>        /* fprintf */
27*0c16b537SWarner Losh #include <string.h>       /* strncmp */
28*0c16b537SWarner Losh #include <sys/types.h>    /* stat, utime */
29*0c16b537SWarner Losh #include <sys/stat.h>     /* stat */
30*0c16b537SWarner Losh #if defined(_MSC_VER)
31*0c16b537SWarner Losh #  include <sys/utime.h>  /* utime */
32*0c16b537SWarner Losh #  include <io.h>         /* _chmod */
33*0c16b537SWarner Losh #else
34*0c16b537SWarner Losh #  include <unistd.h>     /* chown, stat */
35*0c16b537SWarner Losh #  include <utime.h>      /* utime */
36*0c16b537SWarner Losh #endif
37*0c16b537SWarner Losh #include <time.h>         /* time */
38*0c16b537SWarner Losh #include <errno.h>
39*0c16b537SWarner Losh #include "mem.h"          /* U32, U64 */
40*0c16b537SWarner Losh 
41*0c16b537SWarner Losh 
42*0c16b537SWarner Losh /* ************************************************************
43*0c16b537SWarner Losh * Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
44*0c16b537SWarner Losh ***************************************************************/
45*0c16b537SWarner Losh #if defined(_MSC_VER) && (_MSC_VER >= 1400)
46*0c16b537SWarner Losh #   define UTIL_fseek _fseeki64
47*0c16b537SWarner Losh #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
48*0c16b537SWarner Losh #  define UTIL_fseek fseeko
49*0c16b537SWarner Losh #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
50*0c16b537SWarner Losh #   define UTIL_fseek fseeko64
51*0c16b537SWarner Losh #else
52*0c16b537SWarner Losh #   define UTIL_fseek fseek
53*0c16b537SWarner Losh #endif
54*0c16b537SWarner Losh 
55*0c16b537SWarner Losh 
56*0c16b537SWarner Losh /*-****************************************
57*0c16b537SWarner Losh *  Sleep functions: Windows - Posix - others
58*0c16b537SWarner Losh ******************************************/
59*0c16b537SWarner Losh #if defined(_WIN32)
60*0c16b537SWarner Losh #  include <windows.h>
61*0c16b537SWarner Losh #  define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
62*0c16b537SWarner Losh #  define UTIL_sleep(s) Sleep(1000*s)
63*0c16b537SWarner Losh #  define UTIL_sleepMilli(milli) Sleep(milli)
64*0c16b537SWarner Losh #elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
65*0c16b537SWarner Losh #  include <unistd.h>
66*0c16b537SWarner Losh #  include <sys/resource.h> /* setpriority */
67*0c16b537SWarner Losh #  include <time.h>         /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
68*0c16b537SWarner Losh #  if defined(PRIO_PROCESS)
69*0c16b537SWarner Losh #    define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
70*0c16b537SWarner Losh #  else
71*0c16b537SWarner Losh #    define SET_REALTIME_PRIORITY /* disabled */
72*0c16b537SWarner Losh #  endif
73*0c16b537SWarner Losh #  define UTIL_sleep(s) sleep(s)
74*0c16b537SWarner Losh #  if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L)  /* nanosleep requires POSIX.1-2001 */
75*0c16b537SWarner Losh #      define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
76*0c16b537SWarner Losh #  else
77*0c16b537SWarner Losh #      define UTIL_sleepMilli(milli) /* disabled */
78*0c16b537SWarner Losh #  endif
79*0c16b537SWarner Losh #else
80*0c16b537SWarner Losh #  define SET_REALTIME_PRIORITY      /* disabled */
81*0c16b537SWarner Losh #  define UTIL_sleep(s)          /* disabled */
82*0c16b537SWarner Losh #  define UTIL_sleepMilli(milli) /* disabled */
83*0c16b537SWarner Losh #endif
84*0c16b537SWarner Losh 
85*0c16b537SWarner Losh 
86*0c16b537SWarner Losh /* *************************************
87*0c16b537SWarner Losh *  Constants
88*0c16b537SWarner Losh ***************************************/
89*0c16b537SWarner Losh #define LIST_SIZE_INCREASE   (8*1024)
90*0c16b537SWarner Losh 
91*0c16b537SWarner Losh 
92*0c16b537SWarner Losh /*-****************************************
93*0c16b537SWarner Losh *  Compiler specifics
94*0c16b537SWarner Losh ******************************************/
95*0c16b537SWarner Losh #if defined(__INTEL_COMPILER)
96*0c16b537SWarner Losh #  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
97*0c16b537SWarner Losh #endif
98*0c16b537SWarner Losh #if defined(__GNUC__)
99*0c16b537SWarner Losh #  define UTIL_STATIC static __attribute__((unused))
100*0c16b537SWarner Losh #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
101*0c16b537SWarner Losh #  define UTIL_STATIC static inline
102*0c16b537SWarner Losh #elif defined(_MSC_VER)
103*0c16b537SWarner Losh #  define UTIL_STATIC static __inline
104*0c16b537SWarner Losh #else
105*0c16b537SWarner Losh #  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
106*0c16b537SWarner Losh #endif
107*0c16b537SWarner Losh 
108*0c16b537SWarner Losh 
109*0c16b537SWarner Losh /*-****************************************
110*0c16b537SWarner Losh *  Console log
111*0c16b537SWarner Losh ******************************************/
112*0c16b537SWarner Losh static int g_utilDisplayLevel;
113*0c16b537SWarner Losh #define UTIL_DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
114*0c16b537SWarner Losh #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
115*0c16b537SWarner Losh 
116*0c16b537SWarner Losh 
117*0c16b537SWarner Losh /*-****************************************
118*0c16b537SWarner Losh *  Time functions
119*0c16b537SWarner Losh ******************************************/
120*0c16b537SWarner Losh #if defined(_WIN32)   /* Windows */
121*0c16b537SWarner Losh     typedef LARGE_INTEGER UTIL_time_t;
122*0c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
123*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
124*0c16b537SWarner Losh     {
125*0c16b537SWarner Losh         static LARGE_INTEGER ticksPerSecond;
126*0c16b537SWarner Losh         static int init = 0;
127*0c16b537SWarner Losh         if (!init) {
128*0c16b537SWarner Losh             if (!QueryPerformanceFrequency(&ticksPerSecond))
129*0c16b537SWarner Losh                 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
130*0c16b537SWarner Losh             init = 1;
131*0c16b537SWarner Losh         }
132*0c16b537SWarner Losh         return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
133*0c16b537SWarner Losh     }
134*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
135*0c16b537SWarner Losh     {
136*0c16b537SWarner Losh         static LARGE_INTEGER ticksPerSecond;
137*0c16b537SWarner Losh         static int init = 0;
138*0c16b537SWarner Losh         if (!init) {
139*0c16b537SWarner Losh             if (!QueryPerformanceFrequency(&ticksPerSecond))
140*0c16b537SWarner Losh                 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
141*0c16b537SWarner Losh             init = 1;
142*0c16b537SWarner Losh         }
143*0c16b537SWarner Losh         return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
144*0c16b537SWarner Losh     }
145*0c16b537SWarner Losh #elif defined(__APPLE__) && defined(__MACH__)
146*0c16b537SWarner Losh     #include <mach/mach_time.h>
147*0c16b537SWarner Losh     typedef U64 UTIL_time_t;
148*0c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
149*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
150*0c16b537SWarner Losh     {
151*0c16b537SWarner Losh         static mach_timebase_info_data_t rate;
152*0c16b537SWarner Losh         static int init = 0;
153*0c16b537SWarner Losh         if (!init) {
154*0c16b537SWarner Losh             mach_timebase_info(&rate);
155*0c16b537SWarner Losh             init = 1;
156*0c16b537SWarner Losh         }
157*0c16b537SWarner Losh         return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL;
158*0c16b537SWarner Losh     }
159*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
160*0c16b537SWarner Losh     {
161*0c16b537SWarner Losh         static mach_timebase_info_data_t rate;
162*0c16b537SWarner Losh         static int init = 0;
163*0c16b537SWarner Losh         if (!init) {
164*0c16b537SWarner Losh             mach_timebase_info(&rate);
165*0c16b537SWarner Losh             init = 1;
166*0c16b537SWarner Losh         }
167*0c16b537SWarner Losh         return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
168*0c16b537SWarner Losh     }
169*0c16b537SWarner Losh #elif (PLATFORM_POSIX_VERSION >= 200112L)
170*0c16b537SWarner Losh     #include <time.h>
171*0c16b537SWarner Losh     typedef struct timespec UTIL_freq_t;
172*0c16b537SWarner Losh     typedef struct timespec UTIL_time_t;
173*0c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void)
174*0c16b537SWarner Losh     {
175*0c16b537SWarner Losh         UTIL_time_t time;
176*0c16b537SWarner Losh         if (clock_gettime(CLOCK_MONOTONIC, &time))
177*0c16b537SWarner Losh             UTIL_DISPLAYLEVEL(1, "ERROR: Failed to get time\n");   /* we could also exit() */
178*0c16b537SWarner Losh         return time;
179*0c16b537SWarner Losh     }
180*0c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
181*0c16b537SWarner Losh     {
182*0c16b537SWarner Losh         UTIL_time_t diff;
183*0c16b537SWarner Losh         if (end.tv_nsec < begin.tv_nsec) {
184*0c16b537SWarner Losh             diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
185*0c16b537SWarner Losh             diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
186*0c16b537SWarner Losh         } else {
187*0c16b537SWarner Losh             diff.tv_sec = end.tv_sec - begin.tv_sec;
188*0c16b537SWarner Losh             diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
189*0c16b537SWarner Losh         }
190*0c16b537SWarner Losh         return diff;
191*0c16b537SWarner Losh     }
192*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
193*0c16b537SWarner Losh     {
194*0c16b537SWarner Losh         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
195*0c16b537SWarner Losh         U64 micro = 0;
196*0c16b537SWarner Losh         micro += 1000000ULL * diff.tv_sec;
197*0c16b537SWarner Losh         micro += diff.tv_nsec / 1000ULL;
198*0c16b537SWarner Losh         return micro;
199*0c16b537SWarner Losh     }
200*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
201*0c16b537SWarner Losh     {
202*0c16b537SWarner Losh         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
203*0c16b537SWarner Losh         U64 nano = 0;
204*0c16b537SWarner Losh         nano += 1000000000ULL * diff.tv_sec;
205*0c16b537SWarner Losh         nano += diff.tv_nsec;
206*0c16b537SWarner Losh         return nano;
207*0c16b537SWarner Losh     }
208*0c16b537SWarner Losh #else   /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
209*0c16b537SWarner Losh     typedef clock_t UTIL_time_t;
210*0c16b537SWarner Losh     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
211*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
212*0c16b537SWarner Losh     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
213*0c16b537SWarner Losh #endif
214*0c16b537SWarner Losh 
215*0c16b537SWarner Losh 
216*0c16b537SWarner Losh /* returns time span in microseconds */
217*0c16b537SWarner Losh UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart )
218*0c16b537SWarner Losh {
219*0c16b537SWarner Losh     UTIL_time_t const clockEnd = UTIL_getTime();
220*0c16b537SWarner Losh     return UTIL_getSpanTimeMicro(clockStart, clockEnd);
221*0c16b537SWarner Losh }
222*0c16b537SWarner Losh 
223*0c16b537SWarner Losh 
224*0c16b537SWarner Losh UTIL_STATIC void UTIL_waitForNextTick(void)
225*0c16b537SWarner Losh {
226*0c16b537SWarner Losh     UTIL_time_t const clockStart = UTIL_getTime();
227*0c16b537SWarner Losh     UTIL_time_t clockEnd;
228*0c16b537SWarner Losh     do {
229*0c16b537SWarner Losh         clockEnd = UTIL_getTime();
230*0c16b537SWarner Losh     } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
231*0c16b537SWarner Losh }
232*0c16b537SWarner Losh 
233*0c16b537SWarner Losh 
234*0c16b537SWarner Losh 
235*0c16b537SWarner Losh /*-****************************************
236*0c16b537SWarner Losh *  File functions
237*0c16b537SWarner Losh ******************************************/
238*0c16b537SWarner Losh #if defined(_MSC_VER)
239*0c16b537SWarner Losh     #define chmod _chmod
240*0c16b537SWarner Losh     typedef struct __stat64 stat_t;
241*0c16b537SWarner Losh #else
242*0c16b537SWarner Losh     typedef struct stat stat_t;
243*0c16b537SWarner Losh #endif
244*0c16b537SWarner Losh 
245*0c16b537SWarner Losh 
246*0c16b537SWarner Losh UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
247*0c16b537SWarner Losh {
248*0c16b537SWarner Losh     int res = 0;
249*0c16b537SWarner Losh     struct utimbuf timebuf;
250*0c16b537SWarner Losh 
251*0c16b537SWarner Losh     timebuf.actime = time(NULL);
252*0c16b537SWarner Losh     timebuf.modtime = statbuf->st_mtime;
253*0c16b537SWarner Losh     res += utime(filename, &timebuf);  /* set access and modification times */
254*0c16b537SWarner Losh 
255*0c16b537SWarner Losh #if !defined(_WIN32)
256*0c16b537SWarner Losh     res += chown(filename, statbuf->st_uid, statbuf->st_gid);  /* Copy ownership */
257*0c16b537SWarner Losh #endif
258*0c16b537SWarner Losh 
259*0c16b537SWarner Losh     res += chmod(filename, statbuf->st_mode & 07777);  /* Copy file permissions */
260*0c16b537SWarner Losh 
261*0c16b537SWarner Losh     errno = 0;
262*0c16b537SWarner Losh     return -res; /* number of errors is returned */
263*0c16b537SWarner Losh }
264*0c16b537SWarner Losh 
265*0c16b537SWarner Losh 
266*0c16b537SWarner Losh UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
267*0c16b537SWarner Losh {
268*0c16b537SWarner Losh     int r;
269*0c16b537SWarner Losh #if defined(_MSC_VER)
270*0c16b537SWarner Losh     r = _stat64(infilename, statbuf);
271*0c16b537SWarner Losh     if (r || !(statbuf->st_mode & S_IFREG)) return 0;   /* No good... */
272*0c16b537SWarner Losh #else
273*0c16b537SWarner Losh     r = stat(infilename, statbuf);
274*0c16b537SWarner Losh     if (r || !S_ISREG(statbuf->st_mode)) return 0;   /* No good... */
275*0c16b537SWarner Losh #endif
276*0c16b537SWarner Losh     return 1;
277*0c16b537SWarner Losh }
278*0c16b537SWarner Losh 
279*0c16b537SWarner Losh 
280*0c16b537SWarner Losh UTIL_STATIC int UTIL_isRegularFile(const char* infilename)
281*0c16b537SWarner Losh {
282*0c16b537SWarner Losh     stat_t statbuf;
283*0c16b537SWarner Losh     return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
284*0c16b537SWarner Losh }
285*0c16b537SWarner Losh 
286*0c16b537SWarner Losh 
287*0c16b537SWarner Losh UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
288*0c16b537SWarner Losh {
289*0c16b537SWarner Losh     int r;
290*0c16b537SWarner Losh     stat_t statbuf;
291*0c16b537SWarner Losh #if defined(_MSC_VER)
292*0c16b537SWarner Losh     r = _stat64(infilename, &statbuf);
293*0c16b537SWarner Losh     if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
294*0c16b537SWarner Losh #else
295*0c16b537SWarner Losh     r = stat(infilename, &statbuf);
296*0c16b537SWarner Losh     if (!r && S_ISDIR(statbuf.st_mode)) return 1;
297*0c16b537SWarner Losh #endif
298*0c16b537SWarner Losh     return 0;
299*0c16b537SWarner Losh }
300*0c16b537SWarner Losh 
301*0c16b537SWarner Losh UTIL_STATIC U32 UTIL_isLink(const char* infilename)
302*0c16b537SWarner Losh {
303*0c16b537SWarner Losh #if defined(_WIN32)
304*0c16b537SWarner Losh     /* no symlinks on windows */
305*0c16b537SWarner Losh     (void)infilename;
306*0c16b537SWarner Losh #else
307*0c16b537SWarner Losh     int r;
308*0c16b537SWarner Losh     stat_t statbuf;
309*0c16b537SWarner Losh     r = lstat(infilename, &statbuf);
310*0c16b537SWarner Losh     if (!r && S_ISLNK(statbuf.st_mode)) return 1;
311*0c16b537SWarner Losh #endif
312*0c16b537SWarner Losh     return 0;
313*0c16b537SWarner Losh }
314*0c16b537SWarner Losh 
315*0c16b537SWarner Losh 
316*0c16b537SWarner Losh UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
317*0c16b537SWarner Losh {
318*0c16b537SWarner Losh     int r;
319*0c16b537SWarner Losh #if defined(_MSC_VER)
320*0c16b537SWarner Losh     struct __stat64 statbuf;
321*0c16b537SWarner Losh     r = _stat64(infilename, &statbuf);
322*0c16b537SWarner Losh     if (r || !(statbuf.st_mode & S_IFREG)) return 0;   /* No good... */
323*0c16b537SWarner Losh #elif defined(__MINGW32__) && defined (__MSVCRT__)
324*0c16b537SWarner Losh     struct _stati64 statbuf;
325*0c16b537SWarner Losh     r = _stati64(infilename, &statbuf);
326*0c16b537SWarner Losh     if (r || !(statbuf.st_mode & S_IFREG)) return 0;   /* No good... */
327*0c16b537SWarner Losh #else
328*0c16b537SWarner Losh     struct stat statbuf;
329*0c16b537SWarner Losh     r = stat(infilename, &statbuf);
330*0c16b537SWarner Losh     if (r || !S_ISREG(statbuf.st_mode)) return 0;   /* No good... */
331*0c16b537SWarner Losh #endif
332*0c16b537SWarner Losh     return (U64)statbuf.st_size;
333*0c16b537SWarner Losh }
334*0c16b537SWarner Losh 
335*0c16b537SWarner Losh 
336*0c16b537SWarner Losh UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
337*0c16b537SWarner Losh {
338*0c16b537SWarner Losh     U64 total = 0;
339*0c16b537SWarner Losh     unsigned n;
340*0c16b537SWarner Losh     for (n=0; n<nbFiles; n++)
341*0c16b537SWarner Losh         total += UTIL_getFileSize(fileNamesTable[n]);
342*0c16b537SWarner Losh     return total;
343*0c16b537SWarner Losh }
344*0c16b537SWarner Losh 
345*0c16b537SWarner Losh 
346*0c16b537SWarner Losh /*
347*0c16b537SWarner Losh  * A modified version of realloc().
348*0c16b537SWarner Losh  * If UTIL_realloc() fails the original block is freed.
349*0c16b537SWarner Losh */
350*0c16b537SWarner Losh UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
351*0c16b537SWarner Losh {
352*0c16b537SWarner Losh     void *newptr = realloc(ptr, size);
353*0c16b537SWarner Losh     if (newptr) return newptr;
354*0c16b537SWarner Losh     free(ptr);
355*0c16b537SWarner Losh     return NULL;
356*0c16b537SWarner Losh }
357*0c16b537SWarner Losh 
358*0c16b537SWarner Losh #ifdef _WIN32
359*0c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
360*0c16b537SWarner Losh 
361*0c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
362*0c16b537SWarner Losh {
363*0c16b537SWarner Losh     char* path;
364*0c16b537SWarner Losh     int dirLength, fnameLength, pathLength, nbFiles = 0;
365*0c16b537SWarner Losh     WIN32_FIND_DATAA cFile;
366*0c16b537SWarner Losh     HANDLE hFile;
367*0c16b537SWarner Losh 
368*0c16b537SWarner Losh     dirLength = (int)strlen(dirName);
369*0c16b537SWarner Losh     path = (char*) malloc(dirLength + 3);
370*0c16b537SWarner Losh     if (!path) return 0;
371*0c16b537SWarner Losh 
372*0c16b537SWarner Losh     memcpy(path, dirName, dirLength);
373*0c16b537SWarner Losh     path[dirLength] = '\\';
374*0c16b537SWarner Losh     path[dirLength+1] = '*';
375*0c16b537SWarner Losh     path[dirLength+2] = 0;
376*0c16b537SWarner Losh 
377*0c16b537SWarner Losh     hFile=FindFirstFileA(path, &cFile);
378*0c16b537SWarner Losh     if (hFile == INVALID_HANDLE_VALUE) {
379*0c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
380*0c16b537SWarner Losh         return 0;
381*0c16b537SWarner Losh     }
382*0c16b537SWarner Losh     free(path);
383*0c16b537SWarner Losh 
384*0c16b537SWarner Losh     do {
385*0c16b537SWarner Losh         fnameLength = (int)strlen(cFile.cFileName);
386*0c16b537SWarner Losh         path = (char*) malloc(dirLength + fnameLength + 2);
387*0c16b537SWarner Losh         if (!path) { FindClose(hFile); return 0; }
388*0c16b537SWarner Losh         memcpy(path, dirName, dirLength);
389*0c16b537SWarner Losh         path[dirLength] = '\\';
390*0c16b537SWarner Losh         memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
391*0c16b537SWarner Losh         pathLength = dirLength+1+fnameLength;
392*0c16b537SWarner Losh         path[pathLength] = 0;
393*0c16b537SWarner Losh         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
394*0c16b537SWarner Losh             if (strcmp (cFile.cFileName, "..") == 0 ||
395*0c16b537SWarner Losh                 strcmp (cFile.cFileName, ".") == 0) continue;
396*0c16b537SWarner Losh 
397*0c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
398*0c16b537SWarner Losh             if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
399*0c16b537SWarner Losh         }
400*0c16b537SWarner Losh         else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
401*0c16b537SWarner Losh             if (*bufStart + *pos + pathLength >= *bufEnd) {
402*0c16b537SWarner Losh                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
403*0c16b537SWarner Losh                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
404*0c16b537SWarner Losh                 *bufEnd = *bufStart + newListSize;
405*0c16b537SWarner Losh                 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
406*0c16b537SWarner Losh             }
407*0c16b537SWarner Losh             if (*bufStart + *pos + pathLength < *bufEnd) {
408*0c16b537SWarner Losh                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
409*0c16b537SWarner Losh                 *pos += pathLength + 1;
410*0c16b537SWarner Losh                 nbFiles++;
411*0c16b537SWarner Losh             }
412*0c16b537SWarner Losh         }
413*0c16b537SWarner Losh         free(path);
414*0c16b537SWarner Losh     } while (FindNextFileA(hFile, &cFile));
415*0c16b537SWarner Losh 
416*0c16b537SWarner Losh     FindClose(hFile);
417*0c16b537SWarner Losh     return nbFiles;
418*0c16b537SWarner Losh }
419*0c16b537SWarner Losh 
420*0c16b537SWarner Losh #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
421*0c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
422*0c16b537SWarner Losh #  include <dirent.h>       /* opendir, readdir */
423*0c16b537SWarner Losh #  include <string.h>       /* strerror, memcpy */
424*0c16b537SWarner Losh 
425*0c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
426*0c16b537SWarner Losh {
427*0c16b537SWarner Losh     DIR *dir;
428*0c16b537SWarner Losh     struct dirent *entry;
429*0c16b537SWarner Losh     char* path;
430*0c16b537SWarner Losh     int dirLength, fnameLength, pathLength, nbFiles = 0;
431*0c16b537SWarner Losh 
432*0c16b537SWarner Losh     if (!(dir = opendir(dirName))) {
433*0c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
434*0c16b537SWarner Losh         return 0;
435*0c16b537SWarner Losh     }
436*0c16b537SWarner Losh 
437*0c16b537SWarner Losh     dirLength = (int)strlen(dirName);
438*0c16b537SWarner Losh     errno = 0;
439*0c16b537SWarner Losh     while ((entry = readdir(dir)) != NULL) {
440*0c16b537SWarner Losh         if (strcmp (entry->d_name, "..") == 0 ||
441*0c16b537SWarner Losh             strcmp (entry->d_name, ".") == 0) continue;
442*0c16b537SWarner Losh         fnameLength = (int)strlen(entry->d_name);
443*0c16b537SWarner Losh         path = (char*) malloc(dirLength + fnameLength + 2);
444*0c16b537SWarner Losh         if (!path) { closedir(dir); return 0; }
445*0c16b537SWarner Losh         memcpy(path, dirName, dirLength);
446*0c16b537SWarner Losh 
447*0c16b537SWarner Losh         path[dirLength] = '/';
448*0c16b537SWarner Losh         memcpy(path+dirLength+1, entry->d_name, fnameLength);
449*0c16b537SWarner Losh         pathLength = dirLength+1+fnameLength;
450*0c16b537SWarner Losh         path[pathLength] = 0;
451*0c16b537SWarner Losh 
452*0c16b537SWarner Losh         if (!followLinks && UTIL_isLink(path)) {
453*0c16b537SWarner Losh             UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
454*0c16b537SWarner Losh             continue;
455*0c16b537SWarner Losh         }
456*0c16b537SWarner Losh 
457*0c16b537SWarner Losh         if (UTIL_isDirectory(path)) {
458*0c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
459*0c16b537SWarner Losh             if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
460*0c16b537SWarner Losh         } else {
461*0c16b537SWarner Losh             if (*bufStart + *pos + pathLength >= *bufEnd) {
462*0c16b537SWarner Losh                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
463*0c16b537SWarner Losh                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
464*0c16b537SWarner Losh                 *bufEnd = *bufStart + newListSize;
465*0c16b537SWarner Losh                 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
466*0c16b537SWarner Losh             }
467*0c16b537SWarner Losh             if (*bufStart + *pos + pathLength < *bufEnd) {
468*0c16b537SWarner Losh                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
469*0c16b537SWarner Losh                 *pos += pathLength + 1;
470*0c16b537SWarner Losh                 nbFiles++;
471*0c16b537SWarner Losh             }
472*0c16b537SWarner Losh         }
473*0c16b537SWarner Losh         free(path);
474*0c16b537SWarner Losh         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
475*0c16b537SWarner Losh     }
476*0c16b537SWarner Losh 
477*0c16b537SWarner Losh     if (errno != 0) {
478*0c16b537SWarner Losh         UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno));
479*0c16b537SWarner Losh         free(*bufStart);
480*0c16b537SWarner Losh         *bufStart = NULL;
481*0c16b537SWarner Losh     }
482*0c16b537SWarner Losh     closedir(dir);
483*0c16b537SWarner Losh     return nbFiles;
484*0c16b537SWarner Losh }
485*0c16b537SWarner Losh 
486*0c16b537SWarner Losh #else
487*0c16b537SWarner Losh 
488*0c16b537SWarner Losh UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
489*0c16b537SWarner Losh {
490*0c16b537SWarner Losh     (void)bufStart; (void)bufEnd; (void)pos;
491*0c16b537SWarner Losh     UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
492*0c16b537SWarner Losh     return 0;
493*0c16b537SWarner Losh }
494*0c16b537SWarner Losh 
495*0c16b537SWarner Losh #endif /* #ifdef _WIN32 */
496*0c16b537SWarner Losh 
497*0c16b537SWarner Losh /*
498*0c16b537SWarner Losh  * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
499*0c16b537SWarner Losh  *                       and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
500*0c16b537SWarner Losh  * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
501*0c16b537SWarner Losh  * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
502*0c16b537SWarner Losh  */
503*0c16b537SWarner Losh UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb, int followLinks)
504*0c16b537SWarner Losh {
505*0c16b537SWarner Losh     size_t pos;
506*0c16b537SWarner Losh     unsigned i, nbFiles;
507*0c16b537SWarner Losh     char* buf = (char*)malloc(LIST_SIZE_INCREASE);
508*0c16b537SWarner Losh     char* bufend = buf + LIST_SIZE_INCREASE;
509*0c16b537SWarner Losh     const char** fileTable;
510*0c16b537SWarner Losh 
511*0c16b537SWarner Losh     if (!buf) return NULL;
512*0c16b537SWarner Losh 
513*0c16b537SWarner Losh     for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
514*0c16b537SWarner Losh         if (!UTIL_isDirectory(inputNames[i])) {
515*0c16b537SWarner Losh             size_t const len = strlen(inputNames[i]);
516*0c16b537SWarner Losh             if (buf + pos + len >= bufend) {
517*0c16b537SWarner Losh                 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
518*0c16b537SWarner Losh                 buf = (char*)UTIL_realloc(buf, newListSize);
519*0c16b537SWarner Losh                 bufend = buf + newListSize;
520*0c16b537SWarner Losh                 if (!buf) return NULL;
521*0c16b537SWarner Losh             }
522*0c16b537SWarner Losh             if (buf + pos + len < bufend) {
523*0c16b537SWarner Losh                 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
524*0c16b537SWarner Losh                 pos += len + 1;
525*0c16b537SWarner Losh                 nbFiles++;
526*0c16b537SWarner Losh             }
527*0c16b537SWarner Losh         } else {
528*0c16b537SWarner Losh             nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
529*0c16b537SWarner Losh             if (buf == NULL) return NULL;
530*0c16b537SWarner Losh     }   }
531*0c16b537SWarner Losh 
532*0c16b537SWarner Losh     if (nbFiles == 0) { free(buf); return NULL; }
533*0c16b537SWarner Losh 
534*0c16b537SWarner Losh     fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
535*0c16b537SWarner Losh     if (!fileTable) { free(buf); return NULL; }
536*0c16b537SWarner Losh 
537*0c16b537SWarner Losh     for (i=0, pos=0; i<nbFiles; i++) {
538*0c16b537SWarner Losh         fileTable[i] = buf + pos;
539*0c16b537SWarner Losh         pos += strlen(fileTable[i]) + 1;
540*0c16b537SWarner Losh     }
541*0c16b537SWarner Losh 
542*0c16b537SWarner Losh     if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
543*0c16b537SWarner Losh 
544*0c16b537SWarner Losh     *allocatedBuffer = buf;
545*0c16b537SWarner Losh     *allocatedNamesNb = nbFiles;
546*0c16b537SWarner Losh 
547*0c16b537SWarner Losh     return fileTable;
548*0c16b537SWarner Losh }
549*0c16b537SWarner Losh 
550*0c16b537SWarner Losh 
551*0c16b537SWarner Losh UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
552*0c16b537SWarner Losh {
553*0c16b537SWarner Losh     if (allocatedBuffer) free(allocatedBuffer);
554*0c16b537SWarner Losh     if (filenameTable) free((void*)filenameTable);
555*0c16b537SWarner Losh }
556*0c16b537SWarner Losh 
557*0c16b537SWarner Losh /* count the number of physical cores */
558*0c16b537SWarner Losh #if defined(_WIN32) || defined(WIN32)
559*0c16b537SWarner Losh 
560*0c16b537SWarner Losh #include <windows.h>
561*0c16b537SWarner Losh 
562*0c16b537SWarner Losh typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
563*0c16b537SWarner Losh 
564*0c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
565*0c16b537SWarner Losh {
566*0c16b537SWarner Losh     static int numPhysicalCores = 0;
567*0c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
568*0c16b537SWarner Losh 
569*0c16b537SWarner Losh     {   LPFN_GLPI glpi;
570*0c16b537SWarner Losh         BOOL done = FALSE;
571*0c16b537SWarner Losh         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
572*0c16b537SWarner Losh         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
573*0c16b537SWarner Losh         DWORD returnLength = 0;
574*0c16b537SWarner Losh         size_t byteOffset = 0;
575*0c16b537SWarner Losh 
576*0c16b537SWarner Losh         glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
577*0c16b537SWarner Losh                                          "GetLogicalProcessorInformation");
578*0c16b537SWarner Losh 
579*0c16b537SWarner Losh         if (glpi == NULL) {
580*0c16b537SWarner Losh             goto failed;
581*0c16b537SWarner Losh         }
582*0c16b537SWarner Losh 
583*0c16b537SWarner Losh         while(!done) {
584*0c16b537SWarner Losh             DWORD rc = glpi(buffer, &returnLength);
585*0c16b537SWarner Losh             if (FALSE == rc) {
586*0c16b537SWarner Losh                 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
587*0c16b537SWarner Losh                     if (buffer)
588*0c16b537SWarner Losh                         free(buffer);
589*0c16b537SWarner Losh                     buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
590*0c16b537SWarner Losh 
591*0c16b537SWarner Losh                     if (buffer == NULL) {
592*0c16b537SWarner Losh                         perror("zstd");
593*0c16b537SWarner Losh                         exit(1);
594*0c16b537SWarner Losh                     }
595*0c16b537SWarner Losh                 } else {
596*0c16b537SWarner Losh                     /* some other error */
597*0c16b537SWarner Losh                     goto failed;
598*0c16b537SWarner Losh                 }
599*0c16b537SWarner Losh             } else {
600*0c16b537SWarner Losh                 done = TRUE;
601*0c16b537SWarner Losh             }
602*0c16b537SWarner Losh         }
603*0c16b537SWarner Losh 
604*0c16b537SWarner Losh         ptr = buffer;
605*0c16b537SWarner Losh 
606*0c16b537SWarner Losh         while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
607*0c16b537SWarner Losh 
608*0c16b537SWarner Losh             if (ptr->Relationship == RelationProcessorCore) {
609*0c16b537SWarner Losh                 numPhysicalCores++;
610*0c16b537SWarner Losh             }
611*0c16b537SWarner Losh 
612*0c16b537SWarner Losh             ptr++;
613*0c16b537SWarner Losh             byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
614*0c16b537SWarner Losh         }
615*0c16b537SWarner Losh 
616*0c16b537SWarner Losh         free(buffer);
617*0c16b537SWarner Losh 
618*0c16b537SWarner Losh         return numPhysicalCores;
619*0c16b537SWarner Losh     }
620*0c16b537SWarner Losh 
621*0c16b537SWarner Losh failed:
622*0c16b537SWarner Losh     /* try to fall back on GetSystemInfo */
623*0c16b537SWarner Losh     {   SYSTEM_INFO sysinfo;
624*0c16b537SWarner Losh         GetSystemInfo(&sysinfo);
625*0c16b537SWarner Losh         numPhysicalCores = sysinfo.dwNumberOfProcessors;
626*0c16b537SWarner Losh         if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
627*0c16b537SWarner Losh     }
628*0c16b537SWarner Losh     return numPhysicalCores;
629*0c16b537SWarner Losh }
630*0c16b537SWarner Losh 
631*0c16b537SWarner Losh #elif defined(__APPLE__)
632*0c16b537SWarner Losh 
633*0c16b537SWarner Losh #include <sys/sysctl.h>
634*0c16b537SWarner Losh 
635*0c16b537SWarner Losh /* Use apple-provided syscall
636*0c16b537SWarner Losh  * see: man 3 sysctl */
637*0c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
638*0c16b537SWarner Losh {
639*0c16b537SWarner Losh     static S32 numPhysicalCores = 0; /* apple specifies int32_t */
640*0c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
641*0c16b537SWarner Losh 
642*0c16b537SWarner Losh     {   size_t size = sizeof(S32);
643*0c16b537SWarner Losh         int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
644*0c16b537SWarner Losh         if (ret != 0) {
645*0c16b537SWarner Losh             if (errno == ENOENT) {
646*0c16b537SWarner Losh                 /* entry not present, fall back on 1 */
647*0c16b537SWarner Losh                 numPhysicalCores = 1;
648*0c16b537SWarner Losh             } else {
649*0c16b537SWarner Losh                 perror("zstd: can't get number of physical cpus");
650*0c16b537SWarner Losh                 exit(1);
651*0c16b537SWarner Losh             }
652*0c16b537SWarner Losh         }
653*0c16b537SWarner Losh 
654*0c16b537SWarner Losh         return numPhysicalCores;
655*0c16b537SWarner Losh     }
656*0c16b537SWarner Losh }
657*0c16b537SWarner Losh 
658*0c16b537SWarner Losh #elif defined(__linux__)
659*0c16b537SWarner Losh 
660*0c16b537SWarner Losh /* parse /proc/cpuinfo
661*0c16b537SWarner Losh  * siblings / cpu cores should give hyperthreading ratio
662*0c16b537SWarner Losh  * otherwise fall back on sysconf */
663*0c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
664*0c16b537SWarner Losh {
665*0c16b537SWarner Losh     static int numPhysicalCores = 0;
666*0c16b537SWarner Losh 
667*0c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
668*0c16b537SWarner Losh 
669*0c16b537SWarner Losh     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
670*0c16b537SWarner Losh     if (numPhysicalCores == -1) {
671*0c16b537SWarner Losh         /* value not queryable, fall back on 1 */
672*0c16b537SWarner Losh         return numPhysicalCores = 1;
673*0c16b537SWarner Losh     }
674*0c16b537SWarner Losh 
675*0c16b537SWarner Losh     /* try to determine if there's hyperthreading */
676*0c16b537SWarner Losh     {   FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
677*0c16b537SWarner Losh #define BUF_SIZE 80
678*0c16b537SWarner Losh         char buff[BUF_SIZE];
679*0c16b537SWarner Losh 
680*0c16b537SWarner Losh         int siblings = 0;
681*0c16b537SWarner Losh         int cpu_cores = 0;
682*0c16b537SWarner Losh         int ratio = 1;
683*0c16b537SWarner Losh 
684*0c16b537SWarner Losh         if (cpuinfo == NULL) {
685*0c16b537SWarner Losh             /* fall back on the sysconf value */
686*0c16b537SWarner Losh             return numPhysicalCores;
687*0c16b537SWarner Losh         }
688*0c16b537SWarner Losh 
689*0c16b537SWarner Losh         /* assume the cpu cores/siblings values will be constant across all
690*0c16b537SWarner Losh          * present processors */
691*0c16b537SWarner Losh         while (!feof(cpuinfo)) {
692*0c16b537SWarner Losh             if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
693*0c16b537SWarner Losh                 if (strncmp(buff, "siblings", 8) == 0) {
694*0c16b537SWarner Losh                     const char* const sep = strchr(buff, ':');
695*0c16b537SWarner Losh                     if (*sep == '\0') {
696*0c16b537SWarner Losh                         /* formatting was broken? */
697*0c16b537SWarner Losh                         goto failed;
698*0c16b537SWarner Losh                     }
699*0c16b537SWarner Losh 
700*0c16b537SWarner Losh                     siblings = atoi(sep + 1);
701*0c16b537SWarner Losh                 }
702*0c16b537SWarner Losh                 if (strncmp(buff, "cpu cores", 9) == 0) {
703*0c16b537SWarner Losh                     const char* const sep = strchr(buff, ':');
704*0c16b537SWarner Losh                     if (*sep == '\0') {
705*0c16b537SWarner Losh                         /* formatting was broken? */
706*0c16b537SWarner Losh                         goto failed;
707*0c16b537SWarner Losh                     }
708*0c16b537SWarner Losh 
709*0c16b537SWarner Losh                     cpu_cores = atoi(sep + 1);
710*0c16b537SWarner Losh                 }
711*0c16b537SWarner Losh             } else if (ferror(cpuinfo)) {
712*0c16b537SWarner Losh                 /* fall back on the sysconf value */
713*0c16b537SWarner Losh                 goto failed;
714*0c16b537SWarner Losh             }
715*0c16b537SWarner Losh         }
716*0c16b537SWarner Losh         if (siblings && cpu_cores) {
717*0c16b537SWarner Losh             ratio = siblings / cpu_cores;
718*0c16b537SWarner Losh         }
719*0c16b537SWarner Losh failed:
720*0c16b537SWarner Losh         fclose(cpuinfo);
721*0c16b537SWarner Losh         return numPhysicalCores = numPhysicalCores / ratio;
722*0c16b537SWarner Losh     }
723*0c16b537SWarner Losh }
724*0c16b537SWarner Losh 
725*0c16b537SWarner Losh #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
726*0c16b537SWarner Losh 
727*0c16b537SWarner Losh /* Use apple-provided syscall
728*0c16b537SWarner Losh  * see: man 3 sysctl */
729*0c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
730*0c16b537SWarner Losh {
731*0c16b537SWarner Losh     static int numPhysicalCores = 0;
732*0c16b537SWarner Losh 
733*0c16b537SWarner Losh     if (numPhysicalCores != 0) return numPhysicalCores;
734*0c16b537SWarner Losh 
735*0c16b537SWarner Losh     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
736*0c16b537SWarner Losh     if (numPhysicalCores == -1) {
737*0c16b537SWarner Losh         /* value not queryable, fall back on 1 */
738*0c16b537SWarner Losh         return numPhysicalCores = 1;
739*0c16b537SWarner Losh     }
740*0c16b537SWarner Losh     return numPhysicalCores;
741*0c16b537SWarner Losh }
742*0c16b537SWarner Losh 
743*0c16b537SWarner Losh #else
744*0c16b537SWarner Losh 
745*0c16b537SWarner Losh UTIL_STATIC int UTIL_countPhysicalCores(void)
746*0c16b537SWarner Losh {
747*0c16b537SWarner Losh     /* assume 1 */
748*0c16b537SWarner Losh     return 1;
749*0c16b537SWarner Losh }
750*0c16b537SWarner Losh 
751*0c16b537SWarner Losh #endif
752*0c16b537SWarner Losh 
753*0c16b537SWarner Losh #if defined (__cplusplus)
754*0c16b537SWarner Losh }
755*0c16b537SWarner Losh #endif
756*0c16b537SWarner Losh 
757*0c16b537SWarner Losh #endif /* UTIL_H_MODULE */
758