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