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.h"
17 #include "htrace_event_parser.h"
18 #include "measure_filter.h"
19 #include "process_filter.h"
20 #include "stat_filter.h"
21 #include "symbols_filter.h"
22 namespace SysTuning {
23 namespace TraceStreamer {
HtraceClockDetailParser(TraceDataCache * dataCache,const TraceStreamerFilters * filters)24 HtraceClockDetailParser::HtraceClockDetailParser(TraceDataCache* dataCache, const TraceStreamerFilters* filters)
25 : EventParserBase(dataCache, filters)
26 {
27 for (auto i = 0; i < MEM_MAX; i++) {
28 memNameDictMap_.insert(std::make_pair(static_cast<MemInfoType>(i),
29 traceDataCache_->GetDataIndex(config_.memNameMap_.at(static_cast<MemInfoType>(i)))));
30 }
31 }
32
33 HtraceClockDetailParser::~HtraceClockDetailParser() = default;
Parse(TracePluginResult * tracePacket) const34 void HtraceClockDetailParser::Parse(TracePluginResult* tracePacket) const
35 {
36 if (streamFilters_->clockFilter_->HasInitSnapShot()) {
37 TS_LOGW("already has clock snapshot!!!");
38 return;
39 }
40 if (!tracePacket->clocks_detail_size()) {
41 TS_LOGE("!!! no clock snapshot");
42 return;
43 }
44 std::vector<SnapShot> snapShot;
45 TS_LOGI("got clock snapshot");
46 for (int i = 0; i < tracePacket->clocks_detail_size(); i++) {
47 auto clockInfo = tracePacket->mutable_clocks_detail(i);
48 TS_LOGI("clockid:%d, ts:%llu", clockInfo->id(),
49 static_cast<unsigned long long>(clockInfo->time().tv_nsec() + clockInfo->time().tv_sec() * SEC_TO_NS));
50 snapShot.push_back(SnapShot{static_cast<ClockId>(clockInfo->id()),
51 clockInfo->time().tv_nsec() + clockInfo->time().tv_sec() * SEC_TO_NS});
52 }
53 if (snapShot.size()) {
54 streamFilters_->clockFilter_->AddClockSnapshot(snapShot);
55 }
56 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_CLOCK_SYNC, STAT_EVENT_RECEIVED);
57 }
58
Parse(const ProfilerTraceFileHeader * profilerTraceFileHeader) const59 void HtraceClockDetailParser::Parse(const ProfilerTraceFileHeader* profilerTraceFileHeader) const
60 {
61 if (streamFilters_->clockFilter_->HasInitSnapShot()) {
62 TS_LOGW("already has clock snapshot!!!");
63 return;
64 }
65 if (!profilerTraceFileHeader->data.boottime) {
66 TS_LOGW("Profiler header has no clock snapshot!!!");
67 return;
68 }
69
70 std::vector<SnapShot> snapShot;
71 TS_LOGI("got clock snapshot");
72
73 TS_LOGI("clockid: TS_CLOCK_BOOTTIME, ts:%llu", profilerTraceFileHeader->data.boottime);
74 if (profilerTraceFileHeader->data.boottime) {
75 snapShot.push_back(SnapShot{TS_CLOCK_BOOTTIME, profilerTraceFileHeader->data.boottime});
76 }
77
78 TS_LOGI("clockid: TS_CLOCK_REALTIME, ts:%llu", profilerTraceFileHeader->data.realtime);
79 if (profilerTraceFileHeader->data.realtime) {
80 snapShot.push_back(SnapShot{TS_CLOCK_REALTIME, profilerTraceFileHeader->data.realtime});
81 }
82
83 TS_LOGI("clockid: TS_CLOCK_REALTIME_COARSE, ts:%llu", profilerTraceFileHeader->data.realtimeCoarse);
84 if (profilerTraceFileHeader->data.realtimeCoarse) {
85 snapShot.push_back(SnapShot{TS_CLOCK_REALTIME_COARSE, profilerTraceFileHeader->data.realtimeCoarse});
86 }
87
88 TS_LOGI("clockid: TS_MONOTONIC, ts:%llu", profilerTraceFileHeader->data.monotonic);
89 if (profilerTraceFileHeader->data.monotonic) {
90 snapShot.push_back(SnapShot{TS_MONOTONIC, profilerTraceFileHeader->data.monotonic});
91 }
92
93 TS_LOGI("clockid: TS_MONOTONIC_COARSE, ts:%llu", profilerTraceFileHeader->data.monotonicCoarse);
94 if (profilerTraceFileHeader->data.monotonicCoarse) {
95 snapShot.push_back(SnapShot{TS_MONOTONIC_COARSE, profilerTraceFileHeader->data.monotonicCoarse});
96 }
97
98 TS_LOGI("clockid: TS_MONOTONIC_RAW, ts:%llu", profilerTraceFileHeader->data.monotonicRaw);
99 if (profilerTraceFileHeader->data.monotonicRaw) {
100 snapShot.push_back(SnapShot{TS_MONOTONIC_RAW, profilerTraceFileHeader->data.monotonicRaw});
101 }
102
103 if (snapShot.size()) {
104 streamFilters_->clockFilter_->AddClockSnapshot(snapShot);
105 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_CLOCK_SYNC, STAT_EVENT_RECEIVED);
106 }
107 }
108 } // namespace TraceStreamer
109 } // namespace SysTuning
110