You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by to...@apache.org on 2018/02/05 04:43:38 UTC

[1/2] kudu git commit: jsonwriter: small optimization for PB fields

Repository: kudu
Updated Branches:
  refs/heads/master 2bff29b59 -> bf6c2c07b


jsonwriter: small optimization for PB fields

This avoids extra non-inlinable calls into the protobuf library:
- extra calls to GetReflection()
- calls to FieldSize() while looping through repeated fields

This has a small benefit on all PBs and a big benefit on those PBs with
a lot of repeated fields. The latter should be a nice boost particularly
when serializing metrics.

Before:
TestJsonWriter.BenchmarkAllTypes      Throughput: 157.609MB/sec
TestJsonWriter.BenchmarkNestedMessage Throughput: 124.048MB/sec
TestJsonWriter.BenchmarkRepeatedInt64 Throughput: 113.77MB/sec

After:
TestJsonWriter.BenchmarkAllTypes      Throughput: 163.327MB/sec  (+3.6%)
TestJsonWriter.BenchmarkNestedMessage Throughput: 127.633MB/sec  (+2.9%)
TestJsonWriter.BenchmarkRepeatedInt64 Throughput: 158.229MB/sec  (+39.1%)

Change-Id: Iceee0096a3af9300fca3dd18508a12e140e99b31
Reviewed-on: http://gerrit.cloudera.org:8080/9184
Reviewed-by: Sailesh Mukil <sa...@cloudera.com>
Tested-by: Kudu Jenkins
Reviewed-by: Todd Lipcon <to...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/f27f5ada
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/f27f5ada
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/f27f5ada

Branch: refs/heads/master
Commit: f27f5adaedabf5aac53583a2cd425c6a38484b78
Parents: 2bff29b
Author: Todd Lipcon <to...@apache.org>
Authored: Thu Feb 1 14:57:36 2018 -0800
Committer: Todd Lipcon <to...@apache.org>
Committed: Sat Feb 3 01:53:02 2018 +0000

----------------------------------------------------------------------
 src/kudu/util/jsonwriter-test.cc | 27 ++++++++++++++++++++++++---
 src/kudu/util/jsonwriter.cc      | 15 ++++++++-------
 src/kudu/util/jsonwriter.h       |  5 ++++-
 3 files changed, 36 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/f27f5ada/src/kudu/util/jsonwriter-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/jsonwriter-test.cc b/src/kudu/util/jsonwriter-test.cc
index 608d9c0..6f9b10d 100644
--- a/src/kudu/util/jsonwriter-test.cc
+++ b/src/kudu/util/jsonwriter-test.cc
@@ -29,12 +29,17 @@
 #include "kudu/util/stopwatch.h"
 #include "kudu/util/test_util.h"
 
+namespace google { namespace protobuf { class Message; } }
+
+using google::protobuf::Message;
 using jsonwriter_test::TestAllTypes;
 
 namespace kudu {
 
 class TestJsonWriter : public KuduTest {
  protected:
+  void DoBenchmark(const Message& pb);
+
   TestAllTypes MakeAllTypesPB() {
     TestAllTypes pb;
     pb.set_optional_int32(1);
@@ -170,9 +175,7 @@ TEST_F(TestJsonWriter, TestPBNestedMessage) {
             JsonWriter::ToJson(pb, JsonWriter::COMPACT));
 }
 
-TEST_F(TestJsonWriter, Benchmark) {
-  TestAllTypes pb = MakeAllTypesPB();
-
+void TestJsonWriter::DoBenchmark(const Message& pb) {
   int64_t total_len = 0;
   Stopwatch sw;
   sw.start();
@@ -191,5 +194,23 @@ TEST_F(TestJsonWriter, Benchmark) {
   LOG(INFO) << "Throughput: " << mbps << "MB/sec";
 }
 
+TEST_F(TestJsonWriter, BenchmarkAllTypes) {
+  DoBenchmark(MakeAllTypesPB());
+}
+
+TEST_F(TestJsonWriter, BenchmarkNestedMessage) {
+  TestAllTypes pb;
+  pb.add_repeated_nested_message()->set_int_field(12345);
+  pb.mutable_optional_nested_message()->set_int_field(54321);
+  DoBenchmark(pb);
+}
+
+TEST_F(TestJsonWriter, BenchmarkRepeatedInt64) {
+  TestAllTypes pb;
+  for (int i = 0; i < 10000; i++) {
+    pb.add_repeated_int64(i);
+  }
+  DoBenchmark(pb);
+}
 
 } // namespace kudu

http://git-wip-us.apache.org/repos/asf/kudu/blob/f27f5ada/src/kudu/util/jsonwriter.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/jsonwriter.cc b/src/kudu/util/jsonwriter.cc
index f194d5b..3a5580c 100644
--- a/src/kudu/util/jsonwriter.cc
+++ b/src/kudu/util/jsonwriter.cc
@@ -188,19 +188,20 @@ void JsonWriter::Protobuf(const Message& pb) {
     String(field->name());
     if (field->is_repeated()) {
       StartArray();
-      for (int i = 0; i < reflection->FieldSize(pb, field); i++) {
-        ProtobufRepeatedField(pb, field, i);
+      int size = reflection->FieldSize(pb, field);
+      for (int i = 0; i < size; i++) {
+        ProtobufRepeatedField(pb, reflection, field, i);
       }
       EndArray();
     } else {
-      ProtobufField(pb, field);
+      ProtobufField(pb, reflection, field);
     }
   }
   EndObject();
 }
 
-void JsonWriter::ProtobufField(const Message& pb, const FieldDescriptor* field) {
-  const Reflection* reflection = pb.GetReflection();
+void JsonWriter::ProtobufField(const Message& pb, const Reflection* reflection,
+                               const FieldDescriptor* field) {
   switch (field->cpp_type()) {
     case FieldDescriptor::CPPTYPE_INT32:
       Int(reflection->GetInt32(pb, field));
@@ -238,8 +239,8 @@ void JsonWriter::ProtobufField(const Message& pb, const FieldDescriptor* field)
   }
 }
 
-void JsonWriter::ProtobufRepeatedField(const Message& pb, const FieldDescriptor* field, int index) {
-  const Reflection* reflection = pb.GetReflection();
+void JsonWriter::ProtobufRepeatedField(const Message& pb, const Reflection* reflection,
+                                       const FieldDescriptor* field, int index) {
   switch (field->cpp_type()) {
     case FieldDescriptor::CPPTYPE_INT32:
       Int(reflection->GetRepeatedInt32(pb, field, index));

http://git-wip-us.apache.org/repos/asf/kudu/blob/f27f5ada/src/kudu/util/jsonwriter.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/jsonwriter.h b/src/kudu/util/jsonwriter.h
index 860bfaa..24b4575 100644
--- a/src/kudu/util/jsonwriter.h
+++ b/src/kudu/util/jsonwriter.h
@@ -27,8 +27,9 @@
 
 namespace google {
 namespace protobuf {
-class Message;
 class FieldDescriptor;
+class Message;
+class Reflection;
 } // namespace protobuf
 } // namespace google
 
@@ -85,8 +86,10 @@ class JsonWriter {
 
  private:
   void ProtobufField(const google::protobuf::Message& pb,
+                     const google::protobuf::Reflection* reflection,
                      const google::protobuf::FieldDescriptor* field);
   void ProtobufRepeatedField(const google::protobuf::Message& pb,
+                             const google::protobuf::Reflection* reflection,
                              const google::protobuf::FieldDescriptor* field,
                              int index);
 


[2/2] kudu git commit: rpc: micro-optimize delayed task handling

Posted by to...@apache.org.
rpc: micro-optimize delayed task handling

Previously we put all pending DelayedTasks in an STL set<>. However, we
don't really need a set -- using an intrusive doubly linked list is
sufficient and provides O(1) removal instead of O(lg n). This speeds up
the new unit test significantly.

I measured the new test using the following command line before and
after three times:
  periodic-test --gtest_filter=\*Perf\* --gtest_repeat=10 2>&1 | grep User | tee -a /tmp/after

and then ran a t-test on the difference in CPU time:

data:  d.before and d.after
t = 19.097, df = 48.327, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.09643424 0.11912596
sample estimates:
mean of x mean of y
0.4324359 0.3246558

So this saves a noticeable amount of CPU when there are a lot of pending
tasks.

Change-Id: I3b6be5ef7e8f464f3bc4c62f904e2692b30ddc65
Reviewed-on: http://gerrit.cloudera.org:8080/9048
Tested-by: Kudu Jenkins
Reviewed-by: Michael Ho <kw...@cloudera.com>
Reviewed-by: Todd Lipcon <to...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/bf6c2c07
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/bf6c2c07
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/bf6c2c07

Branch: refs/heads/master
Commit: bf6c2c07b1edfc8d38d64ffb242b3f303d54144b
Parents: f27f5ad
Author: Todd Lipcon <to...@apache.org>
Authored: Wed Jan 17 15:59:44 2018 -0800
Committer: Todd Lipcon <to...@apache.org>
Committed: Mon Feb 5 04:21:34 2018 +0000

----------------------------------------------------------------------
 src/kudu/rpc/periodic-test.cc | 36 ++++++++++++++++++++++++++++++++++++
 src/kudu/rpc/reactor.cc       | 19 +++++++++++--------
 src/kudu/rpc/reactor.h        |  3 +--
 3 files changed, 48 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/bf6c2c07/src/kudu/rpc/periodic-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/rpc/periodic-test.cc b/src/kudu/rpc/periodic-test.cc
index 08f379f..cd793bd 100644
--- a/src/kudu/rpc/periodic-test.cc
+++ b/src/kudu/rpc/periodic-test.cc
@@ -18,19 +18,25 @@
 #include <atomic>
 #include <cstdint>
 #include <memory>
+#include <ostream>
 #include <string>
 #include <utility>
+#include <vector>
 
+#include <glog/logging.h>
 #include <gtest/gtest.h>
 
 #include "kudu/rpc/messenger.h"
 #include "kudu/rpc/periodic.h"
 #include "kudu/util/monotime.h"
+#include "kudu/util/scoped_cleanup.h"
+#include "kudu/util/stopwatch.h"
 #include "kudu/util/test_macros.h"
 #include "kudu/util/test_util.h"
 
 using std::atomic;
 using std::shared_ptr;
+using std::vector;
 
 namespace kudu {
 namespace rpc {
@@ -255,5 +261,35 @@ TEST_F(PeriodicTimerTest, TestCallbackRestartsOneShotTimer) {
   messenger->Shutdown();
 }
 
+TEST_F(PeriodicTimerTest, TestPerformance) {
+  const int kNumTimers = 1000;
+  shared_ptr<Messenger> messenger;
+  ASSERT_OK(MessengerBuilder("test")
+            .set_num_reactors(1)
+            .Build(&messenger));
+  SCOPED_CLEANUP({ messenger->Shutdown(); });
+
+  vector<shared_ptr<PeriodicTimer>> timers;
+  for (int i = 0; i < kNumTimers; i++) {
+    timers.emplace_back(PeriodicTimer::Create(
+        messenger,
+        [&] {}, // No-op.
+        MonoDelta::FromMilliseconds(10)));
+    timers.back()->Start();
+  }
+
+  Stopwatch sw(Stopwatch::ALL_THREADS);
+  sw.start();
+  SleepFor(MonoDelta::FromSeconds(1));
+  sw.stop();
+  LOG(INFO) << "User CPU for running " << kNumTimers << " timers for 1 second: "
+            << sw.elapsed().user_cpu_seconds() << "s";
+
+  for (auto& t : timers) {
+    t->Stop();
+  }
+
+}
+
 } // namespace rpc
 } // namespace kudu

http://git-wip-us.apache.org/repos/asf/kudu/blob/bf6c2c07/src/kudu/rpc/reactor.cc
----------------------------------------------------------------------
diff --git a/src/kudu/rpc/reactor.cc b/src/kudu/rpc/reactor.cc
index f7ff78b..e841be6 100644
--- a/src/kudu/rpc/reactor.cc
+++ b/src/kudu/rpc/reactor.cc
@@ -148,7 +148,7 @@ Status ReactorThread::Init() {
   DVLOG(6) << "Called ReactorThread::Init()";
   // Register to get async notifications in our epoll loop.
   async_.set(loop_);
-  async_.set<ReactorThread, &ReactorThread::AsyncHandler>(this);
+  async_.set<ReactorThread, &ReactorThread::AsyncHandler>(this); // NOLINT(*)
   async_.start();
 
   // Register the timer watcher.
@@ -243,10 +243,11 @@ void ReactorThread::ShutdownInternal() {
   // These won't be found in the ReactorThread's list of pending tasks
   // because they've been "run" (that is, they've been scheduled).
   Status aborted = ShutdownError(true); // aborted
-  for (DelayedTask* task : scheduled_tasks_) {
-    task->Abort(aborted); // should also free the task.
+  while (!scheduled_tasks_.empty()) {
+    DelayedTask* t = &scheduled_tasks_.front();
+    scheduled_tasks_.pop_front();
+    t->Abort(aborted); // should also free the task.
   }
-  scheduled_tasks_.clear();
 
   // Remove the OpenSSL thread state.
   ERR_remove_thread_state(nullptr);
@@ -675,14 +676,15 @@ DelayedTask::DelayedTask(boost::function<void(const Status&)> func,
 void DelayedTask::Run(ReactorThread* thread) {
   DCHECK(thread_ == nullptr) << "Task has already been scheduled";
   DCHECK(thread->IsCurrentThread());
+  DCHECK(!is_linked()) << "Should not be linked on pending_tasks_ anymore";
 
   // Schedule the task to run later.
   thread_ = thread;
   timer_.set(thread->loop_);
-  timer_.set<DelayedTask, &DelayedTask::TimerHandler>(this);
+  timer_.set<DelayedTask, &DelayedTask::TimerHandler>(this); // NOLINT(*)
   timer_.start(when_.ToSeconds(), // after
                0);                // repeat
-  thread_->scheduled_tasks_.insert(this);
+  thread_->scheduled_tasks_.push_back(*this);
 }
 
 void DelayedTask::Abort(const Status& abort_status) {
@@ -690,9 +692,10 @@ void DelayedTask::Abort(const Status& abort_status) {
   delete this;
 }
 
-void DelayedTask::TimerHandler(ev::timer& watcher, int revents) {
+void DelayedTask::TimerHandler(ev::timer& /*watcher*/, int revents) {
+  DCHECK(is_linked()) << "should be linked on scheduled_tasks_";
   // We will free this task's memory.
-  thread_->scheduled_tasks_.erase(this);
+  thread_->scheduled_tasks_.erase(thread_->scheduled_tasks_.iterator_to(*this));
 
   if (EV_ERROR & revents) {
     string msg = "Delayed task got an error in its timer handler";

http://git-wip-us.apache.org/repos/asf/kudu/blob/bf6c2c07/src/kudu/rpc/reactor.h
----------------------------------------------------------------------
diff --git a/src/kudu/rpc/reactor.h b/src/kudu/rpc/reactor.h
index cef966b..8884f54 100644
--- a/src/kudu/rpc/reactor.h
+++ b/src/kudu/rpc/reactor.h
@@ -20,7 +20,6 @@
 #include <cstdint>
 #include <list>
 #include <memory>
-#include <set>
 #include <string>
 #include <unordered_map>
 
@@ -287,7 +286,7 @@ class ReactorThread {
   //
   // Each task owns its own memory and must be freed by its TaskRun and
   // Abort members, provided it was allocated on the heap.
-  std::set<DelayedTask*> scheduled_tasks_;
+  boost::intrusive::list<DelayedTask> scheduled_tasks_;
 
   // The current monotonic time.  Updated every coarse_timer_granularity_secs_.
   MonoTime cur_time_;