diff --git a/CMakeLists.txt b/CMakeLists.txt index 399ae5c588..baed603081 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -391,6 +391,7 @@ set(TESTS util/file_reader_writer_test.cc util/heap_test.cc util/histogram_test.cc + util/iostats_context_test.cc util/memenv_test.cc util/mock_env_test.cc util/options_test.cc diff --git a/Makefile b/Makefile index 66e7ac3340..0e3f11e651 100644 --- a/Makefile +++ b/Makefile @@ -340,7 +340,8 @@ TESTS = \ compact_on_deletion_collector_test \ compaction_job_stats_test \ transaction_test \ - ldb_cmd_test + ldb_cmd_test \ + iostats_context_test SUBSET := $(shell echo $(TESTS) |sed s/^.*$(ROCKSDBTESTS_START)/$(ROCKSDBTESTS_START)/) @@ -1045,6 +1046,9 @@ ldb_cmd_test: tools/ldb_cmd_test.o $(LIBOBJECTS) $(TESTHARNESS) ldb: tools/ldb.o $(LIBOBJECTS) $(AM_LINK) +iostats_context_test: util/iostats_context_test.o $(LIBOBJECTS) $(TESTHARNESS) + $(AM_V_CCLD)$(CXX) $^ $(EXEC_LDFLAGS) -o $@ $(LDFLAGS) + #------------------------------------------------- # make install related stuff INSTALL_PATH ?= /usr/local diff --git a/include/rocksdb/iostats_context.h b/include/rocksdb/iostats_context.h index 10ea420cf6..632fe44c82 100644 --- a/include/rocksdb/iostats_context.h +++ b/include/rocksdb/iostats_context.h @@ -18,7 +18,7 @@ struct IOStatsContext { // reset all io-stats counter to zero void Reset(); - std::string ToString() const; + std::string ToString(bool exclude_zero_counters = false) const; // the thread pool id uint64_t thread_pool_id; diff --git a/src.mk b/src.mk index 8c62167719..f980750288 100644 --- a/src.mk +++ b/src.mk @@ -269,6 +269,7 @@ TEST_BENCH_SOURCES = \ utilities/transactions/transaction_test.cc \ utilities/ttl/ttl_test.cc \ utilities/write_batch_with_index/write_batch_with_index_test.cc \ + util/iostats_context_test.cc \ util/log_write_bench.cc \ util/memenv_test.cc \ util/mock_env_test.cc \ diff --git a/util/iostats_context.cc b/util/iostats_context.cc index 666fddcd10..a3c72db966 100644 --- a/util/iostats_context.cc +++ b/util/iostats_context.cc @@ -31,21 +31,24 @@ void IOStatsContext::Reset() { logger_nanos = 0; } -#define IOSTATS_CONTEXT_OUTPUT(counter) #counter << " = " << counter << ", " +#define IOSTATS_CONTEXT_OUTPUT(counter) \ + if (!exclude_zero_counters || counter > 0) { \ + ss << #counter << " = " << counter << ", "; \ + } -std::string IOStatsContext::ToString() const { +std::string IOStatsContext::ToString(bool exclude_zero_counters) const { std::ostringstream ss; - ss << IOSTATS_CONTEXT_OUTPUT(thread_pool_id) - << IOSTATS_CONTEXT_OUTPUT(bytes_read) - << IOSTATS_CONTEXT_OUTPUT(bytes_written) - << IOSTATS_CONTEXT_OUTPUT(open_nanos) - << IOSTATS_CONTEXT_OUTPUT(allocate_nanos) - << IOSTATS_CONTEXT_OUTPUT(write_nanos) - << IOSTATS_CONTEXT_OUTPUT(read_nanos) - << IOSTATS_CONTEXT_OUTPUT(range_sync_nanos) - << IOSTATS_CONTEXT_OUTPUT(fsync_nanos) - << IOSTATS_CONTEXT_OUTPUT(prepare_write_nanos) - << IOSTATS_CONTEXT_OUTPUT(logger_nanos); + IOSTATS_CONTEXT_OUTPUT(thread_pool_id); + IOSTATS_CONTEXT_OUTPUT(bytes_read); + IOSTATS_CONTEXT_OUTPUT(bytes_written); + IOSTATS_CONTEXT_OUTPUT(open_nanos); + IOSTATS_CONTEXT_OUTPUT(allocate_nanos); + IOSTATS_CONTEXT_OUTPUT(write_nanos); + IOSTATS_CONTEXT_OUTPUT(read_nanos); + IOSTATS_CONTEXT_OUTPUT(range_sync_nanos); + IOSTATS_CONTEXT_OUTPUT(fsync_nanos); + IOSTATS_CONTEXT_OUTPUT(prepare_write_nanos); + IOSTATS_CONTEXT_OUTPUT(logger_nanos); return ss.str(); } diff --git a/util/iostats_context_test.cc b/util/iostats_context_test.cc new file mode 100644 index 0000000000..a2884f8a63 --- /dev/null +++ b/util/iostats_context_test.cc @@ -0,0 +1,29 @@ +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +#include "rocksdb/iostats_context.h" +#include "util/testharness.h" + +namespace rocksdb { + +TEST(IOStatsContextTest, ToString) { + iostats_context.Reset(); + iostats_context.bytes_read = 12345; + + std::string zero_included = iostats_context.ToString(); + ASSERT_NE(std::string::npos, zero_included.find("= 0")); + ASSERT_NE(std::string::npos, zero_included.find("= 12345")); + + std::string zero_excluded = iostats_context.ToString(true); + ASSERT_EQ(std::string::npos, zero_excluded.find("= 0")); + ASSERT_NE(std::string::npos, zero_excluded.find("= 12345")); +} + +} // namespace rocksdb + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}