blob: 90baa69a2f61ec6489dcc36d32932a8c5de7dcb0 [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>
8#include <string.h>
[email protected]e43eddf12009-12-29 00:32:529#include <sys/param.h>
[email protected]275a29de2014-04-08 23:19:1610#include <sys/resource.h>
[email protected]05f9b682008-09-29 22:18:0111#include <sys/utsname.h>
[email protected]2a758d612008-09-17 10:09:3912#include <unistd.h>
13
[email protected]047a03f2009-10-07 02:10:2014#include "base/basictypes.h"
15#include "base/file_util.h"
[email protected]c15d1e772013-11-20 11:14:5116#include "base/lazy_instance.h"
[email protected]2a758d612008-09-17 10:09:3917#include "base/logging.h"
[email protected]a4ea1f12013-06-07 18:37:0718#include "base/strings/utf_string_conversions.h"
[email protected]c15d1e772013-11-20 11:14:5119#include "base/sys_info_internal.h"
[email protected]dda29c942012-08-21 19:50:0320#include "base/threading/thread_restrictions.h"
[email protected]2a758d612008-09-17 10:09:3921
[email protected]f7d69972011-06-21 22:34:5022#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>
[email protected]f7d69972011-06-21 22:34:5027#endif
28
[email protected]c15d1e772013-11-20 11:14:5129namespace {
[email protected]2a758d612008-09-17 10:09:3930
[email protected]5f5ac6a2009-11-23 21:57:1131#if !defined(OS_OPENBSD)
[email protected]c15d1e772013-11-20 11:14:5132int NumberOfProcessors() {
[email protected]2a758d612008-09-17 10:09:3933 // It seems that sysconf returns the number of "logical" processors on both
[email protected]5f5ac6a2009-11-23 21:57:1134 // Mac and Linux. So we get the number of "online logical" processors.
[email protected]0203ed72009-07-02 13:59:0835 long res = sysconf(_SC_NPROCESSORS_ONLN);
[email protected]2a758d612008-09-17 10:09:3936 if (res == -1) {
37 NOTREACHED();
38 return 1;
39 }
40
41 return static_cast<int>(res);
[email protected]d632798e2008-09-17 13:10:4542}
[email protected]c15d1e772013-11-20 11:14:5143
44base::LazyInstance<
45 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
46 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
47#endif
48
[email protected]275a29de2014-04-08 23:19:1649int64 AmountOfVirtualMemory() {
50 struct rlimit limit;
51 int result = getrlimit(RLIMIT_DATA, &limit);
52 if (result != 0) {
53 NOTREACHED();
54 return 0;
55 }
56 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur;
57}
58
59base::LazyInstance<
60 base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky
61 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
62
[email protected]c15d1e772013-11-20 11:14:5163} // namespace
64
65namespace base {
66
67#if !defined(OS_OPENBSD)
68int SysInfo::NumberOfProcessors() {
69 return g_lazy_number_of_processors.Get().value();
70}
[email protected]c8587632009-11-12 02:17:1971#endif
[email protected]d632798e2008-09-17 13:10:4572
[email protected]0e91dd22008-09-18 12:34:2473// static
[email protected]275a29de2014-04-08 23:19:1674int64 SysInfo::AmountOfVirtualMemory() {
75 return g_lazy_virtual_memory.Get().value();
76}
77
78// static
[email protected]13326bb2009-10-14 00:41:5679int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
[email protected]dda29c942012-08-21 19:50:0380 base::ThreadRestrictions::AssertIOAllowed();
81
[email protected]0e91dd22008-09-18 12:34:2482 struct statvfs stats;
[email protected]04443582013-03-25 19:03:0683 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
[email protected]02ee34a2008-09-20 01:16:2384 return -1;
[email protected]0e91dd22008-09-18 12:34:2485 return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
86}
87
[email protected]5106b3a2012-10-03 20:10:4488#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]05f9b682008-09-29 22:18:0189// static
[email protected]05f9b682008-09-29 22:18:0190std::string SysInfo::OperatingSystemName() {
[email protected]94f8c952011-06-25 04:54:4191 struct utsname info;
[email protected]05f9b682008-09-29 22:18:0192 if (uname(&info) < 0) {
93 NOTREACHED();
[email protected]007b3f82013-04-09 08:46:4594 return std::string();
[email protected]05f9b682008-09-29 22:18:0195 }
96 return std::string(info.sysname);
97}
[email protected]5106b3a2012-10-03 20:10:4498#endif
[email protected]05f9b682008-09-29 22:18:0199
[email protected]82aeeaa2013-05-07 04:52:45100#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]05f9b682008-09-29 22:18:01101// static
102std::string SysInfo::OperatingSystemVersion() {
[email protected]94f8c952011-06-25 04:54:41103 struct utsname info;
[email protected]05f9b682008-09-29 22:18:01104 if (uname(&info) < 0) {
105 NOTREACHED();
[email protected]007b3f82013-04-09 08:46:45106 return std::string();
[email protected]05f9b682008-09-29 22:18:01107 }
108 return std::string(info.release);
109}
[email protected]6e001bb2011-05-25 20:53:18110#endif
[email protected]05f9b682008-09-29 22:18:01111
112// static
[email protected]0b6a4fb2012-10-16 01:58:21113std::string SysInfo::OperatingSystemArchitecture() {
[email protected]94f8c952011-06-25 04:54:41114 struct utsname info;
[email protected]05f9b682008-09-29 22:18:01115 if (uname(&info) < 0) {
116 NOTREACHED();
[email protected]007b3f82013-04-09 08:46:45117 return std::string();
[email protected]05f9b682008-09-29 22:18:01118 }
[email protected]56d0cef2012-09-26 02:49:42119 std::string arch(info.machine);
120 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
121 arch = "x86";
122 } else if (arch == "amd64") {
123 arch = "x86_64";
124 }
125 return arch;
[email protected]05f9b682008-09-29 22:18:01126}
127
[email protected]037fce02009-01-22 01:42:15128// static
129size_t SysInfo::VMAllocationGranularity() {
130 return getpagesize();
131}
132
[email protected]2a758d612008-09-17 10:09:39133} // namespace base