blob: 07d08b72bbcb51fd8e550ab79409c55c6dff9af4 [file] [log] [blame]
stuartmorgan@chromium.org56f1efd2011-05-26 05:53:18 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
deanm@chromium.org42a34eb2008-09-17 19:09:39 +09002// 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>
evan@chromium.org875bb6e2009-12-29 09:32:52 +09009#include <sys/param.h>
mark@chromium.orgb93c0542008-09-30 07:18:01 +090010#include <sys/utsname.h>
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090011#include <unistd.h>
12
brettw@chromium.org50c94652009-10-07 11:10:20 +090013#include "base/basictypes.h"
14#include "base/file_util.h"
jochen@chromium.org4abdb3e2013-11-20 20:14:51 +090015#include "base/lazy_instance.h"
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090016#include "base/logging.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090017#include "base/strings/utf_string_conversions.h"
jochen@chromium.org4abdb3e2013-11-20 20:14:51 +090018#include "base/sys_info_internal.h"
haruki@chromium.orgb0efbba2012-08-22 04:50:03 +090019#include "base/threading/thread_restrictions.h"
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090020
michaelbai@google.com2251c622011-06-22 07:34:50 +090021#if defined(OS_ANDROID)
22#include <sys/vfs.h>
23#define statvfs statfs // Android uses a statvfs-like statfs struct and call.
24#else
25#include <sys/statvfs.h>
michaelbai@google.com2251c622011-06-22 07:34:50 +090026#endif
27
jochen@chromium.org4abdb3e2013-11-20 20:14:51 +090028namespace {
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090029
evan@chromium.orgfae72682009-11-24 06:57:11 +090030#if !defined(OS_OPENBSD)
jochen@chromium.org4abdb3e2013-11-20 20:14:51 +090031int NumberOfProcessors() {
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090032 // It seems that sysconf returns the number of "logical" processors on both
evan@chromium.orgfae72682009-11-24 06:57:11 +090033 // Mac and Linux. So we get the number of "online logical" processors.
jeremy@chromium.org92b13a12009-07-02 22:59:08 +090034 long res = sysconf(_SC_NPROCESSORS_ONLN);
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090035 if (res == -1) {
36 NOTREACHED();
37 return 1;
38 }
39
40 return static_cast<int>(res);
deanm@chromium.org319f0442008-09-17 22:10:45 +090041}
jochen@chromium.org4abdb3e2013-11-20 20:14:51 +090042
43base::LazyInstance<
44 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
45 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
46#endif
47
48} // namespace
49
50namespace base {
51
52#if !defined(OS_OPENBSD)
53int SysInfo::NumberOfProcessors() {
54 return g_lazy_number_of_processors.Get().value();
55}
evan@chromium.org84b05b82009-11-12 11:17:19 +090056#endif
deanm@chromium.org319f0442008-09-17 22:10:45 +090057
deanm@chromium.orgee45e8f2008-09-18 21:34:24 +090058// static
tony@chromium.org1e25e002009-10-14 09:41:56 +090059int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
haruki@chromium.orgb0efbba2012-08-22 04:50:03 +090060 base::ThreadRestrictions::AssertIOAllowed();
61
deanm@chromium.orgee45e8f2008-09-18 21:34:24 +090062 struct statvfs stats;
phajdan.jr@chromium.org1f2edf32013-03-26 04:03:06 +090063 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
rvargas@google.comaf000292008-09-20 10:16:23 +090064 return -1;
deanm@chromium.orgee45e8f2008-09-18 21:34:24 +090065 return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
66}
67
dfalcantara@chromium.orgefbe5312012-10-04 05:10:44 +090068#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
mark@chromium.orgb93c0542008-09-30 07:18:01 +090069// static
mark@chromium.orgb93c0542008-09-30 07:18:01 +090070std::string SysInfo::OperatingSystemName() {
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090071 struct utsname info;
mark@chromium.orgb93c0542008-09-30 07:18:01 +090072 if (uname(&info) < 0) {
73 NOTREACHED();
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +090074 return std::string();
mark@chromium.orgb93c0542008-09-30 07:18:01 +090075 }
76 return std::string(info.sysname);
77}
dfalcantara@chromium.orgefbe5312012-10-04 05:10:44 +090078#endif
mark@chromium.orgb93c0542008-09-30 07:18:01 +090079
zmo@chromium.orgad89ff52013-05-07 13:52:45 +090080#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
mark@chromium.orgb93c0542008-09-30 07:18:01 +090081// static
82std::string SysInfo::OperatingSystemVersion() {
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090083 struct utsname info;
mark@chromium.orgb93c0542008-09-30 07:18:01 +090084 if (uname(&info) < 0) {
85 NOTREACHED();
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +090086 return std::string();
mark@chromium.orgb93c0542008-09-30 07:18:01 +090087 }
88 return std::string(info.release);
89}
stuartmorgan@chromium.org56f1efd2011-05-26 05:53:18 +090090#endif
mark@chromium.orgb93c0542008-09-30 07:18:01 +090091
92// static
thestig@chromium.orge1706652012-10-16 10:58:21 +090093std::string SysInfo::OperatingSystemArchitecture() {
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090094 struct utsname info;
mark@chromium.orgb93c0542008-09-30 07:18:01 +090095 if (uname(&info) < 0) {
96 NOTREACHED();
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +090097 return std::string();
mark@chromium.orgb93c0542008-09-30 07:18:01 +090098 }
thestig@chromium.orgc9137242012-09-26 11:49:42 +090099 std::string arch(info.machine);
100 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
101 arch = "x86";
102 } else if (arch == "amd64") {
103 arch = "x86_64";
104 }
105 return arch;
mark@chromium.orgb93c0542008-09-30 07:18:01 +0900106}
107
agl@chromium.org3d71fda2009-01-22 10:42:15 +0900108// static
109size_t SysInfo::VMAllocationGranularity() {
110 return getpagesize();
111}
112
deanm@chromium.org42a34eb2008-09-17 19:09:39 +0900113} // namespace base