summaryrefslogtreecommitdiff
path: root/runtimes/contrib/xtrace/src/event_collector.cc
blob: 2b37bf4607dacf6034c5e131ebbf9331f13bff2d (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
/*
 * 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);
}