blob: 5d1c450139a575705c3c5046ed32c565e4b5e404 [file] [log] [blame]
[email protected]6e001bb2011-05-25 20:53:181// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]2a758d612008-09-17 10:09:392// 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>
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9#include <stdint.h>
[email protected]2a758d612008-09-17 10:09:3910#include <string.h>
[email protected]e43eddf12009-12-29 00:32:5211#include <sys/param.h>
[email protected]275a29de2014-04-08 23:19:1612#include <sys/resource.h>
[email protected]05f9b682008-09-29 22:18:0113#include <sys/utsname.h>
[email protected]2a758d612008-09-17 10:09:3914#include <unistd.h>
15
[email protected]e3177dd52014-08-13 20:22:1416#include "base/files/file_util.h"
[email protected]c15d1e772013-11-20 11:14:5117#include "base/lazy_instance.h"
[email protected]2a758d612008-09-17 10:09:3918#include "base/logging.h"
[email protected]a4ea1f12013-06-07 18:37:0719#include "base/strings/utf_string_conversions.h"
[email protected]c15d1e772013-11-20 11:14:5120#include "base/sys_info_internal.h"
[email protected]dda29c942012-08-21 19:50:0321#include "base/threading/thread_restrictions.h"
avi9b6f42932015-12-26 22:15:1422#include "build/build_config.h"
[email protected]2a758d612008-09-17 10:09:3923
[email protected]f7d69972011-06-21 22:34:5024#if defined(OS_ANDROID)
25#include <sys/vfs.h>
26#define statvfs statfs // Android uses a statvfs-like statfs struct and call.
27#else
28#include <sys/statvfs.h>
[email protected]f7d69972011-06-21 22:34:5029#endif
30
[email protected]c15d1e772013-11-20 11:14:5131namespace {
[email protected]2a758d612008-09-17 10:09:3932
[email protected]5f5ac6a2009-11-23 21:57:1133#if !defined(OS_OPENBSD)
[email protected]c15d1e772013-11-20 11:14:5134int NumberOfProcessors() {
[email protected]d617e8b2014-08-18 04:52:4835 // sysconf returns the number of "logical" (not "physical") processors on both
36 // Mac and Linux. So we get the number of max available "logical" processors.
37 //
38 // Note that the number of "currently online" processors may be fewer than the
39 // returned value of NumberOfProcessors(). On some platforms, the kernel may
40 // make some processors offline intermittently, to save power when system
41 // loading is low.
42 //
43 // One common use case that needs to know the processor count is to create
44 // optimal number of threads for optimization. It should make plan according
45 // to the number of "max available" processors instead of "currently online"
46 // ones. The kernel should be smart enough to make all processors online when
47 // it has sufficient number of threads waiting to run.
48 long res = sysconf(_SC_NPROCESSORS_CONF);
[email protected]2a758d612008-09-17 10:09:3949 if (res == -1) {
50 NOTREACHED();
51 return 1;
52 }
53
54 return static_cast<int>(res);
[email protected]d632798e2008-09-17 13:10:4555}
[email protected]c15d1e772013-11-20 11:14:5156
phoglund5b052f02016-07-21 12:41:0457base::LazyInstance<
58 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
[email protected]c15d1e772013-11-20 11:14:5159 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
60#endif
61
avidd4e614352015-12-09 00:44:4962int64_t AmountOfVirtualMemory() {
[email protected]275a29de2014-04-08 23:19:1663 struct rlimit limit;
64 int result = getrlimit(RLIMIT_DATA, &limit);
65 if (result != 0) {
66 NOTREACHED();
67 return 0;
68 }
69 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur;
70}
71
phoglund5b052f02016-07-21 12:41:0472base::LazyInstance<
73 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky
[email protected]275a29de2014-04-08 23:19:1674 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
75
fukino50a44812016-06-10 19:35:5576bool GetDiskSpaceInfo(const base::FilePath& path,
77 int64_t* available_bytes,
78 int64_t* total_bytes) {
79 struct statvfs stats;
80 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
81 return false;
82
phoglund5b052f02016-07-21 12:41:0483 if (available_bytes)
84 *available_bytes = static_cast<int64_t>(stats.f_bavail) * stats.f_frsize;
85 if (total_bytes)
86 *total_bytes = static_cast<int64_t>(stats.f_blocks) * stats.f_frsize;
fukino50a44812016-06-10 19:35:5587 return true;
88}
89
[email protected]c15d1e772013-11-20 11:14:5190} // namespace
91
phoglund5b052f02016-07-21 12:41:0492namespace base {
93
[email protected]c15d1e772013-11-20 11:14:5194#if !defined(OS_OPENBSD)
95int SysInfo::NumberOfProcessors() {
96 return g_lazy_number_of_processors.Get().value();
97}
[email protected]c8587632009-11-12 02:17:1998#endif
[email protected]d632798e2008-09-17 13:10:4599
[email protected]0e91dd22008-09-18 12:34:24100// static
avidd4e614352015-12-09 00:44:49101int64_t SysInfo::AmountOfVirtualMemory() {
[email protected]275a29de2014-04-08 23:19:16102 return g_lazy_virtual_memory.Get().value();
103}
104
105// static
avidd4e614352015-12-09 00:44:49106int64_t SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
[email protected]dda29c942012-08-21 19:50:03107 base::ThreadRestrictions::AssertIOAllowed();
108
fukino50a44812016-06-10 19:35:55109 int64_t available;
110 if (!GetDiskSpaceInfo(path, &available, nullptr))
[email protected]02ee34a2008-09-20 01:16:23111 return -1;
fukino50a44812016-06-10 19:35:55112 return available;
113}
114
115// static
116int64_t SysInfo::AmountOfTotalDiskSpace(const FilePath& path) {
117 base::ThreadRestrictions::AssertIOAllowed();
118
119 int64_t total;
120 if (!GetDiskSpaceInfo(path, nullptr, &total))
121 return -1;
122 return total;
[email protected]0e91dd22008-09-18 12:34:24123}
124
[email protected]5106b3a2012-10-03 20:10:44125#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]05f9b682008-09-29 22:18:01126// static
[email protected]05f9b682008-09-29 22:18:01127std::string SysInfo::OperatingSystemName() {
[email protected]94f8c952011-06-25 04:54:41128 struct utsname info;
[email protected]05f9b682008-09-29 22:18:01129 if (uname(&info) < 0) {
130 NOTREACHED();
[email protected]007b3f82013-04-09 08:46:45131 return std::string();
[email protected]05f9b682008-09-29 22:18:01132 }
133 return std::string(info.sysname);
134}
[email protected]5106b3a2012-10-03 20:10:44135#endif
[email protected]05f9b682008-09-29 22:18:01136
[email protected]82aeeaa2013-05-07 04:52:45137#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]05f9b682008-09-29 22:18:01138// static
139std::string SysInfo::OperatingSystemVersion() {
[email protected]94f8c952011-06-25 04:54:41140 struct utsname info;
[email protected]05f9b682008-09-29 22:18:01141 if (uname(&info) < 0) {
142 NOTREACHED();
[email protected]007b3f82013-04-09 08:46:45143 return std::string();
[email protected]05f9b682008-09-29 22:18:01144 }
145 return std::string(info.release);
146}
[email protected]6e001bb2011-05-25 20:53:18147#endif
[email protected]05f9b682008-09-29 22:18:01148
149// static
[email protected]0b6a4fb2012-10-16 01:58:21150std::string SysInfo::OperatingSystemArchitecture() {
[email protected]94f8c952011-06-25 04:54:41151 struct utsname info;
[email protected]05f9b682008-09-29 22:18:01152 if (uname(&info) < 0) {
153 NOTREACHED();
[email protected]007b3f82013-04-09 08:46:45154 return std::string();
[email protected]05f9b682008-09-29 22:18:01155 }
[email protected]56d0cef2012-09-26 02:49:42156 std::string arch(info.machine);
157 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
158 arch = "x86";
159 } else if (arch == "amd64") {
160 arch = "x86_64";
161 }
162 return arch;
[email protected]05f9b682008-09-29 22:18:01163}
164
[email protected]037fce02009-01-22 01:42:15165// static
166size_t SysInfo::VMAllocationGranularity() {
167 return getpagesize();
168}
169
[email protected]2a758d612008-09-17 10:09:39170} // namespace base