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_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
19 
20 #include <cstdint>
21 #include <map>
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include "perfetto/ext/base/status_or.h"
29 #include "perfetto/trace_processor/basic_types.h"
30 #include "src/trace_processor/containers/string_pool.h"
31 #include "src/trace_processor/db/table.h"
32 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/static_table_function.h"
33 #include "src/trace_processor/storage/trace_storage.h"
34 #include "src/trace_processor/tables/slice_tables_py.h"
35 
36 namespace perfetto::trace_processor {
37 
38 class ExperimentalSliceLayout : public StaticTableFunction {
39  public:
40   ExperimentalSliceLayout(StringPool* string_pool,
41                           const tables::SliceTable* table);
42   virtual ~ExperimentalSliceLayout() override;
43 
44   Table::Schema CreateSchema() override;
45   std::string TableName() override;
46   uint32_t EstimateRowCount() override;
47   base::StatusOr<std::unique_ptr<Table>> ComputeTable(
48       const std::vector<SqlValue>& arguments) override;
49 
50  private:
51   std::unique_ptr<Table> ComputeLayoutTable(
52       std::vector<tables::SliceTable::RowNumber> rows,
53       StringPool::Id filter_id);
54   static tables::SliceTable::Id InsertSlice(
55       std::map<tables::SliceTable::Id, tables::SliceTable::Id>& id_map,
56       tables::SliceTable::Id id,
57       std::optional<tables::SliceTable::Id> parent_id);
58 
59   // TODO(lalitm): remove this cache and move to having explicitly scoped
60   // lifetimes of dynamic tables.
61   std::unordered_map<StringId, std::unique_ptr<Table>> layout_table_cache_;
62 
63   StringPool* string_pool_;
64   const tables::SliceTable* slice_table_;
65   const StringPool::Id empty_string_id_;
66 };
67 
68 }  // namespace perfetto::trace_processor
69 
70 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
71