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 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.h"
18 
19 #include <cstdint>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <vector>
24 
25 #include "perfetto/base/logging.h"
26 #include "perfetto/ext/base/status_or.h"
27 #include "perfetto/trace_processor/basic_types.h"
28 #include "src/trace_processor/db/column_storage.h"
29 #include "src/trace_processor/db/table.h"
30 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
31 #include "src/trace_processor/storage/trace_storage.h"
32 #include "src/trace_processor/tables/metadata_tables_py.h"
33 #include "src/trace_processor/tables/sched_tables_py.h"
34 
35 namespace perfetto::trace_processor {
36 namespace tables {
37 
38 ExperimentalSchedUpidTable::~ExperimentalSchedUpidTable() = default;
39 
40 }  // namespace tables
41 
ExperimentalSchedUpid(const tables::SchedSliceTable & sched,const tables::ThreadTable & thread)42 ExperimentalSchedUpid::ExperimentalSchedUpid(
43     const tables::SchedSliceTable& sched,
44     const tables::ThreadTable& thread)
45     : sched_slice_table_(&sched), thread_table_(&thread) {}
46 ExperimentalSchedUpid::~ExperimentalSchedUpid() = default;
47 
CreateSchema()48 Table::Schema ExperimentalSchedUpid::CreateSchema() {
49   return tables::ExperimentalSchedUpidTable::ComputeStaticSchema();
50 }
51 
TableName()52 std::string ExperimentalSchedUpid::TableName() {
53   return tables::ExperimentalSchedUpidTable::Name();
54 }
55 
EstimateRowCount()56 uint32_t ExperimentalSchedUpid::EstimateRowCount() {
57   return sched_slice_table_->row_count();
58 }
59 
ComputeTable(const std::vector<SqlValue> & arguments)60 base::StatusOr<std::unique_ptr<Table>> ExperimentalSchedUpid::ComputeTable(
61     const std::vector<SqlValue>& arguments) {
62   PERFETTO_CHECK(arguments.empty());
63   if (!sched_upid_table_) {
64     sched_upid_table_ = tables::ExperimentalSchedUpidTable::ExtendParent(
65         *sched_slice_table_, ComputeUpidColumn());
66   }
67   return std::make_unique<Table>(sched_upid_table_->Copy());
68 }
69 
70 ColumnStorage<std::optional<UniquePid>>
ComputeUpidColumn()71 ExperimentalSchedUpid::ComputeUpidColumn() {
72   ColumnStorage<std::optional<UniquePid>> upid;
73   for (uint32_t i = 0; i < sched_slice_table_->row_count(); ++i) {
74     upid.Append(thread_table_->upid()[sched_slice_table_->utid()[i]]);
75   }
76   return upid;
77 }
78 
79 }  // namespace perfetto::trace_processor
80