1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 "pbreader_clock_detail_parser.h"
16 #include "clock_filter_ex.h"
17 #include "common_types.pb.h"
18 #ifdef ENABLE_HTRACE
19 #include "trace_plugin_result.pbreader.h"
20 #endif
21 #include "measure_filter.h"
22 #include "process_filter.h"
23 #include "stat_filter.h"
24 #include <cinttypes>
25
26 namespace SysTuning {
27 namespace TraceStreamer {
PbreaderClockDetailParser(TraceDataCache * dataCache,const TraceStreamerFilters * filters)28 PbreaderClockDetailParser::PbreaderClockDetailParser(TraceDataCache *dataCache, const TraceStreamerFilters *filters)
29 : EventParserBase(dataCache, filters)
30 {
31 for (auto i = 0; i < MEM_MAX; i++) {
32 memNameDictMap_.insert(
33 std::make_pair(static_cast<MemInfoType>(i),
34 traceDataCache_->GetDataIndex(config_.memNameMap_.at(static_cast<MemInfoType>(i)))));
35 }
36 }
37 PbreaderClockDetailParser::~PbreaderClockDetailParser() = default;
38 #ifdef ENABLE_HTRACE
Parse(const ProtoReader::BytesView & tracePacket) const39 void PbreaderClockDetailParser::Parse(const ProtoReader::BytesView &tracePacket) const
40 {
41 if (traceDataCache_->isSplitFile_) {
42 return;
43 }
44 if (streamFilters_->clockFilter_->HasInitSnapShot()) {
45 TS_LOGW("already has clock snapshot!!!");
46 return;
47 }
48 ProtoReader::TracePluginResult_Reader reader((const uint8_t *)(tracePacket.data_), tracePacket.size_);
49 if (!reader.has_clocks_detail()) {
50 TS_LOGE("!!! no clock snapshot");
51 return;
52 }
53 std::vector<SnapShot> snapShot;
54 TS_LOGI("got clock snapshot");
55 for (auto i = reader.clocks_detail(); i; i++) {
56 ProtoReader::ClockDetailMsg_Reader clockInfo(i->ToBytes());
57 auto id = clockInfo.FindDataArea(ProtoReader::ClockDetailMsg_Reader::kIdDataAreaNumber).ToUint32();
58 ProtoReader::ClockDetailMsg_TimeSpec_Reader time(clockInfo.time());
59 TS_LOGI("clockid:%d, ts:%llu", id, static_cast<unsigned long long>(time.tv_nsec() + time.tv_sec() * SEC_TO_NS));
60 snapShot.push_back(SnapShot{static_cast<ClockId>(id), time.tv_nsec() + time.tv_sec() * SEC_TO_NS});
61 }
62 if (!snapShot.empty()) {
63 streamFilters_->clockFilter_->AddClockSnapshot(snapShot);
64 }
65 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_CLOCK_SYNC, STAT_EVENT_RECEIVED);
66 }
67 #endif
68
Parse(const ProfilerTraceFileHeader * profilerTraceFileHeader) const69 void PbreaderClockDetailParser::Parse(const ProfilerTraceFileHeader *profilerTraceFileHeader) const
70 {
71 if (streamFilters_->clockFilter_->HasInitSnapShot()) {
72 TS_LOGW("already has clock snapshot!!!");
73 return;
74 }
75 if (!profilerTraceFileHeader->data.boottime) {
76 TS_LOGW("Profiler header has no clock snapshot!!!");
77 return;
78 }
79 std::vector<SnapShot> snapShot;
80 TS_LOGI("got clock snapshot");
81 TS_LOGI("clockid: TS_CLOCK_BOOTTIME, ts:%" PRIu64 "", profilerTraceFileHeader->data.boottime);
82 if (profilerTraceFileHeader->data.boottime) {
83 snapShot.push_back(SnapShot{TS_CLOCK_BOOTTIME, profilerTraceFileHeader->data.boottime});
84 }
85 TS_LOGI("clockid: TS_CLOCK_REALTIME, ts:%" PRIu64 "", profilerTraceFileHeader->data.realtime);
86 if (profilerTraceFileHeader->data.realtime) {
87 snapShot.push_back(SnapShot{TS_CLOCK_REALTIME, profilerTraceFileHeader->data.realtime});
88 }
89
90 TS_LOGI("clockid: TS_CLOCK_REALTIME_COARSE, ts:%" PRIu64 "", profilerTraceFileHeader->data.realtimeCoarse);
91 if (profilerTraceFileHeader->data.realtimeCoarse) {
92 snapShot.push_back(SnapShot{TS_CLOCK_REALTIME_COARSE, profilerTraceFileHeader->data.realtimeCoarse});
93 }
94
95 TS_LOGI("clockid: TS_MONOTONIC, ts:%" PRIu64 "", profilerTraceFileHeader->data.monotonic);
96 if (profilerTraceFileHeader->data.monotonic) {
97 snapShot.push_back(SnapShot{TS_MONOTONIC, profilerTraceFileHeader->data.monotonic});
98 }
99
100 TS_LOGI("clockid: TS_MONOTONIC_COARSE, ts:%" PRIu64 "", profilerTraceFileHeader->data.monotonicCoarse);
101 if (profilerTraceFileHeader->data.monotonicCoarse) {
102 snapShot.push_back(SnapShot{TS_MONOTONIC_COARSE, profilerTraceFileHeader->data.monotonicCoarse});
103 }
104
105 TS_LOGI("clockid: TS_MONOTONIC_RAW, ts:%" PRIu64 "", profilerTraceFileHeader->data.monotonicRaw);
106 if (profilerTraceFileHeader->data.monotonicRaw) {
107 snapShot.push_back(SnapShot{TS_MONOTONIC_RAW, profilerTraceFileHeader->data.monotonicRaw});
108 }
109
110 if (!snapShot.empty()) {
111 streamFilters_->clockFilter_->AddClockSnapshot(snapShot);
112 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_CLOCK_SYNC, STAT_EVENT_RECEIVED);
113 }
114 }
115 } // namespace TraceStreamer
116 } // namespace SysTuning
117