Show timestamps with millisecond accuracy in chrome://proximity-auth.
[chromium-blink-merge.git] / base / sys_info_posix.cc
blob3d49bf94da925d9032ecdefdb181ed6bc08bf38f
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/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)
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 // 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
39 // loading is low.
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);
47 if (res == -1) {
48 NOTREACHED();
49 return 1;
52 return static_cast<int>(res);
55 base::LazyInstance<
56 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
57 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
58 #endif
60 int64 AmountOfVirtualMemory() {
61 struct rlimit limit;
62 int result = getrlimit(RLIMIT_DATA, &limit);
63 if (result != 0) {
64 NOTREACHED();
65 return 0;
67 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur;
70 base::LazyInstance<
71 base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky
72 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
74 } // namespace
76 namespace base {
78 #if !defined(OS_OPENBSD)
79 int SysInfo::NumberOfProcessors() {
80 return g_lazy_number_of_processors.Get().value();
82 #endif
84 // static
85 int64 SysInfo::AmountOfVirtualMemory() {
86 return g_lazy_virtual_memory.Get().value();
89 // static
90 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
91 base::ThreadRestrictions::AssertIOAllowed();
93 struct statvfs stats;
94 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
95 return -1;
96 return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
99 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
100 // static
101 std::string SysInfo::OperatingSystemName() {
102 struct utsname info;
103 if (uname(&info) < 0) {
104 NOTREACHED();
105 return std::string();
107 return std::string(info.sysname);
109 #endif
111 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
112 // static
113 std::string SysInfo::OperatingSystemVersion() {
114 struct utsname info;
115 if (uname(&info) < 0) {
116 NOTREACHED();
117 return std::string();
119 return std::string(info.release);
121 #endif
123 // static
124 std::string SysInfo::OperatingSystemArchitecture() {
125 struct utsname info;
126 if (uname(&info) < 0) {
127 NOTREACHED();
128 return std::string();
130 std::string arch(info.machine);
131 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
132 arch = "x86";
133 } else if (arch == "amd64") {
134 arch = "x86_64";
136 return arch;
139 // static
140 size_t SysInfo::VMAllocationGranularity() {
141 return getpagesize();
144 } // namespace base