OLD | NEW |
(Empty) | |
| 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" |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_piece.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 int ParseHeapSize(const base::StringPiece& str) { |
| 16 const int64 KB = 1024; |
| 17 const int64 MB = 1024 * KB; |
| 18 const int64 GB = 1024 * MB; |
| 19 CHECK_GT(str.size(), 0u); |
| 20 int64 factor = 1; |
| 21 size_t length = str.size(); |
| 22 if (str[length - 1] == 'k') { |
| 23 factor = KB; |
| 24 length--; |
| 25 } else if (str[length - 1] == 'm') { |
| 26 factor = MB; |
| 27 length--; |
| 28 } else if (str[length - 1] == 'g') { |
| 29 factor = GB; |
| 30 length--; |
| 31 } else { |
| 32 CHECK('0' <= str[length - 1] && str[length - 1] <= '9'); |
| 33 } |
| 34 int64 result = 0; |
| 35 bool parsed = base::StringToInt64(str.substr(0, length), &result); |
| 36 CHECK(parsed); |
| 37 result = result * factor / MB; |
| 38 // dalvik.vm.heapsize property is writable by user, |
| 39 // truncate it to reasonable value to avoid overflows later. |
| 40 result = std::min<int64>(std::max<int64>(32, result), 1024); |
| 41 return static_cast<int>(result); |
| 42 } |
| 43 |
| 44 int GetDalvikHeapSizeMB() { |
| 45 char heap_size_str[PROP_VALUE_MAX]; |
| 46 __system_property_get("dalvik.vm.heapsize", heap_size_str); |
| 47 return ParseHeapSize(heap_size_str); |
| 48 } |
| 49 |
| 50 } // anonymous namespace |
| 51 |
| 52 namespace base { |
| 53 |
| 54 int SysInfo::DalvikHeapSizeMB() { |
| 55 static int heap_size = GetDalvikHeapSizeMB(); |
| 56 return heap_size; |
| 57 } |
| 58 |
| 59 } // namespace base |
OLD | NEW |