Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 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" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 26 | #include "support/StringSupport.h" |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 27 | |
| 28 | #ifndef BARE_METAL |
| 29 | #include <fstream> |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 30 | #include <iterator> |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 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); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 46 | |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 47 | if(meminfo_f.is_open()) |
| 48 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 49 | std::string line; |
| 50 | while(bool(getline(meminfo_f, line))) |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 51 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 52 | std::istringstream iss(line); |
| 53 | std::vector<std::string> tokens((std::istream_iterator<std::string>(iss)), |
| 54 | std::istream_iterator<std::string>()); |
| 55 | if(tokens[0] == "MemTotal:") |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 56 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 57 | total = arm_compute::support::cpp11::stoul(tokens[1], nullptr); |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 58 | } |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 59 | else if(tokens[0] == "MemFree:") |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 60 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 61 | memfree = arm_compute::support::cpp11::stoul(tokens[1], nullptr); |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 62 | } |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 63 | else if(tokens[0] == "Buffers:") |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 64 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 65 | buffer = arm_compute::support::cpp11::stoul(tokens[1], nullptr); |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 66 | } |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 67 | else if(tokens[0] == "Cached:") |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 68 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 69 | memcache = arm_compute::support::cpp11::stoul(tokens[1], nullptr); |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 70 | } |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 71 | } |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 72 | free = memfree + (buffer + memcache); |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 73 | } |
| 74 | #endif // ifndef BARE_METAL |
| 75 | } |
| 76 | |
| 77 | } // namespace |
| 78 | |
| 79 | namespace arm_compute |
| 80 | { |
| 81 | void MEMInfo::set_policy(MemoryPolicy policy) |
| 82 | { |
| 83 | _policy = policy; |
| 84 | } |
| 85 | |
| 86 | MemoryPolicy MEMInfo::get_policy() |
| 87 | { |
| 88 | return _policy; |
| 89 | } |
| 90 | MemoryPolicy MEMInfo::_policy = { MemoryPolicy::NORMAL }; |
| 91 | |
| 92 | MEMInfo::MEMInfo() |
| 93 | : _total(0), _free(0), _buffer(0) |
| 94 | { |
| 95 | parse_mem_info(_total, _free, _buffer); |
| 96 | } |
| 97 | |
| 98 | size_t MEMInfo::get_total_in_kb() const |
| 99 | { |
| 100 | return _total; |
| 101 | } |
| 102 | |
| 103 | } // namespace arm_compute |