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 "util/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
39 DurationEventBuilder(const std::string &ts) : _ts{ts} {}
41 DurationEvent build(const EventCollector::Event &evt_collected, const std::string &ph) const
45 evt.name = evt_collected.label;
46 evt.tid = evt_collected.backend;
50 evt.args = evt_collected.userData;
60 inline void emit_rusage(EventRecorder *rec, const std::string &ts)
64 getrusage(RUSAGE_SELF, &ru);
71 evt.values["value"] = std::to_string(ru.ru_maxrss);
82 evt.values["value"] = std::to_string(ru.ru_minflt);
91 void EventCollector::onEvent(const Event &event)
93 auto ts = timestamp();
98 _rec->emit(DurationEventBuilder(ts).build(event, "B"));
102 _rec->emit(DurationEventBuilder(ts).build(event, "E"));
106 // TODO: Add resurece measurement(e.g. RSS)
107 // when ready with low overhead in release build
109 emit_rusage(_rec, ts);