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