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 #ifndef CLOCK_FILTER_H 17 #define CLOCK_FILTER_H 18 19 #include <map> 20 #include <string> 21 #include <unordered_map> 22 #include <vector> 23 #include "filter_base.h" 24 #include "trace_data_cache.h" 25 #include "trace_streamer_filters.h" 26 #include "ts_common.h" 27 28 namespace SysTuning { 29 namespace TraceStreamer { 30 /* 31 * TS_REALTIME: A settable system-wide clock that measures real time. Its time represents seconds and nanoseconds 32 * since the Epoch. 33 * TS_REALTIME_COARSE: A faster but less precise version of TS_REALTIME. This clock is not settable. 34 * TS_MONOTONIC: The number of seconds that the system has been running since it was booted.The CLOCK_MONOTONIC 35 * clock is not affected by discontinuous jumps in the system time ,but is affected by the incremental adjustments 36 * performed by adjtime(3) and NTP. This clock does not count time that the system is suspended. 37 * TS_MONOTONIC_COARSE: A faster but less precise version of TS_MONOTONIC. 38 * TS_MONOTONIC_RAW: Similar to TS_MONOTONIC, but provides access to a raw hardware-based time that is not subject 39 * to NTP adjustments or the incremental adjustments performed by adjtime(3). This clock does not count time that the 40 * system is suspended. 41 * TS_BOOTTIME: A nonsettable system-wide clock that is identical to TS_MONOTONIC, except that it also includes 42 * any time that the system is suspended. 43 */ 44 45 using ClockId = uint32_t; 46 struct SnapShot { 47 ClockId clockId; 48 uint64_t ts; 49 }; 50 class ClockFilter : private FilterBase { 51 public: 52 using ConvertClockMap = std::map<uint64_t, int64_t>; 53 54 ClockFilter(TraceDataCache* dataCache, const TraceStreamerFilters* filter); 55 ~ClockFilter() override; 56 SetPrimaryClock(ClockId primary)57 void SetPrimaryClock(ClockId primary) 58 { 59 primaryClock_ = primary; 60 } GetPrimaryClock()61 ClockId GetPrimaryClock() const 62 { 63 return primaryClock_; 64 } 65 uint64_t ToPrimaryTraceTime(ClockId srcClockId, uint64_t srcTs) const; 66 uint64_t Convert(ClockId srcClockId, uint64_t srcTs, ClockId desClockId) const; 67 void AddClockSnapshot(const std::vector<SnapShot>& snapShot); HasInitSnapShot()68 bool HasInitSnapShot() const 69 { 70 return hasInitSnapShot_; 71 } 72 73 private: 74 static std::string GenClockKey(ClockId srcClockId, ClockId desClockId); 75 void AddConvertClockMap(ClockId srcClockId, ClockId dstClockId, uint64_t srcTs, uint64_t dstTs); 76 77 private: 78 std::unordered_map<std::string, ConvertClockMap> clockMaps_ = {}; 79 80 ClockId primaryClock_; 81 bool hasInitSnapShot_ = false; 82 TraceDataCache* dataCache_; 83 }; 84 } // namespace TraceStreamer 85 } // namespace SysTuning 86 87 #endif // CLOCK_FILTER_H 88