• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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                            uint64_t prevPior,
45                            uint64_t prevState,
46                            uint32_t nextPid,
47                            uint64_t nextPior,
48                            DataIndex nextInfo);
49     bool InsertBlockedReasonEvent(uint64_t ts,
50                                   uint64_t cpu,
51                                   uint32_t iTid,
52                                   bool iowait,
53                                   DataIndex caller,
54                                   uint32_t delay);
55     void InsertWakeupEvent(uint64_t ts, uint32_t internalTid, bool isWaking = false);
56     bool InsertProcessExitEvent(uint64_t ts, uint64_t cpu, uint32_t pid);
57     bool InsertProcessFreeEvent(uint64_t ts, uint32_t pid);
58     void InsertRunnableBinderEvent(uint32_t transactionId, uint32_t iTid);
59     void InsertRunnableBinderRecvEvent(uint32_t transactionId, uint32_t iTid);
60     void Finish() const;
61     void Clear();
62 
63 private:
64     struct BinderTransactionInfo {
65         uint32_t iTidFrom;
66         uint32_t iTidTo;
67         uint64_t schedSliceRow;
68         uint64_t threadStateRow;
69     };
70     void CheckWakeupEvent(uint32_t internalTid);
71     uint64_t RemberInternalTidInStateTable(uint32_t uid, uint64_t row, uint64_t state = TASK_INVALID);
72     uint64_t RowOfInternalTidInStateTable(uint32_t uid) const;
73     void ClearInternalTidInStateTable(uint32_t uid);
74     uint64_t StateOfInternalTidInStateTable(uint32_t uid) const;
75     void TransactionClear(uint32_t iTidFrom, uint32_t transactionId);
76     void ProcNextPidSwitchEvent(uint64_t ts, uint64_t cpu, uint32_t prevPid, uint32_t nextPid, DataIndex nextInfo);
77     void ProcPrevPidSwitchEvent(uint64_t ts,
78                                 uint64_t cpu,
79                                 uint32_t prevPid,
80                                 uint64_t prevState,
81                                 BinderTransactionInfo& btInfo);
82     std::map<uint64_t, uint64_t> cpuToRowThreadState_ = {};
83     typedef struct {
84         uint32_t iTid;
85         uint64_t row;
86     } RowPos;
87     std::map<uint64_t, RowPos> cpuToRowSched_ = {};
88 
89     std::map<uint64_t, uint64_t> lastWakeUpMsg_ = {};
90     std::map<uint32_t, uint64_t> pidToThreadSliceRow_ = {};
91     struct TPthread {
92         uint64_t row_;
93         uint64_t state_;
94     };
95     std::map<uint32_t, TPthread> internalTidToRowThreadState_ = {};
96     const DataIndex ioWait_ = traceDataCache_->GetDataIndex("iowait");
97     const DataIndex nextInfo_ = traceDataCache_->GetDataIndex("next_info");
98     const DataIndex caller_ = traceDataCache_->GetDataIndex("caller");
99     const DataIndex delay_ = traceDataCache_->GetDataIndex("delay");
100     std::map<uint64_t, uint64_t> toRunnableTid_ = {};
101 
102     std::unordered_map<uint32_t, uint32_t> iTidToTransaction_;
103     std::unordered_map<uint32_t, BinderTransactionInfo> transactionIdToInfo_;
104 };
105 } // namespace TraceStreamer
106 } // namespace SysTuning
107 #endif // CPU_FILTER_H
108