• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "clock_filter.h"
17 #include <algorithm>
18 #include <map>
19 
20 namespace SysTuning {
21 namespace TraceStreamer {
ClockFilter(TraceDataCache * dataCache,const TraceStreamerFilters * filter)22 ClockFilter::ClockFilter(TraceDataCache* dataCache, const TraceStreamerFilters* filter)
23     : FilterBase(dataCache, filter), primaryClock_(BuiltinClocks::TS_CLOCK_BOOTTIME), dataCache_(dataCache)
24 {
25 }
26 
~ClockFilter()27 ClockFilter::~ClockFilter() {}
28 
GenClockKey(ClockId srcClockId,ClockId desClockId)29 std::string ClockFilter::GenClockKey(ClockId srcClockId, ClockId desClockId)
30 {
31     std::string ret;
32     ret += std::to_string(srcClockId);
33     ret += ",";
34     ret += std::to_string(desClockId);
35     return ret;
36 }
37 
ToPrimaryTraceTime(ClockId srcClockId,uint64_t srcTs) const38 uint64_t ClockFilter::ToPrimaryTraceTime(ClockId srcClockId, uint64_t srcTs) const
39 {
40     if (srcClockId == primaryClock_) {
41         return srcTs;
42     }
43     return Convert(srcClockId, srcTs, primaryClock_);
44 }
45 
Convert(ClockId srcClockId,uint64_t srcTs,ClockId desClockId) const46 uint64_t ClockFilter::Convert(ClockId srcClockId, uint64_t srcTs, ClockId desClockId) const
47 {
48     std::string&& clockKey = GenClockKey(srcClockId, desClockId);
49     auto keyIt = clockMaps_.find(clockKey);
50     if (keyIt == clockMaps_.end()) {
51         return srcTs;
52     }
53 
54     auto tsIt = keyIt->second.upper_bound(srcTs);
55     if (tsIt != keyIt->second.begin()) {
56         tsIt--;
57     }
58 
59     if (tsIt->second >= 0) {
60         return srcTs + static_cast<uint64_t>(tsIt->second);
61     } else {
62         return srcTs - static_cast<uint64_t>(0 - tsIt->second);
63     }
64 }
65 
AddConvertClockMap(ClockId srcClockId,ClockId dstClockId,uint64_t srcTs,uint64_t dstTs)66 void ClockFilter::AddConvertClockMap(ClockId srcClockId, ClockId dstClockId, uint64_t srcTs, uint64_t dstTs)
67 {
68     std::string&& clockKey = GenClockKey(srcClockId, dstClockId);
69     auto keyIt = clockMaps_.find(clockKey);
70     if (keyIt == clockMaps_.end()) {
71         ConvertClockMap newConvertMap = {{srcTs, dstTs - srcTs}};
72         clockMaps_[clockKey] = newConvertMap;
73     } else {
74         clockMaps_[clockKey].insert(std::make_pair(srcTs, dstTs - srcTs));
75     }
76 }
77 
AddClockSnapshot(const std::vector<SnapShot> & snapShot)78 void ClockFilter::AddClockSnapshot(const std::vector<SnapShot>& snapShot)
79 {
80     ClockId srcId, desId;
81     const int theDataBeforeLast = 2;
82     for (srcId = 0; srcId < snapShot.size() - 1; srcId++) {
83         ClockId srcClockId = snapShot[srcId].clockId;
84         uint64_t srcTs = snapShot[srcId].ts;
85         traceDataCache_->GetClockSnapshotData()->AppendNewSnapshot(srcClockId,
86                                                                    srcTs, dataCache_->GetConstStatAndInfo().clockid2ClockNameMap_.at(static_cast<BuiltinClocks>(srcClockId)));
87         for (desId = srcId + 1; desId < snapShot.size(); desId++) {
88             ClockId desClockId = snapShot[desId].clockId;
89             uint64_t desTs = snapShot[desId].ts;
90             if ((srcId == snapShot.size() - theDataBeforeLast) and (desId == snapShot.size() - 1)) {
91                 traceDataCache_->GetClockSnapshotData()->AppendNewSnapshot(desClockId, desTs,
92                                                                            dataCache_->GetConstStatAndInfo().clockid2ClockNameMap_.at(static_cast<BuiltinClocks>(desClockId)));
93             }
94             AddConvertClockMap(srcClockId, desClockId, srcTs, desTs);
95             AddConvertClockMap(desClockId, srcClockId, desTs, srcTs);
96         }
97     }
98     hasInitSnapShot_ = true;
99 }
100 } // namespace TraceStreamer
101 } // namespace SysTuning
102