1 /*
2  * Copyright (C) 2021 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_FLAT_SLICE_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
19 
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "perfetto/ext/base/status_or.h"
26 #include "perfetto/trace_processor/basic_types.h"
27 #include "src/trace_processor/containers/string_pool.h"
28 #include "src/trace_processor/db/table.h"
29 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/static_table_function.h"
30 #include "src/trace_processor/tables/slice_tables_py.h"
31 
32 namespace perfetto {
33 namespace trace_processor {
34 
35 class TraceProcessorContext;
36 
37 // Dynamic table generator for "flat slice" table.
38 //
39 // The concept of a "flat slice" is to take the data in the slice table and
40 // remove all notion of nesting; we do this by, at any point in time, taking the
41 // most specific active slice (i.e. the slice at the bottom of the stack) and
42 // representing that as the *only* slice that was running during that period.
43 //
44 // This concept becomes very useful when you try and linearise a trace and
45 // compare it with other traces spanning the same user action; "self time" (i.e.
46 // time spent in a slice but *not* any children) is easily computed and span
47 // joins with thread state become possible without limiting to only depth zero
48 // slices.
49 //
50 // This table also adds "gap slices" which fill in the gap between top level
51 // slices with a sentinal values so that comparision of the gap between slices
52 // is also possible.
53 //
54 // As input, this generator takes a start and end timestamp between
55 // which slices should be picked; we do this rather than just using the trace
56 // bounds so that the "gap slices" start and end at the appropriate place.
57 //
58 // Note that for the start bound we will *not* pick any slice which started
59 // before the bound even if it finished after. This is dissimilar to span join
60 // (which picks all slices with ts + dur >= bound) and is more akin to doing
61 // a simple ts >= bound. However, slices *will* be truncated at the end
62 // if they would spill past the provided end bound.
63 class ExperimentalFlatSlice : public StaticTableFunction {
64  public:
65   explicit ExperimentalFlatSlice(TraceProcessorContext* context);
66 
67   Table::Schema CreateSchema() override;
68   std::string TableName() override;
69   uint32_t EstimateRowCount() override;
70   base::StatusOr<std::unique_ptr<Table>> ComputeTable(
71       const std::vector<SqlValue>& arguments) override;
72 
73   // Visibile for testing.
74   static std::unique_ptr<tables::ExperimentalFlatSliceTable>
75   ComputeFlatSliceTable(const tables::SliceTable&,
76                         StringPool*,
77                         int64_t start_bound,
78                         int64_t end_bound);
79 
80  private:
81   TraceProcessorContext* context_ = nullptr;
82 };
83 
84 }  // namespace trace_processor
85 }  // namespace perfetto
86 
87 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
88