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

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: Address feedback, 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
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 size (specified with unit 'k','m' or 'g').
48 // Returns a value in bytes.
49 int64 ParseSizeBytes(const base::StringPiece& str) {
48 const int64 KB = 1024; 50 const int64 KB = 1024;
49 const int64 MB = 1024 * KB; 51 const int64 MB = 1024 * KB;
50 const int64 GB = 1024 * MB; 52 const int64 GB = 1024 * MB;
51 CHECK_GT(str.size(), 0u); 53 CHECK_GT(str.size(), 0u);
52 int64 factor = 1; 54 int64 unit_multiplier = 1;
53 size_t length = str.size(); 55 size_t length = str.size();
54 if (str[length - 1] == 'k') { 56 if (str[length - 1] == 'k') {
55 factor = KB; 57 unit_multiplier = KB;
56 length--; 58 length--;
57 } else if (str[length - 1] == 'm') { 59 } else if (str[length - 1] == 'm') {
58 factor = MB; 60 unit_multiplier = MB;
59 length--; 61 length--;
60 } else if (str[length - 1] == 'g') { 62 } else if (str[length - 1] == 'g') {
61 factor = GB; 63 unit_multiplier = GB;
62 length--; 64 length--;
63 } else { 65 } else {
64 CHECK('0' <= str[length - 1] && str[length - 1] <= '9'); 66 CHECK('0' <= str[length - 1] && str[length - 1] <= '9');
jar (doing other things) 2013/02/13 21:54:11 It is bothersome that the un-tagged number is limi
epenner 2013/02/14 04:18:36 Sorry, how do you figure it's limited to 10billion
jar (doing other things) 2013/02/15 00:18:23 You are correct. I misread the code in a fast (mi
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 CHECK(parsed);
69 result = result * factor / MB; 71 return result * unit_multiplier;
jar (doing other things) 2013/02/13 21:54:11 This newer code is still problematic, as it ignore
epenner 2013/02/14 04:18:36 Thanks, good point! So, I guess I'll need to add o
70 // dalvik.vm.heapsize property is writable by user,
71 // truncate it to reasonable value to avoid overflows later.
72 result = std::min<int64>(std::max<int64>(32, result), 1024);
73 return static_cast<int>(result);
74 } 72 }
75 73
76 int GetDalvikHeapSizeMB() { 74 int GetDalvikHeapSizeMB() {
77 char heap_size_str[PROP_VALUE_MAX]; 75 char heap_size_str[PROP_VALUE_MAX];
78 __system_property_get("dalvik.vm.heapsize", heap_size_str); 76 __system_property_get("dalvik.vm.heapsize", heap_size_str);
79 return ParseHeapSize(heap_size_str); 77 // dalvik.vm.heapsize property is writable by a root user.
78 // Clamp it to reasonable range as a sanity check.
79 const int64 MB = 1024 * 1024;
80 int64 result = ParseSizeBytes(heap_size_str);
81 result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB;
jar (doing other things) 2013/02/13 21:54:11 This bounding was partially (almost) done on line
epenner 2013/02/14 04:18:36 Since we will want to re-use the parsing function
jar (doing other things) 2013/02/15 00:18:23 OK. On 2013/02/14 04:18:36, epenner wrote:
82 return static_cast<int>(result);
83 }
84
85 int GetDalvikHeapGrowthLimitMB() {
86 char heap_size_str[PROP_VALUE_MAX];
87 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
88 // dalvik.vm.heapgrowthlimit property is writable by a root user.
89 // Clamp it to reasonable range as a sanity check.
90 const int64 MB = 1024 * 1024;
91 int64 result = ParseSizeBytes(heap_size_str);
92 result = std::min<int64>(std::max<int64>(16 * MB, result), 1024 * MB) / MB;
jar (doing other things) 2013/02/14 01:48:41 Why do you clamp this to a different range than th
epenner 2013/02/14 04:18:36 The heap growth limit is typically half of the hea
jar (doing other things) 2013/02/15 00:18:23 nit: Please add comments explaining where these bo
93 return static_cast<int>(result);
80 } 94 }
81 95
82 } // anonymous namespace 96 } // anonymous namespace
83 97
84 namespace base { 98 namespace base {
85 99
86 std::string SysInfo::OperatingSystemName() { 100 std::string SysInfo::OperatingSystemName() {
87 return "Android"; 101 return "Android";
88 } 102 }
89 103
(...skipping 25 matching lines...) Expand all
115 // Parse out the numbers. 129 // Parse out the numbers.
116 ParseOSVersionNumbers(os_version_str, major_version, minor_version, 130 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
117 bugfix_version); 131 bugfix_version);
118 } 132 }
119 133
120 int SysInfo::DalvikHeapSizeMB() { 134 int SysInfo::DalvikHeapSizeMB() {
121 static int heap_size = GetDalvikHeapSizeMB(); 135 static int heap_size = GetDalvikHeapSizeMB();
122 return heap_size; 136 return heap_size;
123 } 137 }
124 138
139 int SysInfo::DalvikHeapGrowthLimitMB() {
140 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
141 return heap_growth_limit;
142 }
143
144
125 } // namespace base 145 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698