• 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 #ifndef CPU_FILTER_H
17 #define CPU_FILTER_H
18 
19 #include <cstdint>
20 #include <limits>
21 #include <map>
22 #include <string_view>
23 #include <tuple>
24 #include <unordered_map>
25 
26 #include "filter_base.h"
27 #include "trace_data_cache.h"
28 #include "trace_streamer_filters.h"
29 #include "ts_common.h"
30 #include "config_filter.h"
31 
32 namespace SysTuning {
33 namespace TraceStreamer {
34 class CpuFilter : private FilterBase {
35 public:
36     CpuFilter(TraceDataCache *dataCache, const TraceStreamerFilters *filter);
37     CpuFilter(const CpuFilter &) = delete;
38     CpuFilter &operator=(const CpuFilter &) = delete;
39     ~CpuFilter() override;
40 
41 public:
42     void InsertSwitchEvent(uint64_t ts,
43                            uint64_t cpu,
44                            uint32_t prevPid,
45                            int32_t prevPrio,
46                            uint64_t prevState,
47                            uint32_t nextPid,
48                            int32_t nextPrio,
49                            DataIndex nextInfo);
50     bool InsertBlockedReasonEvent(uint64_t cpu, uint32_t iTid, bool iowait, DataIndex caller, uint32_t delay);
51     void InsertWakeupEvent(uint64_t ts, uint32_t internalTid, bool isWaking = false);
52     bool InsertProcessExitEvent(uint64_t ts, uint64_t cpu, uint32_t pid);
53     bool InsertProcessFreeEvent(uint64_t ts, uint32_t pid);
54     void InsertRunnableBinderEvent(uint32_t transactionId, uint32_t iTid);
55     void InsertRunnableBinderRecvEvent(uint32_t transactionId, uint32_t iTid);
56     void Finish() const;
57     void Clear();
58     void UpdateProcessData(bool isFinish = false) const;
UpdateReadySize()59     void UpdateReadySize()
60     {
61         UpdateProcessData();
62         uint64_t minSchedSliceRowToBeUpdated = INVALID_UINT64;
63         for (const auto &[_, binderTransactionInfo] : transactionIdToInfo_) {
64             if (minSchedSliceRowToBeUpdated > binderTransactionInfo.schedSliceRow) {
65                 minSchedSliceRowToBeUpdated = binderTransactionInfo.schedSliceRow;
66             }
67         }
68         UpdateSchedSliceReadySize(minSchedSliceRowToBeUpdated);
69     }
70 
71 private:
72     struct BinderTransactionInfo {
73         uint32_t iTidFrom;
74         uint32_t iTidTo;
75         uint64_t schedSliceRow;
76         uint64_t threadStateRow;
77     };
78     void CheckWakeupEvent(uint32_t internalTid);
79     uint64_t RemberInternalTidInStateTable(uint32_t uid, uint64_t row, uint64_t state = TASK_INVALID);
80     uint64_t RowOfInternalTidInStateTable(uint32_t uid) const;
81     void ClearInternalTidInStateTable(uint32_t uid);
82     uint64_t StateOfInternalTidInStateTable(uint32_t uid) const;
83     void TransactionClear(uint32_t iTidFrom, uint32_t transactionId);
84     void ProcNextPidSwitchEvent(uint64_t ts, uint64_t cpu, uint32_t prevPid, uint32_t nextPid, DataIndex nextInfo);
85     void ProcPrevPidSwitchEvent(uint64_t ts,
86                                 uint64_t cpu,
87                                 uint32_t prevPid,
88                                 uint64_t prevState,
89                                 BinderTransactionInfo &btInfo);
90     bool UpdateSchedSliceReadySize(uint64_t minSchedSliceRowToBeUpdated = INVALID_UINT64);
91 
92 private:
93     std::map<uint64_t, uint64_t> cpuToRowThreadState_ = {};
94     typedef struct {
95         uint32_t iTid;
96         uint64_t row;
97     } RowPos;
98     std::map<uint64_t, RowPos> cpuToRowSched_ = {};
99 
100     std::map<uint64_t, uint64_t> lastWakeUpMsg_ = {};
101     std::map<uint32_t, uint64_t> pidToThreadSliceRow_ = {};
102     struct TPthread {
103         uint64_t row_;
104         uint64_t state_;
105     };
106     std::map<uint32_t, TPthread> internalTidToRowThreadState_ = {};
107     const DataIndex ioWait_ = traceDataCache_->GetDataIndex("iowait");
108     const DataIndex nextInfo_ = traceDataCache_->GetDataIndex("next_info");
109     const DataIndex caller_ = traceDataCache_->GetDataIndex("caller");
110     const DataIndex delay_ = traceDataCache_->GetDataIndex("delay");
111     std::map<uint64_t, uint64_t> toRunnableTid_ = {};
112 
113     std::unordered_map<uint32_t, uint32_t> iTidToTransaction_;
114     std::unordered_map<uint32_t, BinderTransactionInfo> transactionIdToInfo_;
115 };
116 } // namespace TraceStreamer
117 } // namespace SysTuning
118 #endif // CPU_FILTER_H
119