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 SRC_THREAD_TABLE_H 17 #define SRC_THREAD_TABLE_H 18 19 #include "table_base.h" 20 #include "trace_data_cache.h" 21 22 namespace SysTuning { 23 namespace TraceStreamer { 24 class ThreadTable : public TableBase { 25 public: 26 explicit ThreadTable(const TraceDataCache* dataCache); 27 ~ThreadTable() override; 28 std::unique_ptr<TableBase::Cursor> CreateCursor() override; 29 30 private: GetSize()31 int64_t GetSize() override 32 { 33 return dataCache_->ThreadSize(); 34 } 35 void GetOrbyes(FilterConstraints& fc, EstimatedIndexInfo& ei) override; 36 void FilterByConstraint(FilterConstraints& threadfc, 37 double& threadfilterCost, 38 size_t threadrowCount, 39 uint32_t threadcurrenti) override; 40 int32_t Update(int32_t argc, sqlite3_value** argv, sqlite3_int64* pRowid) override; 41 42 class Cursor : public TableBase::Cursor { 43 public: 44 explicit Cursor(const TraceDataCache* dataCache, TableBase* table); 45 ~Cursor() override; 46 void FilterIpid(unsigned char op, uint64_t value); 47 void FilterTid(unsigned char op, uint64_t value); 48 void FilterSwitchCount(unsigned char op, uint64_t value); 49 void FilterIndex(int32_t col, unsigned char op, sqlite3_value* argv); 50 int32_t Filter(const FilterConstraints& fc, sqlite3_value** argv) override; 51 int32_t Column(int32_t col) const override; 52 void FilterId(unsigned char op, sqlite3_value* argv) override; 53 54 private: 55 void SetNameColumn(const Thread& thread) const; 56 template <typename Value, typename Size> HandleIpidConstraint(bool remove,bool & changed,Value value,Size size,const std::deque<SysTuning::TraceStdtype::Thread> & threadQueue)57 void HandleIpidConstraint(bool remove, 58 bool& changed, 59 Value value, 60 Size size, 61 const std::deque<SysTuning::TraceStdtype::Thread>& threadQueue) 62 { 63 if (remove) { 64 for (auto idx = indexMapBack_->rowIndex_.begin(); idx != indexMapBack_->rowIndex_.end();) { 65 if (threadQueue[*idx].switchCount_ != value) { 66 idx++; 67 } else { 68 changed = true; 69 rowIndexBak_.push_back(*idx); 70 idx++; 71 } 72 } 73 if (changed) { 74 indexMapBack_->rowIndex_ = rowIndexBak_; 75 } 76 } else { 77 for (auto idx = 0; idx < size; idx++) { 78 if (threadQueue[idx].switchCount_ == value) { 79 indexMapBack_->rowIndex_.push_back(idx); 80 } 81 } 82 } 83 indexMapBack_->FixSize(); 84 } 85 template <typename Value, typename Size> HandleSwitchCount(bool remove,bool & isChanged,Value value,Size size,const std::deque<SysTuning::TraceStdtype::Thread> & threadQueue)86 void HandleSwitchCount(bool remove, 87 bool& isChanged, 88 Value value, 89 Size size, 90 const std::deque<SysTuning::TraceStdtype::Thread>& threadQueue) 91 { 92 if (remove) { 93 for (auto i = indexMapBack_->rowIndex_.begin(); i != indexMapBack_->rowIndex_.end();) { 94 if (threadQueue[*i].internalPid_ != value) { 95 i++; 96 } else { 97 isChanged = true; 98 rowIndexBak_.push_back(*i); 99 i++; 100 } 101 } 102 if (isChanged) { 103 indexMapBack_->rowIndex_ = rowIndexBak_; 104 } 105 } else { 106 for (auto i = 0; i < size; i++) { 107 if (threadQueue[i].internalPid_ == value) { 108 indexMapBack_->rowIndex_.push_back(i); 109 } 110 } 111 } 112 indexMapBack_->FixSize(); 113 } 114 void HandleIpidConstraint(const std::deque<SysTuning::TraceStdtype::Thread>& threadQueue, 115 std::size_t size, 116 bool remove, 117 bool changed); 118 std::vector<TableRowId> rowIndexBak_; 119 IndexMap* indexMapBack_ = nullptr; 120 }; 121 }; 122 } // namespace TraceStreamer 123 } // namespace SysTuning 124 125 #endif // SRC_THREAD_TABLE_H 126