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.
5 #include "base/sys_info.h"
7 #include <sys/system_properties.h>
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/stringprintf.h"
16 // Default version of Android to fall back to when actual version numbers
17 // cannot be acquired. This is an obviously invalid version number. Code
18 // doing version comparison should treat it as so and fail all comparisons.
19 const int kDefaultAndroidMajorVersion
= 0;
20 const int kDefaultAndroidMinorVersion
= 0;
21 const int kDefaultAndroidBugfixVersion
= 0;
23 // Parse out the OS version numbers from the system properties.
24 void ParseOSVersionNumbers(const char* os_version_str
,
27 int32
*bugfix_version
) {
28 if (os_version_str
[0]) {
29 // Try to parse out the version numbers from the string.
30 int num_read
= sscanf(os_version_str
, "%d.%d.%d", major_version
,
31 minor_version
, bugfix_version
);
34 // If we don't have a full set of version numbers, make the extras 0.
35 if (num_read
< 2) *minor_version
= 0;
36 if (num_read
< 3) *bugfix_version
= 0;
41 // For some reason, we couldn't parse the version number string.
42 *major_version
= kDefaultAndroidMajorVersion
;
43 *minor_version
= kDefaultAndroidMinorVersion
;
44 *bugfix_version
= kDefaultAndroidBugfixVersion
;
47 // Parses a system property (specified with unit 'k','m' or 'g').
48 // Returns a value in bytes.
49 // Returns -1 if the string could not be parsed.
50 int64
ParseSystemPropertyBytes(const base::StringPiece
& str
) {
51 const int64 KB
= 1024;
52 const int64 MB
= 1024 * KB
;
53 const int64 GB
= 1024 * MB
;
56 int64 unit_multiplier
= 1;
57 size_t length
= str
.size();
58 if (str
[length
- 1] == 'k') {
61 } else if (str
[length
- 1] == 'm') {
64 } else if (str
[length
- 1] == 'g') {
69 bool parsed
= base::StringToInt64(str
.substr(0, length
), &result
);
70 bool negative
= result
<= 0;
71 bool overflow
= result
>= std::numeric_limits
<int64
>::max() / unit_multiplier
;
72 if (!parsed
|| negative
|| overflow
)
74 return result
* unit_multiplier
;
77 int GetDalvikHeapSizeMB() {
78 char heap_size_str
[PROP_VALUE_MAX
];
79 __system_property_get("dalvik.vm.heapsize", heap_size_str
);
80 // dalvik.vm.heapsize property is writable by a root user.
81 // Clamp it to reasonable range as a sanity check,
82 // a typical android device will never have less than 48MB.
83 const int64 MB
= 1024 * 1024;
84 int64 result
= ParseSystemPropertyBytes(heap_size_str
);
86 // We should consider not exposing these values if they are not reliable.
87 LOG(ERROR
) << "Can't parse dalvik.vm.heapsize: " << heap_size_str
;
88 result
= base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
90 result
= std::min
<int64
>(std::max
<int64
>(32 * MB
, result
), 1024 * MB
) / MB
;
91 return static_cast<int>(result
);
94 int GetDalvikHeapGrowthLimitMB() {
95 char heap_size_str
[PROP_VALUE_MAX
];
96 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str
);
97 // dalvik.vm.heapgrowthlimit property is writable by a root user.
98 // Clamp it to reasonable range as a sanity check,
99 // a typical android device will never have less than 24MB.
100 const int64 MB
= 1024 * 1024;
101 int64 result
= ParseSystemPropertyBytes(heap_size_str
);
103 // We should consider not exposing these values if they are not reliable.
104 LOG(ERROR
) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str
;
105 result
= base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
107 result
= std::min
<int64
>(std::max
<int64
>(16 * MB
, result
), 512 * MB
) / MB
;
108 return static_cast<int>(result
);
111 } // anonymous namespace
115 std::string
SysInfo::OperatingSystemName() {
119 std::string
SysInfo::GetAndroidBuildCodename() {
120 char os_version_codename_str
[PROP_VALUE_MAX
];
121 __system_property_get("ro.build.version.codename", os_version_codename_str
);
122 return std::string(os_version_codename_str
);
125 std::string
SysInfo::GetAndroidBuildID() {
126 char os_build_id_str
[PROP_VALUE_MAX
];
127 __system_property_get("ro.build.id", os_build_id_str
);
128 return std::string(os_build_id_str
);
131 std::string
SysInfo::GetDeviceName() {
132 char device_model_str
[PROP_VALUE_MAX
];
133 __system_property_get("ro.product.model", device_model_str
);
134 return std::string(device_model_str
);
137 std::string
SysInfo::OperatingSystemVersion() {
138 int32 major
, minor
, bugfix
;
139 OperatingSystemVersionNumbers(&major
, &minor
, &bugfix
);
140 return StringPrintf("%d.%d.%d", major
, minor
, bugfix
);
143 void SysInfo::OperatingSystemVersionNumbers(int32
* major_version
,
144 int32
* minor_version
,
145 int32
* bugfix_version
) {
146 // Read the version number string out from the properties.
147 char os_version_str
[PROP_VALUE_MAX
];
148 __system_property_get("ro.build.version.release", os_version_str
);
150 // Parse out the numbers.
151 ParseOSVersionNumbers(os_version_str
, major_version
, minor_version
,
155 int SysInfo::DalvikHeapSizeMB() {
156 static int heap_size
= GetDalvikHeapSizeMB();
160 int SysInfo::DalvikHeapGrowthLimitMB() {
161 static int heap_growth_limit
= GetDalvikHeapGrowthLimitMB();
162 return heap_growth_limit
;