rocksdb/monitoring/iostats_context.cc
Yanqin Jin fd00f39f97 Disable IOStatsContext/PerfContext if no thread local (#8117)
Summary:
Before this PR, `get_iostats_context()` will silently return a nullptr if no thread_local support is detected.
This can be the result of build_detect_platform's failure to compile the simple code snippet on certain platforms, as
reported in https://github.com/facebook/mysql-5.6/issues/904.
To be safe, we should fail the compilation if user does not opt out IOStatsContext and
ROCKSDB_SUPPORT_THREAD_LOCAL is not defined.

If RocksDB relies on c++11, can we just always use thread_local? It turns out there might be
performance concerns (https://github.com/facebook/rocksdb/issues/5774),
which is beyond the scope of this PR. We can revisit this later. Here, we stick to the original impl.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/8117

Reviewed By: ajkr

Differential Revision: D27356847

Pulled By: riversand963

fbshipit-source-id: f7d5776842277598d8341b955febb601946801ae
2021-04-13 07:56:59 -07:00

77 lines
2.2 KiB
C++

// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
#include <sstream>
#include "monitoring/iostats_context_imp.h"
#include "rocksdb/env.h"
namespace ROCKSDB_NAMESPACE {
#ifdef NIOSTATS_CONTEXT
// Should not be used because the counters are not thread-safe.
// Put here just to make get_iostats_context() simple without ifdef.
static IOStatsContext iostats_context;
#elif defined(ROCKSDB_SUPPORT_THREAD_LOCAL)
__thread IOStatsContext iostats_context;
#else
#error \
"No thread-local support. Disable iostats context with -DNIOSTATS_CONTEXT."
#endif
IOStatsContext* get_iostats_context() {
return &iostats_context;
}
void IOStatsContext::Reset() {
#ifndef NIOSTATS_CONTEXT
thread_pool_id = Env::Priority::TOTAL;
bytes_read = 0;
bytes_written = 0;
open_nanos = 0;
allocate_nanos = 0;
write_nanos = 0;
read_nanos = 0;
range_sync_nanos = 0;
prepare_write_nanos = 0;
fsync_nanos = 0;
logger_nanos = 0;
cpu_write_nanos = 0;
cpu_read_nanos = 0;
#endif //! NIOSTATS_CONTEXT
}
#define IOSTATS_CONTEXT_OUTPUT(counter) \
if (!exclude_zero_counters || counter > 0) { \
ss << #counter << " = " << counter << ", "; \
}
std::string IOStatsContext::ToString(bool exclude_zero_counters) const {
#ifdef NIOSTATS_CONTEXT
(void)exclude_zero_counters;
return "";
#else
std::ostringstream 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(cpu_write_nanos);
IOSTATS_CONTEXT_OUTPUT(cpu_read_nanos);
std::string str = ss.str();
str.erase(str.find_last_not_of(", ") + 1);
return str;
#endif //! NIOSTATS_CONTEXT
}
} // namespace ROCKSDB_NAMESPACE