[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 | |
| 7 | #include <sys/system_properties.h> |
| 8 | |
| 9 | #include "base/logging.h" |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 11 | #include "base/strings/string_piece.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame^] | 12 | #include "base/strings/stringprintf.h" |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 16 | // Default version of Android to fall back to when actual version numbers |
| 17 | // cannot be acquired. |
| 18 | // TODO(dfalcantara): Keep this reasonably up to date with the latest publicly |
| 19 | // available version of Android. |
| 20 | static const int kDefaultAndroidMajorVersion = 4; |
| 21 | static const int kDefaultAndroidMinorVersion = 0; |
| 22 | static const int kDefaultAndroidBugfixVersion = 3; |
| 23 | |
| 24 | // Parse out the OS version numbers from the system properties. |
| 25 | void ParseOSVersionNumbers(const char* os_version_str, |
| 26 | int32 *major_version, |
| 27 | int32 *minor_version, |
| 28 | int32 *bugfix_version) { |
| 29 | if (os_version_str[0]) { |
| 30 | // Try to parse out the version numbers from the string. |
| 31 | int num_read = sscanf(os_version_str, "%d.%d.%d", major_version, |
| 32 | minor_version, bugfix_version); |
| 33 | |
| 34 | if (num_read > 0) { |
| 35 | // If we don't have a full set of version numbers, make the extras 0. |
| 36 | if (num_read < 2) *minor_version = 0; |
| 37 | if (num_read < 3) *bugfix_version = 0; |
| 38 | return; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // For some reason, we couldn't parse the version number string. |
| 43 | *major_version = kDefaultAndroidMajorVersion; |
| 44 | *minor_version = kDefaultAndroidMinorVersion; |
| 45 | *bugfix_version = kDefaultAndroidBugfixVersion; |
| 46 | } |
| 47 | |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 48 | // Parses a system property (specified with unit 'k','m' or 'g'). |
| 49 | // Returns a value in bytes. |
| 50 | // Returns -1 if the string could not be parsed. |
| 51 | int64 ParseSystemPropertyBytes(const base::StringPiece& str) { |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 52 | const int64 KB = 1024; |
| 53 | const int64 MB = 1024 * KB; |
| 54 | const int64 GB = 1024 * MB; |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 55 | if (str.size() == 0u) |
| 56 | return -1; |
| 57 | int64 unit_multiplier = 1; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 58 | size_t length = str.size(); |
| 59 | if (str[length - 1] == 'k') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 60 | unit_multiplier = KB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 61 | length--; |
| 62 | } else if (str[length - 1] == 'm') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 63 | unit_multiplier = MB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 64 | length--; |
| 65 | } else if (str[length - 1] == 'g') { |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 66 | unit_multiplier = GB; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 67 | length--; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 68 | } |
| 69 | int64 result = 0; |
| 70 | bool parsed = base::StringToInt64(str.substr(0, length), &result); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 71 | bool negative = result <= 0; |
| 72 | bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier; |
| 73 | if (!parsed || negative || overflow) |
| 74 | return -1; |
| 75 | return result * unit_multiplier; |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int GetDalvikHeapSizeMB() { |
| 79 | char heap_size_str[PROP_VALUE_MAX]; |
| 80 | __system_property_get("dalvik.vm.heapsize", heap_size_str); |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 81 | // dalvik.vm.heapsize property is writable by a root user. |
| 82 | // Clamp it to reasonable range as a sanity check, |
| 83 | // a typical android device will never have less than 48MB. |
| 84 | const int64 MB = 1024 * 1024; |
| 85 | int64 result = ParseSystemPropertyBytes(heap_size_str); |
| 86 | if (result == -1) { |
| 87 | // We should consider not exposing these values if they are not reliable. |
| 88 | LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str; |
| 89 | result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3; |
| 90 | } |
| 91 | result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB; |
| 92 | return static_cast<int>(result); |
| 93 | } |
| 94 | |
| 95 | int GetDalvikHeapGrowthLimitMB() { |
| 96 | char heap_size_str[PROP_VALUE_MAX]; |
| 97 | __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str); |
| 98 | // dalvik.vm.heapgrowthlimit property is writable by a root user. |
| 99 | // Clamp it to reasonable range as a sanity check, |
| 100 | // a typical android device will never have less than 24MB. |
| 101 | const int64 MB = 1024 * 1024; |
| 102 | int64 result = ParseSystemPropertyBytes(heap_size_str); |
| 103 | if (result == -1) { |
| 104 | // We should consider not exposing these values if they are not reliable. |
| 105 | LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str; |
| 106 | result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6; |
| 107 | } |
| 108 | result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB; |
| 109 | return static_cast<int>(result); |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // anonymous namespace |
| 113 | |
| 114 | namespace base { |
| 115 | |
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 116 | std::string SysInfo::OperatingSystemName() { |
| 117 | return "Android"; |
| 118 | } |
| 119 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 120 | std::string SysInfo::GetAndroidBuildCodename() { |
| 121 | char os_version_codename_str[PROP_VALUE_MAX]; |
| 122 | __system_property_get("ro.build.version.codename", os_version_codename_str); |
| 123 | return std::string(os_version_codename_str); |
| 124 | } |
| 125 | |
| 126 | std::string SysInfo::GetAndroidBuildID() { |
| 127 | char os_build_id_str[PROP_VALUE_MAX]; |
| 128 | __system_property_get("ro.build.id", os_build_id_str); |
| 129 | return std::string(os_build_id_str); |
| 130 | } |
| 131 | |
| 132 | std::string SysInfo::GetDeviceName() { |
| 133 | char device_model_str[PROP_VALUE_MAX]; |
| 134 | __system_property_get("ro.product.model", device_model_str); |
| 135 | return std::string(device_model_str); |
| 136 | } |
| 137 | |
[email protected] | 82aeeaa | 2013-05-07 04:52:45 | [diff] [blame] | 138 | std::string SysInfo::OperatingSystemVersion() { |
| 139 | int32 major, minor, bugfix; |
| 140 | OperatingSystemVersionNumbers(&major, &minor, &bugfix); |
| 141 | return StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 142 | } |
| 143 | |
[email protected] | abef4b33 | 2012-08-21 23:55:52 | [diff] [blame] | 144 | void SysInfo::OperatingSystemVersionNumbers(int32* major_version, |
| 145 | int32* minor_version, |
| 146 | int32* bugfix_version) { |
| 147 | // Read the version number string out from the properties. |
| 148 | char os_version_str[PROP_VALUE_MAX]; |
| 149 | __system_property_get("ro.build.version.release", os_version_str); |
| 150 | |
| 151 | // Parse out the numbers. |
| 152 | ParseOSVersionNumbers(os_version_str, major_version, minor_version, |
| 153 | bugfix_version); |
| 154 | } |
| 155 | |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 156 | int SysInfo::DalvikHeapSizeMB() { |
| 157 | static int heap_size = GetDalvikHeapSizeMB(); |
| 158 | return heap_size; |
| 159 | } |
| 160 | |
[email protected] | 0cea355 | 2013-02-16 18:15:50 | [diff] [blame] | 161 | int SysInfo::DalvikHeapGrowthLimitMB() { |
| 162 | static int heap_growth_limit = GetDalvikHeapGrowthLimitMB(); |
| 163 | return heap_growth_limit; |
| 164 | } |
| 165 | |
| 166 | |
[email protected] | 5702108f | 2012-05-25 15:31:37 | [diff] [blame] | 167 | } // namespace base |