1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 #include <sstream>
7 #include "monitoring/iostats_context_imp.h"
8 #include "rocksdb/env.h"
9 
10 namespace ROCKSDB_NAMESPACE {
11 
12 #ifdef ROCKSDB_SUPPORT_THREAD_LOCAL
13 __thread IOStatsContext iostats_context;
14 #endif
15 
get_iostats_context()16 IOStatsContext* get_iostats_context() {
17 #ifdef ROCKSDB_SUPPORT_THREAD_LOCAL
18   return &iostats_context;
19 #else
20   return nullptr;
21 #endif
22 }
23 
Reset()24 void IOStatsContext::Reset() {
25   thread_pool_id = Env::Priority::TOTAL;
26   bytes_read = 0;
27   bytes_written = 0;
28   open_nanos = 0;
29   allocate_nanos = 0;
30   write_nanos = 0;
31   read_nanos = 0;
32   range_sync_nanos = 0;
33   prepare_write_nanos = 0;
34   fsync_nanos = 0;
35   logger_nanos = 0;
36 }
37 
38 #define IOSTATS_CONTEXT_OUTPUT(counter)         \
39   if (!exclude_zero_counters || counter > 0) {  \
40     ss << #counter << " = " << counter << ", "; \
41   }
42 
ToString(bool exclude_zero_counters) const43 std::string IOStatsContext::ToString(bool exclude_zero_counters) const {
44   std::ostringstream ss;
45   IOSTATS_CONTEXT_OUTPUT(thread_pool_id);
46   IOSTATS_CONTEXT_OUTPUT(bytes_read);
47   IOSTATS_CONTEXT_OUTPUT(bytes_written);
48   IOSTATS_CONTEXT_OUTPUT(open_nanos);
49   IOSTATS_CONTEXT_OUTPUT(allocate_nanos);
50   IOSTATS_CONTEXT_OUTPUT(write_nanos);
51   IOSTATS_CONTEXT_OUTPUT(read_nanos);
52   IOSTATS_CONTEXT_OUTPUT(range_sync_nanos);
53   IOSTATS_CONTEXT_OUTPUT(fsync_nanos);
54   IOSTATS_CONTEXT_OUTPUT(prepare_write_nanos);
55   IOSTATS_CONTEXT_OUTPUT(logger_nanos);
56 
57   std::string str = ss.str();
58   str.erase(str.find_last_not_of(", ") + 1);
59   return str;
60 }
61 
62 }  // namespace ROCKSDB_NAMESPACE
63