[neurun] Introduce EventCollectorGlobal (source) (#9457)
author이한종/On-Device Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Mon, 9 Dec 2019 06:05:25 +0000 (15:05 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 9 Dec 2019 06:05:25 +0000 (15:05 +0900)
Add source file `EventCollectorGlobal.cc` which should have been
included in #9443.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtime/neurun/core/src/util/EventCollectorGlobal.cc [new file with mode: 0644]

diff --git a/runtime/neurun/core/src/util/EventCollectorGlobal.cc b/runtime/neurun/core/src/util/EventCollectorGlobal.cc
new file mode 100644 (file)
index 0000000..6c3594f
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * 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 "util/EventCollectorGlobal.h"
+
+#include <cassert>
+#include <fstream>
+
+#include "util/ConfigSource.h"
+
+namespace neurun
+{
+namespace util
+{
+
+EventCollectorGlobal::EventCollectorGlobal() : _recorder{}, _collector{&_recorder}
+{
+  // DO NOTHING
+}
+
+EventCollectorGlobal::~EventCollectorGlobal()
+{
+  auto path = util::getConfigString(util::config::TRACE_FILEPATH);
+  if (!path.empty())
+  {
+    // TODO Need better way for saved file path than just appending ".global" to the trace file path
+    std::ofstream ofs{path + ".global"};
+    _recorder.writeToFile(ofs);
+  }
+}
+
+EventCollectorGlobal &EventCollectorGlobal::get()
+{
+  static EventCollectorGlobal instance;
+  return instance;
+}
+
+EventDurationBlock::EventDurationBlock(const std::string &tag) : _tag{tag}
+{
+  auto &glob = EventCollectorGlobal::get();
+  glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::BEGIN, "0", _tag});
+}
+EventDurationBlock::~EventDurationBlock()
+{
+  auto &glob = EventCollectorGlobal::get();
+  glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::END, "0", _tag});
+}
+
+EventDurationManual::EventDurationManual(const std::string &tag) : _tag{tag}, _pair{true} {}
+
+EventDurationManual::~EventDurationManual()
+{
+  // Check if it has called begin-end pair
+  assert(_pair);
+}
+
+void EventDurationManual::begin()
+{
+  _pair = false;
+  auto &glob = EventCollectorGlobal::get();
+  glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::BEGIN, "0", _tag});
+}
+
+void EventDurationManual::end()
+{
+  assert(!_pair);
+  _pair = true;
+  auto &glob = EventCollectorGlobal::get();
+  glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::END, "0", _tag});
+}
+
+} // namespace util
+} // namespace neurun