xref: /aosp_15_r20/external/perfetto/src/tracing/ipc/service/relay_ipc_service.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_TRACING_IPC_SERVICE_RELAY_IPC_SERVICE_H_
18 #define SRC_TRACING_IPC_SERVICE_RELAY_IPC_SERVICE_H_
19 
20 #include <limits>
21 #include <list>
22 
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "perfetto/ext/base/sys_types.h"
25 #include "perfetto/ext/base/weak_ptr.h"
26 #include "perfetto/ext/ipc/basic_types.h"
27 #include "perfetto/ext/tracing/core/tracing_service.h"
28 
29 #include "protos/perfetto/ipc/relay_port.ipc.h"
30 
31 namespace perfetto {
32 
33 // Implements the RelayPort IPC service.
34 class RelayIPCService : public protos::gen::RelayPort {
35  public:
36   explicit RelayIPCService(TracingService* core_service);
37   ~RelayIPCService() override = default;
38 
39   void OnClientDisconnected() override;
40   void SyncClock(const protos::gen::SyncClockRequest&,
41                  DeferredSyncClockResponse) override;
42 
43  private:
44   TracingService* const core_service_;
45 
46   using ClockSnapshots =
47       base::FlatHashMap<uint32_t, std::pair<uint64_t, uint64_t>>;
48   struct ClockSnapshotRecords {
49     base::MachineID machine_id = base::kDefaultMachineID;
50 
51     // Keep track of most recent clock snapshots, ordered by local timestamps
52     // (CLOCK_BOOTTIME).
53     std::list<ClockSnapshots> clock_snapshots;
54 
55     uint64_t min_rtt = std::numeric_limits<uint64_t>::max();
56   };
57 
58   TracingService::RelayEndpoint* GetRelayEndpoint(ipc::ClientID);
59 
60   base::FlatHashMap<ipc::ClientID,
61                     std::unique_ptr<TracingService::RelayEndpoint>>
62       relay_endpoints_;
63 
64   base::WeakPtrFactory<RelayIPCService> weak_ptr_factory_;  // Keep last.
65 };
66 
67 }  // namespace perfetto
68 
69 #endif  // SRC_TRACING_IPC_SERVICE_RELAY_IPC_SERVICE_H_
70