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.
5 #include "base/sys_info.h"
10 #include <sys/resource.h>
11 #include <sys/utsname.h>
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"
22 #if defined(OS_ANDROID)
24 #define statvfs statfs // Android uses a statvfs-like statfs struct and call.
26 #include <sys/statvfs.h>
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.
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
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);
52 return static_cast<int>(res);
56 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
57 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
60 int64 AmountOfVirtualMemory() {
62 int result = getrlimit(RLIMIT_DATA, &limit);
67 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur;
71 base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky
72 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
78 #if !defined(OS_OPENBSD)
79 int SysInfo::NumberOfProcessors() {
80 return g_lazy_number_of_processors.Get().value();
85 int64 SysInfo::AmountOfVirtualMemory() {
86 return g_lazy_virtual_memory.Get().value();
90 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
91 base::ThreadRestrictions::AssertIOAllowed();
94 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
96 return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
99 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
101 std::string SysInfo::OperatingSystemName() {
103 if (uname(&info) < 0) {
105 return std::string();
107 return std::string(info.sysname);
111 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
113 std::string SysInfo::OperatingSystemVersion() {
115 if (uname(&info) < 0) {
117 return std::string();
119 return std::string(info.release);
124 std::string SysInfo::OperatingSystemArchitecture() {
126 if (uname(&info) < 0) {
128 return std::string();
130 std::string arch(info.machine);
131 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
133 } else if (arch == "amd64") {
140 size_t SysInfo::VMAllocationGranularity() {
141 return getpagesize();