2 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
14 #if defined (__cplusplus)
20 /*-****************************************
22 ******************************************/
23 #include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
24 #include <stdlib.h> /* malloc */
25 #include <stddef.h> /* size_t, ptrdiff_t */
26 #include <stdio.h> /* fprintf */
27 #include <string.h> /* strncmp */
28 #include <sys/types.h> /* stat, utime */
29 #include <sys/stat.h> /* stat, chmod */
31 # include <sys/utime.h> /* utime */
32 # include <io.h> /* _chmod */
34 # include <unistd.h> /* chown, stat */
35 # include <utime.h> /* utime */
37 #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
39 #include "mem.h" /* U32, U64 */
42 /* ************************************************************
43 * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
44 ***************************************************************/
45 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
46 # define UTIL_fseek _fseeki64
47 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
48 # define UTIL_fseek fseeko
49 #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
50 # define UTIL_fseek fseeko64
52 # define UTIL_fseek fseek
56 /*-*************************************************
57 * Sleep & priority functions: Windows - Posix - others
58 ***************************************************/
61 # define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
62 # define UTIL_sleep(s) Sleep(1000*s)
63 # define UTIL_sleepMilli(milli) Sleep(milli)
65 #elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
66 # include <unistd.h> /* sleep */
67 # define UTIL_sleep(s) sleep(s)
68 # if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */
69 # define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
71 # define UTIL_sleepMilli(milli) /* disabled */
73 # if ZSTD_SETPRIORITY_SUPPORT
74 # include <sys/resource.h> /* setpriority */
75 # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
77 # define SET_REALTIME_PRIORITY /* disabled */
80 #else /* unknown non-unix operating systen */
81 # define UTIL_sleep(s) /* disabled */
82 # define UTIL_sleepMilli(milli) /* disabled */
83 # define SET_REALTIME_PRIORITY /* disabled */
87 /* *************************************
89 ***************************************/
90 #define LIST_SIZE_INCREASE (8*1024)
93 /*-****************************************
95 ******************************************/
96 #if defined(__INTEL_COMPILER)
97 # pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
100 # define UTIL_STATIC static __attribute__((unused))
101 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
102 # define UTIL_STATIC static inline
103 #elif defined(_MSC_VER)
104 # define UTIL_STATIC static __inline
106 # define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
110 /*-****************************************
112 ******************************************/
113 static int g_utilDisplayLevel;
114 #define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
115 #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
118 /*-****************************************
120 ******************************************/
121 #if defined(_WIN32) /* Windows */
122 #define UTIL_TIME_INITIALIZER { { 0, 0 } }
123 typedef LARGE_INTEGER UTIL_time_t;
125 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
126 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
128 static LARGE_INTEGER ticksPerSecond;
131 if (!QueryPerformanceFrequency(&ticksPerSecond))
132 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
135 return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
137 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
139 static LARGE_INTEGER ticksPerSecond;
142 if (!QueryPerformanceFrequency(&ticksPerSecond))
143 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
146 return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
149 #elif defined(__APPLE__) && defined(__MACH__)
151 #include <mach/mach_time.h>
152 #define UTIL_TIME_INITIALIZER 0
153 typedef U64 UTIL_time_t;
155 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
156 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
158 static mach_timebase_info_data_t rate;
161 mach_timebase_info(&rate);
164 return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL;
166 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
168 static mach_timebase_info_data_t rate;
171 mach_timebase_info(&rate);
174 return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
177 #elif (PLATFORM_POSIX_VERSION >= 200112L) \
178 && (defined(__UCLIBC__) \
179 || (defined(__GLIBC__) \
180 && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) \
181 || (__GLIBC__ > 2))))
183 #define UTIL_TIME_INITIALIZER { 0, 0 }
184 typedef struct timespec UTIL_freq_t;
185 typedef struct timespec UTIL_time_t;
187 UTIL_STATIC UTIL_time_t UTIL_getTime(void)
190 if (clock_gettime(CLOCK_MONOTONIC, &time))
191 UTIL_DISPLAYLEVEL(1, "ERROR: Failed to get time\n"); /* we could also exit() */
195 UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
198 if (end.tv_nsec < begin.tv_nsec) {
199 diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
200 diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
202 diff.tv_sec = end.tv_sec - begin.tv_sec;
203 diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
208 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
210 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
212 micro += 1000000ULL * diff.tv_sec;
213 micro += diff.tv_nsec / 1000ULL;
217 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
219 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
221 nano += 1000000000ULL * diff.tv_sec;
222 nano += diff.tv_nsec;
226 #else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
227 typedef clock_t UTIL_time_t;
228 #define UTIL_TIME_INITIALIZER 0
229 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
230 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
231 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
234 #define SEC_TO_MICRO 1000000
236 /* returns time span in microseconds */
237 UTIL_STATIC U64 UTIL_clockSpanMicro(UTIL_time_t clockStart )
239 UTIL_time_t const clockEnd = UTIL_getTime();
240 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
243 /* returns time span in microseconds */
244 UTIL_STATIC U64 UTIL_clockSpanNano(UTIL_time_t clockStart )
246 UTIL_time_t const clockEnd = UTIL_getTime();
247 return UTIL_getSpanTimeNano(clockStart, clockEnd);
250 UTIL_STATIC void UTIL_waitForNextTick(void)
252 UTIL_time_t const clockStart = UTIL_getTime();
253 UTIL_time_t clockEnd;
255 clockEnd = UTIL_getTime();
256 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
261 /*-****************************************
263 ******************************************/
264 #if defined(_MSC_VER)
266 typedef struct __stat64 stat_t;
268 typedef struct stat stat_t;
272 UTIL_STATIC int UTIL_isRegularFile(const char* infilename);
275 UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
278 struct utimbuf timebuf;
280 if (!UTIL_isRegularFile(filename))
283 timebuf.actime = time(NULL);
284 timebuf.modtime = statbuf->st_mtime;
285 res += utime(filename, &timebuf); /* set access and modification times */
288 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
291 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
294 return -res; /* number of errors is returned */
298 UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
301 #if defined(_MSC_VER)
302 r = _stat64(infilename, statbuf);
303 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
305 r = stat(infilename, statbuf);
306 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
312 UTIL_STATIC int UTIL_isRegularFile(const char* infilename)
315 return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
319 UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
323 #if defined(_MSC_VER)
324 r = _stat64(infilename, &statbuf);
325 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
327 r = stat(infilename, &statbuf);
328 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
333 UTIL_STATIC U32 UTIL_isLink(const char* infilename)
335 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
336 #ifndef __STRICT_ANSI__
337 #if defined(_BSD_SOURCE) \
338 || (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) \
339 || (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) \
340 || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) \
341 || (defined(__APPLE__) && defined(__MACH__))
344 r = lstat(infilename, &statbuf);
345 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
353 #define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
354 UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
356 if (!UTIL_isRegularFile(infilename)) return UTIL_FILESIZE_UNKNOWN;
358 #if defined(_MSC_VER)
359 struct __stat64 statbuf;
360 r = _stat64(infilename, &statbuf);
361 if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
362 #elif defined(__MINGW32__) && defined (__MSVCRT__)
363 struct _stati64 statbuf;
364 r = _stati64(infilename, &statbuf);
365 if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
368 r = stat(infilename, &statbuf);
369 if (r || !S_ISREG(statbuf.st_mode)) return UTIL_FILESIZE_UNKNOWN;
371 return (U64)statbuf.st_size;
376 UTIL_STATIC U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles)
381 for (n=0; n<nbFiles; n++) {
382 U64 const size = UTIL_getFileSize(fileNamesTable[n]);
383 error |= (size == UTIL_FILESIZE_UNKNOWN);
386 return error ? UTIL_FILESIZE_UNKNOWN : total;
391 * A modified version of realloc().
392 * If UTIL_realloc() fails the original block is freed.
394 UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
396 void *newptr = realloc(ptr, size);
397 if (newptr) return newptr;
403 # define UTIL_HAS_CREATEFILELIST
405 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
408 int dirLength, fnameLength, pathLength, nbFiles = 0;
409 WIN32_FIND_DATAA cFile;
412 dirLength = (int)strlen(dirName);
413 path = (char*) malloc(dirLength + 3);
416 memcpy(path, dirName, dirLength);
417 path[dirLength] = '\\';
418 path[dirLength+1] = '*';
419 path[dirLength+2] = 0;
421 hFile=FindFirstFileA(path, &cFile);
422 if (hFile == INVALID_HANDLE_VALUE) {
423 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
429 fnameLength = (int)strlen(cFile.cFileName);
430 path = (char*) malloc(dirLength + fnameLength + 2);
431 if (!path) { FindClose(hFile); return 0; }
432 memcpy(path, dirName, dirLength);
433 path[dirLength] = '\\';
434 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
435 pathLength = dirLength+1+fnameLength;
436 path[pathLength] = 0;
437 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
438 if (strcmp (cFile.cFileName, "..") == 0 ||
439 strcmp (cFile.cFileName, ".") == 0) continue;
441 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
442 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
444 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
445 if (*bufStart + *pos + pathLength >= *bufEnd) {
446 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
447 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
448 *bufEnd = *bufStart + newListSize;
449 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
451 if (*bufStart + *pos + pathLength < *bufEnd) {
452 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
453 *pos += pathLength + 1;
458 } while (FindNextFileA(hFile, &cFile));
464 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
465 # define UTIL_HAS_CREATEFILELIST
466 # include <dirent.h> /* opendir, readdir */
467 # include <string.h> /* strerror, memcpy */
469 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
472 struct dirent *entry;
474 int dirLength, fnameLength, pathLength, nbFiles = 0;
476 if (!(dir = opendir(dirName))) {
477 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
481 dirLength = (int)strlen(dirName);
483 while ((entry = readdir(dir)) != NULL) {
484 if (strcmp (entry->d_name, "..") == 0 ||
485 strcmp (entry->d_name, ".") == 0) continue;
486 fnameLength = (int)strlen(entry->d_name);
487 path = (char*) malloc(dirLength + fnameLength + 2);
488 if (!path) { closedir(dir); return 0; }
489 memcpy(path, dirName, dirLength);
491 path[dirLength] = '/';
492 memcpy(path+dirLength+1, entry->d_name, fnameLength);
493 pathLength = dirLength+1+fnameLength;
494 path[pathLength] = 0;
496 if (!followLinks && UTIL_isLink(path)) {
497 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
501 if (UTIL_isDirectory(path)) {
502 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
503 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
505 if (*bufStart + *pos + pathLength >= *bufEnd) {
506 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
507 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
508 *bufEnd = *bufStart + newListSize;
509 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
511 if (*bufStart + *pos + pathLength < *bufEnd) {
512 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
513 *pos += pathLength + 1;
518 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
522 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno));
532 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
534 (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
535 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
539 #endif /* #ifdef _WIN32 */
542 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
543 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
544 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
545 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
547 UTIL_STATIC const char**
548 UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
549 char** allocatedBuffer, unsigned* allocatedNamesNb,
554 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
555 char* bufend = buf + LIST_SIZE_INCREASE;
556 const char** fileTable;
558 if (!buf) return NULL;
560 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
561 if (!UTIL_isDirectory(inputNames[i])) {
562 size_t const len = strlen(inputNames[i]);
563 if (buf + pos + len >= bufend) {
564 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
565 buf = (char*)UTIL_realloc(buf, newListSize);
566 bufend = buf + newListSize;
567 if (!buf) return NULL;
569 if (buf + pos + len < bufend) {
570 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
575 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
576 if (buf == NULL) return NULL;
579 if (nbFiles == 0) { free(buf); return NULL; }
581 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
582 if (!fileTable) { free(buf); return NULL; }
584 for (i=0, pos=0; i<nbFiles; i++) {
585 fileTable[i] = buf + pos;
586 pos += strlen(fileTable[i]) + 1;
589 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
591 *allocatedBuffer = buf;
592 *allocatedNamesNb = nbFiles;
598 UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
600 if (allocatedBuffer) free(allocatedBuffer);
601 if (filenameTable) free((void*)filenameTable);
604 /* count the number of physical cores */
605 #if defined(_WIN32) || defined(WIN32)
609 typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
611 UTIL_STATIC int UTIL_countPhysicalCores(void)
613 static int numPhysicalCores = 0;
614 if (numPhysicalCores != 0) return numPhysicalCores;
618 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
619 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
620 DWORD returnLength = 0;
621 size_t byteOffset = 0;
623 glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
624 "GetLogicalProcessorInformation");
631 DWORD rc = glpi(buffer, &returnLength);
633 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
636 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
638 if (buffer == NULL) {
643 /* some other error */
653 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
655 if (ptr->Relationship == RelationProcessorCore) {
660 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
665 return numPhysicalCores;
669 /* try to fall back on GetSystemInfo */
670 { SYSTEM_INFO sysinfo;
671 GetSystemInfo(&sysinfo);
672 numPhysicalCores = sysinfo.dwNumberOfProcessors;
673 if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
675 return numPhysicalCores;
678 #elif defined(__APPLE__)
680 #include <sys/sysctl.h>
682 /* Use apple-provided syscall
683 * see: man 3 sysctl */
684 UTIL_STATIC int UTIL_countPhysicalCores(void)
686 static S32 numPhysicalCores = 0; /* apple specifies int32_t */
687 if (numPhysicalCores != 0) return numPhysicalCores;
689 { size_t size = sizeof(S32);
690 int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
692 if (errno == ENOENT) {
693 /* entry not present, fall back on 1 */
694 numPhysicalCores = 1;
696 perror("zstd: can't get number of physical cpus");
701 return numPhysicalCores;
705 #elif defined(__linux__)
707 /* parse /proc/cpuinfo
708 * siblings / cpu cores should give hyperthreading ratio
709 * otherwise fall back on sysconf */
710 UTIL_STATIC int UTIL_countPhysicalCores(void)
712 static int numPhysicalCores = 0;
714 if (numPhysicalCores != 0) return numPhysicalCores;
716 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
717 if (numPhysicalCores == -1) {
718 /* value not queryable, fall back on 1 */
719 return numPhysicalCores = 1;
722 /* try to determine if there's hyperthreading */
723 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
731 if (cpuinfo == NULL) {
732 /* fall back on the sysconf value */
733 return numPhysicalCores;
736 /* assume the cpu cores/siblings values will be constant across all
737 * present processors */
738 while (!feof(cpuinfo)) {
739 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
740 if (strncmp(buff, "siblings", 8) == 0) {
741 const char* const sep = strchr(buff, ':');
743 /* formatting was broken? */
747 siblings = atoi(sep + 1);
749 if (strncmp(buff, "cpu cores", 9) == 0) {
750 const char* const sep = strchr(buff, ':');
752 /* formatting was broken? */
756 cpu_cores = atoi(sep + 1);
758 } else if (ferror(cpuinfo)) {
759 /* fall back on the sysconf value */
763 if (siblings && cpu_cores) {
764 ratio = siblings / cpu_cores;
768 return numPhysicalCores = numPhysicalCores / ratio;
772 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
774 /* Use apple-provided syscall
775 * see: man 3 sysctl */
776 UTIL_STATIC int UTIL_countPhysicalCores(void)
778 static int numPhysicalCores = 0;
780 if (numPhysicalCores != 0) return numPhysicalCores;
782 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
783 if (numPhysicalCores == -1) {
784 /* value not queryable, fall back on 1 */
785 return numPhysicalCores = 1;
787 return numPhysicalCores;
792 UTIL_STATIC int UTIL_countPhysicalCores(void)
800 #if defined (__cplusplus)
804 #endif /* UTIL_H_MODULE */