• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/sys_info.h"
6 
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/param.h>
10 #include <sys/utsname.h>
11 #include <unistd.h>
12 
13 #include "base/basictypes.h"
14 #include "base/file_util.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/sys_info_internal.h"
19 #include "base/threading/thread_restrictions.h"
20 
21 #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>
26 #endif
27 
28 namespace {
29 
30 #if !defined(OS_OPENBSD)
NumberOfProcessors()31 int NumberOfProcessors() {
32   // It seems that sysconf returns the number of "logical" processors on both
33   // Mac and Linux.  So we get the number of "online logical" processors.
34   long res = sysconf(_SC_NPROCESSORS_ONLN);
35   if (res == -1) {
36     NOTREACHED();
37     return 1;
38   }
39 
40   return static_cast<int>(res);
41 }
42 
43 base::LazyInstance<
44     base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
45     g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
46 #endif
47 
48 }  // namespace
49 
50 namespace base {
51 
52 #if !defined(OS_OPENBSD)
NumberOfProcessors()53 int SysInfo::NumberOfProcessors() {
54   return g_lazy_number_of_processors.Get().value();
55 }
56 #endif
57 
58 // static
AmountOfFreeDiskSpace(const FilePath & path)59 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
60   base::ThreadRestrictions::AssertIOAllowed();
61 
62   struct statvfs stats;
63   if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
64     return -1;
65   return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
66 }
67 
68 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
69 // static
OperatingSystemName()70 std::string SysInfo::OperatingSystemName() {
71   struct utsname info;
72   if (uname(&info) < 0) {
73     NOTREACHED();
74     return std::string();
75   }
76   return std::string(info.sysname);
77 }
78 #endif
79 
80 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
81 // static
OperatingSystemVersion()82 std::string SysInfo::OperatingSystemVersion() {
83   struct utsname info;
84   if (uname(&info) < 0) {
85     NOTREACHED();
86     return std::string();
87   }
88   return std::string(info.release);
89 }
90 #endif
91 
92 // static
OperatingSystemArchitecture()93 std::string SysInfo::OperatingSystemArchitecture() {
94   struct utsname info;
95   if (uname(&info) < 0) {
96     NOTREACHED();
97     return std::string();
98   }
99   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;
106 }
107 
108 // static
VMAllocationGranularity()109 size_t SysInfo::VMAllocationGranularity() {
110   return getpagesize();
111 }
112 
113 }  // namespace base
114