summaryrefslogtreecommitdiff
path: root/runtimes/contrib/xtrace/src/event_collector.cc
diff options
context:
space:
mode:
Diffstat (limited to 'runtimes/contrib/xtrace/src/event_collector.cc')
-rw-r--r--runtimes/contrib/xtrace/src/event_collector.cc157
1 files changed, 157 insertions, 0 deletions
diff --git a/runtimes/contrib/xtrace/src/event_collector.cc b/runtimes/contrib/xtrace/src/event_collector.cc
new file mode 100644
index 000000000..2b37bf460
--- /dev/null
+++ b/runtimes/contrib/xtrace/src/event_collector.cc
@@ -0,0 +1,157 @@
+/*
+ * 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 "event_collector.h"
+#include "benchmark_event.h"
+
+// xtrace-internal libraries
+#include "str.h"
+
+// NNFW-internal libraries
+#include <xdata.h>
+#include <cpp14/memory.h>
+
+// C++ standard libraries
+#include <chrono>
+#include <iostream>
+
+// POSIX standard libraries
+#include <sys/time.h>
+#include <sys/resource.h>
+
+using nnfw::cpp14::make_unique;
+
+namespace
+{
+
+std::string timestamp(void)
+{
+ auto now = std::chrono::steady_clock::now();
+ return std::to_string(
+ std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count());
+}
+
+class DurationEventBuilder
+{
+public:
+ DurationEventBuilder(const std::string &ts) : _ts{ts} {}
+
+ DurationEvent build(const std::string &name, const std::string &ph) const
+ {
+ DurationEvent evt;
+
+ evt.name = name;
+ evt.ph = ph;
+ evt.ts = _ts;
+
+ return evt;
+ }
+
+private:
+ std::string _ts;
+};
+
+void emit_rusage(EventRecorder *rec, const std::string &ts)
+{
+ struct rusage ru;
+
+ getrusage(RUSAGE_SELF, &ru);
+ {
+ CounterEvent evt;
+
+ evt.name = "maxrss";
+ evt.ph = "C";
+ evt.ts = ts;
+ evt.values["value"] = std::to_string(ru.ru_maxrss);
+
+ rec->emit(evt);
+ }
+
+ {
+ CounterEvent evt;
+
+ evt.name = "minflt";
+ evt.ph = "C";
+ evt.ts = ts;
+ evt.values["value"] = std::to_string(ru.ru_minflt);
+
+ rec->emit(evt);
+ }
+}
+
+std::ostream &operator<<(std::ostream &os, const BMPhase &phase)
+{
+ os << ((phase == Warmup) ? "Warmup" : "Iteration");
+ return os;
+}
+
+std::ostream &operator<<(std::ostream &os, const std::chrono::milliseconds &dur)
+{
+ os << dur.count();
+ return os;
+}
+
+} // namespace
+
+void EventCollector::notify(const xray::event *e)
+{
+ auto ts = timestamp();
+
+ // Record trace events (region enter/leave)
+ if (e->cat() == xdata::trace::category::get())
+ {
+ auto info = xdata::trace::category::get()->info();
+
+ switch (info->action())
+ {
+ case xdata::trace::enter:
+ _rec->emit(DurationEventBuilder(ts).build(info->region()->name(), "B"));
+ break;
+
+ case xdata::trace::leave:
+ _rec->emit(DurationEventBuilder(ts).build(info->region()->name(), "E"));
+ break;
+ }
+ }
+
+ // Record benchmark events
+ if (e->cat() == BMCategory::get())
+ {
+ auto make_head = [](const BMPhase &phase, uint32_t iter) { return str(phase, " ", iter); };
+
+ if (auto info = dynamic_cast<const BMBegin *>(BMCategory::get()->event()))
+ {
+ auto name = str(info->phase, info->cur_iter);
+ _rec->emit(DurationEventBuilder(ts).build(name, "B"));
+
+ auto head = make_head(info->phase, info->cur_iter);
+ std::cout << head << std::endl;
+ }
+
+ if (auto info = dynamic_cast<const BMEnd *>(BMCategory::get()->event()))
+ {
+ auto name = str(info->phase, info->cur_iter);
+ _rec->emit(DurationEventBuilder(ts).build(name, "E"));
+
+ auto head = make_head(info->phase, info->cur_iter);
+ std::cout << head << " - done " << std::endl;
+ std::cout << head << " takes " << info->elapsed << "ms" << std::endl;
+ }
+ }
+
+ // Trace resource usage per each event notification
+ emit_rusage(_rec, ts);
+}