• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()19 std::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   std::string os(info.sysname);
28   if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
29     arch = "x86";
30   } else if (arch == "i86pc") {
31     // Solaris and illumos systems report 'i86pc' (an Intel x86 PC) as their
32     // machine for both 32-bit and 64-bit x86 systems.  Considering the rarity
33     // of 32-bit systems at this point, it is safe to assume 64-bit.
34     arch = "x86_64";
35   } else if (arch == "amd64") {
36     arch = "x86_64";
37   } else if (os == "AIX" || os == "OS400") {
38     arch = "ppc64";
39   } else if (std::string(info.sysname) == "OS/390") {
40     arch = "s390x";
41   }
42   return arch;
43 #elif defined(OS_WIN)
44   SYSTEM_INFO system_info = {};
45   ::GetNativeSystemInfo(&system_info);
46   switch (system_info.wProcessorArchitecture) {
47     case PROCESSOR_ARCHITECTURE_INTEL:
48       return "x86";
49     case PROCESSOR_ARCHITECTURE_AMD64:
50       return "x86_64";
51     case PROCESSOR_ARCHITECTURE_IA64:
52       return "ia64";
53   }
54   return std::string();
55 #else
56 #error
57 #endif
58 }
59 
NumberOfProcessors()60 int NumberOfProcessors() {
61 #if defined(OS_ZOS)
62   return __get_num_online_cpus();
63 
64 #elif defined(OS_POSIX)
65   // sysconf returns the number of "logical" (not "physical") processors on both
66   // Mac and Linux.  So we get the number of max available "logical" processors.
67   //
68   // Note that the number of "currently online" processors may be fewer than the
69   // returned value of NumberOfProcessors(). On some platforms, the kernel may
70   // make some processors offline intermittently, to save power when system
71   // loading is low.
72   //
73   // One common use case that needs to know the processor count is to create
74   // optimal number of threads for optimization. It should make plan according
75   // to the number of "max available" processors instead of "currently online"
76   // ones. The kernel should be smart enough to make all processors online when
77   // it has sufficient number of threads waiting to run.
78   long res = sysconf(_SC_NPROCESSORS_CONF);
79   if (res == -1) {
80     NOTREACHED();
81     return 1;
82   }
83 
84   return static_cast<int>(res);
85 #elif defined(OS_WIN)
86   return ::GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
87 #else
88 #error
89 #endif
90 }
91