xref: /aosp_15_r20/external/libchrome/ipc/ipc_perftest_util.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef IPC_IPC_PERFTEST_UTIL_H_
6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_PERFTEST_UTIL_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <string>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/process/process_metrics.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h"
16*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
17*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel.h"
18*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_listener.h"
19*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_message.h"
20*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_sender.h"
21*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_test.mojom.h"
22*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/bindings/binding.h"
23*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/system/core.h"
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker namespace IPC {
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker scoped_refptr<base::SingleThreadTaskRunner> GetIOThreadTaskRunner();
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker // This channel listener just replies to all messages with the exact same
30*635a8641SAndroid Build Coastguard Worker // message. It assumes each message has one string parameter. When the string
31*635a8641SAndroid Build Coastguard Worker // "quit" is sent, it will exit.
32*635a8641SAndroid Build Coastguard Worker class ChannelReflectorListener : public Listener {
33*635a8641SAndroid Build Coastguard Worker  public:
34*635a8641SAndroid Build Coastguard Worker   ChannelReflectorListener();
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker   ~ChannelReflectorListener() override;
37*635a8641SAndroid Build Coastguard Worker 
38*635a8641SAndroid Build Coastguard Worker   void Init(Sender* channel, const base::Closure& quit_closure);
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker   bool OnMessageReceived(const Message& message) override;
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker   void OnHello();
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker   void OnPing(const std::string& payload);
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker   void OnSyncPing(const std::string& payload, std::string* response);
47*635a8641SAndroid Build Coastguard Worker 
48*635a8641SAndroid Build Coastguard Worker   void OnQuit();
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker   void Send(IPC::Message* message);
51*635a8641SAndroid Build Coastguard Worker 
52*635a8641SAndroid Build Coastguard Worker  private:
53*635a8641SAndroid Build Coastguard Worker   Sender* channel_;
54*635a8641SAndroid Build Coastguard Worker   base::Closure quit_closure_;
55*635a8641SAndroid Build Coastguard Worker };
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker // This class locks the current thread to a particular CPU core. This is
58*635a8641SAndroid Build Coastguard Worker // important because otherwise the different threads and processes of these
59*635a8641SAndroid Build Coastguard Worker // tests end up on different CPU cores which means that all of the cores are
60*635a8641SAndroid Build Coastguard Worker // lightly loaded so the OS (Windows and Linux) fails to ramp up the CPU
61*635a8641SAndroid Build Coastguard Worker // frequency, leading to unpredictable and often poor performance.
62*635a8641SAndroid Build Coastguard Worker class LockThreadAffinity {
63*635a8641SAndroid Build Coastguard Worker  public:
64*635a8641SAndroid Build Coastguard Worker   explicit LockThreadAffinity(int cpu_number);
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker   ~LockThreadAffinity();
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker  private:
69*635a8641SAndroid Build Coastguard Worker   bool affinity_set_ok_;
70*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
71*635a8641SAndroid Build Coastguard Worker   DWORD_PTR old_affinity_;
72*635a8641SAndroid Build Coastguard Worker #elif defined(OS_LINUX)
73*635a8641SAndroid Build Coastguard Worker   cpu_set_t old_cpuset_;
74*635a8641SAndroid Build Coastguard Worker #endif
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(LockThreadAffinity);
77*635a8641SAndroid Build Coastguard Worker };
78*635a8641SAndroid Build Coastguard Worker 
79*635a8641SAndroid Build Coastguard Worker // Avoid core 0 due to conflicts with Intel's Power Gadget.
80*635a8641SAndroid Build Coastguard Worker // Setting thread affinity will fail harmlessly on single/dual core machines.
81*635a8641SAndroid Build Coastguard Worker const int kSharedCore = 2;
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker class MojoPerfTestClient {
84*635a8641SAndroid Build Coastguard Worker  public:
85*635a8641SAndroid Build Coastguard Worker   MojoPerfTestClient();
86*635a8641SAndroid Build Coastguard Worker 
87*635a8641SAndroid Build Coastguard Worker   ~MojoPerfTestClient();
88*635a8641SAndroid Build Coastguard Worker 
89*635a8641SAndroid Build Coastguard Worker   int Run(MojoHandle handle);
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker  private:
92*635a8641SAndroid Build Coastguard Worker   base::MessageLoop main_message_loop_;
93*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<ChannelReflectorListener> listener_;
94*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<Channel> channel_;
95*635a8641SAndroid Build Coastguard Worker   mojo::ScopedMessagePipeHandle handle_;
96*635a8641SAndroid Build Coastguard Worker };
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker class ReflectorImpl : public IPC::mojom::Reflector {
99*635a8641SAndroid Build Coastguard Worker  public:
100*635a8641SAndroid Build Coastguard Worker   explicit ReflectorImpl(mojo::ScopedMessagePipeHandle handle,
101*635a8641SAndroid Build Coastguard Worker                          const base::Closure& quit_closure);
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker   ~ReflectorImpl() override;
104*635a8641SAndroid Build Coastguard Worker 
105*635a8641SAndroid Build Coastguard Worker  private:
106*635a8641SAndroid Build Coastguard Worker   // IPC::mojom::Reflector:
107*635a8641SAndroid Build Coastguard Worker   void Ping(const std::string& value, PingCallback callback) override;
108*635a8641SAndroid Build Coastguard Worker 
109*635a8641SAndroid Build Coastguard Worker   void SyncPing(const std::string& value, PingCallback callback) override;
110*635a8641SAndroid Build Coastguard Worker 
111*635a8641SAndroid Build Coastguard Worker   void Quit() override;
112*635a8641SAndroid Build Coastguard Worker 
113*635a8641SAndroid Build Coastguard Worker   base::Closure quit_closure_;
114*635a8641SAndroid Build Coastguard Worker   mojo::Binding<IPC::mojom::Reflector> binding_;
115*635a8641SAndroid Build Coastguard Worker };
116*635a8641SAndroid Build Coastguard Worker 
117*635a8641SAndroid Build Coastguard Worker }  // namespace IPC
118*635a8641SAndroid Build Coastguard Worker 
119*635a8641SAndroid Build Coastguard Worker #endif  // IPC_IPC_PERFTEST_UTIL_H_
120