[nnpkgrun] Do not always init MemoryPoller (#3999)
authorHanjoung Lee <hanjoung.lee@samsung.com>
Thu, 27 Aug 2020 07:13:38 +0000 (16:13 +0900)
committerGitHub <noreply@github.com>
Thu, 27 Aug 2020 07:13:38 +0000 (16:13 +0900)
Initialize only when memory polling is on.

On macOS, it crashes since MemoryPoller initialization fails, even
when the user does not set memory polling on.

ONE-DCO-1.0-Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>

runtime/libs/benchmark/include/benchmark/Phases.h
runtime/libs/benchmark/src/Phases.cpp

index abb0561..936a897 100644 (file)
@@ -47,7 +47,7 @@ public:
   }
 
   const PhaseOption &option() const { return _option; }
-  const MemoryPoller &mem_poll() const { return _mem_poll; }
+  const MemoryPoller &mem_poll() const { return *_mem_poll; }
   const Phase &at(const std::string &tag) const { return _phases.at(tag); }
 
 private:
@@ -57,7 +57,7 @@ private:
 private:
   const PhaseOption _option;
   std::unordered_map<std::string, Phase> _phases;
-  MemoryPoller _mem_poll;
+  std::unique_ptr<MemoryPoller> _mem_poll;
 };
 
 } // namespace benchmark
index 81da508..9ab67cf 100644 (file)
@@ -46,11 +46,13 @@ void SleepForMicros(uint64_t micros)
 namespace benchmark
 {
 
-Phases::Phases(const PhaseOption &option)
-    : _option(option),
-      _mem_poll(std::chrono::milliseconds(option.memory_interval), option.memory_gpu)
+Phases::Phases(const PhaseOption &option) : _option(option)
 {
-  // DO NOTHING
+  if (_option.memory)
+  {
+    _mem_poll = std::make_unique<MemoryPoller>(std::chrono::milliseconds(option.memory_interval),
+                                               option.memory_gpu);
+  }
 }
 
 void Phases::run(const std::string &tag, const PhaseFunc &exec, const PhaseFunc *post,
@@ -61,7 +63,7 @@ void Phases::run(const std::string &tag, const PhaseFunc &exec, const PhaseFunc
   for (uint32_t i = 0; i < loop_num; ++i)
   {
     if (!option_disable && _option.memory)
-      _mem_poll.start(p);
+      _mem_poll->start(p);
 
     uint64_t t = 0u;
     t = nowMicros();
@@ -71,15 +73,15 @@ void Phases::run(const std::string &tag, const PhaseFunc &exec, const PhaseFunc
     t = nowMicros() - t;
 
     if (!option_disable && _option.memory)
-      _mem_poll.end(p);
+      _mem_poll->end(p);
 
     phase.time.emplace_back(t);
 
     if (!option_disable && _option.memory)
     {
-      phase.memory[MemoryType::RSS].emplace_back(_mem_poll.getRssMap().at(p));
-      phase.memory[MemoryType::HWM].emplace_back(_mem_poll.getHwmMap().at(p));
-      phase.memory[MemoryType::PSS].emplace_back(_mem_poll.getPssMap().at(p));
+      phase.memory[MemoryType::RSS].emplace_back(_mem_poll->getRssMap().at(p));
+      phase.memory[MemoryType::HWM].emplace_back(_mem_poll->getHwmMap().at(p));
+      phase.memory[MemoryType::PSS].emplace_back(_mem_poll->getPssMap().at(p));
     }
 
     if (post)