1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "htrace_clock_detail_parser.h"
16 #include "clock_filter_ex.h"
17 #include "htrace_event_parser.h"
18 #include "measure_filter.h"
19 #include "process_filter.h"
20 #include "stat_filter.h"
21 #include <cinttypes>
22
23 namespace SysTuning {
24 namespace TraceStreamer {
HtraceClockDetailParser(TraceDataCache * dataCache,const TraceStreamerFilters * filters)25 HtraceClockDetailParser::HtraceClockDetailParser(TraceDataCache* dataCache, const TraceStreamerFilters* filters)
26 : EventParserBase(dataCache, filters)
27 {
28 for (auto i = 0; i < MEM_MAX; i++) {
29 memNameDictMap_.insert(
30 std::make_pair(static_cast<MemInfoType>(i),
31 traceDataCache_->GetDataIndex(config_.memNameMap_.at(static_cast<MemInfoType>(i)))));
32 }
33 }
34
35 HtraceClockDetailParser::~HtraceClockDetailParser() = default;
Parse(const ProtoReader::BytesView & tracePacket) const36 void HtraceClockDetailParser::Parse(const ProtoReader::BytesView& tracePacket) const
37 {
38 if (traceDataCache_->isSplitFile_) {
39 return;
40 }
41 if (streamFilters_->clockFilter_->HasInitSnapShot()) {
42 TS_LOGW("already has clock snapshot!!!");
43 return;
44 }
45 ProtoReader::TracePluginResult_Reader reader((const uint8_t*)(tracePacket.data_), tracePacket.size_);
46 if (!reader.has_clocks_detail()) {
47 TS_LOGE("!!! no clock snapshot");
48 return;
49 }
50 std::vector<SnapShot> snapShot;
51 TS_LOGI("got clock snapshot");
52 for (auto i = reader.clocks_detail(); i; i++) {
53 ProtoReader::ClockDetailMsg_Reader clockInfo(i->ToBytes());
54 auto id = clockInfo.FindDataArea(ProtoReader::ClockDetailMsg_Reader::kIdDataAreaNumber).ToUint32();
55 ProtoReader::ClockDetailMsg_TimeSpec_Reader time(clockInfo.time());
56 TS_LOGI("clockid:%d, ts:%llu", id, static_cast<unsigned long long>(time.tv_nsec() + time.tv_sec() * SEC_TO_NS));
57 snapShot.push_back(SnapShot{static_cast<ClockId>(id), time.tv_nsec() + time.tv_sec() * SEC_TO_NS});
58 }
59 if (!snapShot.empty()) {
60 streamFilters_->clockFilter_->AddClockSnapshot(snapShot);
61 }
62 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_CLOCK_SYNC, STAT_EVENT_RECEIVED);
63 }
64
Parse(const ProfilerTraceFileHeader * profilerTraceFileHeader) const65 void HtraceClockDetailParser::Parse(const ProfilerTraceFileHeader* profilerTraceFileHeader) const
66 {
67 if (streamFilters_->clockFilter_->HasInitSnapShot()) {
68 TS_LOGW("already has clock snapshot!!!");
69 return;
70 }
71 if (!profilerTraceFileHeader->data.boottime) {
72 TS_LOGW("Profiler header has no clock snapshot!!!");
73 return;
74 }
75
76 std::vector<SnapShot> snapShot;
77 TS_LOGI("got clock snapshot");
78
79 TS_LOGI("clockid: TS_CLOCK_BOOTTIME, ts:%" PRIu64 "", profilerTraceFileHeader->data.boottime);
80 if (profilerTraceFileHeader->data.boottime) {
81 snapShot.push_back(SnapShot{TS_CLOCK_BOOTTIME, profilerTraceFileHeader->data.boottime});
82 }
83
84 TS_LOGI("clockid: TS_CLOCK_REALTIME, ts:%" PRIu64 "", profilerTraceFileHeader->data.realtime);
85 if (profilerTraceFileHeader->data.realtime) {
86 snapShot.push_back(SnapShot{TS_CLOCK_REALTIME, profilerTraceFileHeader->data.realtime});
87 }
88
89 TS_LOGI("clockid: TS_CLOCK_REALTIME_COARSE, ts:%" PRIu64 "", profilerTraceFileHeader->data.realtimeCoarse);
90 if (profilerTraceFileHeader->data.realtimeCoarse) {
91 snapShot.push_back(SnapShot{TS_CLOCK_REALTIME_COARSE, profilerTraceFileHeader->data.realtimeCoarse});
92 }
93
94 TS_LOGI("clockid: TS_MONOTONIC, ts:%" PRIu64 "", profilerTraceFileHeader->data.monotonic);
95 if (profilerTraceFileHeader->data.monotonic) {
96 snapShot.push_back(SnapShot{TS_MONOTONIC, profilerTraceFileHeader->data.monotonic});
97 }
98
99 TS_LOGI("clockid: TS_MONOTONIC_COARSE, ts:%" PRIu64 "", profilerTraceFileHeader->data.monotonicCoarse);
100 if (profilerTraceFileHeader->data.monotonicCoarse) {
101 snapShot.push_back(SnapShot{TS_MONOTONIC_COARSE, profilerTraceFileHeader->data.monotonicCoarse});
102 }
103
104 TS_LOGI("clockid: TS_MONOTONIC_RAW, ts:%" PRIu64 "", profilerTraceFileHeader->data.monotonicRaw);
105 if (profilerTraceFileHeader->data.monotonicRaw) {
106 snapShot.push_back(SnapShot{TS_MONOTONIC_RAW, profilerTraceFileHeader->data.monotonicRaw});
107 }
108
109 if (!snapShot.empty()) {
110 streamFilters_->clockFilter_->AddClockSnapshot(snapShot);
111 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_CLOCK_SYNC, STAT_EVENT_RECEIVED);
112 }
113 }
114 } // namespace TraceStreamer
115 } // namespace SysTuning
116