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 2016/04/18 20:39:26 UTC

incubator-kudu git commit: Add a test for the code cache

Repository: incubator-kudu
Updated Branches:
  refs/heads/master d50964eb4 -> 43c9c8760


Add a test for the code cache

This isn't a regression test for any particular existing bug. However, there
were some bugs in some revisions of the NVM cache work that were not exposed by
existing tests, and this seems to catch them.

Change-Id: I0b8f450c5f81f2b21f90e5f1059ca59550ca7fb6
Reviewed-on: http://gerrit.cloudera.org:8080/2618
Tested-by: Kudu Jenkins
Reviewed-by: Dan Burkert <da...@cloudera.com>


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

Branch: refs/heads/master
Commit: 43c9c87604f3b6f3dd286c63344bf18a2db08c21
Parents: d50964e
Author: Todd Lipcon <to...@apache.org>
Authored: Wed Mar 23 15:55:48 2016 -0700
Committer: Todd Lipcon <to...@apache.org>
Committed: Mon Apr 18 18:39:09 2016 +0000

----------------------------------------------------------------------
 src/kudu/codegen/codegen-test.cc        | 48 ++++++++++++++++++++++++++++
 src/kudu/codegen/compilation_manager.cc |  6 +++-
 src/kudu/codegen/compilation_manager.h  |  1 -
 3 files changed, 53 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/43c9c876/src/kudu/codegen/codegen-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/codegen/codegen-test.cc b/src/kudu/codegen/codegen-test.cc
index 0e60011..dccf155 100644
--- a/src/kudu/codegen/codegen-test.cc
+++ b/src/kudu/codegen/codegen-test.cc
@@ -15,13 +15,16 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#include <algorithm>
 #include <string>
 #include <vector>
 
 #include <glog/logging.h>
+#include <glog/stl_logging.h>
 #include <gmock/gmock.h>
 
 #include "kudu/codegen/code_generator.h"
+#include "kudu/codegen/compilation_manager.h"
 #include "kudu/codegen/row_projector.h"
 #include "kudu/common/row.h"
 #include "kudu/common/rowblock.h"
@@ -38,12 +41,15 @@ using std::string;
 using std::vector;
 
 DECLARE_bool(codegen_dump_mc);
+DECLARE_int32(codegen_cache_capacity);
 
 namespace kudu {
 
 typedef RowProjector NoCodegenRP;
 typedef codegen::RowProjector CodegenRP;
 
+using codegen::CompilationManager;
+
 class CodegenTest : public KuduTest {
  public:
   CodegenTest()
@@ -359,4 +365,46 @@ TEST_F(CodegenTest, TestDumpMC) {
   EXPECT_THAT(msgs[0], testing::ContainsRegex("retq"));
 }
 
+// Basic test for the CompilationManager code cache.
+// This runs a bunch of compilation tasks and ensures that the cache
+// sometimes hits on the second attempt for the same projection.
+TEST_F(CodegenTest, TestCodeCache) {
+  Singleton<CompilationManager>::UnsafeReset();
+  FLAGS_codegen_cache_capacity = 10;
+  CompilationManager* cm = CompilationManager::GetSingleton();
+
+  for (int pass = 0; pass < 2; pass++) {
+    int num_hits = 0;
+
+    // Generate all permutations of the first four columns (24 permutations).
+    // For each such permutation, we'll create a projection and request code generation.
+    vector<size_t> perm = { 0, 1, 2, 3 };
+    do {
+      SCOPED_TRACE(perm);
+      Schema projection;
+      ASSERT_OK(CreatePartialSchema(perm, &projection));
+
+      gscoped_ptr<CodegenRP> projector;
+      if (cm->RequestRowProjector(&base_, &projection, &projector)) {
+        num_hits++;
+      }
+      cm->Wait();
+    } while (std::next_permutation(perm.begin(), perm.end()));
+
+    if (pass == 0) {
+      // On the first pass, the cache should have been empty and gotten 0 hits.
+      ASSERT_EQ(0, num_hits);
+    } else {
+      // Otherwise, we expect to have gotten some hits.
+      // If our cache were a perfect LRU implementation, then we would actually
+      // expect 0 hits here as well, since we are accessing the entries in
+      // exactly the same order as we inserted them, and thus would evict
+      // an entry before we look for it again. But, our LRU cache is sharded
+      // so we expect to get some hits on the second time.
+      ASSERT_GT(num_hits, 0);
+      ASSERT_LT(num_hits, 24);
+    }
+  }
+}
+
 } // namespace kudu

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/43c9c876/src/kudu/codegen/compilation_manager.cc
----------------------------------------------------------------------
diff --git a/src/kudu/codegen/compilation_manager.cc b/src/kudu/codegen/compilation_manager.cc
index f704152..a6fcd66 100644
--- a/src/kudu/codegen/compilation_manager.cc
+++ b/src/kudu/codegen/compilation_manager.cc
@@ -49,6 +49,10 @@ DEFINE_bool(codegen_time_compilation, false, "Whether to print time that each co
 TAG_FLAG(codegen_time_compilation, experimental);
 TAG_FLAG(codegen_time_compilation, runtime);
 
+DEFINE_int32(codegen_cache_capacity, 100, "Number of entries which may be stored in the "
+             "code generation cache.");
+TAG_FLAG(codegen_cache_capacity, experimental);
+
 METRIC_DEFINE_gauge_int64(server, code_cache_hits, "Codegen Cache Hits",
                           kudu::MetricUnit::kCacheHits,
                           "Number of codegen cache hits since start",
@@ -118,7 +122,7 @@ class CompilationTask : public Runnable {
 } // anonymous namespace
 
 CompilationManager::CompilationManager()
-  : cache_(kDefaultCacheCapacity),
+  : cache_(FLAGS_codegen_cache_capacity),
     hit_counter_(0),
     query_counter_(0) {
   CHECK_OK(ThreadPoolBuilder("compiler_manager_pool")

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/43c9c876/src/kudu/codegen/compilation_manager.h
----------------------------------------------------------------------
diff --git a/src/kudu/codegen/compilation_manager.h b/src/kudu/codegen/compilation_manager.h
index 0533c58..4dad444 100644
--- a/src/kudu/codegen/compilation_manager.h
+++ b/src/kudu/codegen/compilation_manager.h
@@ -98,7 +98,6 @@ class CompilationManager {
   AtomicInt<int64_t> hit_counter_;
   AtomicInt<int64_t> query_counter_;
 
-  static const int kDefaultCacheCapacity = 100;
   static const int kThreadTimeoutMs = 100;
 
   DISALLOW_COPY_AND_ASSIGN(CompilationManager);