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 #ifndef HTRACE_NATIVE_HOOK_PARSER_H 16 #define HTRACE_NATIVE_HOOK_PARSER_H 17 #include <cstdint> 18 #include <map> 19 #include <set> 20 #include <string> 21 #include "double_map.h" 22 #include "htrace_event_parser.h" 23 #include "htrace_plugin_time_parser.h" 24 #include "native_hook_result.pb.h" 25 #include "quatra_map.h" 26 #include "trace_streamer_config.h" 27 #include "trace_streamer_filters.h" 28 29 namespace SysTuning { 30 namespace TraceStreamer { 31 class HtraceNativeHookParser : public HtracePluginTimeParser { 32 public: 33 HtraceNativeHookParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx); 34 ~HtraceNativeHookParser(); 35 void SortNativeHookData(BatchNativeHookData& tracePacket); 36 void FinishParseNativeHookData(); 37 void Finish(); 38 39 private: 40 class NativeHookFrameTemp { 41 public: NativeHookFrameTemp()42 NativeHookFrameTemp() {} NativeHookFrameTemp(uint64_t fileId,uint64_t symbolId,uint32_t depth,uint64_t offset,uint64_t symbolOffset)43 NativeHookFrameTemp(uint64_t fileId, uint64_t symbolId, uint32_t depth, uint64_t offset, uint64_t symbolOffset) 44 : fileId_(fileId), symbolId_(symbolId), depth_(depth), offset_(offset), symbolOffset_(symbolOffset) 45 { 46 } ~NativeHookFrameTemp()47 ~NativeHookFrameTemp() {} 48 uint64_t fileId_ = 0; 49 uint64_t symbolId_ = 0; 50 uint32_t depth_ = 0; 51 uint32_t ip_ = 0; 52 uint32_t sp_ = 0; 53 uint64_t offset_ = 0; 54 uint64_t symbolOffset_ = 0; 55 }; 56 template <class T1, class T2> 57 void UpdateMap(std::unordered_map<T1, T2>& sourceMap, T1 key, T2 value); 58 void MaybeParseNativeHookData(); 59 void ParseNativeHookData(const uint64_t timeStamp, const NativeHookData* nativeHookData); 60 uint64_t ParseNativeHookFrame(const RepeatedPtrField<::Frame>& repeatedFrame); 61 void MaybeUpdateCurrentSizeDur(uint64_t row, uint64_t timeStamp, bool isMalloc); 62 void UpdateThreadNameWithNativeHookData() const; 63 void ParseAllocEvent(uint64_t newTimeStamp, const NativeHookData* nativeHookData); 64 void ParseFreeEvent(uint64_t newTimeStamp, const NativeHookData* nativeHookData); 65 void ParseMmapEvent(uint64_t newTimeStamp, const NativeHookData* nativeHookData); 66 void ParseMunmapEvent(uint64_t newTimeStamp, const NativeHookData* nativeHookData); 67 void ParseTagEvent(const NativeHookData* nativeHookData); 68 void ParseFileEvent(const NativeHookData* nativeHookData); 69 void ParseSymbolEvent(const NativeHookData* nativeHookData); 70 void ParseThreadEvent(const NativeHookData* nativeHookData); 71 uint64_t callChainId_ = 0; 72 DoubleMap<uint32_t, uint64_t, uint64_t> addrToAllocEventRow_; 73 DoubleMap<uint32_t, uint64_t, uint64_t> addrToMmapEventRow_; 74 uint64_t lastMallocEventRaw_ = INVALID_UINT64; 75 uint64_t lastMmapEventRaw_ = INVALID_UINT64; 76 std::multimap<uint64_t, std::unique_ptr<NativeHookData>> tsNativeHookQueue_ = {}; 77 std::unordered_map<uint32_t, uint64_t> threadNameIdToThreadName_ = {}; 78 std::unordered_map<uint32_t, uint32_t> itidToThreadNameId_ = {}; 79 QuatraMap<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> frameToFrameId_; 80 std::set<DataIndex> invalidLibPathIndexs_ = {}; 81 std::map<uint32_t, uint64_t> filePathIdToFilePathName_ = {}; 82 const size_t MAX_CACHE_SIZE = 200000; 83 }; 84 } // namespace TraceStreamer 85 } // namespace SysTuning 86 87 #endif // HTRACE_NATIVE_HOOK_PARSER_H 88