2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "EventCollector.h"
19 // C++ standard libraries
22 // POSIX standard libraries
24 #include <sys/resource.h>
29 std::string timestamp(void)
31 auto now = std::chrono::steady_clock::now();
32 return std::to_string(
33 std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count());
36 class DurationEventBuilder : public EventCollector::EventVisitor
39 DurationEventBuilder(const std::string &ts) : _ts{ts} {}
41 std::unique_ptr<SubgDurationEvent> build(const EventCollector::SubgEvent &evt_collected,
42 const std::string &ph) const
44 auto dur_evt = std::make_unique<SubgDurationEvent>();
46 // The following will be set by a child of EventsWriter:
47 // dur_evt.name, dur_evt.tid
50 dur_evt->tracing_ctx = evt_collected.tracing_ctx;
52 dur_evt->session_index = evt_collected.session_index;
53 dur_evt->subg_index = evt_collected.subg_index;
55 dur_evt->args = evt_collected.userData;
57 dur_evt->args.emplace_back("session", std::to_string(evt_collected.session_index));
58 dur_evt->args.emplace_back("subgraph", std::to_string(evt_collected.subg_index));
64 std::unique_ptr<OpSeqDurationEvent> build(const EventCollector::OpSeqEvent &evt_collected,
65 const std::string &ph) const
67 auto dur_evt = std::make_unique<OpSeqDurationEvent>();
69 // The following will be set by a child of EventsWriter:
70 // dur_evt.name, dur_evt.tid
73 dur_evt->tracing_ctx = evt_collected.tracing_ctx;
75 dur_evt->session_index = evt_collected.session_index;
76 dur_evt->subg_index = evt_collected.subg_index;
78 dur_evt->backend = evt_collected.backend;
79 dur_evt->op_index = evt_collected.op_index;
80 dur_evt->op_name = evt_collected.op_name;
82 dur_evt->args = evt_collected.userData;
84 dur_evt->args.emplace_back("session", std::to_string(evt_collected.session_index));
85 dur_evt->args.emplace_back("subgraph", std::to_string(evt_collected.subg_index));
96 inline void emit_rusage(EventRecorder *rec, const std::string &ts)
100 getrusage(RUSAGE_SELF, &ru);
107 evt.values["value"] = std::to_string(ru.ru_maxrss);
118 evt.values["value"] = std::to_string(ru.ru_minflt);
127 template <typename EventT> void EventCollector::onEvent(const EventT &event)
129 auto ts = timestamp();
131 DurationEventBuilder builder(ts);
137 auto duration_evt = builder.build(event, "B");
138 _rec->emit(std::move(duration_evt));
143 auto duration_evt = builder.build(event, "E");
144 _rec->emit(std::move(duration_evt));
149 // TODO: Add resurece measurement(e.g. RSS)
150 // when ready with low overhead in release build
152 emit_rusage(_rec, ts);
156 // template instantiation
157 template void EventCollector::onEvent<EventCollector::SubgEvent>(const SubgEvent &event);
158 template void EventCollector::onEvent<EventCollector::OpSeqEvent>(const OpSeqEvent &event);