Add DriveAppRegistryObserver.
[chromium-blink-merge.git] / base / sys_info_posix.cc
blob90baa69a2f61ec6489dcc36d32932a8c5de7dcb0
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"
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>
14 #include "base/basictypes.h"
15 #include "base/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)
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
29 namespace {
31 #if !defined(OS_OPENBSD)
32 int NumberOfProcessors() {
33 // It seems that sysconf returns the number of "logical" processors on both
34 // Mac and Linux. So we get the number of "online logical" processors.
35 long res = sysconf(_SC_NPROCESSORS_ONLN);
36 if (res == -1) {
37 NOTREACHED();
38 return 1;
41 return static_cast<int>(res);
44 base::LazyInstance<
45 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
46 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
47 #endif
49 int64 AmountOfVirtualMemory() {
50 struct rlimit limit;
51 int result = getrlimit(RLIMIT_DATA, &limit);
52 if (result != 0) {
53 NOTREACHED();
54 return 0;
56 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur;
59 base::LazyInstance<
60 base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky
61 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
63 } // namespace
65 namespace base {
67 #if !defined(OS_OPENBSD)
68 int SysInfo::NumberOfProcessors() {
69 return g_lazy_number_of_processors.Get().value();
71 #endif
73 // static
74 int64 SysInfo::AmountOfVirtualMemory() {
75 return g_lazy_virtual_memory.Get().value();
78 // static
79 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
80 base::ThreadRestrictions::AssertIOAllowed();
82 struct statvfs stats;
83 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
84 return -1;
85 return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
88 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
89 // static
90 std::string SysInfo::OperatingSystemName() {
91 struct utsname info;
92 if (uname(&info) < 0) {
93 NOTREACHED();
94 return std::string();
96 return std::string(info.sysname);
98 #endif
100 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
101 // static
102 std::string SysInfo::OperatingSystemVersion() {
103 struct utsname info;
104 if (uname(&info) < 0) {
105 NOTREACHED();
106 return std::string();
108 return std::string(info.release);
110 #endif
112 // static
113 std::string SysInfo::OperatingSystemArchitecture() {
114 struct utsname info;
115 if (uname(&info) < 0) {
116 NOTREACHED();
117 return std::string();
119 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";
125 return arch;
128 // static
129 size_t SysInfo::VMAllocationGranularity() {
130 return getpagesize();
133 } // namespace base