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 "htrace_disk_io_parser.h"
16 #include "clock_filter.h"
17 #include "htrace_event_parser.h"
18 #include "process_filter.h"
19 #include "stat_filter.h"
20 namespace SysTuning {
21 namespace TraceStreamer {
HtraceDiskIOParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)22 HtraceDiskIOParser::HtraceDiskIOParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
23 : HtracePluginTimeParser(dataCache, ctx)
24 {
25 }
26
~HtraceDiskIOParser()27 HtraceDiskIOParser::~HtraceDiskIOParser()
28 {
29 TS_LOGI("diskio ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
30 static_cast<unsigned long long>(GetPluginEndTime()));
31 }
Parse(DiskioData & tracePacket,uint64_t ts)32 void HtraceDiskIOParser::Parse(DiskioData& tracePacket, uint64_t ts)
33 {
34 auto stat = tracePacket.statsdata();
35 if (!stat.statsinfo_size()) {
36 return;
37 }
38 double rdCountPerSec = 0; // The amount of data read from the device per second kB_read/s
39 double wrCountPerSec = 0; // The amount of data written to the device per second kB_wrtn/s
40 uint64_t rdCount = 0; // Total amount of data read kB_read
41 uint64_t wrCount = 0; // The total amount of data written kB_wrtn
42 for (auto i = 0; i < stat.statsinfo_size(); i++) {
43 auto statsInfo = stat.statsinfo(i);
44 rdCountPerSec += statsInfo.rd_per_sec();
45 wrCountPerSec += statsInfo.wr_per_sec();
46 rdCount += statsInfo.rd_kb();
47 wrCount += statsInfo.wr_kb();
48 }
49
50 streamFilters_->statFilter_->IncreaseStat(TRACE_DISKIO, STAT_EVENT_RECEIVED);
51 diskIOData_.push_back(TsDiskIOData{ts, tracePacket.rd_sectors_kb(), tracePacket.wr_sectors_kb(),
52 tracePacket.prev_rd_sectors_kb(), tracePacket.prev_wr_sectors_kb(),
53 rdCountPerSec, wrCountPerSec, rdCount, wrCount});
54 }
Finish()55 void HtraceDiskIOParser::Finish()
56 {
57 auto cmp = [](const TsDiskIOData& a, const TsDiskIOData& b) { return a.ts < b.ts; };
58 std::sort(diskIOData_.begin(), diskIOData_.end(), cmp);
59 bool first = true;
60 uint64_t lastTs = 0;
61 for (auto itor = diskIOData_.begin(); itor != diskIOData_.end(); itor++) {
62 itor->ts = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, itor->ts);
63 UpdatePluginTimeRange(TS_CLOCK_REALTIME, itor->ts, itor->ts);
64 if (first) {
65 lastTs = itor->ts;
66 first = false;
67 continue;
68 }
69 auto dur = itor->ts - lastTs;
70 auto durS = 1.0 * dur / SEC_TO_NS;
71 traceDataCache_->GetDiskIOData()->AppendNewData(
72 itor->ts, itor->ts - lastTs, itor->rdSectorsKb, itor->wrSectorsKb,
73 1.0 * (itor->rdSectorsKb - itor->prevRdSectorsKb) / durS,
74 1.0 * (itor->wrSectorsKb - itor->prevWrSectorsKb) / durS, itor->rdCountPerSec, itor->wrCountPerSec,
75 itor->rdCount, itor->wrCount);
76 lastTs = itor->ts;
77 }
78 diskIOData_.clear();
79 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
80 }
81 } // namespace TraceStreamer
82 } // namespace SysTuning
83