]> git-server-git.apps.pok.os.sepia.ceph.com Git - rocksdb.git/commitdiff
[rocksdb][refactor] statistic printing code to one place
authorAbhishek Kona <abhishekk@fb.com>
Wed, 19 Jun 2013 03:28:41 +0000 (20:28 -0700)
committerAbhishek Kona <abhishekk@fb.com>
Wed, 19 Jun 2013 03:28:41 +0000 (20:28 -0700)
Summary: $title

Test Plan: db_bench --statistics=1

Reviewers: haobo

Reviewed By: haobo

CC: leveldb
Differential Revision: https://reviews.facebook.net/D11373

db/db_bench.cc
db/db_impl.cc
db/db_impl.h
include/leveldb/statistics.h
util/statistics.cc [new file with mode: 0644]

index 2b30b80a7aed638bd9605fcfe7029b3e717f2318..179896a77f2204c4e8d35a803233744f0ecbc129 100644 (file)
@@ -15,6 +15,7 @@
 #include "leveldb/write_batch.h"
 #include "leveldb/statistics.h"
 #include "port/port.h"
+#include "util/bit_set.h"
 #include "util/crc32c.h"
 #include "util/histogram.h"
 #include "util/mutexlock.h"
@@ -22,7 +23,6 @@
 #include "util/stack_trace.h"
 #include "util/string_util.h"
 #include "util/testutil.h"
-#include "util/bit_set.h"
 #include "hdfs/env_hdfs.h"
 
 // Comma-separated list of operations to run in the specified order
@@ -738,32 +738,6 @@ class Benchmark {
 #endif
   }
 
-  void PrintHistogram(const Histograms& histogram_type,
-                      const std::string& name) {
-    HistogramData histogramData;
-    dbstats->histogramData(histogram_type, &histogramData);
-    fprintf(stdout, "%s statistics Percentiles :", name.c_str());
-    fprintf(stdout, "50 : %f ", histogramData.median);
-    fprintf(stdout, "95 : %f ", histogramData.percentile95);
-    fprintf(stdout, "99 : %f\n", histogramData.percentile99);
-  }
-
-  void PrintTicker(const Tickers& ticker, const std::string& name) {
-    fprintf(stdout, "%s COUNT : %ld\n",
-            name.c_str(), dbstats->getTickerCount(ticker));
-  }
-
-  void PrintStatistics() {
-    if (FLAGS_statistics) {
-      for (auto& t : TickersNameMap) {
-        PrintTicker(t.first, t.second);
-      }
-      for (auto& h : HistogramsNameMap) {
-        PrintHistogram(h.first, h.second);
-      }
-    }
-  }
-
  public:
   Benchmark()
   : cache_(FLAGS_cache_size >= 0 ?
@@ -949,7 +923,9 @@ unique_ptr<char []> GenerateKeyFromInt(int v, const char* suffix = "")
         RunBenchmark(num_threads, name, method);
       }
     }
-    PrintStatistics();
+    if (FLAGS_statistics) {
+     fprintf(stdout, "STATISTICS:\n%s\n", dbstats->ToString().c_str());
+    }
   }
 
  private:
index c8827abaa74e3b1338590d16a4731b99153cc7d3..c5c156feb4faadfd531bd3e719caafc2920a7b0d 100644 (file)
@@ -314,38 +314,12 @@ const Status DBImpl::CreateArchivalDirectory() {
   return Status::OK();
 }
 
-void DBImpl::PrintHistogram(Histograms histogram_type, std::string name) {
-  assert(options_.statistics);
-  HistogramData histogramData;
-  options_.statistics->histogramData(histogram_type, &histogramData);
-  Log(options_.info_log, "%s statistics Percentiles :", name.c_str());
-  Log(options_.info_log, "50 : %f ",histogramData.median);
-  Log(options_.info_log, "95 : %f ", histogramData.percentile95);
-  Log(options_.info_log, "99 : %f\n", histogramData.percentile99);
-}
-
 void DBImpl::PrintStatistics() {
   auto dbstats = options_.statistics;
   if (dbstats) {
     Log(options_.info_log,
-        "Statistics counters:\n"
-        "File opened:%ld closed:%ld errors:%ld\n"
-        "Block Cache Hit Count:%ld Block Cache Miss Count:%ld\n"
-        "Bloom Filter Useful: %ld \n"
-        "Compaction key_drop_newer_entry: %ld key_drop_obsolete: %ld "
-        "Compaction key_drop_user: %ld\n",
-        dbstats->getTickerCount(NO_FILE_OPENS),
-        dbstats->getTickerCount(NO_FILE_CLOSES),
-        dbstats->getTickerCount(NO_FILE_ERRORS),
-        dbstats->getTickerCount(BLOCK_CACHE_HIT),
-        dbstats->getTickerCount(BLOCK_CACHE_MISS),
-        dbstats->getTickerCount(BLOOM_FILTER_USEFUL),
-        dbstats->getTickerCount(COMPACTION_KEY_DROP_NEWER_ENTRY),
-        dbstats->getTickerCount(COMPACTION_KEY_DROP_OBSOLETE),
-        dbstats->getTickerCount(COMPACTION_KEY_DROP_USER));
-    PrintHistogram(DB_GET, "DB_GET");
-    PrintHistogram(DB_WRITE, "DB_WRITE");
-    PrintHistogram(COMPACTION_TIME, "COMPACTION_TIME");
+        "STATISTCS:\n %s",
+        dbstats->ToString().c_str());
   }
 }
 
index df04607d7e7123e5e2721b901022732b580a468f..fb687902055c92249c032ad2457477540bdf3d5e 100644 (file)
@@ -212,8 +212,6 @@ class DBImpl : public DB {
 
   Status ReadFirstLine(const std::string& fname, WriteBatch* const batch);
 
-  void PrintHistogram(Histograms histogram_type, std::string name);
-
   void PrintStatistics();
 
   // dump leveldb.stats to LOG
index ca35b88a29df3bb42a6db0c5858042e65aa528d7..7007b3ec748d8b77a15b5aa49fd05d580be2c5cb 100644 (file)
@@ -186,7 +186,8 @@ class Statistics {
   virtual void measureTime(Histograms histogramType, uint64_t time) = 0;
 
   virtual void histogramData(Histograms type, HistogramData * const data) = 0;
-
+  // String representation of the statistic object.
+  std::string ToString();
 };
 
 // Create a concrete DBStatistics object
diff --git a/util/statistics.cc b/util/statistics.cc
new file mode 100644 (file)
index 0000000..a3fbf31
--- /dev/null
@@ -0,0 +1,55 @@
+#include "leveldb/statistics.h"
+#include <cstdio>
+
+namespace leveldb {
+
+namespace {
+// a buffer size used for temp string buffers
+const int kBufferSize = 200;
+
+std::string HistogramToString (
+    Statistics* dbstats,
+    const Histograms& histogram_type,
+    const std::string& name) {
+
+  char buffer[kBufferSize];
+  HistogramData histogramData;
+  dbstats->histogramData(histogram_type, &histogramData);
+  snprintf(
+      buffer,
+      kBufferSize,
+      "%s statistics Percentiles :=> 50 : %f 95 : %f 99 : %f\n",
+      name.c_str(),
+      histogramData.median,
+      histogramData.percentile95,
+      histogramData.percentile99
+  );
+  return std::string(buffer);
+};
+
+std::string TickerToString (
+    Statistics* dbstats,
+    const Tickers& ticker,
+    const std::string& name) {
+
+  char buffer[kBufferSize];
+  snprintf(buffer, kBufferSize, "%s COUNT : %ld\n",
+            name.c_str(), dbstats->getTickerCount(ticker));
+  return std::string(buffer);
+};
+} // namespace
+
+std::string Statistics::ToString() {
+  std::string res;
+  res.reserve(20000);
+  for (const auto& t : TickersNameMap) {
+    res.append(TickerToString(this, t.first, t.second));
+  }
+  for (const auto& h : HistogramsNameMap) {
+    res.append(HistogramToString(this, h.first, h.second));
+  }
+  res.shrink_to_fit();
+  return res;
+}
+
+} // namespace leveldb