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 #include "ebpf_data_parser.h"
16 #include "stat_filter.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
EbpfDataParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)20 EbpfDataParser::EbpfDataParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
21 : EventParserBase(dataCache, ctx),
22 EbpfBase(dataCache, ctx),
23 FileSystemDataParser(dataCache, ctx),
24 PagedMemoryDataParser(dataCache, ctx),
25 BioLatencyDataParser(dataCache, ctx),
26 ebpfDataReader_(std::make_unique<EbpfDataReader>(dataCache, ctx))
27 {
28 }
~EbpfDataParser()29 EbpfDataParser::~EbpfDataParser()
30 {
31 TS_LOGI("EBPF all event data ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(ebpfAllEventStartTime_),
32 static_cast<unsigned long long>(ebpfAllEventEndTime_));
33 }
34
Init(const std::deque<uint8_t> & dequeBuffer,uint64_t size)35 bool EbpfDataParser::Init(const std::deque<uint8_t>& dequeBuffer, uint64_t size)
36 {
37 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF, STAT_EVENT_RECEIVED);
38 if (!ebpfDataReader_->InitEbpfData(dequeBuffer, size)) {
39 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF, STAT_EVENT_NOTSUPPORTED);
40 TS_LOGE("InitEbpfData failed!");
41 return false;
42 }
43 if (!InitEbpfDataParser(ebpfDataReader_.get())) {
44 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF, STAT_EVENT_NOTSUPPORTED);
45 return false;
46 }
47 return true;
48 }
49
InitAndParseEbpfData(const std::deque<uint8_t> & dequeBuffer,uint64_t size)50 void EbpfDataParser::InitAndParseEbpfData(const std::deque<uint8_t>& dequeBuffer, uint64_t size)
51 {
52 if (!Init(dequeBuffer, size)) {
53 return;
54 }
55 if (ebpfDataReader_->GetFileSystemEventMap().size()) {
56 ParseFileSystemEvent();
57 }
58 if (ebpfDataReader_->GetPagedMemoryMap().size()) {
59 ParsePagedMemoryEvent();
60 }
61 if (ebpfDataReader_->GetBIOSampleMap().size()) {
62 ParseBioLatencyEvent();
63 }
64 }
65
Finish()66 void EbpfDataParser::Finish()
67 {
68 ebpfAllEventStartTime_ = std::min(FileSystemDataParser::timeParser_->GetPluginStartTime(), ebpfAllEventStartTime_);
69 ebpfAllEventStartTime_ = std::min(PagedMemoryDataParser::timeParser_->GetPluginStartTime(), ebpfAllEventStartTime_);
70 ebpfAllEventStartTime_ = std::min(BioLatencyDataParser::timeParser_->GetPluginStartTime(), ebpfAllEventStartTime_);
71 ebpfAllEventEndTime_ = std::max(FileSystemDataParser::timeParser_->GetPluginEndTime(), ebpfAllEventEndTime_);
72 ebpfAllEventEndTime_ = std::max(PagedMemoryDataParser::timeParser_->GetPluginEndTime(), ebpfAllEventEndTime_);
73 ebpfAllEventEndTime_ = std::max(BioLatencyDataParser::timeParser_->GetPluginEndTime(), ebpfAllEventEndTime_);
74 // Update trace_range when there is only ebpf data in the trace file
75 if (traceDataCache_->traceStartTime_ == INVALID_UINT64 || traceDataCache_->traceEndTime_ == 0) {
76 traceDataCache_->MixTraceTime(ebpfAllEventStartTime_, ebpfAllEventEndTime_);
77 } else {
78 TS_LOGI("EBPF data time is not updated, maybe this trace file has other data");
79 }
80 }
SetEbpfDataOffset(uint64_t offset)81 void EbpfDataParser::SetEbpfDataOffset(uint64_t offset)
82 {
83 ebpfSplitter.SetEbpfDataOffset(offset);
84 }
SetSpliteTimeRange(uint64_t splitFileMinTs,uint64_t splitFileMaxTs)85 void EbpfDataParser::SetSpliteTimeRange(uint64_t splitFileMinTs, uint64_t splitFileMaxTs)
86 {
87 ebpfSplitter.SetSpliteTimeRange(splitFileMinTs, splitFileMaxTs);
88 }
AddAndSplitEbpfData(std::deque<uint8_t> & dequeBuffer)89 bool EbpfDataParser::AddAndSplitEbpfData(std::deque<uint8_t>& dequeBuffer)
90 {
91 return ebpfSplitter.AddAndSplitEbpfData(dequeBuffer);
92 }
93 } // namespace TraceStreamer
94 } // namespace SysTuning
95