blob: ab760e5579721843c64244f59d52cf23c037c7cc [file] [log] [blame]
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +09001// Copyright (c) 2012 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
rmcilroy@chromium.org9ed6d6a2014-07-18 14:41:54 +09007#include <dlfcn.h>
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +09008#include <sys/system_properties.h>
9
c.shu@samsung.comda21aff2014-06-27 01:55:27 +090010#include "base/android/sys_utils.h"
11#include "base/lazy_instance.h"
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090012#include "base/logging.h"
brettw@chromium.orgabcde5c2013-02-07 11:57:22 +090013#include "base/strings/string_number_conversions.h"
tfarina@chromium.orgb6d49112013-03-30 23:29:00 +090014#include "base/strings/string_piece.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090015#include "base/strings/stringprintf.h"
c.shu@samsung.comda21aff2014-06-27 01:55:27 +090016#include "base/sys_info_internal.h"
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090017
rmcilroy@chromium.org9ed6d6a2014-07-18 14:41:54 +090018// TODO(rmcilroy): Update API level when 'L' gets an official API level.
19#if (__ANDROID_API__ >= 9999 /* 'L' */)
20
21namespace {
22
23typedef int (SystemPropertyGetFunction)(const char*, char*);
24
25SystemPropertyGetFunction* DynamicallyLoadRealSystemPropertyGet() {
26 // libc.so should already be open, get a handle to it.
27 void* handle = dlopen("libc.so", RTLD_NOLOAD);
28 if (!handle) {
29 LOG(FATAL) << "Cannot dlopen libc.so: " << dlerror();
30 }
31 SystemPropertyGetFunction* real_system_property_get =
32 reinterpret_cast<SystemPropertyGetFunction*>(
33 dlsym(handle, "__system_property_get"));
34 if (!real_system_property_get) {
35 LOG(FATAL) << "Cannot resolve __system_property_get(): " << dlerror();
36 }
37 return real_system_property_get;
38}
39
40static base::LazyInstance<base::internal::LazySysInfoValue<
41 SystemPropertyGetFunction*, DynamicallyLoadRealSystemPropertyGet> >::Leaky
42 g_lazy_real_system_property_get = LAZY_INSTANCE_INITIALIZER;
43
44} // namespace
45
46// Android 'L' removes __system_property_get from the NDK, however it is still
47// a hidden symbol in libc. Until we remove all calls of __system_property_get
48// from Chrome we work around this by defining a weak stub here, which uses
49// dlsym to but ensures that Chrome uses the real system
50// implementatation when loaded. http://crbug.com/392191.
51int __system_property_get(const char* name, char* value) {
52 return g_lazy_real_system_property_get.Get().value()(name, value);
53}
54
55#endif
56
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090057namespace {
58
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +090059// Default version of Android to fall back to when actual version numbers
boliu@chromium.org61e0bea2014-07-10 17:05:21 +090060// cannot be acquired. Use the latest Android release with a higher bug fix
61// version to avoid unnecessarily comparison errors with the latest release.
62// This should be manually kept up-to-date on each Android release.
63const int kDefaultAndroidMajorVersion = 4;
64const int kDefaultAndroidMinorVersion = 4;
65const int kDefaultAndroidBugfixVersion = 99;
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +090066
67// Parse out the OS version numbers from the system properties.
68void ParseOSVersionNumbers(const char* os_version_str,
69 int32 *major_version,
70 int32 *minor_version,
71 int32 *bugfix_version) {
72 if (os_version_str[0]) {
73 // Try to parse out the version numbers from the string.
74 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
75 minor_version, bugfix_version);
76
77 if (num_read > 0) {
78 // If we don't have a full set of version numbers, make the extras 0.
79 if (num_read < 2) *minor_version = 0;
80 if (num_read < 3) *bugfix_version = 0;
81 return;
82 }
83 }
84
85 // For some reason, we couldn't parse the version number string.
86 *major_version = kDefaultAndroidMajorVersion;
87 *minor_version = kDefaultAndroidMinorVersion;
88 *bugfix_version = kDefaultAndroidBugfixVersion;
89}
90
epenner@chromium.org066b81b2013-02-17 03:15:50 +090091// Parses a system property (specified with unit 'k','m' or 'g').
92// Returns a value in bytes.
93// Returns -1 if the string could not be parsed.
94int64 ParseSystemPropertyBytes(const base::StringPiece& str) {
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090095 const int64 KB = 1024;
96 const int64 MB = 1024 * KB;
97 const int64 GB = 1024 * MB;
epenner@chromium.org066b81b2013-02-17 03:15:50 +090098 if (str.size() == 0u)
99 return -1;
100 int64 unit_multiplier = 1;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900101 size_t length = str.size();
102 if (str[length - 1] == 'k') {
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900103 unit_multiplier = KB;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900104 length--;
105 } else if (str[length - 1] == 'm') {
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900106 unit_multiplier = MB;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900107 length--;
108 } else if (str[length - 1] == 'g') {
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900109 unit_multiplier = GB;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900110 length--;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900111 }
112 int64 result = 0;
113 bool parsed = base::StringToInt64(str.substr(0, length), &result);
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900114 bool negative = result <= 0;
115 bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier;
116 if (!parsed || negative || overflow)
117 return -1;
118 return result * unit_multiplier;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900119}
120
121int GetDalvikHeapSizeMB() {
122 char heap_size_str[PROP_VALUE_MAX];
123 __system_property_get("dalvik.vm.heapsize", heap_size_str);
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900124 // dalvik.vm.heapsize property is writable by a root user.
125 // Clamp it to reasonable range as a sanity check,
126 // a typical android device will never have less than 48MB.
127 const int64 MB = 1024 * 1024;
128 int64 result = ParseSystemPropertyBytes(heap_size_str);
129 if (result == -1) {
130 // We should consider not exposing these values if they are not reliable.
131 LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str;
132 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
133 }
134 result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB;
135 return static_cast<int>(result);
136}
137
138int GetDalvikHeapGrowthLimitMB() {
139 char heap_size_str[PROP_VALUE_MAX];
140 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
141 // dalvik.vm.heapgrowthlimit property is writable by a root user.
142 // Clamp it to reasonable range as a sanity check,
143 // a typical android device will never have less than 24MB.
144 const int64 MB = 1024 * 1024;
145 int64 result = ParseSystemPropertyBytes(heap_size_str);
146 if (result == -1) {
147 // We should consider not exposing these values if they are not reliable.
148 LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str;
149 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
150 }
151 result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB;
152 return static_cast<int>(result);
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900153}
154
155} // anonymous namespace
156
157namespace base {
158
dfalcantara@chromium.orgefbe5312012-10-04 05:10:44 +0900159std::string SysInfo::OperatingSystemName() {
160 return "Android";
161}
162
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +0900163std::string SysInfo::GetAndroidBuildCodename() {
164 char os_version_codename_str[PROP_VALUE_MAX];
165 __system_property_get("ro.build.version.codename", os_version_codename_str);
166 return std::string(os_version_codename_str);
167}
168
169std::string SysInfo::GetAndroidBuildID() {
170 char os_build_id_str[PROP_VALUE_MAX];
171 __system_property_get("ro.build.id", os_build_id_str);
172 return std::string(os_build_id_str);
173}
174
175std::string SysInfo::GetDeviceName() {
176 char device_model_str[PROP_VALUE_MAX];
177 __system_property_get("ro.product.model", device_model_str);
178 return std::string(device_model_str);
179}
180
zmo@chromium.orgad89ff52013-05-07 13:52:45 +0900181std::string SysInfo::OperatingSystemVersion() {
182 int32 major, minor, bugfix;
183 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
184 return StringPrintf("%d.%d.%d", major, minor, bugfix);
185}
186
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +0900187void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
188 int32* minor_version,
189 int32* bugfix_version) {
190 // Read the version number string out from the properties.
191 char os_version_str[PROP_VALUE_MAX];
192 __system_property_get("ro.build.version.release", os_version_str);
193
194 // Parse out the numbers.
195 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
196 bugfix_version);
197}
198
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900199int SysInfo::DalvikHeapSizeMB() {
200 static int heap_size = GetDalvikHeapSizeMB();
201 return heap_size;
202}
203
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900204int SysInfo::DalvikHeapGrowthLimitMB() {
205 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
206 return heap_growth_limit;
207}
208
c.shu@samsung.comda21aff2014-06-27 01:55:27 +0900209static base::LazyInstance<
210 base::internal::LazySysInfoValue<bool,
211 android::SysUtils::IsLowEndDeviceFromJni> >::Leaky
212 g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER;
213
214bool SysInfo::IsLowEndDevice() {
215 return g_lazy_low_end_device.Get().value();
216}
217
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900218
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900219} // namespace base