Compute Library
 21.02
MEMUtils.cpp
Go to the documentation of this file.
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  */
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 {
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 {
82 {
83  _policy = policy;
84 }
85 
87 {
88  return _policy;
89 }
90 MemoryPolicy MEMInfo::_policy = { MemoryPolicy::NORMAL };
91 
93  : _total(0), _free(0), _buffer(0)
94 {
95  parse_mem_info(_total, _free, _buffer);
96 }
97 
99 {
100  return _total;
101 }
102 
103 } // namespace arm_compute
unsigned long stoul(const std::string &str, std::size_t *pos=0, NumericBase base=NumericBase::BASE_10)
Convert string values to unsigned long.
Definition: StringSupport.h:91
Copyright (c) 2017-2021 Arm Limited.
static MemoryPolicy get_policy()
Definition: MEMUtils.cpp:86
size_t get_total_in_kb() const
Return the total amount of RAM memory in the system expressed in KB.
Definition: MEMUtils.cpp:98
static void set_policy(MemoryPolicy policy)
Definition: MEMUtils.cpp:81
MemoryPolicy
Global memory policy.
Definition: CPPTypes.h:59