• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "trace_data_cache.h"
17 
18 #include <fcntl.h>
19 #include <stack>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 
24 #include "animation_table.h"
25 #include "app_startup_table.h"
26 #include "args_table.h"
27 #include "bio_latency_sample_table.h"
28 #include "callstack_table.h"
29 #include "clk_event_filter_table.h"
30 #include "clock_event_filter_table.h"
31 #include "clock_snapshot_table.h"
32 #include "cpu_measure_filter_table.h"
33 #include "cpu_usage_info_table.h"
34 #include "data_dict_table.h"
35 #include "data_type_table.h"
36 #include "datasource_clockid_table.h"
37 #include "device_info_table.h"
38 #include "device_state_table.h"
39 #include "dma_fence_table.h"
40 #include "disk_io_table.h"
41 #include "dynamic_frame_table.h"
42 #include "ebpf_callstack_table.h"
43 #include "file.h"
44 #include "file_system_sample_table.h"
45 #include "filter_table.h"
46 #include "frame_maps_table.h"
47 #include "frame_slice_table.h"
48 #include "gpu_slice_table.h"
49 #include "js_config_table.h"
50 #include "js_cpu_profiler_node_table.h"
51 #include "js_cpu_profiler_sample_table.h"
52 #include "js_heap_edges_table.h"
53 #include "js_heap_files_table.h"
54 #include "js_heap_info_table.h"
55 #include "js_heap_location_table.h"
56 #include "js_heap_nodes_table.h"
57 #include "js_heap_sample_table.h"
58 #include "js_heap_string_table.h"
59 #include "js_heap_trace_function_info_table.h"
60 #include "js_heap_trace_node_table.h"
61 #include "hidump_table.h"
62 #include "instants_table.h"
63 #include "irq_table.h"
64 #include "live_process_table.h"
65 #include "log_table.h"
66 #include "measure_table.h"
67 #include "memory_ashmem_table.h"
68 #include "memory_dma_table.h"
69 #include "memory_process_gpu_table.h"
70 #include "memory_window_gpu_table.h"
71 #include "memory_cpu_table.h"
72 #include "memory_profile_table.h"
73 #include "memory_rs_image_table.h"
74 #include "meta_table.h"
75 #include "native_hook_frame_table.h"
76 #include "native_hook_statistic_table.h"
77 #include "native_hook_table.h"
78 #include "network_table.h"
79 #include "paged_memory_sample_table.h"
80 #include "perf_call_chain_table.h"
81 #include "perf_files_table.h"
82 #include "perf_report_table.h"
83 #include "perf_sample_table.h"
84 #include "perf_thread_table.h"
85 #include "perf_napi_async_table.h"
86 #include "process_measure_filter_table.h"
87 #include "process_table.h"
88 #include "range_table.h"
89 #include "raw_table.h"
90 #include "sched_slice_table.h"
91 #include "smaps_table.h"
92 #include "span_join.h"
93 #include "sqlite3.h"
94 #include "stat_table.h"
95 #include "so_static_initalization_table.h"
96 #include "string_to_numerical.h"
97 #include "symbols_table.h"
98 #include "sysevent_all_event_table.h"
99 #include "sysevent_measure_table.h"
100 #include "sysevent_subkey_table.h"
101 #include "system_call_table.h"
102 #include "system_event_filter_table.h"
103 #include "table_base.h"
104 #include "task_pool_table.h"
105 #include "thread_state_table.h"
106 #include "thread_table.h"
107 #include "trace_config_table.h"
108 #include "xpower_app_detaile_cpu_table.h"
109 #include "xpower_app_detaile_display_table.h"
110 #include "xpower_app_detaile_gpu_table.h"
111 #include "xpower_app_detaile_wifi_table.h"
112 #include "xpower_app_statistic_table.h"
113 #include "xpower_component_top_table.h"
114 namespace SysTuning {
115 namespace TraceStreamer {
116 constexpr uint8_t CPU_ID_FORMAT_WIDTH = 3;
117 constexpr uint8_t TIME_PRECISION_SIX = 6;
TraceDataCache()118 TraceDataCache::TraceDataCache()
119 {
120     InitDB();
121 }
122 
~TraceDataCache()123 TraceDataCache::~TraceDataCache() {}
InitEbpfDB()124 void TraceDataCache::InitEbpfDB()
125 {
126     TableBase::TableDeclare<EbpfCallStackTable>(*db_, this, "ebpf_callstack");
127     TableBase::TableDeclare<BioLatencySampleTable>(*db_, this, "bio_latency_sample");
128     TableBase::TableDeclare<FileSystemSampleTable>(*db_, this, "file_system_sample");
129     TableBase::TableDeclare<PagedMemorySampleTable>(*db_, this, "paged_memory_sample");
130 }
InitNativeMemoryDB()131 void TraceDataCache::InitNativeMemoryDB()
132 {
133     TableBase::TableDeclare<NativeHookTable>(*db_, this, "native_hook");
134     TableBase::TableDeclare<NativeHookFrameTable>(*db_, this, "native_hook_frame");
135     TableBase::TableDeclare<NativeHookStatisticTable>(*db_, this, "native_hook_statistic");
136 }
InitArkTsDB()137 void TraceDataCache::InitArkTsDB()
138 {
139     TableBase::TableDeclare<JsHeapFilesTable>(*db_, this, "js_heap_files");
140     TableBase::TableDeclare<JsHeapEdgesTable>(*db_, this, "js_heap_edges");
141     TableBase::TableDeclare<JsHeapInfoTable>(*db_, this, "js_heap_info");
142     TableBase::TableDeclare<JsHeapLocationTable>(*db_, this, "js_heap_location");
143     TableBase::TableDeclare<JsHeapNodesTable>(*db_, this, "js_heap_nodes");
144     TableBase::TableDeclare<JsHeapSampleTable>(*db_, this, "js_heap_sample");
145     TableBase::TableDeclare<JsHeapStringTable>(*db_, this, "js_heap_string");
146     TableBase::TableDeclare<JsHeapTraceFunctionInfoTable>(*db_, this, "js_heap_trace_function_info");
147     TableBase::TableDeclare<JsHeapTraceNodeTable>(*db_, this, "js_heap_trace_node");
148     TableBase::TableDeclare<JsCpuProfilerNodeTable>(*db_, this, "js_cpu_profiler_node");
149     TableBase::TableDeclare<JsCpuProfilerSampleTable>(*db_, this, "js_cpu_profiler_sample");
150     TableBase::TableDeclare<JsConfigTable>(*db_, this, "js_config");
151 }
InitHiperfDB()152 void TraceDataCache::InitHiperfDB()
153 {
154     TableBase::TableDeclare<PerfReportTable>(*db_, this, "perf_report");
155     TableBase::TableDeclare<PerfSampleTable>(*db_, this, "perf_sample");
156     TableBase::TableDeclare<PerfCallChainTable>(*db_, this, "perf_callchain");
157     TableBase::TableDeclare<PerfThreadTable>(*db_, this, "perf_thread");
158     TableBase::TableDeclare<PerfFilesTable>(*db_, this, "perf_files");
159     TableBase::TableDeclare<PerfNapiAsyncTable>(*db_, this, "perf_napi_async");
160 }
InitMeasureDB()161 void TraceDataCache::InitMeasureDB()
162 {
163     TableBase::TableDeclare<MeasureTable>(*db_, this, "measure");
164     TableBase::TableDeclare<MeasureTable>(*db_, this, "sys_mem_measure");
165     TableBase::TableDeclare<MeasureTable>(*db_, this, "process_measure");
166     TableBase::TableDeclare<MeasureTable>(*db_, this, "xpower_measure");
167     TableBase::TableDeclare<CpuMeasureFilterTable>(*db_, this, "cpu_measure_filter");
168     TableBase::TableDeclare<FilterTable>(*db_, this, "measure_filter");
169     TableBase::TableDeclare<ProcessMeasureFilterTable>(*db_, this, "process_measure_filter");
170     TableBase::TableDeclare<ClockEventFilterTable>(*db_, this, "clock_event_filter");
171     TableBase::TableDeclare<ClkEventFilterTable>(*db_, this, "clk_event_filter");
172 }
InitBaseDB()173 void TraceDataCache::InitBaseDB()
174 {
175     TableBase::TableDeclare<ProcessTable>(*db_, this, "process");
176     TableBase::TableDeclare<ThreadTable>(*db_, this, "thread");
177     TableBase::TableDeclare<RangeTable>(*db_, this, "trace_range");
178     TableBase::TableDeclare<DataTypeTable>(*db_, this, "data_type");
179     TableBase::TableDeclare<SpanJoin>(*db_, this, "span_join");
180     TableBase::TableDeclare<SymbolsTable>(*db_, this, "symbols");
181     TableBase::TableDeclare<StatTable>(*db_, this, "stat");
182     TableBase::TableDeclare<ArgsTable>(*db_, this, "args");
183     TableBase::TableDeclare<MetaTable>(*db_, this, "meta");
184     TableBase::TableDeclare<TraceConfigTable>(*db_, this, "trace_config");
185     TableBase::TableDeclare<DataDictTable>(*db_, this, "data_dict");
186     TableBase::TableDeclare<DataSourceClockIdTableTable>(*db_, this, "datasource_clockid");
187     TableBase::TableDeclare<ClockSnapShotTable>(*db_, this, "clock_snapshot");
188 }
InitTemplateDB()189 void TraceDataCache::InitTemplateDB()
190 {
191     // task pool business
192     TableBase::TableDeclare<TaskPoolTable>(*db_, this, "task_pool");
193     // app start up business
194     TableBase::TableDeclare<AppStartupTable>(*db_, this, "app_startup");
195     // so initalize business
196     TableBase::TableDeclare<SoStaticInitalizationTable>(*db_, this, "static_initalize");
197     // animation business
198     TableBase::TableDeclare<AnimationTable>(*db_, this, "animation");
199     TableBase::TableDeclare<DynamicFrameTable>(*db_, this, "dynamic_frame");
200     TableBase::TableDeclare<DeviceInfoTable>(*db_, this, "device_info");
201 }
InitRenderServiceDB()202 void TraceDataCache::InitRenderServiceDB()
203 {
204     TableBase::TableDeclare<FrameSliceTable>(*db_, this, "frame_slice");
205     TableBase::TableDeclare<FrameMapsTable>(*db_, this, "frame_maps");
206     TableBase::TableDeclare<GPUSliceTable>(*db_, this, "gpu_slice");
207 }
InitMemoryDB()208 void TraceDataCache::InitMemoryDB()
209 {
210     TableBase::TableDeclare<SmapsTable>(*db_, this, "smaps");
211     TableBase::TableDeclare<MemoryAshMemTable>(*db_, this, "memory_ashmem");
212     TableBase::TableDeclare<MemoryDmaTable>(*db_, this, "memory_dma");
213     TableBase::TableDeclare<MemoryProcessGpuTable>(*db_, this, "memory_process_gpu");
214     TableBase::TableDeclare<MemoryWindowGpuTable>(*db_, this, "memory_window_gpu");
215     TableBase::TableDeclare<MemoryCpuTable>(*db_, this, "memory_cpu");
216     TableBase::TableDeclare<MemoryProfileTable>(*db_, this, "memory_profile");
217     TableBase::TableDeclare<MemoryRSImageTable>(*db_, this, "memory_rs_image");
218 }
InitHisysEventDB()219 void TraceDataCache::InitHisysEventDB()
220 {
221     TableBase::TableDeclare<SysEventSubkeyTable>(*db_, this, "app_name");
222     TableBase::TableDeclare<SysEventMeasureTable>(*db_, this, "hisys_event_measure");
223     TableBase::TableDeclare<DeviceStateTable>(*db_, this, "device_state");
224     TableBase::TableDeclare<SysEventAllEventTable>(*db_, this, "hisys_all_event");
225 }
InitXPowerDB()226 void TraceDataCache::InitXPowerDB()
227 {
228     TableBase::TableDeclare<XpowerAppDetaileDisplayTable>(*db_, this, "xpower_app_detail_display");
229     TableBase::TableDeclare<XpowerAppStatisticTable>(*db_, this, "xpower_app_statistic");
230     TableBase::TableDeclare<XpowerAppDetaileCpuTable>(*db_, this, "xpower_app_detail_cpu");
231     TableBase::TableDeclare<XpowerAppDetaileGpuTable>(*db_, this, "xpower_app_detail_gpu");
232     TableBase::TableDeclare<XpowerAppDetaileWifiTable>(*db_, this, "xpower_app_detail_wifi");
233     TableBase::TableDeclare<XpowerComponentTopTable>(*db_, this, "xpower_component_top");
234 }
InitDB()235 void TraceDataCache::InitDB()
236 {
237     if (dbInited_) {
238         return;
239     }
240     InitBaseDB();
241     InitEbpfDB();
242     InitNativeMemoryDB();
243     InitArkTsDB();
244     InitHiperfDB();
245     InitMeasureDB();
246     InitTemplateDB();
247     InitRenderServiceDB();
248     InitMemoryDB();
249     InitHisysEventDB();
250     InitXPowerDB();
251     TableBase::TableDeclare<RawTable>(*db_, this, "raw");
252     TableBase::TableDeclare<InstantsTable>(*db_, this, "instant");
253     TableBase::TableDeclare<SchedSliceTable>(*db_, this, "sched_slice");
254     TableBase::TableDeclare<ThreadStateTable>(*db_, this, "thread_state");
255     TableBase::TableDeclare<CallStackTable>(*db_, this, "callstack");
256     TableBase::TableDeclare<IrqTable>(*db_, this, "irq");
257     TableBase::TableDeclare<HidumpTable>(*db_, this, "hidump");
258     TableBase::TableDeclare<SystemCallTable>(*db_, this, "syscall");
259     TableBase::TableDeclare<LogTable>(*db_, this, "log");
260     TableBase::TableDeclare<NetworkTable>(*db_, this, "network");
261     TableBase::TableDeclare<SystemEventFilterTable>(*db_, this, "sys_event_filter");
262     TableBase::TableDeclare<DiskIOTable>(*db_, this, "diskio");
263     TableBase::TableDeclare<CpuUsageInfoTable>(*db_, this, "cpu_usage");
264     TableBase::TableDeclare<LiveProcessTable>(*db_, this, "live_process");
265     TableBase::TableDeclare<DmaFenceTable>(*db_, this, "dma_fence");
266     dbInited_ = true;
267 }
AnimationTraceEnabled() const268 bool TraceDataCache::AnimationTraceEnabled() const
269 {
270     return animationTraceEnabled_;
271 }
UpdateAnimationTraceStatus(bool status)272 void TraceDataCache::UpdateAnimationTraceStatus(bool status)
273 {
274     animationTraceEnabled_ = status;
275 }
TaskPoolTraceEnabled() const276 bool TraceDataCache::TaskPoolTraceEnabled() const
277 {
278     return taskPoolTraceEnabled_;
279 }
UpdateTaskPoolTraceStatus(bool status)280 void TraceDataCache::UpdateTaskPoolTraceStatus(bool status)
281 {
282     taskPoolTraceEnabled_ = status;
283 }
AppStartTraceEnabled() const284 bool TraceDataCache::AppStartTraceEnabled() const
285 {
286     return appStartTraceEnabled_;
287 }
UpdateAppStartTraceStatus(bool status)288 void TraceDataCache::UpdateAppStartTraceStatus(bool status)
289 {
290     appStartTraceEnabled_ = status;
291 }
BinderRunnableTraceEnabled() const292 bool TraceDataCache::BinderRunnableTraceEnabled() const
293 {
294     return binderRunnableTraceEnabled_;
295 }
UpdateBinderRunnableTraceStatus(bool status)296 void TraceDataCache::UpdateBinderRunnableTraceStatus(bool status)
297 {
298     binderRunnableTraceEnabled_ = status;
299 }
HMKernelTraceEnabled() const300 bool TraceDataCache::HMKernelTraceEnabled() const
301 {
302     return HMKernelTraceEnabled_;
303 }
UpdateHMKernelTraceStatus(bool status)304 void TraceDataCache::UpdateHMKernelTraceStatus(bool status)
305 {
306     HMKernelTraceEnabled_ = status;
307 }
RawTraceCutStartTsEnabled() const308 bool TraceDataCache::RawTraceCutStartTsEnabled() const
309 {
310     return rawTraceCutStartTsEnabled_;
311 }
UpdateRawTraceCutStartTsStatus(bool status)312 void TraceDataCache::UpdateRawTraceCutStartTsStatus(bool status)
313 {
314     rawTraceCutStartTsEnabled_ = status;
315 }
SplitFileMaxTime()316 uint64_t TraceDataCache::SplitFileMaxTime()
317 {
318     return splitFileMaxTs_;
319 }
SplitFileMinTime()320 uint64_t TraceDataCache::SplitFileMinTime()
321 {
322     return splitFileMinTs_;
323 }
SetSplitFileMaxTime(uint64_t maxTs)324 void TraceDataCache::SetSplitFileMaxTime(uint64_t maxTs)
325 {
326     splitFileMaxTs_ = maxTs;
327 }
SetSplitFileMinTime(uint64_t minTs)328 void TraceDataCache::SetSplitFileMinTime(uint64_t minTs)
329 {
330     splitFileMinTs_ = minTs;
331 }
HookCommProtos()332 std::deque<std::unique_ptr<std::string>> &TraceDataCache::HookCommProtos()
333 {
334     return hookCommProtos_;
335 }
ClearHookCommProtos()336 void TraceDataCache::ClearHookCommProtos()
337 {
338     hookCommProtos_.clear();
339 }
ExportPerfReadableText(const std::string & outputName,TraceDataDB::ResultCallBack resultCallBack)340 int32_t TraceDataCache::ExportPerfReadableText(const std::string &outputName,
341                                                TraceDataDB::ResultCallBack resultCallBack)
342 {
343     int32_t perfFd = base::OpenFile(outputName, O_CREAT | O_RDWR, TS_PERMISSION_RW);
344     TS_CHECK_TRUE(perfFd != -1, 1, "Failed to create file: %s, err:%s", outputName.c_str(), strerror(errno));
345     std::unique_ptr<int32_t, std::function<void(int32_t *)>> fp(&perfFd, [](int32_t *fp) { close(*fp); });
346     TS_CHECK_TRUE(ftruncate(perfFd, 0) != -1, 1, "Failed to ftruncate file: %s, err:%s", outputName.c_str(),
347                   strerror(errno));
348     TS_LOGI("ExportPerfReadableText begin...");
349     std::string perfBufferLine;
350     perfBufferLine.reserve(G_CHUNK_SIZE);
351     for (uint64_t row = 0; row < perfSample_.Size();) {
352         ExportPerfSampleToFile(perfBufferLine, perfFd, outputName, row);
353         TS_CHECK_TRUE(write(perfFd, perfBufferLine.data(), perfBufferLine.size()) != -1, 1,
354                       "Failed to write file: %s, err:%s", outputName.c_str(), strerror(errno));
355         perfBufferLine.clear();
356         if (++row != perfSample_.Size() && perfBufferLine.size() < FLUSH_CHUNK_THRESHOLD) {
357             continue;
358         }
359     }
360     TS_LOGI("ExportPerfReadableText end...");
361     return 0;
362 }
ExportPerfSampleToFile(std::string & perfBufferLine,int32_t perfFd,const std::string & outputName,uint64_t row)363 void TraceDataCache::ExportPerfSampleToFile(std::string &perfBufferLine,
364                                             int32_t perfFd,
365                                             const std::string &outputName,
366                                             uint64_t row)
367 {
368     std::string perfTaskName;
369     std::string cpuIdStr = std::to_string(perfSample_.CpuIds()[row]);
370     std::string eventTypeName;
371     auto perfTaskId = perfSample_.Tids()[row];
372     if (perfTaskId == 0) {
373         auto threadDataRow = 0;
374         perfTaskName = GetDataFromDict(GetConstThreadData(threadDataRow).nameIndex_);
375     } else {
376         auto perfThreadTidItor = std::find(perfThread_.Tids().begin(), perfThread_.Tids().end(), perfTaskId);
377         if (perfThreadTidItor != perfThread_.Tids().end()) {
378             auto perfThreadRow = std::distance(perfThread_.Tids().begin(), perfThreadTidItor);
379             perfTaskName = GetDataFromDict(perfThread_.ThreadNames()[perfThreadRow]);
380         }
381     }
382     auto perfReportIdItor =
383         std::find(perfReport_.IdsData().begin(), perfReport_.IdsData().end(), perfSample_.EventTypeIds()[row]);
384     if (perfReportIdItor != perfReport_.IdsData().end()) {
385         auto perfReportRow = std::distance(perfReport_.IdsData().begin(), perfReportIdItor);
386         eventTypeName = GetDataFromDict(perfReport_.Values()[perfReportRow]);
387     }
388     perfBufferLine.append(perfTaskName);
389     perfBufferLine.append("  ").append(std::to_string(perfTaskId));
390     perfBufferLine.append(" [")
391         .append(std::string(CPU_ID_FORMAT_WIDTH - cpuIdStr.size(), '0'))
392         .append(cpuIdStr)
393         .append("]");
394     perfBufferLine.append(" ")
395         .append(base::ConvertTimestampToSecStr(perfSample_.TimeStampData()[row], TIME_PRECISION_SIX))
396         .append(":");
397     perfBufferLine.append("          ").append(std::to_string(perfSample_.EventCounts()[row]));
398     perfBufferLine.append(" ").append(eventTypeName).append(" \r\n");
399     ExportPerfCallChaninText(perfSample_.SampleIds()[row], perfBufferLine);
400 }
ExportPerfCallChaninText(uint32_t callChainId,std::string & bufferLine)401 void TraceDataCache::ExportPerfCallChaninText(uint32_t callChainId, std::string &bufferLine)
402 {
403     std::stack<uint64_t> callChainStackRows;
404     auto perfCallChainItor =
405         std::lower_bound(perfCallChain_.CallChainIds().begin(), perfCallChain_.CallChainIds().end(), callChainId);
406     while (perfCallChainItor != perfCallChain_.CallChainIds().end() && callChainId == *perfCallChainItor) {
407         auto perfCallChainRow = std::distance(perfCallChain_.CallChainIds().begin(), perfCallChainItor);
408         callChainStackRows.emplace(perfCallChainRow);
409         ++perfCallChainItor;
410     }
411     while (!callChainStackRows.empty()) {
412         auto perfCallChainRow = callChainStackRows.top();
413         callChainStackRows.pop();
414         auto formatIp = base::number(perfCallChain_.Ips()[perfCallChainRow], base::INTEGER_RADIX_TYPE_HEX);
415         formatIp = std::string(base::INTEGER_RADIX_TYPE_HEX - formatIp.size(), ' ') + formatIp;
416         std::string filePath("[unknown]");
417         auto curFileId = perfCallChain_.FileIds()[perfCallChainRow];
418         auto perfFileIdItor = std::lower_bound(perfFiles_.FileIds().begin(), perfFiles_.FileIds().end(), curFileId);
419         if (perfFileIdItor != perfFiles_.FileIds().end()) {
420             auto perfFileRow = std::distance(perfFiles_.FileIds().begin(), perfFileIdItor);
421             filePath = GetDataFromDict(perfFiles_.FilePaths()[perfFileRow]);
422         }
423         bufferLine.append("\t").append(formatIp);
424         auto nameStr = GetDataFromDict(perfCallChain_.Names()[perfCallChainRow]);
425         bufferLine.append(" [").append(nameStr).append("]");
426         bufferLine.append(" (").append(filePath).append(")\r\n");
427     }
428     bufferLine.append("\r\n");
429 }
ExportHookReadableText(const std::string & outputName,TraceDataDB::ResultCallBack resultCallBack)430 int32_t TraceDataCache::ExportHookReadableText(const std::string &outputName,
431                                                TraceDataDB::ResultCallBack resultCallBack)
432 {
433     int32_t hookFd = base::OpenFile(outputName, O_CREAT | O_RDWR, TS_PERMISSION_RW);
434     TS_CHECK_TRUE(hookFd != -1, 1, "Failed to create file: %s, err:%s", outputName.c_str(), strerror(errno));
435     std::unique_ptr<int32_t, std::function<void(int32_t *)>> fp(&hookFd, [](int32_t *fp) { close(*fp); });
436     TS_CHECK_TRUE(ftruncate(hookFd, 0) != -1, 1, "Failed to ftruncate file: %s, err:%s", outputName.c_str(),
437                   strerror(errno));
438     TS_LOGI("ExportHookReadableText begin...");
439     std::string hookBufferLine;
440     hookBufferLine.reserve(G_CHUNK_SIZE);
441     ExportHookDataReadableText(hookFd, hookBufferLine);
442     ExportHookStatisticReadableText(hookFd, hookBufferLine);
443     TS_LOGI("ExportHookReadableText end...");
444     return 0;
445 }
ExportHookDataReadableText(int32_t fd,std::string & bufferLine)446 bool TraceDataCache::ExportHookDataReadableText(int32_t fd, std::string &bufferLine)
447 {
448     for (uint64_t row = 0; row < nativeHookData_.Size();) {
449         auto itid = nativeHookData_.InternalTidsData()[row];
450         auto hookTaskId = internalThreadsData_[itid].tid_;
451         auto hookTaskName = GetDataFromDict(internalThreadsData_[itid].nameIndex_);
452         bufferLine.append(hookTaskName);
453         bufferLine.append("  ").append(std::to_string(hookTaskId));
454         bufferLine.append(" ").append("[---]"); // default HookData event cpu id
455         bufferLine.append(" ")
456             .append(base::ConvertTimestampToSecStr(nativeHookData_.TimeStampData()[row], TIME_PRECISION_SIX))
457             .append(":");
458         bufferLine.append("          ").append("1"); // default HookData event event cnt
459         bufferLine.append(" ").append(nativeHookData_.EventTypes()[row]).append(" \r\n");
460         ExportHookCallChaninText(nativeHookData_.CallChainIds()[row], bufferLine);
461         if (++row != nativeHookData_.Size() && bufferLine.size() < FLUSH_CHUNK_THRESHOLD) {
462             continue;
463         }
464         TS_CHECK_TRUE(write(fd, bufferLine.data(), bufferLine.size()) != -1, false,
465                       "Failed to write HookData file, err:%s", strerror(errno));
466         bufferLine.clear();
467     }
468     return true;
469 }
ExportHookStatisticReadableText(int32_t fd,std::string & bufferLine)470 bool TraceDataCache::ExportHookStatisticReadableText(int32_t fd, std::string &bufferLine)
471 {
472     std::map<uint32_t, std::string_view> statisticEventTypeMap = {
473         {static_cast<uint32_t>(HookMemoryType::MALLOC), "AllocEvent"},
474         {static_cast<uint32_t>(HookMemoryType::MMAP), "MmapEvent"},
475         {static_cast<uint32_t>(HookMemoryType::FILE_PAGE_MSG), "FilePageEvent"},
476         {static_cast<uint32_t>(HookMemoryType::MEMORY_USING_MSG), "MemoryUsingEvent"}};
477     for (uint64_t row = 0; row < nativeHookStatisticData_.Size();) {
478         auto ipid = nativeHookStatisticData_.Ipids()[row];
479         auto statisticTaskId = internalProcessesData_[ipid].pid_;
480         auto statisticTaskName = internalProcessesData_[ipid].cmdLine_;
481         std::string_view eventType;
482         auto statisticEventTypeItor = statisticEventTypeMap.find(nativeHookStatisticData_.MemoryTypes()[row]);
483         if (statisticEventTypeItor != statisticEventTypeMap.end()) {
484             eventType = statisticEventTypeItor->second;
485         }
486         bufferLine.append(statisticTaskName);
487         bufferLine.append("  ").append(std::to_string(statisticTaskId));
488         bufferLine.append(" ").append("[---]"); // default HookStatistic event cpu id
489         bufferLine.append(" ")
490             .append(base::ConvertTimestampToSecStr(nativeHookStatisticData_.TimeStampData()[row], TIME_PRECISION_SIX))
491             .append(":");
492         bufferLine.append("          ").append("1"); // default HookStatistic event event cnt
493         bufferLine.append(" ").append(eventType).append(" \r\n");
494         ExportHookCallChaninText(nativeHookStatisticData_.CallChainIds()[row], bufferLine);
495         if (++row != nativeHookStatisticData_.Size() && bufferLine.size() < FLUSH_CHUNK_THRESHOLD) {
496             continue;
497         }
498         TS_CHECK_TRUE(write(fd, bufferLine.data(), bufferLine.size()) != -1, false,
499                       "Failed to write HookStatistic file, err:%s", strerror(errno));
500         bufferLine.clear();
501     }
502     return true;
503 }
ExportHookCallChaninText(uint32_t callChainId,std::string & bufferLine)504 void TraceDataCache::ExportHookCallChaninText(uint32_t callChainId, std::string &bufferLine)
505 {
506     auto hookFrameCallChainItor = std::lower_bound(nativeHookFrameData_.CallChainIds().begin(),
507                                                    nativeHookFrameData_.CallChainIds().end(), callChainId);
508     while (hookFrameCallChainItor != nativeHookFrameData_.CallChainIds().end() &&
509            callChainId == *hookFrameCallChainItor) {
510         auto hookCallChainRow = std::distance(nativeHookFrameData_.CallChainIds().begin(), hookFrameCallChainItor);
511         auto hookFrameIp = base::number(nativeHookFrameData_.Ips()[hookCallChainRow], base::INTEGER_RADIX_TYPE_HEX);
512         hookFrameIp = std::string(base::INTEGER_RADIX_TYPE_HEX - hookFrameIp.size(), ' ') + hookFrameIp;
513         std::string hookSymName("unknown");
514         std::string hookFilePath("[unknown]");
515         if (nativeHookFrameData_.SymbolNames()[hookCallChainRow] != INVALID_UINT64) {
516             hookSymName = GetDataFromDict(nativeHookFrameData_.SymbolNames()[hookCallChainRow]);
517         }
518         if (nativeHookFrameData_.FilePaths()[hookCallChainRow] != INVALID_UINT64) {
519             hookFilePath = GetDataFromDict(nativeHookFrameData_.FilePaths()[hookCallChainRow]);
520         }
521         bufferLine.append("\t").append(hookFrameIp);
522         bufferLine.append(" [").append(hookSymName).append("]");
523         bufferLine.append(" (").append(hookFilePath).append(")\r\n");
524         ++hookFrameCallChainItor;
525     }
526     bufferLine.append("\r\n");
527 }
ExportEbpfReadableText(const std::string & outputName,TraceDataDB::ResultCallBack resultCallBack)528 int32_t TraceDataCache::ExportEbpfReadableText(const std::string &outputName,
529                                                TraceDataDB::ResultCallBack resultCallBack)
530 {
531     int32_t ebpfFd = base::OpenFile(outputName, O_CREAT | O_RDWR, TS_PERMISSION_RW);
532     TS_CHECK_TRUE(ebpfFd != -1, 1, "Failed to create file: %s, err:%s", outputName.c_str(), strerror(errno));
533     std::unique_ptr<int32_t, std::function<void(int32_t *)>> fp(&ebpfFd, [](int32_t *fp) { close(*fp); });
534     TS_CHECK_TRUE(ftruncate(ebpfFd, 0) != -1, 1, "Failed to ftruncate file: %s, err:%s", outputName.c_str(),
535                   strerror(errno));
536     TS_LOGI("ExportEbpfReadableText begin...");
537     EbpfEventTypeMap ebpfEventTypeMap = {{EBPF_DATA_TYPE::ITEM_EVENT_MAPS, "MapsEvent"},
538                                          {EBPF_DATA_TYPE::ITEM_SYMBOL_INFO, "SymbolEvent"},
539                                          {EBPF_DATA_TYPE::ITEM_EVENT_FS, "FsEvent"},
540                                          {EBPF_DATA_TYPE::ITEM_EVENT_VM, "VmEvent"},
541                                          {EBPF_DATA_TYPE::ITEM_EVENT_BIO, "BioEvent"},
542                                          {EBPF_DATA_TYPE::ITEM_EVENT_STR, "StrEvent"},
543                                          {EBPF_DATA_TYPE::ITEM_EVENT_KENEL_SYMBOL_INFO, "KernelSymbolEvent"}};
544     std::string ebpfBufferLine;
545     ebpfBufferLine.reserve(G_CHUNK_SIZE);
546     ExportEbpfFileSystemReadableText(ebpfFd, ebpfBufferLine, ebpfEventTypeMap);
547     ExportEbpfPagedMemReadableText(ebpfFd, ebpfBufferLine, ebpfEventTypeMap);
548     ExportEbpfBIOReadableText(ebpfFd, ebpfBufferLine, ebpfEventTypeMap);
549     TS_LOGI("ExportEbpfReadableText end...");
550     return 0;
551 }
ExportEbpfFileSystemReadableText(int32_t fd,std::string & bufferLine,const EbpfEventTypeMap & ebpfEventTypeMap)552 bool TraceDataCache::ExportEbpfFileSystemReadableText(int32_t fd,
553                                                       std::string &bufferLine,
554                                                       const EbpfEventTypeMap &ebpfEventTypeMap)
555 {
556     for (uint64_t row = 0; row < fileSamplingTableData_.Size();) {
557         auto fileSysTaskId = internalThreadsData_[fileSamplingTableData_.Itids()[row]].tid_;
558         auto fileSysTaskName = GetDataFromDict(internalThreadsData_[fileSamplingTableData_.Itids()[row]].nameIndex_);
559         std::string_view fileSampleEventType;
560         auto ebpfEventTypeItor = ebpfEventTypeMap.find(fileSamplingTableData_.Types()[row]);
561         if (ebpfEventTypeItor != ebpfEventTypeMap.end()) {
562             fileSampleEventType = ebpfEventTypeItor->second;
563         }
564         bufferLine.append(fileSysTaskName);
565         bufferLine.append("  ").append(std::to_string(fileSysTaskId));
566         bufferLine.append(" ").append("[---]"); // default FileSystem event cpu id
567         bufferLine.append(" ")
568             .append(base::ConvertTimestampToSecStr(fileSamplingTableData_.StartTs()[row], TIME_PRECISION_SIX))
569             .append(":");
570         bufferLine.append("          ").append("1"); // default FileSystem event cnt
571         bufferLine.append(" ").append(fileSampleEventType).append(" \r\n");
572         ExportEbpfCallChaninText(fileSamplingTableData_.CallChainIds()[row], bufferLine);
573         if (++row != fileSamplingTableData_.Size() && bufferLine.size() < FLUSH_CHUNK_THRESHOLD) {
574             continue;
575         }
576         TS_CHECK_TRUE(write(fd, bufferLine.data(), bufferLine.size()) != -1, false,
577                       "Failed to write FileSystem event file err:%s", strerror(errno));
578         bufferLine.clear();
579     }
580     return true;
581 }
ExportEbpfPagedMemReadableText(int32_t fd,std::string & bufferLine,const EbpfEventTypeMap & ebpfEventTypeMap)582 bool TraceDataCache::ExportEbpfPagedMemReadableText(int32_t fd,
583                                                     std::string &bufferLine,
584                                                     const EbpfEventTypeMap &ebpfEventTypeMap)
585 {
586     for (uint64_t row = 0; row < pagedMemorySampleData_.Size();) {
587         auto pagedMemTaskId = internalThreadsData_[pagedMemorySampleData_.Itids()[row]].tid_;
588         auto pagedMemTaskName = GetDataFromDict(internalThreadsData_[pagedMemorySampleData_.Itids()[row]].nameIndex_);
589         std::string_view pageMemEventType;
590         auto ebpfEventTypeItor = ebpfEventTypeMap.find(pagedMemorySampleData_.Types()[row]);
591         if (ebpfEventTypeItor != ebpfEventTypeMap.end()) {
592             pageMemEventType = ebpfEventTypeItor->second;
593         }
594         bufferLine.append(pagedMemTaskName);
595         bufferLine.append("  ").append(std::to_string(pagedMemTaskId));
596         bufferLine.append(" ").append("[---]"); // default PagedMem event cpu id
597         bufferLine.append(" ")
598             .append(base::ConvertTimestampToSecStr(pagedMemorySampleData_.StartTs()[row], TIME_PRECISION_SIX))
599             .append(":");
600         bufferLine.append("          ").append("1"); // default PagedMem event cnt
601         bufferLine.append(" ").append(pageMemEventType).append(" \r\n");
602         ExportEbpfCallChaninText(pagedMemorySampleData_.CallChainIds()[row], bufferLine);
603         if (++row != pagedMemorySampleData_.Size() && bufferLine.size() < FLUSH_CHUNK_THRESHOLD) {
604             continue;
605         }
606         TS_CHECK_TRUE(write(fd, bufferLine.data(), bufferLine.size()) != -1, false,
607                       "Failed to write PagedMem event file err:%s", strerror(errno));
608         bufferLine.clear();
609     }
610     return true;
611 }
ExportEbpfBIOReadableText(int32_t fd,std::string & bufferLine,const EbpfEventTypeMap & ebpfEventTypeMap)612 bool TraceDataCache::ExportEbpfBIOReadableText(int32_t fd,
613                                                std::string &bufferLine,
614                                                const EbpfEventTypeMap &ebpfEventTypeMap)
615 {
616     for (uint64_t row = 0; row < bioLatencySampleData_.Size();) {
617         auto bioTaskId = internalThreadsData_[bioLatencySampleData_.Itids()[row]].tid_;
618         auto bioTaskName = GetDataFromDict(internalThreadsData_[bioLatencySampleData_.Itids()[row]].nameIndex_);
619         std::string_view bioEventType;
620         auto ebpfEventTypeItor = ebpfEventTypeMap.find(bioLatencySampleData_.Types()[row]);
621         if (ebpfEventTypeItor != ebpfEventTypeMap.end()) {
622             bioEventType = ebpfEventTypeItor->second;
623         }
624         bufferLine.append(bioTaskName);
625         bufferLine.append("  ").append(std::to_string(bioTaskId));
626         bufferLine.append(" ").append("[---]"); // default BIO event cpu id
627         bufferLine.append(" ")
628             .append(base::ConvertTimestampToSecStr(bioLatencySampleData_.StartTs()[row], TIME_PRECISION_SIX))
629             .append(":");
630         bufferLine.append("          ").append("1"); // default BIO event cnt
631         bufferLine.append(" ").append(bioEventType).append(" \r\n");
632         ExportEbpfCallChaninText(bioLatencySampleData_.CallChainIds()[row], bufferLine);
633         if (++row != bioLatencySampleData_.Size() && bufferLine.size() < FLUSH_CHUNK_THRESHOLD) {
634             continue;
635         }
636         TS_CHECK_TRUE(write(fd, bufferLine.data(), bufferLine.size()) != -1, false,
637                       "Failed to write BIO event file err:%s", strerror(errno));
638         bufferLine.clear();
639     }
640     return true;
641 }
ExportEbpfCallChaninText(uint32_t callChainId,std::string & bufferLine)642 void TraceDataCache::ExportEbpfCallChaninText(uint32_t callChainId, std::string &bufferLine)
643 {
644     auto ebpfCallChainItor = std::lower_bound(ebpfCallStackData_.CallChainIds().begin(),
645                                               ebpfCallStackData_.CallChainIds().end(), callChainId);
646     while (ebpfCallChainItor != ebpfCallStackData_.CallChainIds().end() && callChainId == *ebpfCallChainItor) {
647         auto ebpfCallChainRow = std::distance(ebpfCallStackData_.CallChainIds().begin(), ebpfCallChainItor);
648         auto ebpfFrameIp = GetDataFromDict(ebpfCallStackData_.Ips()[ebpfCallChainRow]).substr(HEX_PREFIX.size());
649         ebpfFrameIp = std::string(base::INTEGER_RADIX_TYPE_HEX - ebpfFrameIp.size(), ' ') + ebpfFrameIp;
650         std::string ebpfSymName("unknown");
651         std::string ebpfFilePath("[unknown]");
652         if (ebpfCallStackData_.SymbolIds()[ebpfCallChainRow] != INVALID_UINT64) {
653             ebpfSymName = GetDataFromDict(ebpfCallStackData_.SymbolIds()[ebpfCallChainRow]);
654         }
655         if (ebpfCallStackData_.FilePathIds()[ebpfCallChainRow] != INVALID_UINT64) {
656             ebpfFilePath = GetDataFromDict(ebpfCallStackData_.FilePathIds()[ebpfCallChainRow]);
657         }
658         bufferLine.append("\t").append(ebpfFrameIp);
659         bufferLine.append(" [").append(ebpfSymName).append("]");
660         bufferLine.append(" (").append(ebpfFilePath).append(")\r\n");
661         ++ebpfCallChainItor;
662     }
663     bufferLine.append("\r\n");
664 }
ClearAllExportedCacheData()665 void TraceDataCache::ClearAllExportedCacheData()
666 {
667     // ftrace plugin
668     argSet_.ClearExportedData();
669     filterData_.ClearExportedData();
670     clkEventFilterData_.ClearExportedData();
671     measureData_.ClearExportedData();
672     clockEventFilterData_.ClearExportedData();
673     processMeasureData_.ClearExportedData();
674     processMeasureFilterData_.ClearExportedData();
675     cpuMeasureData_.ClearExportedData();
676     rawData_.ClearExportedData();
677     instantsData_.ClearExportedData();
678     schedSliceData_.ClearExportedData();
679     irqData_.ClearExportedData();
680     sysMemMeasureData_.ClearExportedData();
681     sysCallData_.ClearExportedData();
682     frameSliceData_.ClearExportedData();
683     frameMapsData_.ClearExportedData();
684     gpuSliceData_.ClearExportedData();
685 }
UpdateAllReadySize()686 void TraceDataCache::UpdateAllReadySize()
687 {
688     // ftrace plugin datacache
689     measureData_.UpdateReadySize(measureData_.Size());
690     processMeasureData_.UpdateReadySize(processMeasureData_.Size());
691     tableToCompletedSize_["measure"] = measureData_.readySize_;
692     tableToCompletedSize_["process_measure"] = processMeasureData_.readySize_;
693     tableToCompletedSize_["frame_slice"] = frameSliceData_.readySize_;
694     tableToCompletedSize_["sched_slice"] = schedSliceData_.readySize_;
695     tableToCompletedSize_["irq"] = irqData_.readySize_;
696 
697     argSet_.UpdateReadySize(argSet_.Size());
698     filterData_.UpdateReadySize(filterData_.Size());
699     clkEventFilterData_.UpdateReadySize(clkEventFilterData_.Size());
700     clockEventFilterData_.UpdateReadySize(clockEventFilterData_.Size());
701     processMeasureFilterData_.UpdateReadySize(processMeasureFilterData_.Size());
702     cpuMeasureData_.UpdateReadySize(cpuMeasureData_.Size());
703     rawData_.UpdateReadySize(rawData_.Size());
704 
705     instantsData_.UpdateReadySize(instantsData_.Size());
706     sysCallData_.UpdateReadySize(sysCallData_.Size());
707     frameMapsData_.UpdateReadySize(frameMapsData_.Size());
708     gpuSliceData_.UpdateReadySize(gpuSliceData_.Size());
709 }
710 } // namespace TraceStreamer
711 } // namespace SysTuning
712