1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use libc::{c_int, c_uchar, c_void};
use crate::{ffi, ffi_util::from_cstr, Cache, Error, DB};
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(i32)]
pub enum PerfStatsLevel {
Uninitialized = 0,
Disable,
EnableCount,
EnableTimeExceptForMutex,
EnableTimeAndCPUTimeExceptForMutex,
EnableTime,
OutOfBound,
}
#[derive(Debug, Copy, Clone, PartialEq)]
#[non_exhaustive]
#[repr(i32)]
pub enum PerfMetric {
UserKeyComparisonCount = 0,
BlockCacheHitCount = 1,
BlockReadCount = 2,
BlockReadByte = 3,
BlockReadTime = 4,
BlockChecksumTime = 5,
BlockDecompressTime = 6,
GetReadBytes = 7,
MultigetReadBytes = 8,
IterReadBytes = 9,
InternalKeySkippedCount = 10,
InternalDeleteSkippedCount = 11,
InternalRecentSkippedCount = 12,
InternalMergeCount = 13,
GetSnapshotTime = 14,
GetFromMemtableTime = 15,
GetFromMemtableCount = 16,
GetPostProcessTime = 17,
GetFromOutputFilesTime = 18,
SeekOnMemtableTime = 19,
SeekOnMemtableCount = 20,
NextOnMemtableCount = 21,
PrevOnMemtableCount = 22,
SeekChildSeekTime = 23,
SeekChildSeekCount = 24,
SeekMinHeapTime = 25,
SeekMaxHeapTime = 26,
SeekInternalSeekTime = 27,
FindNextUserEntryTime = 28,
WriteWalTime = 29,
WriteMemtableTime = 30,
WriteDelayTime = 31,
WritePreAndPostProcessTime = 32,
DbMutexLockNanos = 33,
DbConditionWaitNanos = 34,
MergeOperatorTimeNanos = 35,
ReadIndexBlockNanos = 36,
ReadFilterBlockNanos = 37,
NewTableBlockIterNanos = 38,
NewTableIteratorNanos = 39,
BlockSeekNanos = 40,
FindTableNanos = 41,
BloomMemtableHitCount = 42,
BloomMemtableMissCount = 43,
BloomSstHitCount = 44,
BloomSstMissCount = 45,
KeyLockWaitTime = 46,
KeyLockWaitCount = 47,
EnvNewSequentialFileNanos = 48,
EnvNewRandomAccessFileNanos = 49,
EnvNewWritableFileNanos = 50,
EnvReuseWritableFileNanos = 51,
EnvNewRandomRwFileNanos = 52,
EnvNewDirectoryNanos = 53,
EnvFileExistsNanos = 54,
EnvGetChildrenNanos = 55,
EnvGetChildrenFileAttributesNanos = 56,
EnvDeleteFileNanos = 57,
EnvCreateDirNanos = 58,
EnvCreateDirIfMissingNanos = 59,
EnvDeleteDirNanos = 60,
EnvGetFileSizeNanos = 61,
EnvGetFileModificationTimeNanos = 62,
EnvRenameFileNanos = 63,
EnvLinkFileNanos = 64,
EnvLockFileNanos = 65,
EnvUnlockFileNanos = 66,
EnvNewLoggerNanos = 67,
TotalMetricCount = 68,
}
pub fn set_perf_stats(lvl: PerfStatsLevel) {
unsafe {
ffi::rocksdb_set_perf_level(lvl as c_int);
}
}
pub struct PerfContext {
pub(crate) inner: *mut ffi::rocksdb_perfcontext_t,
}
impl Default for PerfContext {
fn default() -> PerfContext {
let ctx = unsafe { ffi::rocksdb_perfcontext_create() };
if ctx.is_null() {
panic!("Could not create Perf Context");
}
PerfContext { inner: ctx }
}
}
impl Drop for PerfContext {
fn drop(&mut self) {
unsafe {
ffi::rocksdb_perfcontext_destroy(self.inner);
}
}
}
impl PerfContext {
pub fn reset(&mut self) {
unsafe {
ffi::rocksdb_perfcontext_reset(self.inner);
}
}
pub fn report(&self, exclude_zero_counters: bool) -> String {
unsafe {
let ptr = ffi::rocksdb_perfcontext_report(self.inner, exclude_zero_counters as c_uchar);
let report = from_cstr(ptr);
libc::free(ptr as *mut c_void);
report
}
}
pub fn metric(&self, id: PerfMetric) -> u64 {
unsafe { ffi::rocksdb_perfcontext_metric(self.inner, id as c_int) }
}
}
pub struct MemoryUsageStats {
pub mem_table_total: u64,
pub mem_table_unflushed: u64,
pub mem_table_readers_total: u64,
pub cache_total: u64,
}
struct MemoryUsage {
inner: *mut ffi::rocksdb_memory_usage_t,
}
impl Drop for MemoryUsage {
fn drop(&mut self) {
unsafe {
ffi::rocksdb_approximate_memory_usage_destroy(self.inner);
}
}
}
impl MemoryUsage {
fn approximate_mem_table_total(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_mem_table_total(self.inner) }
}
fn approximate_mem_table_unflushed(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_mem_table_unflushed(self.inner) }
}
fn approximate_mem_table_readers_total(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_mem_table_readers_total(self.inner) }
}
fn approximate_cache_total(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_cache_total(self.inner) }
}
}
struct MemoryUsageBuilder {
inner: *mut ffi::rocksdb_memory_consumers_t,
}
impl Drop for MemoryUsageBuilder {
fn drop(&mut self) {
unsafe {
ffi::rocksdb_memory_consumers_destroy(self.inner);
}
}
}
impl MemoryUsageBuilder {
fn new() -> Result<Self, Error> {
let mc = unsafe { ffi::rocksdb_memory_consumers_create() };
if mc.is_null() {
Err(Error::new(
"Could not create MemoryUsage builder".to_owned(),
))
} else {
Ok(Self { inner: mc })
}
}
fn add_db(&mut self, db: &DB) {
unsafe {
ffi::rocksdb_memory_consumers_add_db(self.inner, db.inner);
}
}
fn add_cache(&mut self, cache: &Cache) {
unsafe {
ffi::rocksdb_memory_consumers_add_cache(self.inner, cache.0.inner);
}
}
fn build(&self) -> Result<MemoryUsage, Error> {
unsafe {
let mu = ffi_try!(ffi::rocksdb_approximate_memory_usage_create(self.inner));
Ok(MemoryUsage { inner: mu })
}
}
}
pub fn get_memory_usage_stats(
dbs: Option<&[&DB]>,
caches: Option<&[&Cache]>,
) -> Result<MemoryUsageStats, Error> {
let mut builder = MemoryUsageBuilder::new()?;
if let Some(dbs_) = dbs {
dbs_.iter().for_each(|db| builder.add_db(db));
}
if let Some(caches_) = caches {
caches_.iter().for_each(|cache| builder.add_cache(cache));
}
let mu = builder.build()?;
Ok(MemoryUsageStats {
mem_table_total: mu.approximate_mem_table_total(),
mem_table_unflushed: mu.approximate_mem_table_unflushed(),
mem_table_readers_total: mu.approximate_mem_table_readers_total(),
cache_total: mu.approximate_cache_total(),
})
}