xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/active_chrome_processes_tracker.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_IMPORTERS_PROTO_ACTIVE_CHROME_PROCESSES_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_ACTIVE_CHROME_PROCESSES_TRACKER_H_
19 
20 #include <optional>
21 #include <set>
22 #include <vector>
23 
24 #include "perfetto/ext/base/flat_hash_map.h"
25 #include "src/trace_processor/storage/trace_storage.h"
26 #include "src/trace_processor/types/trace_processor_context.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
31 struct ProcessWithDataLoss {
32   UniquePid upid;
33   // If not std::nullopt, the process data is reliable from this point until
34   // the end of the trace.
35   std::optional<int64_t> reliable_from;
36 };
37 
38 // Tracks ActiveProcesses metadata packets from ChromeTrackEvent,
39 // and process descriptors.
40 // Computes a list of processes with missing data based on this information.
41 class ActiveChromeProcessesTracker {
42  public:
ActiveChromeProcessesTracker(TraceProcessorContext * context)43   explicit ActiveChromeProcessesTracker(TraceProcessorContext* context)
44       : context_(context) {}
45 
46   void AddActiveProcessMetadata(int64_t timestamp, UniquePid upid);
AddProcessDescriptor(int64_t timestamp,UniquePid upid)47   void AddProcessDescriptor(int64_t timestamp, UniquePid upid) {
48     process_data_[upid].descriptor_timestamps.insert(timestamp);
49   }
50   std::vector<ProcessWithDataLoss> GetProcessesWithDataLoss() const;
51   void NotifyEndOfFile();
52 
53  private:
54   struct ProcessData {
55     std::set<int64_t> metadata_timestamps;
56     std::set<int64_t> descriptor_timestamps;
57   };
58 
59   TraceProcessorContext* context_;
60   base::FlatHashMap<UniquePid, ProcessData> process_data_;
61   // Metadata timestamps across all processes.
62   std::set<int64_t> global_metadata_timestamps_;
63 };
64 
65 }  // namespace trace_processor
66 }  // namespace perfetto
67 
68 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_ACTIVE_CHROME_PROCESSES_TRACKER_H_
69