xref: /aosp_15_r20/external/perfetto/src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2022 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_FUNCTIONS_TO_FTRACE_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_TO_FTRACE_H_
19 
20 #include "perfetto/ext/base/flat_hash_map.h"
21 #include "perfetto/ext/base/string_writer.h"
22 #include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
23 #include "src/trace_processor/storage/trace_storage.h"
24 #include "src/trace_processor/types/trace_processor_context.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 
29 class SystraceSerializer {
30  public:
31   using ScopedCString = std::unique_ptr<char, void (*)(void*)>;
32 
33   explicit SystraceSerializer(TraceProcessorContext* context);
34 
35   ScopedCString SerializeToString(uint32_t raw_row);
36 
37  private:
38   using StringIdMap =
39       base::FlatHashMap<StringId, std::vector<std::optional<uint32_t>>>;
40 
41   void SerializePrefix(uint32_t raw_row, base::StringWriter* writer);
42 
43   StringIdMap proto_id_to_arg_index_by_event_;
44   const TraceStorage* storage_ = nullptr;
45   TraceProcessorContext* context_ = nullptr;
46 };
47 
48 struct ToFtrace : public SqlFunction {
49   struct Context {
50     const TraceStorage* storage;
51     SystraceSerializer serializer;
52   };
53 
54   static base::Status Run(Context*,
55                           size_t argc,
56                           sqlite3_value** argv,
57                           SqlValue& out,
58                           Destructors& destructors);
59 };
60 
61 }  // namespace trace_processor
62 }  // namespace perfetto
63 
64 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_TO_FTRACE_H_
65