1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SRC_TRACE_PROCESSOR_DYNAMIC_THREAD_STATE_GENERATOR_H_ 18 #define SRC_TRACE_PROCESSOR_DYNAMIC_THREAD_STATE_GENERATOR_H_ 19 20 #include "src/trace_processor/sqlite/db_sqlite_table.h" 21 22 #include "perfetto/ext/base/flat_hash_map.h" 23 #include "src/trace_processor/storage/trace_storage.h" 24 25 namespace perfetto { 26 namespace trace_processor { 27 28 class TraceProcessorContext; 29 30 // Dynamic table implementing the thread state table. 31 // This table is a basically the same as sched with extra information added 32 // about wakeups (obtained from sched_waking/sched_wakeup). 33 class ThreadStateGenerator : public DbSqliteTable::DynamicTableGenerator { 34 public: 35 explicit ThreadStateGenerator(TraceProcessorContext* context); 36 ~ThreadStateGenerator() override; 37 38 Table::Schema CreateSchema() override; 39 std::string TableName() override; 40 uint32_t EstimateRowCount() override; 41 base::Status ValidateConstraints(const QueryConstraints&) override; 42 base::Status ComputeTable(const std::vector<Constraint>& cs, 43 const std::vector<Order>& ob, 44 const BitVector& cols_used, 45 std::unique_ptr<Table>& table_return) override; 46 47 // Visible for testing. 48 std::unique_ptr<tables::ThreadStateTable> ComputeThreadStateTable( 49 int64_t trace_end_ts); 50 51 private: 52 struct ThreadSchedInfo { 53 base::Optional<int64_t> desched_ts; 54 base::Optional<StringId> desched_end_state; 55 base::Optional<uint32_t> scheduled_row; 56 base::Optional<bool> io_wait; 57 base::Optional<int64_t> runnable_ts; 58 base::Optional<StringId> blocked_function; 59 }; 60 using TidInfoMap = base::FlatHashMap<UniqueTid, 61 ThreadSchedInfo, 62 base::AlreadyHashed<UniqueTid>, 63 base::QuadraticProbe, 64 /*AppendOnly=*/true>; 65 66 void AddSchedEvent(const Table& sched, 67 uint32_t sched_idx, 68 TidInfoMap& state_map, 69 int64_t trace_end_ts, 70 tables::ThreadStateTable* table); 71 72 void AddWakingEvent(const Table& wakeup, 73 uint32_t wakeup_idx, 74 TidInfoMap& state_map); 75 76 void AddBlockedReasonEvent(const Table& blocked_reason, 77 uint32_t blocked_idx, 78 TidInfoMap& state_map); 79 80 void FlushPendingEventsForThread(UniqueTid utid, 81 const ThreadSchedInfo&, 82 tables::ThreadStateTable* table, 83 base::Optional<int64_t> end_ts); 84 85 std::unique_ptr<tables::ThreadStateTable> unsorted_thread_state_table_; 86 base::Optional<Table> sorted_thread_state_table_; 87 88 const StringId running_string_id_; 89 const StringId runnable_string_id_; 90 91 TraceProcessorContext* context_ = nullptr; 92 }; 93 94 } // namespace trace_processor 95 } // namespace perfetto 96 97 #endif // SRC_TRACE_PROCESSOR_DYNAMIC_THREAD_STATE_GENERATOR_H_ 98