diff --git a/db/db_bench.cc b/db/db_bench.cc index 2b30b80a7..179896a77 100644 --- a/db/db_bench.cc +++ b/db/db_bench.cc @@ -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 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: diff --git a/db/db_impl.cc b/db/db_impl.cc index c8827abaa..c5c156feb 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -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()); } } diff --git a/db/db_impl.h b/db/db_impl.h index df04607d7..fb6879020 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -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 diff --git a/include/leveldb/statistics.h b/include/leveldb/statistics.h index ca35b88a2..7007b3ec7 100644 --- a/include/leveldb/statistics.h +++ b/include/leveldb/statistics.h @@ -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 index 000000000..a3fbf3135 --- /dev/null +++ b/util/statistics.cc @@ -0,0 +1,55 @@ +#include "leveldb/statistics.h" +#include + +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