[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 | |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 12 | #include "base/android/sys_utils.h" |
| 13 | #include "base/lazy_instance.h" |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 16 | #include "base/strings/string_piece.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 17 | #include "base/strings/stringprintf.h" |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 18 | #include "base/sys_info_internal.h" |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 19 | |
fdegans | 27660a3 | 2014-10-24 13:00:18 | [diff] [blame] | 20 | #if (__ANDROID_API__ >= 21 /* 5.0 - Lollipop */) |
[email protected] | 2e4045d | 2014-07-18 05:41:54 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
| 24 | typedef int (SystemPropertyGetFunction)(const char*, char*); |
| 25 | |
| 26 | SystemPropertyGetFunction* 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 | |
| 41 | static 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. |
fdegans | 3c7624d6 | 2015-01-07 16:17:47 | [diff] [blame] | 52 | BASE_EXPORT int __system_property_get(const char* name, char* value) { |
[email protected] | 2e4045d | 2014-07-18 05:41:54 | [diff] [blame] | 53 | return g_lazy_real_system_property_get.Get().value()(name, value); |
| 54 | } |
| 55 | |
| 56 | #endif |
| 57 | |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 58 | namespace { |
| 59 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 60 | // Default version of Android to fall back to when actual version numbers |
[email protected] | de874b4 | 2014-07-10 08:05:21 | [diff] [blame] | 61 | // 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. |
boliu | 61dcc02 | 2015-10-14 16:52:30 | [diff] [blame] | 64 | const int kDefaultAndroidMajorVersion = 6; |
| 65 | const int kDefaultAndroidMinorVersion = 0; |
[email protected] | de874b4 | 2014-07-10 08:05:21 | [diff] [blame] | 66 | const int kDefaultAndroidBugfixVersion = 99; |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 67 | |
| 68 | // Parse out the OS version numbers from the system properties. |
| 69 | void ParseOSVersionNumbers(const char* os_version_str, |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 70 | int32_t* major_version, |
| 71 | int32_t* minor_version, |
| 72 | int32_t* bugfix_version) { |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 73 | 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] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 92 | // 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. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 95 | int64_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] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 99 | if (str.size() == 0u) |
| 100 | return -1; |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 101 | int64_t unit_multiplier = 1; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 102 | size_t length = str.size(); |
| 103 | if (str[length - 1] == 'k') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 104 | unit_multiplier = KB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 105 | length--; |
| 106 | } else if (str[length - 1] == 'm') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 107 | unit_multiplier = MB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 108 | length--; |
| 109 | } else if (str[length - 1] == 'g') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 110 | unit_multiplier = GB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 111 | length--; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 112 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 113 | int64_t result = 0; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 114 | bool parsed = base::StringToInt64(str.substr(0, length), &result); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 115 | bool negative = result <= 0; |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 116 | bool overflow = |
| 117 | result >= std::numeric_limits<int64_t>::max() / unit_multiplier; |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 118 | if (!parsed || negative || overflow) |
| 119 | return -1; |
| 120 | return result * unit_multiplier; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int GetDalvikHeapSizeMB() { |
| 124 | char heap_size_str[PROP_VALUE_MAX]; |
| 125 | __system_property_get("dalvik.vm.heapsize", heap_size_str); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 126 | // 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. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 129 | const int64_t MB = 1024 * 1024; |
| 130 | int64_t result = ParseSystemPropertyBytes(heap_size_str); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 131 | 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 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 136 | result = |
| 137 | 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] | 138 | return static_cast<int>(result); |
| 139 | } |
| 140 | |
| 141 | int 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. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 147 | const int64_t MB = 1024 * 1024; |
| 148 | int64_t result = ParseSystemPropertyBytes(heap_size_str); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 149 | 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 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 154 | 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] | 155 | return static_cast<int>(result); |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // anonymous namespace |
| 159 | |
| 160 | namespace base { |
| 161 | |
tdresser | ae416695 | 2015-07-16 15:41:04 | [diff] [blame] | 162 | std::string SysInfo::HardwareModelName() { |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 163 | 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 | |
tdresser | ae416695 | 2015-07-16 15:41:04 | [diff] [blame] | 168 | std::string SysInfo::OperatingSystemName() { |
| 169 | return "Android"; |
| 170 | } |
| 171 | |
[email protected] | 82aeeaa | 2013-05-07 04:52:45 | [diff] [blame] | 172 | std::string SysInfo::OperatingSystemVersion() { |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 173 | int32_t major, minor, bugfix; |
[email protected] | 82aeeaa | 2013-05-07 04:52:45 | [diff] [blame] | 174 | OperatingSystemVersionNumbers(&major, &minor, &bugfix); |
| 175 | return StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 176 | } |
| 177 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 178 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 179 | int32_t* minor_version, |
| 180 | int32_t* bugfix_version) { |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 181 | // 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 | |
tdresser | ae416695 | 2015-07-16 15:41:04 | [diff] [blame] | 190 | std::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 | |
| 196 | std::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] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 202 | int SysInfo::DalvikHeapSizeMB() { |
| 203 | static int heap_size = GetDalvikHeapSizeMB(); |
| 204 | return heap_size; |
| 205 | } |
| 206 | |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 207 | int SysInfo::DalvikHeapGrowthLimitMB() { |
| 208 | static int heap_growth_limit = GetDalvikHeapGrowthLimitMB(); |
| 209 | return heap_growth_limit; |
| 210 | } |
| 211 | |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 212 | static base::LazyInstance< |
| 213 | base::internal::LazySysInfoValue<bool, |
| 214 | android::SysUtils::IsLowEndDeviceFromJni> >::Leaky |
| 215 | g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER; |
| 216 | |
| 217 | bool SysInfo::IsLowEndDevice() { |
| 218 | return g_lazy_low_end_device.Get().value(); |
| 219 | } |
| 220 | |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 221 | |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 222 | } // namespace base |