1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "util/sys_info.h" 6 7 #include "base/logging.h" 8 #include "util/build_config.h" 9 10 #if defined(OS_POSIX) 11 #include <sys/utsname.h> 12 #include <unistd.h> 13 #endif 14 15 #if defined(OS_WIN) 16 #include <windows.h> 17 #endif 18 OperatingSystemArchitecture()19std::string OperatingSystemArchitecture() { 20 #if defined(OS_POSIX) 21 struct utsname info; 22 if (uname(&info) < 0) { 23 NOTREACHED(); 24 return std::string(); 25 } 26 std::string arch(info.machine); 27 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { 28 arch = "x86"; 29 } else if (arch == "amd64") { 30 arch = "x86_64"; 31 } else if (std::string(info.sysname) == "AIX") { 32 arch = "ppc64"; 33 } 34 return arch; 35 #elif defined(OS_WIN) 36 SYSTEM_INFO system_info = {}; 37 ::GetNativeSystemInfo(&system_info); 38 switch (system_info.wProcessorArchitecture) { 39 case PROCESSOR_ARCHITECTURE_INTEL: 40 return "x86"; 41 case PROCESSOR_ARCHITECTURE_AMD64: 42 return "x86_64"; 43 case PROCESSOR_ARCHITECTURE_IA64: 44 return "ia64"; 45 } 46 return std::string(); 47 #else 48 #error 49 #endif 50 } 51 NumberOfProcessors()52int NumberOfProcessors() { 53 #if defined(OS_POSIX) 54 // sysconf returns the number of "logical" (not "physical") processors on both 55 // Mac and Linux. So we get the number of max available "logical" processors. 56 // 57 // Note that the number of "currently online" processors may be fewer than the 58 // returned value of NumberOfProcessors(). On some platforms, the kernel may 59 // make some processors offline intermittently, to save power when system 60 // loading is low. 61 // 62 // One common use case that needs to know the processor count is to create 63 // optimal number of threads for optimization. It should make plan according 64 // to the number of "max available" processors instead of "currently online" 65 // ones. The kernel should be smart enough to make all processors online when 66 // it has sufficient number of threads waiting to run. 67 long res = sysconf(_SC_NPROCESSORS_CONF); 68 if (res == -1) { 69 NOTREACHED(); 70 return 1; 71 } 72 73 return static_cast<int>(res); 74 #elif defined(OS_WIN) 75 return ::GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); 76 #else 77 #error 78 #endif 79 } 80