Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/CPP/CPPTypes.h" |
| 25 | #include "arm_compute/core/Error.h" |
| 26 | #include "support/ToolchainSupport.h" |
| 27 | |
| 28 | #ifndef BARE_METAL |
| 29 | #include <fstream> |
| 30 | #include <regex> |
| 31 | #include <sstream> |
| 32 | #endif // ifndef BARE_METAL |
| 33 | |
| 34 | namespace |
| 35 | { |
| 36 | void parse_mem_info(size_t &total, size_t &free, size_t &buffer) |
| 37 | { |
| 38 | free = 0; |
| 39 | total = 0; |
| 40 | buffer = 0; |
| 41 | #ifndef BARE_METAL |
| 42 | size_t memcache = 0; |
| 43 | size_t memfree = 0; |
| 44 | std::ifstream meminfo_f; |
| 45 | meminfo_f.open("/proc/meminfo", std::ios::in); |
| 46 | if(meminfo_f.is_open()) |
| 47 | { |
| 48 | std::stringstream str_stream; |
| 49 | str_stream << meminfo_f.rdbuf(); |
| 50 | const std::string str = str_stream.str(); |
| 51 | try |
| 52 | { |
| 53 | std::smatch match; |
| 54 | if(std::regex_search(str, match, std::regex("MemTotal: (.*)kB")) && match.size() > 1) |
| 55 | { |
| 56 | const std::string result = match.str(1); |
| 57 | total = std::stoul(result, nullptr, 0); |
| 58 | } |
| 59 | if(std::regex_search(str, match, std::regex("MemFree: (.*)kB")) && match.size() > 1) |
| 60 | { |
| 61 | const std::string result = match.str(1); |
| 62 | memfree = std::stoul(result, nullptr, 0); |
| 63 | } |
| 64 | if(std::regex_search(str, match, std::regex("Buffers: (.*)kB")) && match.size() > 1) |
| 65 | { |
| 66 | const std::string result = match.str(1); |
| 67 | buffer = std::stoul(result, nullptr, 0); |
| 68 | } |
| 69 | if(std::regex_search(str, match, std::regex("Cached: (.*)kB")) && match.size() > 1) |
| 70 | { |
| 71 | const std::string result = match.str(1); |
| 72 | memcache = std::stoul(result, nullptr, 0); |
| 73 | } |
| 74 | free = memfree + (buffer + memcache); |
| 75 | } |
| 76 | catch(std::regex_error &e) |
| 77 | { |
| 78 | // failed parsing /proc/meminfo |
| 79 | // return 0s on all fields |
| 80 | } |
| 81 | } |
| 82 | #endif // ifndef BARE_METAL |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | namespace arm_compute |
| 88 | { |
| 89 | void MEMInfo::set_policy(MemoryPolicy policy) |
| 90 | { |
| 91 | _policy = policy; |
| 92 | } |
| 93 | |
| 94 | MemoryPolicy MEMInfo::get_policy() |
| 95 | { |
| 96 | return _policy; |
| 97 | } |
| 98 | MemoryPolicy MEMInfo::_policy = { MemoryPolicy::NORMAL }; |
| 99 | |
| 100 | MEMInfo::MEMInfo() |
| 101 | : _total(0), _free(0), _buffer(0) |
| 102 | { |
| 103 | parse_mem_info(_total, _free, _buffer); |
| 104 | } |
| 105 | |
| 106 | size_t MEMInfo::get_total_in_kb() const |
| 107 | { |
| 108 | return _total; |
| 109 | } |
| 110 | |
| 111 | } // namespace arm_compute |