| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "base/sys_info.h" | |
| 6 | |
| 7 #include <errno.h> | |
| 8 #include <string.h> | |
| 9 #include <sys/param.h> | |
| 10 #include <sys/resource.h> | |
| 11 #include <sys/utsname.h> | |
| 12 #include <unistd.h> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/files/file_util.h" | |
| 16 #include "base/lazy_instance.h" | |
| 17 #include "base/logging.h" | |
| 18 #include "base/strings/utf_string_conversions.h" | |
| 19 #include "base/sys_info_internal.h" | |
| 20 #include "base/threading/thread_restrictions.h" | |
| 21 | |
| 22 #if defined(OS_ANDROID) | |
| 23 #include <sys/vfs.h> | |
| 24 #define statvfs statfs // Android uses a statvfs-like statfs struct and call. | |
| 25 #else | |
| 26 #include <sys/statvfs.h> | |
| 27 #endif | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 #if !defined(OS_OPENBSD) | |
| 32 int NumberOfProcessors() { | |
| 33 // sysconf returns the number of "logical" (not "physical") processors on both | |
| 34 // Mac and Linux. So we get the number of max available "logical" processors. | |
| 35 // | |
| 36 // Note that the number of "currently online" processors may be fewer than the | |
| 37 // returned value of NumberOfProcessors(). On some platforms, the kernel may | |
| 38 // make some processors offline intermittently, to save power when system | |
| 39 // loading is low. | |
| 40 // | |
| 41 // One common use case that needs to know the processor count is to create | |
| 42 // optimal number of threads for optimization. It should make plan according | |
| 43 // to the number of "max available" processors instead of "currently online" | |
| 44 // ones. The kernel should be smart enough to make all processors online when | |
| 45 // it has sufficient number of threads waiting to run. | |
| 46 long res = sysconf(_SC_NPROCESSORS_CONF); | |
| 47 if (res == -1) { | |
| 48 NOTREACHED(); | |
| 49 return 1; | |
| 50 } | |
| 51 | |
| 52 return static_cast<int>(res); | |
| 53 } | |
| 54 | |
| 55 base::LazyInstance< | |
| 56 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky | |
| 57 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; | |
| 58 #endif | |
| 59 | |
| 60 int64 AmountOfVirtualMemory() { | |
| 61 struct rlimit limit; | |
| 62 int result = getrlimit(RLIMIT_DATA, &limit); | |
| 63 if (result != 0) { | |
| 64 NOTREACHED(); | |
| 65 return 0; | |
| 66 } | |
| 67 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; | |
| 68 } | |
| 69 | |
| 70 base::LazyInstance< | |
| 71 base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky | |
| 72 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 namespace base { | |
| 77 | |
| 78 #if !defined(OS_OPENBSD) | |
| 79 int SysInfo::NumberOfProcessors() { | |
| 80 return g_lazy_number_of_processors.Get().value(); | |
| 81 } | |
| 82 #endif | |
| 83 | |
| 84 // static | |
| 85 int64 SysInfo::AmountOfVirtualMemory() { | |
| 86 return g_lazy_virtual_memory.Get().value(); | |
| 87 } | |
| 88 | |
| 89 // static | |
| 90 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { | |
| 91 base::ThreadRestrictions::AssertIOAllowed(); | |
| 92 | |
| 93 struct statvfs stats; | |
| 94 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) | |
| 95 return -1; | |
| 96 return static_cast<int64>(stats.f_bavail) * stats.f_frsize; | |
| 97 } | |
| 98 | |
| 99 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
| 100 // static | |
| 101 std::string SysInfo::OperatingSystemName() { | |
| 102 struct utsname info; | |
| 103 if (uname(&info) < 0) { | |
| 104 NOTREACHED(); | |
| 105 return std::string(); | |
| 106 } | |
| 107 return std::string(info.sysname); | |
| 108 } | |
| 109 #endif | |
| 110 | |
| 111 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
| 112 // static | |
| 113 std::string SysInfo::OperatingSystemVersion() { | |
| 114 struct utsname info; | |
| 115 if (uname(&info) < 0) { | |
| 116 NOTREACHED(); | |
| 117 return std::string(); | |
| 118 } | |
| 119 return std::string(info.release); | |
| 120 } | |
| 121 #endif | |
| 122 | |
| 123 // static | |
| 124 std::string SysInfo::OperatingSystemArchitecture() { | |
| 125 struct utsname info; | |
| 126 if (uname(&info) < 0) { | |
| 127 NOTREACHED(); | |
| 128 return std::string(); | |
| 129 } | |
| 130 std::string arch(info.machine); | |
| 131 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { | |
| 132 arch = "x86"; | |
| 133 } else if (arch == "amd64") { | |
| 134 arch = "x86_64"; | |
| 135 } | |
| 136 return arch; | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 size_t SysInfo::VMAllocationGranularity() { | |
| 141 return getpagesize(); | |
| 142 } | |
| 143 | |
| 144 } // namespace base | |
| OLD | NEW |