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 std::string &tid, const std::string &name, const std::string &ph) const
58 inline void emit_rusage(EventRecorder *rec, const std::string &ts)
62 getrusage(RUSAGE_SELF, &ru);
69 evt.values["value"] = std::to_string(ru.ru_maxrss);
80 evt.values["value"] = std::to_string(ru.ru_minflt);
89 void EventCollector::onEvent(const Event &event)
91 auto ts = timestamp();
96 _rec->emit(DurationEventBuilder(ts).build(event.backend, event.label, "B"));
100 _rec->emit(DurationEventBuilder(ts).build(event.backend, event.label, "E"));
104 // TODO: Add resurece measurement(e.g. RSS)
105 // when ready with low overhead in release build
107 emit_rusage(_rec, ts);