xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/legacy_v8_cpu_profile_tracker.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_COMMON_LEGACY_V8_CPU_PROFILE_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_LEGACY_V8_CPU_PROFILE_TRACKER_H_
19 
20 #include <cstdint>
21 #include <optional>
22 #include <utility>
23 
24 #include "perfetto/base/status.h"
25 #include "perfetto/ext/base/flat_hash_map.h"
26 #include "perfetto/ext/base/hash.h"
27 #include "perfetto/ext/base/status_or.h"
28 #include "perfetto/ext/base/string_view.h"
29 #include "src/trace_processor/importers/common/virtual_memory_mapping.h"
30 #include "src/trace_processor/storage/trace_storage.h"
31 #include "src/trace_processor/types/trace_processor_context.h"
32 
33 namespace perfetto::trace_processor {
34 
35 // Stores interned callsites for given pid for legacy v8 samples.
36 class LegacyV8CpuProfileTracker {
37  public:
38   explicit LegacyV8CpuProfileTracker(TraceProcessorContext*);
39 
40   // Sets the start timestamp for the given pid.
41   void SetStartTsForSessionAndPid(uint64_t session_id,
42                                   uint32_t pid,
43                                   int64_t ts);
44 
45   // Adds the callsite with for the given session and pid and given raw callsite
46   // id.
47   base::Status AddCallsite(uint64_t session_id,
48                            uint32_t pid,
49                            uint32_t raw_callsite_id,
50                            std::optional<uint32_t> parent_raw_callsite_id,
51                            base::StringView script_url,
52                            base::StringView function_name);
53 
54   // Increments the current timestamp for the given session and pid by
55   // |delta_ts| and returns the resulting full timestamp.
56   base::StatusOr<int64_t> AddDeltaAndGetTs(uint64_t session_id,
57                                            uint32_t pid,
58                                            int64_t delta_ts);
59 
60   // Adds the sample with for the given session and pid/tid and given raw
61   // callsite id.
62   base::Status AddSample(int64_t ts,
63                          uint64_t session_id,
64                          uint32_t pid,
65                          uint32_t tid,
66                          uint32_t raw_callsite_id);
67 
68  private:
69   struct State {
70     int64_t ts;
71     base::FlatHashMap<uint32_t, CallsiteId> callsites;
72     DummyMemoryMapping* mapping;
73   };
74   struct Hasher {
operatorHasher75     uint64_t operator()(const std::pair<uint64_t, uint32_t>& res) {
76       return base::Hasher::Combine(res.first, res.second);
77     }
78   };
79   base::FlatHashMap<std::pair<uint64_t, uint32_t>, State, Hasher>
80       state_by_session_and_pid_;
81 
82   TraceProcessorContext* const context_;
83 };
84 
85 }  // namespace perfetto::trace_processor
86 
87 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_LEGACY_V8_CPU_PROFILE_TRACKER_H_
88