blob: cb25cdc51a3817c7fb2e9fc5a96781164ac0c6b8 [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
5#include "base/sys_info.h"
6
[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
[email protected]35b4f0c2014-06-26 16:55:2712#include "base/android/sys_utils.h"
13#include "base/lazy_instance.h"
[email protected]5702108f2012-05-25 15:31:3714#include "base/logging.h"
[email protected]dfa049e2013-02-07 02:57:2215#include "base/strings/string_number_conversions.h"
[email protected]eb62f7262013-03-30 14:29:0016#include "base/strings/string_piece.h"
[email protected]c851cfd2013-06-10 20:11:1417#include "base/strings/stringprintf.h"
[email protected]35b4f0c2014-06-26 16:55:2718#include "base/sys_info_internal.h"
[email protected]5702108f2012-05-25 15:31:3719
fdegans27660a32014-10-24 13:00:1820#if (__ANDROID_API__ >= 21 /* 5.0 - Lollipop */)
[email protected]2e4045d2014-07-18 05:41:5421
22namespace {
23
24typedef int (SystemPropertyGetFunction)(const char*, char*);
25
26SystemPropertyGetFunction* DynamicallyLoadRealSystemPropertyGet() {
27 // libc.so should already be open, get a handle to it.
28 void* handle = dlopen("libc.so", RTLD_NOLOAD);
29 if (!handle) {
30 LOG(FATAL) << "Cannot dlopen libc.so: " << dlerror();
31 }
32 SystemPropertyGetFunction* real_system_property_get =
33 reinterpret_cast<SystemPropertyGetFunction*>(
34 dlsym(handle, "__system_property_get"));
35 if (!real_system_property_get) {
36 LOG(FATAL) << "Cannot resolve __system_property_get(): " << dlerror();
37 }
38 return real_system_property_get;
39}
40
41static base::LazyInstance<base::internal::LazySysInfoValue<
42 SystemPropertyGetFunction*, DynamicallyLoadRealSystemPropertyGet> >::Leaky
43 g_lazy_real_system_property_get = LAZY_INSTANCE_INITIALIZER;
44
45} // namespace
46
47// Android 'L' removes __system_property_get from the NDK, however it is still
48// a hidden symbol in libc. Until we remove all calls of __system_property_get
49// from Chrome we work around this by defining a weak stub here, which uses
50// dlsym to but ensures that Chrome uses the real system
51// implementatation when loaded. http://crbug.com/392191.
fdegans3c7624d62015-01-07 16:17:4752BASE_EXPORT int __system_property_get(const char* name, char* value) {
[email protected]2e4045d2014-07-18 05:41:5453 return g_lazy_real_system_property_get.Get().value()(name, value);
54}
55
56#endif
57
[email protected]5702108f2012-05-25 15:31:3758namespace {
59
[email protected]abef4b332012-08-21 23:55:5260// Default version of Android to fall back to when actual version numbers
[email protected]de874b42014-07-10 08:05:2161// cannot be acquired. Use the latest Android release with a higher bug fix
62// version to avoid unnecessarily comparison errors with the latest release.
63// This should be manually kept up-to-date on each Android release.
boliu61dcc022015-10-14 16:52:3064const int kDefaultAndroidMajorVersion = 6;
65const int kDefaultAndroidMinorVersion = 0;
[email protected]de874b42014-07-10 08:05:2166const int kDefaultAndroidBugfixVersion = 99;
[email protected]abef4b332012-08-21 23:55:5267
68// Parse out the OS version numbers from the system properties.
69void ParseOSVersionNumbers(const char* os_version_str,
avidd4e614352015-12-09 00:44:4970 int32_t* major_version,
71 int32_t* minor_version,
72 int32_t* bugfix_version) {
[email protected]abef4b332012-08-21 23:55:5273 if (os_version_str[0]) {
74 // Try to parse out the version numbers from the string.
75 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
76 minor_version, bugfix_version);
77
78 if (num_read > 0) {
79 // If we don't have a full set of version numbers, make the extras 0.
80 if (num_read < 2) *minor_version = 0;
81 if (num_read < 3) *bugfix_version = 0;
82 return;
83 }
84 }
85
86 // For some reason, we couldn't parse the version number string.
87 *major_version = kDefaultAndroidMajorVersion;
88 *minor_version = kDefaultAndroidMinorVersion;
89 *bugfix_version = kDefaultAndroidBugfixVersion;
90}
91
[email protected]0cea3552013-02-16 18:15:5092// Parses a system property (specified with unit 'k','m' or 'g').
93// Returns a value in bytes.
94// Returns -1 if the string could not be parsed.
avidd4e614352015-12-09 00:44:4995int64_t ParseSystemPropertyBytes(const base::StringPiece& str) {
96 const int64_t KB = 1024;
97 const int64_t MB = 1024 * KB;
98 const int64_t GB = 1024 * MB;
[email protected]0cea3552013-02-16 18:15:5099 if (str.size() == 0u)
100 return -1;
avidd4e614352015-12-09 00:44:49101 int64_t unit_multiplier = 1;
[email protected]5702108f2012-05-25 15:31:37102 size_t length = str.size();
103 if (str[length - 1] == 'k') {
[email protected]0cea3552013-02-16 18:15:50104 unit_multiplier = KB;
[email protected]5702108f2012-05-25 15:31:37105 length--;
106 } else if (str[length - 1] == 'm') {
[email protected]0cea3552013-02-16 18:15:50107 unit_multiplier = MB;
[email protected]5702108f2012-05-25 15:31:37108 length--;
109 } else if (str[length - 1] == 'g') {
[email protected]0cea3552013-02-16 18:15:50110 unit_multiplier = GB;
[email protected]5702108f2012-05-25 15:31:37111 length--;
[email protected]5702108f2012-05-25 15:31:37112 }
avidd4e614352015-12-09 00:44:49113 int64_t result = 0;
[email protected]5702108f2012-05-25 15:31:37114 bool parsed = base::StringToInt64(str.substr(0, length), &result);
[email protected]0cea3552013-02-16 18:15:50115 bool negative = result <= 0;
avidd4e614352015-12-09 00:44:49116 bool overflow =
117 result >= std::numeric_limits<int64_t>::max() / unit_multiplier;
[email protected]0cea3552013-02-16 18:15:50118 if (!parsed || negative || overflow)
119 return -1;
120 return result * unit_multiplier;
[email protected]5702108f2012-05-25 15:31:37121}
122
123int GetDalvikHeapSizeMB() {
124 char heap_size_str[PROP_VALUE_MAX];
125 __system_property_get("dalvik.vm.heapsize", heap_size_str);
[email protected]0cea3552013-02-16 18:15:50126 // dalvik.vm.heapsize property is writable by a root user.
127 // Clamp it to reasonable range as a sanity check,
128 // a typical android device will never have less than 48MB.
avidd4e614352015-12-09 00:44:49129 const int64_t MB = 1024 * 1024;
130 int64_t result = ParseSystemPropertyBytes(heap_size_str);
[email protected]0cea3552013-02-16 18:15:50131 if (result == -1) {
132 // We should consider not exposing these values if they are not reliable.
133 LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str;
134 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
135 }
avidd4e614352015-12-09 00:44:49136 result =
137 std::min<int64_t>(std::max<int64_t>(32 * MB, result), 1024 * MB) / MB;
[email protected]0cea3552013-02-16 18:15:50138 return static_cast<int>(result);
139}
140
141int GetDalvikHeapGrowthLimitMB() {
142 char heap_size_str[PROP_VALUE_MAX];
143 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
144 // dalvik.vm.heapgrowthlimit property is writable by a root user.
145 // Clamp it to reasonable range as a sanity check,
146 // a typical android device will never have less than 24MB.
avidd4e614352015-12-09 00:44:49147 const int64_t MB = 1024 * 1024;
148 int64_t result = ParseSystemPropertyBytes(heap_size_str);
[email protected]0cea3552013-02-16 18:15:50149 if (result == -1) {
150 // We should consider not exposing these values if they are not reliable.
151 LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str;
152 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
153 }
avidd4e614352015-12-09 00:44:49154 result = std::min<int64_t>(std::max<int64_t>(16 * MB, result), 512 * MB) / MB;
[email protected]0cea3552013-02-16 18:15:50155 return static_cast<int>(result);
[email protected]5702108f2012-05-25 15:31:37156}
157
158} // anonymous namespace
159
160namespace base {
161
tdresserae4166952015-07-16 15:41:04162std::string SysInfo::HardwareModelName() {
[email protected]abef4b332012-08-21 23:55:52163 char device_model_str[PROP_VALUE_MAX];
164 __system_property_get("ro.product.model", device_model_str);
165 return std::string(device_model_str);
166}
167
tdresserae4166952015-07-16 15:41:04168std::string SysInfo::OperatingSystemName() {
169 return "Android";
170}
171
[email protected]82aeeaa2013-05-07 04:52:45172std::string SysInfo::OperatingSystemVersion() {
avidd4e614352015-12-09 00:44:49173 int32_t major, minor, bugfix;
[email protected]82aeeaa2013-05-07 04:52:45174 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
175 return StringPrintf("%d.%d.%d", major, minor, bugfix);
176}
177
avidd4e614352015-12-09 00:44:49178void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
179 int32_t* minor_version,
180 int32_t* bugfix_version) {
[email protected]abef4b332012-08-21 23:55:52181 // Read the version number string out from the properties.
182 char os_version_str[PROP_VALUE_MAX];
183 __system_property_get("ro.build.version.release", os_version_str);
184
185 // Parse out the numbers.
186 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
187 bugfix_version);
188}
189
tdresserae4166952015-07-16 15:41:04190std::string SysInfo::GetAndroidBuildCodename() {
191 char os_version_codename_str[PROP_VALUE_MAX];
192 __system_property_get("ro.build.version.codename", os_version_codename_str);
193 return std::string(os_version_codename_str);
194}
195
196std::string SysInfo::GetAndroidBuildID() {
197 char os_build_id_str[PROP_VALUE_MAX];
198 __system_property_get("ro.build.id", os_build_id_str);
199 return std::string(os_build_id_str);
200}
201
[email protected]5702108f2012-05-25 15:31:37202int SysInfo::DalvikHeapSizeMB() {
203 static int heap_size = GetDalvikHeapSizeMB();
204 return heap_size;
205}
206
[email protected]0cea3552013-02-16 18:15:50207int SysInfo::DalvikHeapGrowthLimitMB() {
208 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
209 return heap_growth_limit;
210}
211
[email protected]35b4f0c2014-06-26 16:55:27212static base::LazyInstance<
213 base::internal::LazySysInfoValue<bool,
214 android::SysUtils::IsLowEndDeviceFromJni> >::Leaky
215 g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER;
216
217bool SysInfo::IsLowEndDevice() {
218 return g_lazy_low_end_device.Get().value();
219}
220
[email protected]0cea3552013-02-16 18:15:50221
[email protected]5702108f2012-05-25 15:31:37222} // namespace base