blob: 3993b8765e2b213ad2179d8e07f93be639001bff [file] [log] [blame]
[email protected]5702108f2012-05-25 15:31:371// 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
Sebastien Marchand75a7cdf2018-11-13 23:47:035#include "base/system/sys_info.h"
[email protected]5702108f2012-05-25 15:31:376
[email protected]2e4045d2014-07-18 05:41:547#include <dlfcn.h>
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9#include <stdint.h>
[email protected]5702108f2012-05-25 15:31:3710#include <sys/system_properties.h>
11
svaisanen2e4bd662016-04-11 07:34:2112#include "base/android/jni_android.h"
[email protected]35b4f0c2014-06-26 16:55:2713#include "base/android/sys_utils.h"
14#include "base/lazy_instance.h"
[email protected]5702108f2012-05-25 15:31:3715#include "base/logging.h"
[email protected]dfa049e2013-02-07 02:57:2216#include "base/strings/string_number_conversions.h"
[email protected]eb62f7262013-03-30 14:29:0017#include "base/strings/string_piece.h"
Guido Urdaneta84618ee2018-08-30 10:32:3718#include "base/strings/string_util.h"
[email protected]c851cfd2013-06-10 20:11:1419#include "base/strings/stringprintf.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0320#include "base/system/sys_info_internal.h"
[email protected]5702108f2012-05-25 15:31:3721
fdegans27660a32014-10-24 13:00:1822#if (__ANDROID_API__ >= 21 /* 5.0 - Lollipop */)
[email protected]2e4045d2014-07-18 05:41:5423
24namespace {
25
Sebastien Marchand75a7cdf2018-11-13 23:47:0326typedef int(SystemPropertyGetFunction)(const char*, char*);
[email protected]2e4045d2014-07-18 05:41:5427
28SystemPropertyGetFunction* DynamicallyLoadRealSystemPropertyGet() {
29 // libc.so should already be open, get a handle to it.
30 void* handle = dlopen("libc.so", RTLD_NOLOAD);
31 if (!handle) {
32 LOG(FATAL) << "Cannot dlopen libc.so: " << dlerror();
33 }
34 SystemPropertyGetFunction* real_system_property_get =
35 reinterpret_cast<SystemPropertyGetFunction*>(
36 dlsym(handle, "__system_property_get"));
37 if (!real_system_property_get) {
38 LOG(FATAL) << "Cannot resolve __system_property_get(): " << dlerror();
39 }
40 return real_system_property_get;
41}
42
43static base::LazyInstance<base::internal::LazySysInfoValue<
Sebastien Marchand75a7cdf2018-11-13 23:47:0344 SystemPropertyGetFunction*,
45 DynamicallyLoadRealSystemPropertyGet>>::Leaky
[email protected]2e4045d2014-07-18 05:41:5446 g_lazy_real_system_property_get = LAZY_INSTANCE_INITIALIZER;
47
48} // namespace
49
50// Android 'L' removes __system_property_get from the NDK, however it is still
51// a hidden symbol in libc. Until we remove all calls of __system_property_get
52// from Chrome we work around this by defining a weak stub here, which uses
53// dlsym to but ensures that Chrome uses the real system
54// implementatation when loaded. http://crbug.com/392191.
fdegans3c7624d62015-01-07 16:17:4755BASE_EXPORT int __system_property_get(const char* name, char* value) {
[email protected]2e4045d2014-07-18 05:41:5456 return g_lazy_real_system_property_get.Get().value()(name, value);
57}
58
59#endif
60
[email protected]5702108f2012-05-25 15:31:3761namespace {
62
[email protected]abef4b332012-08-21 23:55:5263// Default version of Android to fall back to when actual version numbers
[email protected]de874b42014-07-10 08:05:2164// cannot be acquired. Use the latest Android release with a higher bug fix
65// version to avoid unnecessarily comparison errors with the latest release.
thakis3e861de2016-06-14 14:24:0166// This should be manually kept up to date on each Android release.
Bo Liub32baee22019-09-05 17:07:0067const int kDefaultAndroidMajorVersion = 10;
Bo Liu80a94052018-10-19 16:45:1368const int kDefaultAndroidMinorVersion = 0;
[email protected]de874b42014-07-10 08:05:2169const int kDefaultAndroidBugfixVersion = 99;
[email protected]abef4b332012-08-21 23:55:5270
Bo Liu4f2b4e142017-05-26 21:41:5171// Get and parse out the OS version numbers from the system properties.
72// Note if parse fails, the "default" version is returned as fallback.
73void GetOsVersionStringAndNumbers(std::string* version_string,
74 int32_t* major_version,
75 int32_t* minor_version,
76 int32_t* bugfix_version) {
77 // Read the version number string out from the properties.
78 char os_version_str[PROP_VALUE_MAX];
79 __system_property_get("ro.build.version.release", os_version_str);
80
[email protected]abef4b332012-08-21 23:55:5281 if (os_version_str[0]) {
82 // Try to parse out the version numbers from the string.
83 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
84 minor_version, bugfix_version);
85
86 if (num_read > 0) {
87 // If we don't have a full set of version numbers, make the extras 0.
Bo Liu4f2b4e142017-05-26 21:41:5188 if (num_read < 2)
89 *minor_version = 0;
90 if (num_read < 3)
91 *bugfix_version = 0;
92 *version_string = std::string(os_version_str);
[email protected]abef4b332012-08-21 23:55:5293 return;
94 }
95 }
96
97 // For some reason, we couldn't parse the version number string.
98 *major_version = kDefaultAndroidMajorVersion;
99 *minor_version = kDefaultAndroidMinorVersion;
100 *bugfix_version = kDefaultAndroidBugfixVersion;
Bo Liu4f2b4e142017-05-26 21:41:51101 *version_string = ::base::StringPrintf("%d.%d.%d", *major_version,
102 *minor_version, *bugfix_version);
[email protected]abef4b332012-08-21 23:55:52103}
104
[email protected]0cea3552013-02-16 18:15:50105// Parses a system property (specified with unit 'k','m' or 'g').
106// Returns a value in bytes.
107// Returns -1 if the string could not be parsed.
avidd4e614352015-12-09 00:44:49108int64_t ParseSystemPropertyBytes(const base::StringPiece& str) {
109 const int64_t KB = 1024;
110 const int64_t MB = 1024 * KB;
111 const int64_t GB = 1024 * MB;
[email protected]0cea3552013-02-16 18:15:50112 if (str.size() == 0u)
113 return -1;
avidd4e614352015-12-09 00:44:49114 int64_t unit_multiplier = 1;
[email protected]5702108f2012-05-25 15:31:37115 size_t length = str.size();
116 if (str[length - 1] == 'k') {
[email protected]0cea3552013-02-16 18:15:50117 unit_multiplier = KB;
[email protected]5702108f2012-05-25 15:31:37118 length--;
119 } else if (str[length - 1] == 'm') {
[email protected]0cea3552013-02-16 18:15:50120 unit_multiplier = MB;
[email protected]5702108f2012-05-25 15:31:37121 length--;
122 } else if (str[length - 1] == 'g') {
[email protected]0cea3552013-02-16 18:15:50123 unit_multiplier = GB;
[email protected]5702108f2012-05-25 15:31:37124 length--;
[email protected]5702108f2012-05-25 15:31:37125 }
avidd4e614352015-12-09 00:44:49126 int64_t result = 0;
[email protected]5702108f2012-05-25 15:31:37127 bool parsed = base::StringToInt64(str.substr(0, length), &result);
[email protected]0cea3552013-02-16 18:15:50128 bool negative = result <= 0;
avidd4e614352015-12-09 00:44:49129 bool overflow =
130 result >= std::numeric_limits<int64_t>::max() / unit_multiplier;
[email protected]0cea3552013-02-16 18:15:50131 if (!parsed || negative || overflow)
132 return -1;
133 return result * unit_multiplier;
[email protected]5702108f2012-05-25 15:31:37134}
135
136int GetDalvikHeapSizeMB() {
137 char heap_size_str[PROP_VALUE_MAX];
138 __system_property_get("dalvik.vm.heapsize", heap_size_str);
[email protected]0cea3552013-02-16 18:15:50139 // dalvik.vm.heapsize property is writable by a root user.
140 // Clamp it to reasonable range as a sanity check,
141 // a typical android device will never have less than 48MB.
avidd4e614352015-12-09 00:44:49142 const int64_t MB = 1024 * 1024;
143 int64_t result = ParseSystemPropertyBytes(heap_size_str);
[email protected]0cea3552013-02-16 18:15:50144 if (result == -1) {
Sebastien Marchand75a7cdf2018-11-13 23:47:03145 // We should consider not exposing these values if they are not reliable.
146 LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str;
147 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
[email protected]0cea3552013-02-16 18:15:50148 }
avidd4e614352015-12-09 00:44:49149 result =
150 std::min<int64_t>(std::max<int64_t>(32 * MB, result), 1024 * MB) / MB;
[email protected]0cea3552013-02-16 18:15:50151 return static_cast<int>(result);
152}
153
154int GetDalvikHeapGrowthLimitMB() {
155 char heap_size_str[PROP_VALUE_MAX];
156 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
157 // dalvik.vm.heapgrowthlimit property is writable by a root user.
158 // Clamp it to reasonable range as a sanity check,
159 // a typical android device will never have less than 24MB.
avidd4e614352015-12-09 00:44:49160 const int64_t MB = 1024 * 1024;
161 int64_t result = ParseSystemPropertyBytes(heap_size_str);
[email protected]0cea3552013-02-16 18:15:50162 if (result == -1) {
Sebastien Marchand75a7cdf2018-11-13 23:47:03163 // We should consider not exposing these values if they are not reliable.
164 LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str;
165 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
[email protected]0cea3552013-02-16 18:15:50166 }
avidd4e614352015-12-09 00:44:49167 result = std::min<int64_t>(std::max<int64_t>(16 * MB, result), 512 * MB) / MB;
[email protected]0cea3552013-02-16 18:15:50168 return static_cast<int>(result);
[email protected]5702108f2012-05-25 15:31:37169}
170
Guido Urdaneta84618ee2018-08-30 10:32:37171std::string HardwareManufacturerName() {
172 char device_model_str[PROP_VALUE_MAX];
173 __system_property_get("ro.product.manufacturer", device_model_str);
174 return std::string(device_model_str);
175}
176
[email protected]5702108f2012-05-25 15:31:37177} // anonymous namespace
178
179namespace base {
180
tdresserae4166952015-07-16 15:41:04181std::string SysInfo::HardwareModelName() {
[email protected]abef4b332012-08-21 23:55:52182 char device_model_str[PROP_VALUE_MAX];
183 __system_property_get("ro.product.model", device_model_str);
184 return std::string(device_model_str);
185}
186
tdresserae4166952015-07-16 15:41:04187std::string SysInfo::OperatingSystemName() {
188 return "Android";
189}
190
[email protected]82aeeaa2013-05-07 04:52:45191std::string SysInfo::OperatingSystemVersion() {
Bo Liu4f2b4e142017-05-26 21:41:51192 std::string version_string;
avidd4e614352015-12-09 00:44:49193 int32_t major, minor, bugfix;
Bo Liu4f2b4e142017-05-26 21:41:51194 GetOsVersionStringAndNumbers(&version_string, &major, &minor, &bugfix);
195 return version_string;
[email protected]82aeeaa2013-05-07 04:52:45196}
197
avidd4e614352015-12-09 00:44:49198void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
199 int32_t* minor_version,
200 int32_t* bugfix_version) {
Bo Liu4f2b4e142017-05-26 21:41:51201 std::string version_string;
202 GetOsVersionStringAndNumbers(&version_string, major_version, minor_version,
203 bugfix_version);
[email protected]abef4b332012-08-21 23:55:52204}
205
tdresserae4166952015-07-16 15:41:04206std::string SysInfo::GetAndroidBuildCodename() {
207 char os_version_codename_str[PROP_VALUE_MAX];
208 __system_property_get("ro.build.version.codename", os_version_codename_str);
209 return std::string(os_version_codename_str);
210}
211
212std::string SysInfo::GetAndroidBuildID() {
213 char os_build_id_str[PROP_VALUE_MAX];
214 __system_property_get("ro.build.id", os_build_id_str);
215 return std::string(os_build_id_str);
216}
217
[email protected]5702108f2012-05-25 15:31:37218int SysInfo::DalvikHeapSizeMB() {
219 static int heap_size = GetDalvikHeapSizeMB();
220 return heap_size;
221}
222
[email protected]0cea3552013-02-16 18:15:50223int SysInfo::DalvikHeapGrowthLimitMB() {
224 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
225 return heap_growth_limit;
226}
227
Sebastien Marchand75a7cdf2018-11-13 23:47:03228static base::LazyInstance<base::internal::LazySysInfoValue<
229 bool,
230 android::SysUtils::IsLowEndDeviceFromJni>>::Leaky g_lazy_low_end_device =
231 LAZY_INSTANCE_INITIALIZER;
[email protected]35b4f0c2014-06-26 16:55:27232
Eric Karl6c24049a2018-03-13 16:34:03233bool SysInfo::IsLowEndDeviceImpl() {
svaisanen2e4bd662016-04-11 07:34:21234 // This code might be used in some environments
235 // which might not have a Java environment.
236 // Note that we need to call the Java version here.
237 // There exists a complete native implementation in
238 // sys_info.cc but calling that here would mean that
239 // the Java code and the native code would call different
240 // implementations which could give different results.
241 // Also the Java code cannot depend on the native code
242 // since it might not be loaded yet.
243 if (!base::android::IsVMInitialized())
244 return false;
[email protected]35b4f0c2014-06-26 16:55:27245 return g_lazy_low_end_device.Get().value();
246}
247
Guido Urdaneta84618ee2018-08-30 10:32:37248// static
249SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() {
250 HardwareInfo info;
251 info.manufacturer = HardwareManufacturerName();
252 info.model = HardwareModelName();
253 DCHECK(IsStringUTF8(info.manufacturer));
254 DCHECK(IsStringUTF8(info.model));
255 return info;
256}
[email protected]0cea3552013-02-16 18:15:50257
[email protected]5702108f2012-05-25 15:31:37258} // namespace base