blob: 5ae1c2abefee825070deba4e240283e142ee5313 [file] [log] [blame]
Pablo Tello0cf77982018-10-24 15:32:39 +01001/*
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>
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000030#include <iterator>
Pablo Tello0cf77982018-10-24 15:32:39 +010031#include <sstream>
32#endif // ifndef BARE_METAL
33
34namespace
35{
36void 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 Spyrou8e5174c2018-12-04 11:43:23 +000046
Pablo Tello0cf77982018-10-24 15:32:39 +010047 if(meminfo_f.is_open())
48 {
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000049 std::string line;
50 while(bool(getline(meminfo_f, line)))
Pablo Tello0cf77982018-10-24 15:32:39 +010051 {
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000052 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 Tello0cf77982018-10-24 15:32:39 +010056 {
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000057 total = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
Pablo Tello0cf77982018-10-24 15:32:39 +010058 }
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000059 else if(tokens[0] == "MemFree:")
Pablo Tello0cf77982018-10-24 15:32:39 +010060 {
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000061 memfree = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
Pablo Tello0cf77982018-10-24 15:32:39 +010062 }
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000063 else if(tokens[0] == "Buffers:")
Pablo Tello0cf77982018-10-24 15:32:39 +010064 {
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000065 buffer = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
Pablo Tello0cf77982018-10-24 15:32:39 +010066 }
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000067 else if(tokens[0] == "Cached:")
Pablo Tello0cf77982018-10-24 15:32:39 +010068 {
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000069 memcache = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
Pablo Tello0cf77982018-10-24 15:32:39 +010070 }
Pablo Tello0cf77982018-10-24 15:32:39 +010071 }
Michalis Spyrou8e5174c2018-12-04 11:43:23 +000072 free = memfree + (buffer + memcache);
Pablo Tello0cf77982018-10-24 15:32:39 +010073 }
74#endif // ifndef BARE_METAL
75}
76
77} // namespace
78
79namespace arm_compute
80{
81void MEMInfo::set_policy(MemoryPolicy policy)
82{
83 _policy = policy;
84}
85
86MemoryPolicy MEMInfo::get_policy()
87{
88 return _policy;
89}
90MemoryPolicy MEMInfo::_policy = { MemoryPolicy::NORMAL };
91
92MEMInfo::MEMInfo()
93 : _total(0), _free(0), _buffer(0)
94{
95 parse_mem_info(_total, _free, _buffer);
96}
97
98size_t MEMInfo::get_total_in_kb() const
99{
100 return _total;
101}
102
103} // namespace arm_compute