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 #pragma once
6 
7 #include <stdint.h>
8 #include <string>
9 
10 #include "rocksdb/perf_level.h"
11 
12 // A thread local context for gathering io-stats efficiently and transparently.
13 // Use SetPerfLevel(PerfLevel::kEnableTime) to enable time stats.
14 
15 namespace ROCKSDB_NAMESPACE {
16 
17 // EXPERIMENTAL: the IO statistics for tiered storage. It matches with each
18 // item in Temperature class.
19 struct FileIOByTemperature {
20   // the number of bytes read to Temperature::kHot file
21   uint64_t hot_file_bytes_read;
22   // the number of bytes read to Temperature::kWarm file
23   uint64_t warm_file_bytes_read;
24   // the number of bytes read to Temperature::kCold file
25   uint64_t cold_file_bytes_read;
26   // total number of reads to Temperature::kHot file
27   uint64_t hot_file_read_count;
28   // total number of reads to Temperature::kWarm file
29   uint64_t warm_file_read_count;
30   // total number of reads to Temperature::kCold file
31   uint64_t cold_file_read_count;
32   // reset all the statistics to 0.
ResetFileIOByTemperature33   void Reset() {
34     hot_file_bytes_read = 0;
35     warm_file_bytes_read = 0;
36     cold_file_bytes_read = 0;
37     hot_file_read_count = 0;
38     warm_file_read_count = 0;
39     cold_file_read_count = 0;
40   }
41 };
42 
43 struct IOStatsContext {
44   // reset all io-stats counter to zero
45   void Reset();
46 
47   std::string ToString(bool exclude_zero_counters = false) const;
48 
49   // the thread pool id
50   uint64_t thread_pool_id;
51 
52   // number of bytes that has been written.
53   uint64_t bytes_written;
54   // number of bytes that has been read.
55   uint64_t bytes_read;
56 
57   // time spent in open() and fopen().
58   uint64_t open_nanos;
59   // time spent in fallocate().
60   uint64_t allocate_nanos;
61   // time spent in write() and pwrite().
62   uint64_t write_nanos;
63   // time spent in read() and pread()
64   uint64_t read_nanos;
65   // time spent in sync_file_range().
66   uint64_t range_sync_nanos;
67   // time spent in fsync
68   uint64_t fsync_nanos;
69   // time spent in preparing write (fallocate etc).
70   uint64_t prepare_write_nanos;
71   // time spent in Logger::Logv().
72   uint64_t logger_nanos;
73   // CPU time spent in write() and pwrite()
74   uint64_t cpu_write_nanos;
75   // CPU time spent in read() and pread()
76   uint64_t cpu_read_nanos;
77 
78   FileIOByTemperature file_io_stats_by_temperature;
79 };
80 
81 // If RocksDB is compiled with -DNIOSTATS_CONTEXT, then a pointer to a global,
82 // non-thread-local IOStatsContext object will be returned. Attempts to update
83 // this object will be ignored, and reading from it will also be no-op.
84 // Otherwise,
85 // a) if thread-local is supported on the platform, then a pointer to
86 //    a thread-local IOStatsContext object will be returned.
87 // b) if thread-local is NOT supported, then compilation will fail.
88 //
89 // This function never returns nullptr.
90 IOStatsContext* get_iostats_context();
91 
92 }  // namespace ROCKSDB_NAMESPACE
93