summaryrefslogtreecommitdiff
path: root/runtime/libs/benchmark/src/MemoryPoller.cpp
blob: 436d536e4166ac4a034490603df91c46753459aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "benchmark/MemoryPoller.h"
#include <vector>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <cassert>
#include <iostream>

namespace
{

const std::string proc_status_path("/proc/self/status");
const std::string gpu_memory_path("/sys/kernel/debug/mali0/gpu_memory");

bool isStrNumber(const std::string &s)
{
  return !s.empty() &&
         std::find_if(s.begin(), s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
}

std::vector<std::string> splitLine(std::string line, std::string delimiters = " \n\t")
{
  std::vector<std::string> words;
  size_t prev = 0, pos;

  while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos)
  {
    if (pos > prev)
      words.emplace_back(line.substr(prev, pos - prev));
    prev = pos + 1;
  }

  if (prev < line.length())
    words.emplace_back(line.substr(prev, std::string::npos));

  return words;
}

std::vector<std::string> getValueFromFileStatus(const std::string &file, const std::string &key)
{
  std::ifstream ifs(file);
  assert(ifs.is_open());

  std::string line;
  std::vector<std::string> val;

  bool found = false;
  while (std::getline(ifs, line))
  {
    if (line.find(key) != std::string::npos)
    {
      found = true;
      break;
    }
  }
  ifs.close();

  if (!found)
  {
    // NOTE. the process which uses gpu resources cannot be there yet at the model-load phase.
    // At that time, just return empty.
    return val;
  }

  val = splitLine(line);
  return val;
}

} // namespace anonymous

namespace benchmark
{

MemoryPoller::MemoryPoller(std::chrono::milliseconds duration, bool gpu_poll)
    : _duration(duration), _run(false), _term(false), _gpu_poll(gpu_poll)
{
  if (prepareMemoryPolling() == false)
    throw std::runtime_error("failed to prepare memory pooling");

  _thread = std::thread{&MemoryPoller::process, this};
}

bool MemoryPoller::start(Phase phase)
{
  if (std::find(_phases.begin(), _phases.end(), phase) != _phases.end())
  {
    std::cerr << getPhaseString(phase) << " is already processing/processed..." << std::endl;
    return false;
  }

  {
    std::lock_guard<std::mutex> lock(_mutex);
    _phases.emplace_back(phase);
    _rss_map[phase] = 0;
    _hwm_map[phase] = 0;
  }

  _run = true;
  _cond_var_started.notify_all();
  return true;
}

bool MemoryPoller::end(Phase phase)
{
  if (std::find(_phases.begin(), _phases.end(), phase) == _phases.end())
  {
    std::cerr << getPhaseString(phase) << " is not started..." << std::endl;
    return false;
  }

  uint32_t mem = 0;
  bool stop = false;
  {
    std::lock_guard<std::mutex> lock(_mutex);
    _phases.remove(phase);
    stop = (_phases.size() == 0);
  }

  if (_rss_map[phase] == 0)
  {
    uint32_t mem = getVmRSS();
    if (_gpu_poll)
    {
      mem += getGpuMemory();
    }
    _rss_map[phase] = mem;
  }

  if (_hwm_map[phase] == 0)
  {
    uint32_t mem = getVmHWM();
    if (_gpu_poll)
    {
      mem += getGpuMemory();
    }
    _hwm_map[phase] = mem;
  }

  if (stop)
  {
    _run = false;
    _cond_var_started.notify_all();
  }

  return true;
}

void MemoryPoller::process()
{
  std::unique_lock<std::mutex> lock_started(_mutex_started);
  while (true)
  {
    _cond_var_started.wait(lock_started, [&]() { return _run || _term; });
    if (_term)
      break;

    std::unique_lock<std::mutex> lock(_mutex);

    uint32_t cur_rss = getVmRSS();
    uint32_t cur_hwm = getVmHWM();
    if (_gpu_poll)
    {
      auto gpu_mem = getGpuMemory();
      cur_rss += gpu_mem;
      cur_hwm += gpu_mem;
    }

    for (auto &phase : _phases)
    {
      auto &rss = _rss_map.at(phase);
      if (rss < cur_rss)
        rss = cur_rss;
      // hwm is gradually increasing
      auto &hwm = _hwm_map.at(phase);
      hwm = cur_hwm;
    }

    lock.unlock();

    std::this_thread::sleep_for(std::chrono::milliseconds(_duration));
  }
}

bool MemoryPoller::prepareMemoryPolling()
{
  // VmRSS
  {
    std::ifstream ifs(proc_status_path);
    if (!ifs.is_open())
    {
      std::cerr << "failed to open " << proc_status_path << std::endl;
      return false;
    }
    ifs.close();
  }

  // (Additionally) GpuMemory
  if (_gpu_poll)
  {
    std::ifstream ifs(gpu_memory_path);
    if (!ifs.is_open())
    {
      std::cerr << "failed to open " << gpu_memory_path << std::endl;
      return false;
    }
    ifs.close();

    // Needs process name
    auto val = getValueFromFileStatus(proc_status_path, "Name");
    assert(val.size() != 0);
    _process_name = val[1];
  }

  return true;
}

uint32_t MemoryPoller::getVmRSS()
{
  auto val = getValueFromFileStatus(proc_status_path, "VmRSS");
  if (val.size() == 0)
    return 0;
  assert(isStrNumber(val[1]));
  return std::stoul(val[1]);
}

uint32_t MemoryPoller::getVmHWM()
{
  auto val = getValueFromFileStatus(proc_status_path, "VmHWM");
  if (val.size() == 0)
    return 0;
  // key: value
  assert(isStrNumber(val[1]));
  return std::stoul(val[1]);
}

uint32_t MemoryPoller::getGpuMemory()
{
  assert(!_process_name.empty());
  auto val = getValueFromFileStatus(gpu_memory_path, _process_name);
  if (val.size() == 0)
    return 0;
  // process_name -> pid -> gpu_mem -> max_gpu_mem
  assert(isStrNumber(val[2]));
  return std::stoul(val[2]);
}

} // namespace benchmark