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