Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: base/sys_info_android.cc

Issue 12223064: base: Fix parsing and add dalvik-heap-limit (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove CHECKs. Return error instead. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/sys_info.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/sys_info.h" 5 #include "base/sys_info.h"
6 6
7 #include <sys/system_properties.h> 7 #include <sys/system_properties.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_piece.h" 10 #include "base/string_piece.h"
(...skipping 26 matching lines...) Expand all
37 return; 37 return;
38 } 38 }
39 } 39 }
40 40
41 // For some reason, we couldn't parse the version number string. 41 // For some reason, we couldn't parse the version number string.
42 *major_version = kDefaultAndroidMajorVersion; 42 *major_version = kDefaultAndroidMajorVersion;
43 *minor_version = kDefaultAndroidMinorVersion; 43 *minor_version = kDefaultAndroidMinorVersion;
44 *bugfix_version = kDefaultAndroidBugfixVersion; 44 *bugfix_version = kDefaultAndroidBugfixVersion;
45 } 45 }
46 46
47 int ParseHeapSize(const base::StringPiece& str) { 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) {
48 const int64 KB = 1024; 51 const int64 KB = 1024;
49 const int64 MB = 1024 * KB; 52 const int64 MB = 1024 * KB;
50 const int64 GB = 1024 * MB; 53 const int64 GB = 1024 * MB;
51 CHECK_GT(str.size(), 0u); 54 if (str.size() == 0u)
52 int64 factor = 1; 55 return -1;
56 int64 unit_multiplier = 1;
53 size_t length = str.size(); 57 size_t length = str.size();
54 if (str[length - 1] == 'k') { 58 if (str[length - 1] == 'k') {
55 factor = KB; 59 unit_multiplier = KB;
56 length--; 60 length--;
57 } else if (str[length - 1] == 'm') { 61 } else if (str[length - 1] == 'm') {
58 factor = MB; 62 unit_multiplier = MB;
59 length--; 63 length--;
60 } else if (str[length - 1] == 'g') { 64 } else if (str[length - 1] == 'g') {
61 factor = GB; 65 unit_multiplier = GB;
62 length--; 66 length--;
63 } else {
64 CHECK('0' <= str[length - 1] && str[length - 1] <= '9');
65 } 67 }
66 int64 result = 0; 68 int64 result = 0;
67 bool parsed = base::StringToInt64(str.substr(0, length), &result); 69 bool parsed = base::StringToInt64(str.substr(0, length), &result);
68 CHECK(parsed); 70 bool negative = result <= 0;
69 result = result * factor / MB; 71 bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier;
70 // dalvik.vm.heapsize property is writable by user, 72 if (!parsed || negative || overflow)
71 // truncate it to reasonable value to avoid overflows later. 73 return -1;
72 result = std::min<int64>(std::max<int64>(32, result), 1024); 74 return result * unit_multiplier;
73 return static_cast<int>(result);
74 } 75 }
75 76
76 int GetDalvikHeapSizeMB() { 77 int GetDalvikHeapSizeMB() {
77 char heap_size_str[PROP_VALUE_MAX]; 78 char heap_size_str[PROP_VALUE_MAX];
78 __system_property_get("dalvik.vm.heapsize", heap_size_str); 79 __system_property_get("dalvik.vm.heapsize", heap_size_str);
79 return ParseHeapSize(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);
85 if (result == -1) {
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;
89 }
90 result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB;
91 return static_cast<int>(result);
92 }
93
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);
102 if (result == -1) {
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;
106 }
107 result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB;
108 return static_cast<int>(result);
80 } 109 }
81 110
82 } // anonymous namespace 111 } // anonymous namespace
83 112
84 namespace base { 113 namespace base {
85 114
86 std::string SysInfo::OperatingSystemName() { 115 std::string SysInfo::OperatingSystemName() {
87 return "Android"; 116 return "Android";
88 } 117 }
89 118
(...skipping 25 matching lines...) Expand all
115 // Parse out the numbers. 144 // Parse out the numbers.
116 ParseOSVersionNumbers(os_version_str, major_version, minor_version, 145 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
117 bugfix_version); 146 bugfix_version);
118 } 147 }
119 148
120 int SysInfo::DalvikHeapSizeMB() { 149 int SysInfo::DalvikHeapSizeMB() {
121 static int heap_size = GetDalvikHeapSizeMB(); 150 static int heap_size = GetDalvikHeapSizeMB();
122 return heap_size; 151 return heap_size;
123 } 152 }
124 153
154 int SysInfo::DalvikHeapGrowthLimitMB() {
155 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
156 return heap_growth_limit;
157 }
158
159
125 } // namespace base 160 } // namespace base
OLDNEW
« no previous file with comments | « base/sys_info.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698