ipc_perftest_util.h 3.4 KB

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