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