xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/v8_module.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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_IMPORTERS_PROTO_V8_MODULE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_V8_MODULE_H_
19 
20 #include <cstdint>
21 #include <optional>
22 
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "perfetto/protozero/field.h"
25 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
26 #include "src/trace_processor/importers/proto/proto_importer_module.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/tables/v8_tables_py.h"
29 
30 namespace perfetto {
31 namespace protos {
32 namespace pbzero {
33 class TracePacket_Decoder;
34 }
35 }  // namespace protos
36 namespace trace_processor {
37 
38 class PacketSequenceState;
39 struct TracePacketData;
40 class V8Tracker;
41 
42 // Populates v8 related tables.
43 //
44 // This class processes v8 related trace packets and populates the various
45 // tables. In particular it keeps track of v8 Isolates and what code and
46 // associated debug information has been loaded in each of the isolates.
47 class V8Module : public ProtoImporterModule {
48  public:
49   explicit V8Module(TraceProcessorContext* context);
50 
51   ~V8Module() override;
52 
53   ModuleResult TokenizePacket(
54       const protos::pbzero::TracePacket_Decoder& decoder,
55       TraceBlobView* packet,
56       int64_t packet_timestamp,
57       RefPtr<PacketSequenceStateGeneration> state,
58       uint32_t field_id) override;
59 
60   void ParseTracePacketData(const protos::pbzero::TracePacket_Decoder&,
61                             int64_t ts,
62                             const TracePacketData& packet_data,
63                             uint32_t field_id) override;
64 
65  private:
66   void ParseV8JsCode(protozero::ConstBytes bytes,
67                      int64_t ts,
68                      const TracePacketData& data);
69   void ParseV8InternalCode(protozero::ConstBytes bytes,
70                            int64_t ts,
71                            const TracePacketData& data);
72   void ParseV8WasmCode(protozero::ConstBytes bytes,
73                        int64_t ts,
74                        const TracePacketData& data);
75   void ParseV8RegExpCode(protozero::ConstBytes bytes,
76                          int64_t ts,
77                          const TracePacketData& data);
78   void ParseV8CodeMove(protozero::ConstBytes bytes,
79                        int64_t ts,
80                        const TracePacketData& data);
81 
82   // Determine the utid for a code event.
83   // If the passed in decoder has no tid field this method will try the
84   // TracePacketDefaults.
85   template <typename CodeDecoder>
86   std::optional<UniqueTid> GetUtid(PacketSequenceStateGeneration& generation,
87                                    tables::V8IsolateTable::Id isolate_id,
88                                    const CodeDecoder& code);
89   std::optional<uint32_t> GetDefaultTid(
90       PacketSequenceStateGeneration& generation) const;
91   TraceProcessorContext* const context_;
92   V8Tracker* const v8_tracker_;
93   // Caches isolate to pid associations. Used to compute the utid for code
94   // events.
95   base::FlatHashMap<tables::V8IsolateTable::Id, uint32_t> isolate_to_pid_;
96 };
97 
98 }  // namespace trace_processor
99 }  // namespace perfetto
100 
101 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_V8_MODULE_H_
102