blob: 577ca0e6bd17b2cf4316d8166546f657013c3588 [file] [log] [blame]
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +09001// 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"
brettw@chromium.orgabcde5c2013-02-07 11:57:22 +090010#include "base/strings/string_number_conversions.h"
tfarina@chromium.orgb6d49112013-03-30 23:29:00 +090011#include "base/strings/string_piece.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090012#include "base/strings/stringprintf.h"
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090013
14namespace {
15
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +090016// 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.
joth@chromium.org8b981a32013-08-02 13:54:21 +090020const int kDefaultAndroidMajorVersion = 4;
21const int kDefaultAndroidMinorVersion = 3;
22const int kDefaultAndroidBugfixVersion = 0;
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +090023
24// Parse out the OS version numbers from the system properties.
25void 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
epenner@chromium.org066b81b2013-02-17 03:15:50 +090048// 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.
51int64 ParseSystemPropertyBytes(const base::StringPiece& str) {
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090052 const int64 KB = 1024;
53 const int64 MB = 1024 * KB;
54 const int64 GB = 1024 * MB;
epenner@chromium.org066b81b2013-02-17 03:15:50 +090055 if (str.size() == 0u)
56 return -1;
57 int64 unit_multiplier = 1;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090058 size_t length = str.size();
59 if (str[length - 1] == 'k') {
epenner@chromium.org066b81b2013-02-17 03:15:50 +090060 unit_multiplier = KB;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090061 length--;
62 } else if (str[length - 1] == 'm') {
epenner@chromium.org066b81b2013-02-17 03:15:50 +090063 unit_multiplier = MB;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090064 length--;
65 } else if (str[length - 1] == 'g') {
epenner@chromium.org066b81b2013-02-17 03:15:50 +090066 unit_multiplier = GB;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090067 length--;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090068 }
69 int64 result = 0;
70 bool parsed = base::StringToInt64(str.substr(0, length), &result);
epenner@chromium.org066b81b2013-02-17 03:15:50 +090071 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;
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +090076}
77
78int GetDalvikHeapSizeMB() {
79 char heap_size_str[PROP_VALUE_MAX];
80 __system_property_get("dalvik.vm.heapsize", heap_size_str);
epenner@chromium.org066b81b2013-02-17 03:15:50 +090081 // 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
95int 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);
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900110}
111
112} // anonymous namespace
113
114namespace base {
115
dfalcantara@chromium.orgefbe5312012-10-04 05:10:44 +0900116std::string SysInfo::OperatingSystemName() {
117 return "Android";
118}
119
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +0900120std::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
126std::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
132std::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
zmo@chromium.orgad89ff52013-05-07 13:52:45 +0900138std::string SysInfo::OperatingSystemVersion() {
139 int32 major, minor, bugfix;
140 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
141 return StringPrintf("%d.%d.%d", major, minor, bugfix);
142}
143
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +0900144void 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
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900156int SysInfo::DalvikHeapSizeMB() {
157 static int heap_size = GetDalvikHeapSizeMB();
158 return heap_size;
159}
160
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900161int SysInfo::DalvikHeapGrowthLimitMB() {
162 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
163 return heap_growth_limit;
164}
165
166
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900167} // namespace base