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 TRACE_DATA_CACHE_H
17 #define TRACE_DATA_CACHE_H
18 
19 #include <memory>
20 #include "trace_data_cache_reader.h"
21 #include "trace_data_cache_writer.h"
22 #include "trace_data_db.h"
23 
24 namespace SysTuning {
25 namespace TraceStreamer {
26 using namespace TraceStdtype;
27 class TraceDataCache : public TraceDataCacheReader, public TraceDataCacheWriter, public TraceDataDB {
28 public:
29     TraceDataCache();
30     TraceDataCache(const TraceDataCache *dataCache) = delete;
31     TraceDataCache *operator=(const TraceDataCache *dataCache) = delete;
32     ~TraceDataCache() override;
33 
34     bool AnimationTraceEnabled() const;
35     void UpdateAnimationTraceStatus(bool status);
36     bool TaskPoolTraceEnabled() const;
37     void UpdateTaskPoolTraceStatus(bool status);
38     bool AppStartTraceEnabled() const;
39     void UpdateAppStartTraceStatus(bool status);
40     bool BinderRunnableTraceEnabled() const;
41     void UpdateBinderRunnableTraceStatus(bool status);
42     bool HMKernelTraceEnabled() const;
43     void UpdateHMKernelTraceStatus(bool status);
44     bool RawTraceCutStartTsEnabled() const;
45     void UpdateRawTraceCutStartTsStatus(bool status);
46     uint64_t SplitFileMaxTime();
47     uint64_t SplitFileMinTime();
48     void SetSplitFileMaxTime(uint64_t maxTs);
49     void SetSplitFileMinTime(uint64_t minTs);
50     std::deque<std::unique_ptr<std::string>> &HookCommProtos();
51     void ClearHookCommProtos();
52     int32_t ExportPerfReadableText(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack = nullptr);
53     int32_t ExportHookReadableText(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack = nullptr);
54     int32_t ExportEbpfReadableText(const std::string &outputName, TraceDataDB::ResultCallBack resultCallBack = nullptr);
55     void ClearAllExportedCacheData();
56     void UpdateAllReadySize();
57 
58 private:
59     void InitDB();
60     void ExportPerfCallChaninText(uint32_t callChainId, std::string &bufferLine);
61     void ExportHookCallChaninText(uint32_t callChainId, std::string &bufferLine);
62     bool ExportHookDataReadableText(int32_t fd, std::string &bufferLine);
63     bool ExportHookStatisticReadableText(int32_t fd, std::string &bufferLine);
64     using EbpfEventTypeMap = std::map<uint32_t, std::string_view>;
65     bool ExportEbpfFileSystemReadableText(int32_t fd,
66                                           std::string &bufferLine,
67                                           const EbpfEventTypeMap &ebpfEventTypeMap);
68     bool ExportEbpfPagedMemReadableText(int32_t fd, std::string &bufferLine, const EbpfEventTypeMap &ebpfEventTypeMap);
69     bool ExportEbpfBIOReadableText(int32_t fd, std::string &bufferLine, const EbpfEventTypeMap &ebpfEventTypeMap);
70     void ExportEbpfCallChaninText(uint32_t callChainId, std::string &bufferLine);
71     void InitBaseDB();
72     void InitEbpfDB();
73     void InitNativeMemoryDB();
74     void InitArkTsDB();
75     void InitHiperfDB();
76     void InitMeasureDB();
77     void InitTemplateDB();
78     void InitRenderServiceDB();
79     void InitMemoryDB();
80     void InitHisysEventDB();
81     void ExportPerfSampleToFile(std::string &perfBufferLine,
82                                 int32_t perfFd,
83                                 const std::string &outputName,
84                                 uint64_t row);
85 
86 private:
87     bool dbInited_ = false;
88     bool animationTraceEnabled_ = false;
89     bool taskPoolTraceEnabled_ = false;
90     bool appStartTraceEnabled_ = false;
91     bool binderRunnableTraceEnabled_ = false;
92     bool HMKernelTraceEnabled_ = false;
93     bool rawTraceCutStartTsEnabled_ = true;
94     uint64_t splitFileMinTs_ = INVALID_UINT64;
95     uint64_t splitFileMaxTs_ = INVALID_UINT64;
96     std::deque<std::unique_ptr<std::string>> hookCommProtos_;
97 };
98 } // namespace TraceStreamer
99 } // namespace SysTuning
100 
101 #endif // TRACE_DATA_CACHE_H
102