1 /*
2 * Copyright (c) 2018-2020 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/StringSupport.h"
27
28 #ifndef BARE_METAL
29 #include <fstream>
30 #include <iterator>
31 #include <sstream>
32 #endif // ifndef BARE_METAL
33
34 namespace
35 {
parse_mem_info(size_t & total,size_t & free,size_t & buffer)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
47 if(meminfo_f.is_open())
48 {
49 std::string line;
50 while(bool(getline(meminfo_f, line)))
51 {
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:")
56 {
57 total = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
58 }
59 else if(tokens[0] == "MemFree:")
60 {
61 memfree = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
62 }
63 else if(tokens[0] == "Buffers:")
64 {
65 buffer = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
66 }
67 else if(tokens[0] == "Cached:")
68 {
69 memcache = arm_compute::support::cpp11::stoul(tokens[1], nullptr);
70 }
71 }
72 free = memfree + (buffer + memcache);
73 }
74 #endif // ifndef BARE_METAL
75 }
76
77 } // namespace
78
79 namespace arm_compute
80 {
set_policy(MemoryPolicy policy)81 void MEMInfo::set_policy(MemoryPolicy policy)
82 {
83 _policy = policy;
84 }
85
get_policy()86 MemoryPolicy MEMInfo::get_policy()
87 {
88 return _policy;
89 }
90 MemoryPolicy MEMInfo::_policy = { MemoryPolicy::NORMAL };
91
MEMInfo()92 MEMInfo::MEMInfo()
93 : _total(0), _free(0), _buffer(0)
94 {
95 parse_mem_info(_total, _free, _buffer);
96 }
97
get_total_in_kb() const98 size_t MEMInfo::get_total_in_kb() const
99 {
100 return _total;
101 }
102
103 } // namespace arm_compute
104