1a0483764SConrad Meyer /* 2a0483764SConrad Meyer * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. 3a0483764SConrad Meyer * All rights reserved. 4a0483764SConrad Meyer * 5a0483764SConrad Meyer * This source code is licensed under both the BSD-style license (found in the 6a0483764SConrad Meyer * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7a0483764SConrad Meyer * in the COPYING file in the root directory of this source tree). 8a0483764SConrad Meyer * You may select, at your option, one of the above-listed licenses. 9a0483764SConrad Meyer */ 10a0483764SConrad Meyer 11a0483764SConrad Meyer #if defined (__cplusplus) 12a0483764SConrad Meyer extern "C" { 13a0483764SConrad Meyer #endif 14a0483764SConrad Meyer 15a0483764SConrad Meyer 16a0483764SConrad Meyer /*-**************************************** 17a0483764SConrad Meyer * Dependencies 18a0483764SConrad Meyer ******************************************/ 19a0483764SConrad Meyer #include "util.h" /* note : ensure that platform.h is included first ! */ 20a0483764SConrad Meyer #include <errno.h> 21a0483764SConrad Meyer #include <assert.h> 22a0483764SConrad Meyer 23a0483764SConrad Meyer 24a0483764SConrad Meyer int UTIL_fileExist(const char* filename) 25a0483764SConrad Meyer { 26a0483764SConrad Meyer stat_t statbuf; 27a0483764SConrad Meyer #if defined(_MSC_VER) 28a0483764SConrad Meyer int const stat_error = _stat64(filename, &statbuf); 29a0483764SConrad Meyer #else 30a0483764SConrad Meyer int const stat_error = stat(filename, &statbuf); 31a0483764SConrad Meyer #endif 32a0483764SConrad Meyer return !stat_error; 33a0483764SConrad Meyer } 34a0483764SConrad Meyer 35a0483764SConrad Meyer int UTIL_isRegularFile(const char* infilename) 36a0483764SConrad Meyer { 37a0483764SConrad Meyer stat_t statbuf; 38a0483764SConrad Meyer return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */ 39a0483764SConrad Meyer } 40a0483764SConrad Meyer 41a0483764SConrad Meyer int UTIL_getFileStat(const char* infilename, stat_t *statbuf) 42a0483764SConrad Meyer { 43a0483764SConrad Meyer int r; 44a0483764SConrad Meyer #if defined(_MSC_VER) 45a0483764SConrad Meyer r = _stat64(infilename, statbuf); 46a0483764SConrad Meyer if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */ 47a0483764SConrad Meyer #else 48a0483764SConrad Meyer r = stat(infilename, statbuf); 49a0483764SConrad Meyer if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */ 50a0483764SConrad Meyer #endif 51a0483764SConrad Meyer return 1; 52a0483764SConrad Meyer } 53a0483764SConrad Meyer 54a0483764SConrad Meyer int UTIL_setFileStat(const char *filename, stat_t *statbuf) 55a0483764SConrad Meyer { 56a0483764SConrad Meyer int res = 0; 57a0483764SConrad Meyer struct utimbuf timebuf; 58a0483764SConrad Meyer 59a0483764SConrad Meyer if (!UTIL_isRegularFile(filename)) 60a0483764SConrad Meyer return -1; 61a0483764SConrad Meyer 62a0483764SConrad Meyer timebuf.actime = time(NULL); 63a0483764SConrad Meyer timebuf.modtime = statbuf->st_mtime; 64a0483764SConrad Meyer res += utime(filename, &timebuf); /* set access and modification times */ 65a0483764SConrad Meyer 66a0483764SConrad Meyer #if !defined(_WIN32) 67a0483764SConrad Meyer res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ 68a0483764SConrad Meyer #endif 69a0483764SConrad Meyer 70a0483764SConrad Meyer res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */ 71a0483764SConrad Meyer 72a0483764SConrad Meyer errno = 0; 73a0483764SConrad Meyer return -res; /* number of errors is returned */ 74a0483764SConrad Meyer } 75a0483764SConrad Meyer 76a0483764SConrad Meyer U32 UTIL_isDirectory(const char* infilename) 77a0483764SConrad Meyer { 78a0483764SConrad Meyer int r; 79a0483764SConrad Meyer stat_t statbuf; 80a0483764SConrad Meyer #if defined(_MSC_VER) 81a0483764SConrad Meyer r = _stat64(infilename, &statbuf); 82a0483764SConrad Meyer if (!r && (statbuf.st_mode & _S_IFDIR)) return 1; 83a0483764SConrad Meyer #else 84a0483764SConrad Meyer r = stat(infilename, &statbuf); 85a0483764SConrad Meyer if (!r && S_ISDIR(statbuf.st_mode)) return 1; 86a0483764SConrad Meyer #endif 87a0483764SConrad Meyer return 0; 88a0483764SConrad Meyer } 89a0483764SConrad Meyer 902b9c00cbSConrad Meyer int UTIL_isSameFile(const char* file1, const char* file2) 912b9c00cbSConrad Meyer { 922b9c00cbSConrad Meyer #if defined(_MSC_VER) 932b9c00cbSConrad Meyer /* note : Visual does not support file identification by inode. 942b9c00cbSConrad Meyer * The following work-around is limited to detecting exact name repetition only, 952b9c00cbSConrad Meyer * aka `filename` is considered different from `subdir/../filename` */ 962b9c00cbSConrad Meyer return !strcmp(file1, file2); 972b9c00cbSConrad Meyer #else 982b9c00cbSConrad Meyer stat_t file1Stat; 992b9c00cbSConrad Meyer stat_t file2Stat; 1002b9c00cbSConrad Meyer return UTIL_getFileStat(file1, &file1Stat) 1012b9c00cbSConrad Meyer && UTIL_getFileStat(file2, &file2Stat) 1022b9c00cbSConrad Meyer && (file1Stat.st_dev == file2Stat.st_dev) 1032b9c00cbSConrad Meyer && (file1Stat.st_ino == file2Stat.st_ino); 1042b9c00cbSConrad Meyer #endif 1052b9c00cbSConrad Meyer } 1062b9c00cbSConrad Meyer 107a0483764SConrad Meyer U32 UTIL_isLink(const char* infilename) 108a0483764SConrad Meyer { 109a0483764SConrad Meyer /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ 110*4d3f1eafSConrad Meyer #if PLATFORM_POSIX_VERSION >= 200112L 111a0483764SConrad Meyer int r; 112a0483764SConrad Meyer stat_t statbuf; 113a0483764SConrad Meyer r = lstat(infilename, &statbuf); 114a0483764SConrad Meyer if (!r && S_ISLNK(statbuf.st_mode)) return 1; 115a0483764SConrad Meyer #endif 116a0483764SConrad Meyer (void)infilename; 117a0483764SConrad Meyer return 0; 118a0483764SConrad Meyer } 119a0483764SConrad Meyer 120a0483764SConrad Meyer U64 UTIL_getFileSize(const char* infilename) 121a0483764SConrad Meyer { 122a0483764SConrad Meyer if (!UTIL_isRegularFile(infilename)) return UTIL_FILESIZE_UNKNOWN; 123a0483764SConrad Meyer { int r; 124a0483764SConrad Meyer #if defined(_MSC_VER) 125a0483764SConrad Meyer struct __stat64 statbuf; 126a0483764SConrad Meyer r = _stat64(infilename, &statbuf); 127a0483764SConrad Meyer if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; 128a0483764SConrad Meyer #elif defined(__MINGW32__) && defined (__MSVCRT__) 129a0483764SConrad Meyer struct _stati64 statbuf; 130a0483764SConrad Meyer r = _stati64(infilename, &statbuf); 131a0483764SConrad Meyer if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; 132a0483764SConrad Meyer #else 133a0483764SConrad Meyer struct stat statbuf; 134a0483764SConrad Meyer r = stat(infilename, &statbuf); 135a0483764SConrad Meyer if (r || !S_ISREG(statbuf.st_mode)) return UTIL_FILESIZE_UNKNOWN; 136a0483764SConrad Meyer #endif 137a0483764SConrad Meyer return (U64)statbuf.st_size; 138a0483764SConrad Meyer } 139a0483764SConrad Meyer } 140a0483764SConrad Meyer 141a0483764SConrad Meyer 142a0483764SConrad Meyer U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles) 143a0483764SConrad Meyer { 144a0483764SConrad Meyer U64 total = 0; 145a0483764SConrad Meyer int error = 0; 146a0483764SConrad Meyer unsigned n; 147a0483764SConrad Meyer for (n=0; n<nbFiles; n++) { 148a0483764SConrad Meyer U64 const size = UTIL_getFileSize(fileNamesTable[n]); 149a0483764SConrad Meyer error |= (size == UTIL_FILESIZE_UNKNOWN); 150a0483764SConrad Meyer total += size; 151a0483764SConrad Meyer } 152a0483764SConrad Meyer return error ? UTIL_FILESIZE_UNKNOWN : total; 153a0483764SConrad Meyer } 154a0483764SConrad Meyer 155a0483764SConrad Meyer #ifdef _WIN32 156a0483764SConrad Meyer int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) 157a0483764SConrad Meyer { 158a0483764SConrad Meyer char* path; 159a0483764SConrad Meyer int dirLength, fnameLength, pathLength, nbFiles = 0; 160a0483764SConrad Meyer WIN32_FIND_DATAA cFile; 161a0483764SConrad Meyer HANDLE hFile; 162a0483764SConrad Meyer 163a0483764SConrad Meyer dirLength = (int)strlen(dirName); 164a0483764SConrad Meyer path = (char*) malloc(dirLength + 3); 165a0483764SConrad Meyer if (!path) return 0; 166a0483764SConrad Meyer 167a0483764SConrad Meyer memcpy(path, dirName, dirLength); 168a0483764SConrad Meyer path[dirLength] = '\\'; 169a0483764SConrad Meyer path[dirLength+1] = '*'; 170a0483764SConrad Meyer path[dirLength+2] = 0; 171a0483764SConrad Meyer 172a0483764SConrad Meyer hFile=FindFirstFileA(path, &cFile); 173a0483764SConrad Meyer if (hFile == INVALID_HANDLE_VALUE) { 174a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName); 175a0483764SConrad Meyer return 0; 176a0483764SConrad Meyer } 177a0483764SConrad Meyer free(path); 178a0483764SConrad Meyer 179a0483764SConrad Meyer do { 180a0483764SConrad Meyer fnameLength = (int)strlen(cFile.cFileName); 181a0483764SConrad Meyer path = (char*) malloc(dirLength + fnameLength + 2); 182a0483764SConrad Meyer if (!path) { FindClose(hFile); return 0; } 183a0483764SConrad Meyer memcpy(path, dirName, dirLength); 184a0483764SConrad Meyer path[dirLength] = '\\'; 185a0483764SConrad Meyer memcpy(path+dirLength+1, cFile.cFileName, fnameLength); 186a0483764SConrad Meyer pathLength = dirLength+1+fnameLength; 187a0483764SConrad Meyer path[pathLength] = 0; 188a0483764SConrad Meyer if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 189a0483764SConrad Meyer if ( strcmp (cFile.cFileName, "..") == 0 190a0483764SConrad Meyer || strcmp (cFile.cFileName, ".") == 0 ) 191a0483764SConrad Meyer continue; 192a0483764SConrad Meyer /* Recursively call "UTIL_prepareFileList" with the new path. */ 193a0483764SConrad Meyer nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); 194a0483764SConrad Meyer if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } 195a0483764SConrad Meyer } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) 196a0483764SConrad Meyer || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) 197a0483764SConrad Meyer || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) { 198a0483764SConrad Meyer if (*bufStart + *pos + pathLength >= *bufEnd) { 199a0483764SConrad Meyer ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; 200a0483764SConrad Meyer *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); 201a0483764SConrad Meyer if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } 202a0483764SConrad Meyer *bufEnd = *bufStart + newListSize; 203a0483764SConrad Meyer } 204a0483764SConrad Meyer if (*bufStart + *pos + pathLength < *bufEnd) { 205a0483764SConrad Meyer memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */); 206a0483764SConrad Meyer *pos += pathLength + 1; 207a0483764SConrad Meyer nbFiles++; 208a0483764SConrad Meyer } 209a0483764SConrad Meyer } 210a0483764SConrad Meyer free(path); 211a0483764SConrad Meyer } while (FindNextFileA(hFile, &cFile)); 212a0483764SConrad Meyer 213a0483764SConrad Meyer FindClose(hFile); 214a0483764SConrad Meyer return nbFiles; 215a0483764SConrad Meyer } 216a0483764SConrad Meyer 217a0483764SConrad Meyer #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ 218a0483764SConrad Meyer 219a0483764SConrad Meyer int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) 220a0483764SConrad Meyer { 221a0483764SConrad Meyer DIR *dir; 222a0483764SConrad Meyer struct dirent *entry; 223a0483764SConrad Meyer char* path; 224a0483764SConrad Meyer int dirLength, fnameLength, pathLength, nbFiles = 0; 225a0483764SConrad Meyer 226a0483764SConrad Meyer if (!(dir = opendir(dirName))) { 227a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); 228a0483764SConrad Meyer return 0; 229a0483764SConrad Meyer } 230a0483764SConrad Meyer 231a0483764SConrad Meyer dirLength = (int)strlen(dirName); 232a0483764SConrad Meyer errno = 0; 233a0483764SConrad Meyer while ((entry = readdir(dir)) != NULL) { 234a0483764SConrad Meyer if (strcmp (entry->d_name, "..") == 0 || 235a0483764SConrad Meyer strcmp (entry->d_name, ".") == 0) continue; 236a0483764SConrad Meyer fnameLength = (int)strlen(entry->d_name); 237a0483764SConrad Meyer path = (char*) malloc(dirLength + fnameLength + 2); 238a0483764SConrad Meyer if (!path) { closedir(dir); return 0; } 239a0483764SConrad Meyer memcpy(path, dirName, dirLength); 240a0483764SConrad Meyer 241a0483764SConrad Meyer path[dirLength] = '/'; 242a0483764SConrad Meyer memcpy(path+dirLength+1, entry->d_name, fnameLength); 243a0483764SConrad Meyer pathLength = dirLength+1+fnameLength; 244a0483764SConrad Meyer path[pathLength] = 0; 245a0483764SConrad Meyer 246a0483764SConrad Meyer if (!followLinks && UTIL_isLink(path)) { 247a0483764SConrad Meyer UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path); 248*4d3f1eafSConrad Meyer free(path); 249a0483764SConrad Meyer continue; 250a0483764SConrad Meyer } 251a0483764SConrad Meyer 252a0483764SConrad Meyer if (UTIL_isDirectory(path)) { 253a0483764SConrad Meyer nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */ 254a0483764SConrad Meyer if (*bufStart == NULL) { free(path); closedir(dir); return 0; } 255a0483764SConrad Meyer } else { 256a0483764SConrad Meyer if (*bufStart + *pos + pathLength >= *bufEnd) { 257a0483764SConrad Meyer ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; 258a0483764SConrad Meyer *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); 259a0483764SConrad Meyer *bufEnd = *bufStart + newListSize; 260a0483764SConrad Meyer if (*bufStart == NULL) { free(path); closedir(dir); return 0; } 261a0483764SConrad Meyer } 262a0483764SConrad Meyer if (*bufStart + *pos + pathLength < *bufEnd) { 263a0483764SConrad Meyer memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */ 264a0483764SConrad Meyer *pos += pathLength + 1; 265a0483764SConrad Meyer nbFiles++; 266a0483764SConrad Meyer } 267a0483764SConrad Meyer } 268a0483764SConrad Meyer free(path); 269a0483764SConrad Meyer errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */ 270a0483764SConrad Meyer } 271a0483764SConrad Meyer 272a0483764SConrad Meyer if (errno != 0) { 273a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno)); 274a0483764SConrad Meyer free(*bufStart); 275a0483764SConrad Meyer *bufStart = NULL; 276a0483764SConrad Meyer } 277a0483764SConrad Meyer closedir(dir); 278a0483764SConrad Meyer return nbFiles; 279a0483764SConrad Meyer } 280a0483764SConrad Meyer 281a0483764SConrad Meyer #else 282a0483764SConrad Meyer 283a0483764SConrad Meyer int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) 284a0483764SConrad Meyer { 285a0483764SConrad Meyer (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks; 286a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName); 287a0483764SConrad Meyer return 0; 288a0483764SConrad Meyer } 289a0483764SConrad Meyer 290a0483764SConrad Meyer #endif /* #ifdef _WIN32 */ 291a0483764SConrad Meyer 292a0483764SConrad Meyer /* 293a0483764SConrad Meyer * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories, 294a0483764SConrad Meyer * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb). 295a0483764SConrad Meyer * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer) 296a0483764SConrad Meyer * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. 297a0483764SConrad Meyer */ 298a0483764SConrad Meyer const char** 299a0483764SConrad Meyer UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, 300a0483764SConrad Meyer char** allocatedBuffer, unsigned* allocatedNamesNb, 301a0483764SConrad Meyer int followLinks) 302a0483764SConrad Meyer { 303a0483764SConrad Meyer size_t pos; 304a0483764SConrad Meyer unsigned i, nbFiles; 305a0483764SConrad Meyer char* buf = (char*)malloc(LIST_SIZE_INCREASE); 306a0483764SConrad Meyer char* bufend = buf + LIST_SIZE_INCREASE; 307a0483764SConrad Meyer const char** fileTable; 308a0483764SConrad Meyer 309a0483764SConrad Meyer if (!buf) return NULL; 310a0483764SConrad Meyer 311a0483764SConrad Meyer for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) { 312a0483764SConrad Meyer if (!UTIL_isDirectory(inputNames[i])) { 313a0483764SConrad Meyer size_t const len = strlen(inputNames[i]); 314a0483764SConrad Meyer if (buf + pos + len >= bufend) { 315a0483764SConrad Meyer ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; 316a0483764SConrad Meyer buf = (char*)UTIL_realloc(buf, newListSize); 317a0483764SConrad Meyer bufend = buf + newListSize; 318a0483764SConrad Meyer if (!buf) return NULL; 319a0483764SConrad Meyer } 320a0483764SConrad Meyer if (buf + pos + len < bufend) { 321a0483764SConrad Meyer memcpy(buf+pos, inputNames[i], len+1); /* with final \0 */ 322a0483764SConrad Meyer pos += len + 1; 323a0483764SConrad Meyer nbFiles++; 324a0483764SConrad Meyer } 325a0483764SConrad Meyer } else { 326a0483764SConrad Meyer nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks); 327a0483764SConrad Meyer if (buf == NULL) return NULL; 328a0483764SConrad Meyer } } 329a0483764SConrad Meyer 330a0483764SConrad Meyer if (nbFiles == 0) { free(buf); return NULL; } 331a0483764SConrad Meyer 332a0483764SConrad Meyer fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*)); 333a0483764SConrad Meyer if (!fileTable) { free(buf); return NULL; } 334a0483764SConrad Meyer 335a0483764SConrad Meyer for (i=0, pos=0; i<nbFiles; i++) { 336a0483764SConrad Meyer fileTable[i] = buf + pos; 337a0483764SConrad Meyer pos += strlen(fileTable[i]) + 1; 338a0483764SConrad Meyer } 339a0483764SConrad Meyer 340a0483764SConrad Meyer if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; } 341a0483764SConrad Meyer 342a0483764SConrad Meyer *allocatedBuffer = buf; 343a0483764SConrad Meyer *allocatedNamesNb = nbFiles; 344a0483764SConrad Meyer 345a0483764SConrad Meyer return fileTable; 346a0483764SConrad Meyer } 347a0483764SConrad Meyer 3482b9c00cbSConrad Meyer 349a0483764SConrad Meyer /*-**************************************** 350a0483764SConrad Meyer * Console log 351a0483764SConrad Meyer ******************************************/ 352a0483764SConrad Meyer int g_utilDisplayLevel; 353a0483764SConrad Meyer 354a0483764SConrad Meyer 3552b9c00cbSConrad Meyer 356a0483764SConrad Meyer /*-**************************************** 3572b9c00cbSConrad Meyer * count the number of physical cores 358a0483764SConrad Meyer ******************************************/ 359a0483764SConrad Meyer 360a0483764SConrad Meyer #if defined(_WIN32) || defined(WIN32) 361a0483764SConrad Meyer 362a0483764SConrad Meyer #include <windows.h> 363a0483764SConrad Meyer 364a0483764SConrad Meyer typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); 365a0483764SConrad Meyer 366a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 367a0483764SConrad Meyer { 368a0483764SConrad Meyer static int numPhysicalCores = 0; 369a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 370a0483764SConrad Meyer 371a0483764SConrad Meyer { LPFN_GLPI glpi; 372a0483764SConrad Meyer BOOL done = FALSE; 373a0483764SConrad Meyer PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; 374a0483764SConrad Meyer PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; 375a0483764SConrad Meyer DWORD returnLength = 0; 376a0483764SConrad Meyer size_t byteOffset = 0; 377a0483764SConrad Meyer 378a0483764SConrad Meyer glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), 379a0483764SConrad Meyer "GetLogicalProcessorInformation"); 380a0483764SConrad Meyer 381a0483764SConrad Meyer if (glpi == NULL) { 382a0483764SConrad Meyer goto failed; 383a0483764SConrad Meyer } 384a0483764SConrad Meyer 385a0483764SConrad Meyer while(!done) { 386a0483764SConrad Meyer DWORD rc = glpi(buffer, &returnLength); 387a0483764SConrad Meyer if (FALSE == rc) { 388a0483764SConrad Meyer if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { 389a0483764SConrad Meyer if (buffer) 390a0483764SConrad Meyer free(buffer); 391a0483764SConrad Meyer buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength); 392a0483764SConrad Meyer 393a0483764SConrad Meyer if (buffer == NULL) { 394a0483764SConrad Meyer perror("zstd"); 395a0483764SConrad Meyer exit(1); 396a0483764SConrad Meyer } 397a0483764SConrad Meyer } else { 398a0483764SConrad Meyer /* some other error */ 399a0483764SConrad Meyer goto failed; 400a0483764SConrad Meyer } 401a0483764SConrad Meyer } else { 402a0483764SConrad Meyer done = TRUE; 403a0483764SConrad Meyer } 404a0483764SConrad Meyer } 405a0483764SConrad Meyer 406a0483764SConrad Meyer ptr = buffer; 407a0483764SConrad Meyer 408a0483764SConrad Meyer while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) { 409a0483764SConrad Meyer 410a0483764SConrad Meyer if (ptr->Relationship == RelationProcessorCore) { 411a0483764SConrad Meyer numPhysicalCores++; 412a0483764SConrad Meyer } 413a0483764SConrad Meyer 414a0483764SConrad Meyer ptr++; 415a0483764SConrad Meyer byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); 416a0483764SConrad Meyer } 417a0483764SConrad Meyer 418a0483764SConrad Meyer free(buffer); 419a0483764SConrad Meyer 420a0483764SConrad Meyer return numPhysicalCores; 421a0483764SConrad Meyer } 422a0483764SConrad Meyer 423a0483764SConrad Meyer failed: 424a0483764SConrad Meyer /* try to fall back on GetSystemInfo */ 425a0483764SConrad Meyer { SYSTEM_INFO sysinfo; 426a0483764SConrad Meyer GetSystemInfo(&sysinfo); 427a0483764SConrad Meyer numPhysicalCores = sysinfo.dwNumberOfProcessors; 428a0483764SConrad Meyer if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */ 429a0483764SConrad Meyer } 430a0483764SConrad Meyer return numPhysicalCores; 431a0483764SConrad Meyer } 432a0483764SConrad Meyer 433a0483764SConrad Meyer #elif defined(__APPLE__) 434a0483764SConrad Meyer 435a0483764SConrad Meyer #include <sys/sysctl.h> 436a0483764SConrad Meyer 437a0483764SConrad Meyer /* Use apple-provided syscall 438a0483764SConrad Meyer * see: man 3 sysctl */ 439a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 440a0483764SConrad Meyer { 441a0483764SConrad Meyer static S32 numPhysicalCores = 0; /* apple specifies int32_t */ 442a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 443a0483764SConrad Meyer 444a0483764SConrad Meyer { size_t size = sizeof(S32); 445a0483764SConrad Meyer int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0); 446a0483764SConrad Meyer if (ret != 0) { 447a0483764SConrad Meyer if (errno == ENOENT) { 448a0483764SConrad Meyer /* entry not present, fall back on 1 */ 449a0483764SConrad Meyer numPhysicalCores = 1; 450a0483764SConrad Meyer } else { 451a0483764SConrad Meyer perror("zstd: can't get number of physical cpus"); 452a0483764SConrad Meyer exit(1); 453a0483764SConrad Meyer } 454a0483764SConrad Meyer } 455a0483764SConrad Meyer 456a0483764SConrad Meyer return numPhysicalCores; 457a0483764SConrad Meyer } 458a0483764SConrad Meyer } 459a0483764SConrad Meyer 460a0483764SConrad Meyer #elif defined(__linux__) 461a0483764SConrad Meyer 462a0483764SConrad Meyer /* parse /proc/cpuinfo 463a0483764SConrad Meyer * siblings / cpu cores should give hyperthreading ratio 464a0483764SConrad Meyer * otherwise fall back on sysconf */ 465a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 466a0483764SConrad Meyer { 467a0483764SConrad Meyer static int numPhysicalCores = 0; 468a0483764SConrad Meyer 469a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 470a0483764SConrad Meyer 471a0483764SConrad Meyer numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); 472a0483764SConrad Meyer if (numPhysicalCores == -1) { 473a0483764SConrad Meyer /* value not queryable, fall back on 1 */ 474a0483764SConrad Meyer return numPhysicalCores = 1; 475a0483764SConrad Meyer } 476a0483764SConrad Meyer 477a0483764SConrad Meyer /* try to determine if there's hyperthreading */ 478a0483764SConrad Meyer { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r"); 479a0483764SConrad Meyer #define BUF_SIZE 80 480a0483764SConrad Meyer char buff[BUF_SIZE]; 481a0483764SConrad Meyer 482a0483764SConrad Meyer int siblings = 0; 483a0483764SConrad Meyer int cpu_cores = 0; 484a0483764SConrad Meyer int ratio = 1; 485a0483764SConrad Meyer 486a0483764SConrad Meyer if (cpuinfo == NULL) { 487a0483764SConrad Meyer /* fall back on the sysconf value */ 488a0483764SConrad Meyer return numPhysicalCores; 489a0483764SConrad Meyer } 490a0483764SConrad Meyer 491a0483764SConrad Meyer /* assume the cpu cores/siblings values will be constant across all 492a0483764SConrad Meyer * present processors */ 493a0483764SConrad Meyer while (!feof(cpuinfo)) { 494a0483764SConrad Meyer if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) { 495a0483764SConrad Meyer if (strncmp(buff, "siblings", 8) == 0) { 496a0483764SConrad Meyer const char* const sep = strchr(buff, ':'); 497a0483764SConrad Meyer if (*sep == '\0') { 498a0483764SConrad Meyer /* formatting was broken? */ 499a0483764SConrad Meyer goto failed; 500a0483764SConrad Meyer } 501a0483764SConrad Meyer 502a0483764SConrad Meyer siblings = atoi(sep + 1); 503a0483764SConrad Meyer } 504a0483764SConrad Meyer if (strncmp(buff, "cpu cores", 9) == 0) { 505a0483764SConrad Meyer const char* const sep = strchr(buff, ':'); 506a0483764SConrad Meyer if (*sep == '\0') { 507a0483764SConrad Meyer /* formatting was broken? */ 508a0483764SConrad Meyer goto failed; 509a0483764SConrad Meyer } 510a0483764SConrad Meyer 511a0483764SConrad Meyer cpu_cores = atoi(sep + 1); 512a0483764SConrad Meyer } 513a0483764SConrad Meyer } else if (ferror(cpuinfo)) { 514a0483764SConrad Meyer /* fall back on the sysconf value */ 515a0483764SConrad Meyer goto failed; 516a0483764SConrad Meyer } 517a0483764SConrad Meyer } 518a0483764SConrad Meyer if (siblings && cpu_cores) { 519a0483764SConrad Meyer ratio = siblings / cpu_cores; 520a0483764SConrad Meyer } 521a0483764SConrad Meyer failed: 522a0483764SConrad Meyer fclose(cpuinfo); 523a0483764SConrad Meyer return numPhysicalCores = numPhysicalCores / ratio; 524a0483764SConrad Meyer } 525a0483764SConrad Meyer } 526a0483764SConrad Meyer 5272b9c00cbSConrad Meyer #elif defined(__FreeBSD__) 528a0483764SConrad Meyer 5292b9c00cbSConrad Meyer #include <sys/param.h> 5302b9c00cbSConrad Meyer #include <sys/sysctl.h> 5312b9c00cbSConrad Meyer 5322b9c00cbSConrad Meyer /* Use physical core sysctl when available 5332b9c00cbSConrad Meyer * see: man 4 smp, man 3 sysctl */ 5342b9c00cbSConrad Meyer int UTIL_countPhysicalCores(void) 5352b9c00cbSConrad Meyer { 5362b9c00cbSConrad Meyer static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */ 5372b9c00cbSConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 5382b9c00cbSConrad Meyer 5392b9c00cbSConrad Meyer #if __FreeBSD_version >= 1300008 5402b9c00cbSConrad Meyer { size_t size = sizeof(numPhysicalCores); 5412b9c00cbSConrad Meyer int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0); 5422b9c00cbSConrad Meyer if (ret == 0) return numPhysicalCores; 5432b9c00cbSConrad Meyer if (errno != ENOENT) { 5442b9c00cbSConrad Meyer perror("zstd: can't get number of physical cpus"); 5452b9c00cbSConrad Meyer exit(1); 5462b9c00cbSConrad Meyer } 5472b9c00cbSConrad Meyer /* sysctl not present, fall through to older sysconf method */ 5482b9c00cbSConrad Meyer } 5492b9c00cbSConrad Meyer #endif 5502b9c00cbSConrad Meyer 5512b9c00cbSConrad Meyer numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); 5522b9c00cbSConrad Meyer if (numPhysicalCores == -1) { 5532b9c00cbSConrad Meyer /* value not queryable, fall back on 1 */ 5542b9c00cbSConrad Meyer numPhysicalCores = 1; 5552b9c00cbSConrad Meyer } 5562b9c00cbSConrad Meyer return numPhysicalCores; 5572b9c00cbSConrad Meyer } 5582b9c00cbSConrad Meyer 5592b9c00cbSConrad Meyer #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) 5602b9c00cbSConrad Meyer 5612b9c00cbSConrad Meyer /* Use POSIX sysconf 5622b9c00cbSConrad Meyer * see: man 3 sysconf */ 563a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 564a0483764SConrad Meyer { 565a0483764SConrad Meyer static int numPhysicalCores = 0; 566a0483764SConrad Meyer 567a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 568a0483764SConrad Meyer 569a0483764SConrad Meyer numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); 570a0483764SConrad Meyer if (numPhysicalCores == -1) { 571a0483764SConrad Meyer /* value not queryable, fall back on 1 */ 572a0483764SConrad Meyer return numPhysicalCores = 1; 573a0483764SConrad Meyer } 574a0483764SConrad Meyer return numPhysicalCores; 575a0483764SConrad Meyer } 576a0483764SConrad Meyer 577a0483764SConrad Meyer #else 578a0483764SConrad Meyer 579a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 580a0483764SConrad Meyer { 581a0483764SConrad Meyer /* assume 1 */ 582a0483764SConrad Meyer return 1; 583a0483764SConrad Meyer } 584a0483764SConrad Meyer 585a0483764SConrad Meyer #endif 586a0483764SConrad Meyer 587a0483764SConrad Meyer #if defined (__cplusplus) 588a0483764SConrad Meyer } 589a0483764SConrad Meyer #endif 590