[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 1 | // 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] | 2e4045d | 2014-07-18 05:41:54 | [diff] [blame] | 7 | #include <dlfcn.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 10 | #include <sys/system_properties.h> |
| 11 | |
svaisanen | 2e4bd66 | 2016-04-11 07:34:21 | [diff] [blame] | 12 | #include "base/android/jni_android.h" |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 13 | #include "base/android/sys_utils.h" |
| 14 | #include "base/lazy_instance.h" |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 17 | #include "base/strings/string_piece.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 19 | #include "base/sys_info_internal.h" |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 20 | |
fdegans | 27660a3 | 2014-10-24 13:00:18 | [diff] [blame] | 21 | #if (__ANDROID_API__ >= 21 /* 5.0 - Lollipop */) |
[email protected] | 2e4045d | 2014-07-18 05:41:54 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | typedef int (SystemPropertyGetFunction)(const char*, char*); |
| 26 | |
| 27 | SystemPropertyGetFunction* DynamicallyLoadRealSystemPropertyGet() { |
| 28 | // libc.so should already be open, get a handle to it. |
| 29 | void* handle = dlopen("libc.so", RTLD_NOLOAD); |
| 30 | if (!handle) { |
| 31 | LOG(FATAL) << "Cannot dlopen libc.so: " << dlerror(); |
| 32 | } |
| 33 | SystemPropertyGetFunction* real_system_property_get = |
| 34 | reinterpret_cast<SystemPropertyGetFunction*>( |
| 35 | dlsym(handle, "__system_property_get")); |
| 36 | if (!real_system_property_get) { |
| 37 | LOG(FATAL) << "Cannot resolve __system_property_get(): " << dlerror(); |
| 38 | } |
| 39 | return real_system_property_get; |
| 40 | } |
| 41 | |
| 42 | static base::LazyInstance<base::internal::LazySysInfoValue< |
| 43 | SystemPropertyGetFunction*, DynamicallyLoadRealSystemPropertyGet> >::Leaky |
| 44 | g_lazy_real_system_property_get = LAZY_INSTANCE_INITIALIZER; |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | // Android 'L' removes __system_property_get from the NDK, however it is still |
| 49 | // a hidden symbol in libc. Until we remove all calls of __system_property_get |
| 50 | // from Chrome we work around this by defining a weak stub here, which uses |
| 51 | // dlsym to but ensures that Chrome uses the real system |
| 52 | // implementatation when loaded. http://crbug.com/392191. |
fdegans | 3c7624d6 | 2015-01-07 16:17:47 | [diff] [blame] | 53 | BASE_EXPORT int __system_property_get(const char* name, char* value) { |
[email protected] | 2e4045d | 2014-07-18 05:41:54 | [diff] [blame] | 54 | return g_lazy_real_system_property_get.Get().value()(name, value); |
| 55 | } |
| 56 | |
| 57 | #endif |
| 58 | |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 59 | namespace { |
| 60 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 61 | // Default version of Android to fall back to when actual version numbers |
[email protected] | de874b4 | 2014-07-10 08:05:21 | [diff] [blame] | 62 | // cannot be acquired. Use the latest Android release with a higher bug fix |
| 63 | // version to avoid unnecessarily comparison errors with the latest release. |
thakis | 3e861de | 2016-06-14 14:24:01 | [diff] [blame] | 64 | // This should be manually kept up to date on each Android release. |
boliu | ac3dfbdb | 2016-07-22 19:13:16 | [diff] [blame] | 65 | const int kDefaultAndroidMajorVersion = 7; |
boliu | 61dcc02 | 2015-10-14 16:52:30 | [diff] [blame] | 66 | const int kDefaultAndroidMinorVersion = 0; |
[email protected] | de874b4 | 2014-07-10 08:05:21 | [diff] [blame] | 67 | const int kDefaultAndroidBugfixVersion = 99; |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 68 | |
Bo Liu | 4f2b4e14 | 2017-05-26 21:41:51 | [diff] [blame^] | 69 | // Get and parse out the OS version numbers from the system properties. |
| 70 | // Note if parse fails, the "default" version is returned as fallback. |
| 71 | void GetOsVersionStringAndNumbers(std::string* version_string, |
| 72 | int32_t* major_version, |
| 73 | int32_t* minor_version, |
| 74 | int32_t* bugfix_version) { |
| 75 | // Read the version number string out from the properties. |
| 76 | char os_version_str[PROP_VALUE_MAX]; |
| 77 | __system_property_get("ro.build.version.release", os_version_str); |
| 78 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 79 | if (os_version_str[0]) { |
| 80 | // Try to parse out the version numbers from the string. |
| 81 | int num_read = sscanf(os_version_str, "%d.%d.%d", major_version, |
| 82 | minor_version, bugfix_version); |
| 83 | |
| 84 | if (num_read > 0) { |
| 85 | // If we don't have a full set of version numbers, make the extras 0. |
Bo Liu | 4f2b4e14 | 2017-05-26 21:41:51 | [diff] [blame^] | 86 | if (num_read < 2) |
| 87 | *minor_version = 0; |
| 88 | if (num_read < 3) |
| 89 | *bugfix_version = 0; |
| 90 | *version_string = std::string(os_version_str); |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // For some reason, we couldn't parse the version number string. |
| 96 | *major_version = kDefaultAndroidMajorVersion; |
| 97 | *minor_version = kDefaultAndroidMinorVersion; |
| 98 | *bugfix_version = kDefaultAndroidBugfixVersion; |
Bo Liu | 4f2b4e14 | 2017-05-26 21:41:51 | [diff] [blame^] | 99 | *version_string = ::base::StringPrintf("%d.%d.%d", *major_version, |
| 100 | *minor_version, *bugfix_version); |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 103 | // Parses a system property (specified with unit 'k','m' or 'g'). |
| 104 | // Returns a value in bytes. |
| 105 | // Returns -1 if the string could not be parsed. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 106 | int64_t ParseSystemPropertyBytes(const base::StringPiece& str) { |
| 107 | const int64_t KB = 1024; |
| 108 | const int64_t MB = 1024 * KB; |
| 109 | const int64_t GB = 1024 * MB; |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 110 | if (str.size() == 0u) |
| 111 | return -1; |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 112 | int64_t unit_multiplier = 1; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 113 | size_t length = str.size(); |
| 114 | if (str[length - 1] == 'k') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 115 | unit_multiplier = KB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 116 | length--; |
| 117 | } else if (str[length - 1] == 'm') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 118 | unit_multiplier = MB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 119 | length--; |
| 120 | } else if (str[length - 1] == 'g') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 121 | unit_multiplier = GB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 122 | length--; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 123 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 124 | int64_t result = 0; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 125 | bool parsed = base::StringToInt64(str.substr(0, length), &result); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 126 | bool negative = result <= 0; |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 127 | bool overflow = |
| 128 | result >= std::numeric_limits<int64_t>::max() / unit_multiplier; |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 129 | if (!parsed || negative || overflow) |
| 130 | return -1; |
| 131 | return result * unit_multiplier; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int GetDalvikHeapSizeMB() { |
| 135 | char heap_size_str[PROP_VALUE_MAX]; |
| 136 | __system_property_get("dalvik.vm.heapsize", heap_size_str); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 137 | // dalvik.vm.heapsize property is writable by a root user. |
| 138 | // Clamp it to reasonable range as a sanity check, |
| 139 | // a typical android device will never have less than 48MB. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 140 | const int64_t MB = 1024 * 1024; |
| 141 | int64_t result = ParseSystemPropertyBytes(heap_size_str); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 142 | if (result == -1) { |
| 143 | // We should consider not exposing these values if they are not reliable. |
| 144 | LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str; |
| 145 | result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3; |
| 146 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 147 | result = |
| 148 | std::min<int64_t>(std::max<int64_t>(32 * MB, result), 1024 * MB) / MB; |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 149 | return static_cast<int>(result); |
| 150 | } |
| 151 | |
| 152 | int GetDalvikHeapGrowthLimitMB() { |
| 153 | char heap_size_str[PROP_VALUE_MAX]; |
| 154 | __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str); |
| 155 | // dalvik.vm.heapgrowthlimit property is writable by a root user. |
| 156 | // Clamp it to reasonable range as a sanity check, |
| 157 | // a typical android device will never have less than 24MB. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 158 | const int64_t MB = 1024 * 1024; |
| 159 | int64_t result = ParseSystemPropertyBytes(heap_size_str); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 160 | if (result == -1) { |
| 161 | // We should consider not exposing these values if they are not reliable. |
| 162 | LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str; |
| 163 | result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6; |
| 164 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 165 | result = std::min<int64_t>(std::max<int64_t>(16 * MB, result), 512 * MB) / MB; |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 166 | return static_cast<int>(result); |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // anonymous namespace |
| 170 | |
| 171 | namespace base { |
| 172 | |
tdresser | ae416695 | 2015-07-16 15:41:04 | [diff] [blame] | 173 | std::string SysInfo::HardwareModelName() { |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 174 | char device_model_str[PROP_VALUE_MAX]; |
| 175 | __system_property_get("ro.product.model", device_model_str); |
| 176 | return std::string(device_model_str); |
| 177 | } |
| 178 | |
tdresser | ae416695 | 2015-07-16 15:41:04 | [diff] [blame] | 179 | std::string SysInfo::OperatingSystemName() { |
| 180 | return "Android"; |
| 181 | } |
| 182 | |
[email protected] | 82aeeaa | 2013-05-07 04:52:45 | [diff] [blame] | 183 | std::string SysInfo::OperatingSystemVersion() { |
Bo Liu | 4f2b4e14 | 2017-05-26 21:41:51 | [diff] [blame^] | 184 | std::string version_string; |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 185 | int32_t major, minor, bugfix; |
Bo Liu | 4f2b4e14 | 2017-05-26 21:41:51 | [diff] [blame^] | 186 | GetOsVersionStringAndNumbers(&version_string, &major, &minor, &bugfix); |
| 187 | return version_string; |
[email protected] | 82aeeaa | 2013-05-07 04:52:45 | [diff] [blame] | 188 | } |
| 189 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 190 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 191 | int32_t* minor_version, |
| 192 | int32_t* bugfix_version) { |
Bo Liu | 4f2b4e14 | 2017-05-26 21:41:51 | [diff] [blame^] | 193 | std::string version_string; |
| 194 | GetOsVersionStringAndNumbers(&version_string, major_version, minor_version, |
| 195 | bugfix_version); |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 196 | } |
| 197 | |
tdresser | ae416695 | 2015-07-16 15:41:04 | [diff] [blame] | 198 | std::string SysInfo::GetAndroidBuildCodename() { |
| 199 | char os_version_codename_str[PROP_VALUE_MAX]; |
| 200 | __system_property_get("ro.build.version.codename", os_version_codename_str); |
| 201 | return std::string(os_version_codename_str); |
| 202 | } |
| 203 | |
| 204 | std::string SysInfo::GetAndroidBuildID() { |
| 205 | char os_build_id_str[PROP_VALUE_MAX]; |
| 206 | __system_property_get("ro.build.id", os_build_id_str); |
| 207 | return std::string(os_build_id_str); |
| 208 | } |
| 209 | |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 210 | int SysInfo::DalvikHeapSizeMB() { |
| 211 | static int heap_size = GetDalvikHeapSizeMB(); |
| 212 | return heap_size; |
| 213 | } |
| 214 | |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 215 | int SysInfo::DalvikHeapGrowthLimitMB() { |
| 216 | static int heap_growth_limit = GetDalvikHeapGrowthLimitMB(); |
| 217 | return heap_growth_limit; |
| 218 | } |
| 219 | |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 220 | static base::LazyInstance< |
| 221 | base::internal::LazySysInfoValue<bool, |
| 222 | android::SysUtils::IsLowEndDeviceFromJni> >::Leaky |
| 223 | g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER; |
| 224 | |
| 225 | bool SysInfo::IsLowEndDevice() { |
svaisanen | 2e4bd66 | 2016-04-11 07:34:21 | [diff] [blame] | 226 | // This code might be used in some environments |
| 227 | // which might not have a Java environment. |
| 228 | // Note that we need to call the Java version here. |
| 229 | // There exists a complete native implementation in |
| 230 | // sys_info.cc but calling that here would mean that |
| 231 | // the Java code and the native code would call different |
| 232 | // implementations which could give different results. |
| 233 | // Also the Java code cannot depend on the native code |
| 234 | // since it might not be loaded yet. |
| 235 | if (!base::android::IsVMInitialized()) |
| 236 | return false; |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 237 | return g_lazy_low_end_device.Get().value(); |
| 238 | } |
| 239 | |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 240 | |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 241 | } // namespace base |