Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / base / sys_info_posix.cc
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/resource.h>
11 #include <sys/utsname.h>
12 #include <unistd.h>
13
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"
21
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
28
29 namespace {
30
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;
39   }
40
41   return static_cast<int>(res);
42 }
43
44 base::LazyInstance<
45     base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky
46     g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
47 #endif
48
49 int64 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
59 base::LazyInstance<
60     base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky
61     g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
62
63 }  // namespace
64
65 namespace base {
66
67 #if !defined(OS_OPENBSD)
68 int SysInfo::NumberOfProcessors() {
69   return g_lazy_number_of_processors.Get().value();
70 }
71 #endif
72
73 // static
74 int64 SysInfo::AmountOfVirtualMemory() {
75   return g_lazy_virtual_memory.Get().value();
76 }
77
78 // static
79 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
80   base::ThreadRestrictions::AssertIOAllowed();
81
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;
86 }
87
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();
95   }
96   return std::string(info.sysname);
97 }
98 #endif
99
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();
107   }
108   return std::string(info.release);
109 }
110 #endif
111
112 // static
113 std::string SysInfo::OperatingSystemArchitecture() {
114   struct utsname info;
115   if (uname(&info) < 0) {
116     NOTREACHED();
117     return std::string();
118   }
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";
124   }
125   return arch;
126 }
127
128 // static
129 size_t SysInfo::VMAllocationGranularity() {
130   return getpagesize();
131 }
132
133 }  // namespace base