morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "ipc/ipc_perftest_support.h" |
| 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 10 | #include <algorithm> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 11 | #include <memory> |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 12 | #include <string> |
| 13 | |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 14 | #include "base/logging.h" |
tfarina | 4da8275 | 2015-09-16 09:56:21 | [diff] [blame] | 15 | #include "base/macros.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 16 | #include "base/memory/ptr_util.h" |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 17 | #include "base/pickle.h" |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame^] | 18 | #include "base/run_loop.h" |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 19 | #include "base/strings/stringprintf.h" |
| 20 | #include "base/test/perf_time_logger.h" |
| 21 | #include "base/test/test_io_thread.h" |
| 22 | #include "base/threading/thread.h" |
| 23 | #include "base/time/time.h" |
| 24 | #include "build/build_config.h" |
| 25 | #include "ipc/ipc_channel.h" |
| 26 | #include "ipc/ipc_channel_proxy.h" |
| 27 | #include "ipc/ipc_descriptors.h" |
| 28 | #include "ipc/ipc_message_utils.h" |
| 29 | #include "ipc/ipc_sender.h" |
| 30 | |
| 31 | namespace IPC { |
| 32 | namespace test { |
| 33 | |
brucedawson | aa3d0c5 | 2015-02-12 22:33:22 | [diff] [blame] | 34 | // Avoid core 0 due to conflicts with Intel's Power Gadget. |
| 35 | // Setting thread affinity will fail harmlessly on single/dual core machines. |
| 36 | const int kSharedCore = 2; |
| 37 | |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 38 | // This class simply collects stats about abstract "events" (each of which has a |
| 39 | // start time and an end time). |
| 40 | class EventTimeTracker { |
| 41 | public: |
| 42 | explicit EventTimeTracker(const char* name) |
| 43 | : name_(name), |
| 44 | count_(0) { |
| 45 | } |
| 46 | |
| 47 | void AddEvent(const base::TimeTicks& start, const base::TimeTicks& end) { |
| 48 | DCHECK(end >= start); |
| 49 | count_++; |
| 50 | base::TimeDelta duration = end - start; |
| 51 | total_duration_ += duration; |
| 52 | max_duration_ = std::max(max_duration_, duration); |
| 53 | } |
| 54 | |
| 55 | void ShowResults() const { |
| 56 | VLOG(1) << name_ << " count: " << count_; |
| 57 | VLOG(1) << name_ << " total duration: " |
| 58 | << total_duration_.InMillisecondsF() << " ms"; |
| 59 | VLOG(1) << name_ << " average duration: " |
| 60 | << (total_duration_.InMillisecondsF() / static_cast<double>(count_)) |
| 61 | << " ms"; |
| 62 | VLOG(1) << name_ << " maximum duration: " |
| 63 | << max_duration_.InMillisecondsF() << " ms"; |
| 64 | } |
| 65 | |
| 66 | void Reset() { |
| 67 | count_ = 0; |
| 68 | total_duration_ = base::TimeDelta(); |
| 69 | max_duration_ = base::TimeDelta(); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | const std::string name_; |
| 74 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 75 | uint64_t count_; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 76 | base::TimeDelta total_duration_; |
| 77 | base::TimeDelta max_duration_; |
| 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(EventTimeTracker); |
| 80 | }; |
| 81 | |
| 82 | // This channel listener just replies to all messages with the exact same |
| 83 | // message. It assumes each message has one string parameter. When the string |
| 84 | // "quit" is sent, it will exit. |
| 85 | class ChannelReflectorListener : public Listener { |
| 86 | public: |
| 87 | ChannelReflectorListener() |
| 88 | : channel_(NULL), |
| 89 | latency_tracker_("Client messages") { |
| 90 | VLOG(1) << "Client listener up"; |
| 91 | } |
| 92 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 93 | ~ChannelReflectorListener() override { |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 94 | VLOG(1) << "Client listener down"; |
| 95 | latency_tracker_.ShowResults(); |
| 96 | } |
| 97 | |
| 98 | void Init(Channel* channel) { |
| 99 | DCHECK(!channel_); |
| 100 | channel_ = channel; |
| 101 | } |
| 102 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 103 | bool OnMessageReceived(const Message& message) override { |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 104 | CHECK(channel_); |
| 105 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 106 | base::PickleIterator iter(message); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 107 | int64_t time_internal; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 108 | EXPECT_TRUE(iter.ReadInt64(&time_internal)); |
| 109 | int msgid; |
| 110 | EXPECT_TRUE(iter.ReadInt(&msgid)); |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 111 | base::StringPiece payload; |
| 112 | EXPECT_TRUE(iter.ReadStringPiece(&payload)); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 113 | |
| 114 | // Include message deserialization in latency. |
| 115 | base::TimeTicks now = base::TimeTicks::Now(); |
| 116 | |
| 117 | if (payload == "hello") { |
| 118 | latency_tracker_.Reset(); |
| 119 | } else if (payload == "quit") { |
| 120 | latency_tracker_.ShowResults(); |
| 121 | base::MessageLoop::current()->QuitWhenIdle(); |
| 122 | return true; |
| 123 | } else { |
| 124 | // Don't track hello and quit messages. |
| 125 | latency_tracker_.AddEvent( |
| 126 | base::TimeTicks::FromInternalValue(time_internal), now); |
| 127 | } |
| 128 | |
| 129 | Message* msg = new Message(0, 2, Message::PRIORITY_NORMAL); |
| 130 | msg->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 131 | msg->WriteInt(msgid); |
| 132 | msg->WriteString(payload); |
| 133 | channel_->Send(msg); |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | Channel* channel_; |
| 139 | EventTimeTracker latency_tracker_; |
| 140 | }; |
| 141 | |
| 142 | class PerformanceChannelListener : public Listener { |
| 143 | public: |
| 144 | explicit PerformanceChannelListener(const std::string& label) |
| 145 | : label_(label), |
| 146 | sender_(NULL), |
| 147 | msg_count_(0), |
| 148 | msg_size_(0), |
| 149 | count_down_(0), |
| 150 | latency_tracker_("Server messages") { |
| 151 | VLOG(1) << "Server listener up"; |
| 152 | } |
| 153 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 154 | ~PerformanceChannelListener() override { |
dcheng | f3076af | 2014-10-21 18:02:42 | [diff] [blame] | 155 | VLOG(1) << "Server listener down"; |
| 156 | } |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 157 | |
| 158 | void Init(Sender* sender) { |
| 159 | DCHECK(!sender_); |
| 160 | sender_ = sender; |
| 161 | } |
| 162 | |
| 163 | // Call this before running the message loop. |
| 164 | void SetTestParams(int msg_count, size_t msg_size) { |
| 165 | DCHECK_EQ(0, count_down_); |
| 166 | msg_count_ = msg_count; |
| 167 | msg_size_ = msg_size; |
| 168 | count_down_ = msg_count_; |
| 169 | payload_ = std::string(msg_size_, 'a'); |
| 170 | } |
| 171 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 172 | bool OnMessageReceived(const Message& message) override { |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 173 | CHECK(sender_); |
| 174 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 175 | base::PickleIterator iter(message); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 176 | int64_t time_internal; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 177 | EXPECT_TRUE(iter.ReadInt64(&time_internal)); |
| 178 | int msgid; |
| 179 | EXPECT_TRUE(iter.ReadInt(&msgid)); |
| 180 | std::string reflected_payload; |
| 181 | EXPECT_TRUE(iter.ReadString(&reflected_payload)); |
| 182 | |
| 183 | // Include message deserialization in latency. |
| 184 | base::TimeTicks now = base::TimeTicks::Now(); |
| 185 | |
| 186 | if (reflected_payload == "hello") { |
| 187 | // Start timing on hello. |
| 188 | latency_tracker_.Reset(); |
| 189 | DCHECK(!perf_logger_.get()); |
| 190 | std::string test_name = |
| 191 | base::StringPrintf("IPC_%s_Perf_%dx_%u", |
| 192 | label_.c_str(), |
| 193 | msg_count_, |
| 194 | static_cast<unsigned>(msg_size_)); |
| 195 | perf_logger_.reset(new base::PerfTimeLogger(test_name.c_str())); |
| 196 | } else { |
| 197 | DCHECK_EQ(payload_.size(), reflected_payload.size()); |
| 198 | |
| 199 | latency_tracker_.AddEvent( |
| 200 | base::TimeTicks::FromInternalValue(time_internal), now); |
| 201 | |
| 202 | CHECK(count_down_ > 0); |
| 203 | count_down_--; |
| 204 | if (count_down_ == 0) { |
| 205 | perf_logger_.reset(); // Stop the perf timer now. |
| 206 | latency_tracker_.ShowResults(); |
| 207 | base::MessageLoop::current()->QuitWhenIdle(); |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | Message* msg = new Message(0, 2, Message::PRIORITY_NORMAL); |
| 213 | msg->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 214 | msg->WriteInt(count_down_); |
| 215 | msg->WriteString(payload_); |
| 216 | sender_->Send(msg); |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | private: |
| 221 | std::string label_; |
| 222 | Sender* sender_; |
| 223 | int msg_count_; |
| 224 | size_t msg_size_; |
| 225 | |
| 226 | int count_down_; |
| 227 | std::string payload_; |
| 228 | EventTimeTracker latency_tracker_; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 229 | std::unique_ptr<base::PerfTimeLogger> perf_logger_; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 230 | }; |
| 231 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 232 | IPCChannelPerfTestBase::IPCChannelPerfTestBase() = default; |
| 233 | IPCChannelPerfTestBase::~IPCChannelPerfTestBase() = default; |
| 234 | |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 235 | std::vector<PingPongTestParams> |
| 236 | IPCChannelPerfTestBase::GetDefaultTestParams() { |
| 237 | // Test several sizes. We use 12^N for message size, and limit the message |
| 238 | // count to keep the test duration reasonable. |
| 239 | std::vector<PingPongTestParams> list; |
| 240 | list.push_back(PingPongTestParams(12, 50000)); |
| 241 | list.push_back(PingPongTestParams(144, 50000)); |
| 242 | list.push_back(PingPongTestParams(1728, 50000)); |
| 243 | list.push_back(PingPongTestParams(20736, 12000)); |
brucedawson | 903fdfd | 2015-02-05 00:57:52 | [diff] [blame] | 244 | list.push_back(PingPongTestParams(248832, 1000)); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 245 | return list; |
| 246 | } |
| 247 | |
| 248 | void IPCChannelPerfTestBase::RunTestChannelPingPong( |
| 249 | const std::vector<PingPongTestParams>& params) { |
| 250 | Init("PerformanceClient"); |
| 251 | |
| 252 | // Set up IPC channel and start client. |
| 253 | PerformanceChannelListener listener("Channel"); |
| 254 | CreateChannel(&listener); |
| 255 | listener.Init(channel()); |
| 256 | ASSERT_TRUE(ConnectChannel()); |
| 257 | ASSERT_TRUE(StartClient()); |
| 258 | |
brucedawson | aa3d0c5 | 2015-02-12 22:33:22 | [diff] [blame] | 259 | LockThreadAffinity thread_locker(kSharedCore); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 260 | for (size_t i = 0; i < params.size(); i++) { |
| 261 | listener.SetTestParams(params[i].message_count(), |
| 262 | params[i].message_size()); |
| 263 | |
| 264 | // This initial message will kick-start the ping-pong of messages. |
| 265 | Message* message = |
| 266 | new Message(0, 2, Message::PRIORITY_NORMAL); |
| 267 | message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 268 | message->WriteInt(-1); |
| 269 | message->WriteString("hello"); |
| 270 | sender()->Send(message); |
| 271 | |
| 272 | // Run message loop. |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame^] | 273 | base::RunLoop().Run(); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Send quit message. |
| 277 | Message* message = new Message(0, 2, Message::PRIORITY_NORMAL); |
| 278 | message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 279 | message->WriteInt(-1); |
| 280 | message->WriteString("quit"); |
| 281 | sender()->Send(message); |
| 282 | |
| 283 | EXPECT_TRUE(WaitForClientShutdown()); |
| 284 | DestroyChannel(); |
| 285 | } |
| 286 | |
| 287 | void IPCChannelPerfTestBase::RunTestChannelProxyPingPong( |
| 288 | const std::vector<PingPongTestParams>& params) { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 289 | io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 290 | InitWithCustomMessageLoop("PerformanceClient", |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 291 | base::WrapUnique(new base::MessageLoop())); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 292 | |
| 293 | // Set up IPC channel and start client. |
| 294 | PerformanceChannelListener listener("ChannelProxy"); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 295 | CreateChannelProxy(&listener, io_thread_->task_runner()); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 296 | listener.Init(channel_proxy()); |
| 297 | ASSERT_TRUE(StartClient()); |
| 298 | |
brucedawson | aa3d0c5 | 2015-02-12 22:33:22 | [diff] [blame] | 299 | LockThreadAffinity thread_locker(kSharedCore); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 300 | for (size_t i = 0; i < params.size(); i++) { |
| 301 | listener.SetTestParams(params[i].message_count(), |
| 302 | params[i].message_size()); |
| 303 | |
| 304 | // This initial message will kick-start the ping-pong of messages. |
| 305 | Message* message = |
| 306 | new Message(0, 2, Message::PRIORITY_NORMAL); |
| 307 | message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 308 | message->WriteInt(-1); |
| 309 | message->WriteString("hello"); |
| 310 | sender()->Send(message); |
| 311 | |
| 312 | // Run message loop. |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame^] | 313 | base::RunLoop().Run(); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Send quit message. |
| 317 | Message* message = new Message(0, 2, Message::PRIORITY_NORMAL); |
| 318 | message->WriteInt64(base::TimeTicks::Now().ToInternalValue()); |
| 319 | message->WriteInt(-1); |
| 320 | message->WriteString("quit"); |
| 321 | sender()->Send(message); |
| 322 | |
| 323 | EXPECT_TRUE(WaitForClientShutdown()); |
| 324 | DestroyChannelProxy(); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 325 | |
| 326 | io_thread_.reset(); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | PingPongTestClient::PingPongTestClient() |
| 331 | : listener_(new ChannelReflectorListener()) { |
| 332 | } |
| 333 | |
| 334 | PingPongTestClient::~PingPongTestClient() { |
| 335 | } |
| 336 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 337 | std::unique_ptr<Channel> PingPongTestClient::CreateChannel(Listener* listener) { |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 338 | return Channel::CreateClient(IPCTestBase::GetChannelName("PerformanceClient"), |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 339 | listener); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | int PingPongTestClient::RunMain() { |
brucedawson | aa3d0c5 | 2015-02-12 22:33:22 | [diff] [blame] | 343 | LockThreadAffinity thread_locker(kSharedCore); |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 344 | std::unique_ptr<Channel> channel = CreateChannel(listener_.get()); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 345 | listener_->Init(channel.get()); |
| 346 | CHECK(channel->Connect()); |
| 347 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame^] | 348 | base::RunLoop().Run(); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | scoped_refptr<base::TaskRunner> PingPongTestClient::task_runner() { |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 353 | return main_message_loop_.task_runner(); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 354 | } |
| 355 | |
brucedawson | aa3d0c5 | 2015-02-12 22:33:22 | [diff] [blame] | 356 | LockThreadAffinity::LockThreadAffinity(int cpu_number) |
| 357 | : affinity_set_ok_(false) { |
| 358 | #if defined(OS_WIN) |
brucedawson | d3beca7 | 2015-12-10 02:22:44 | [diff] [blame] | 359 | const DWORD_PTR thread_mask = static_cast<DWORD_PTR>(1) << cpu_number; |
brucedawson | aa3d0c5 | 2015-02-12 22:33:22 | [diff] [blame] | 360 | old_affinity_ = SetThreadAffinityMask(GetCurrentThread(), thread_mask); |
| 361 | affinity_set_ok_ = old_affinity_ != 0; |
| 362 | #elif defined(OS_LINUX) |
| 363 | cpu_set_t cpuset; |
| 364 | CPU_ZERO(&cpuset); |
| 365 | CPU_SET(cpu_number, &cpuset); |
| 366 | auto get_result = sched_getaffinity(0, sizeof(old_cpuset_), &old_cpuset_); |
| 367 | DCHECK_EQ(0, get_result); |
| 368 | auto set_result = sched_setaffinity(0, sizeof(cpuset), &cpuset); |
| 369 | // Check for get_result failure, even though it should always succeed. |
| 370 | affinity_set_ok_ = (set_result == 0) && (get_result == 0); |
| 371 | #endif |
| 372 | if (!affinity_set_ok_) |
| 373 | LOG(WARNING) << "Failed to set thread affinity to CPU " << cpu_number; |
| 374 | } |
| 375 | |
| 376 | LockThreadAffinity::~LockThreadAffinity() { |
| 377 | if (!affinity_set_ok_) |
| 378 | return; |
| 379 | #if defined(OS_WIN) |
| 380 | auto set_result = SetThreadAffinityMask(GetCurrentThread(), old_affinity_); |
| 381 | DCHECK_NE(0u, set_result); |
| 382 | #elif defined(OS_LINUX) |
| 383 | auto set_result = sched_setaffinity(0, sizeof(old_cpuset_), &old_cpuset_); |
| 384 | DCHECK_EQ(0, set_result); |
| 385 | #endif |
| 386 | } |
| 387 | |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 388 | } // namespace test |
| 389 | } // namespace IPC |