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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Key types to index the _MALLCTL NAMESPACE_.
//!
//! The [`Name`] and [`Mib`]/[`MibStr`] types are provided as safe indices into
//! the _MALLCTL NAMESPACE_. These are constructed from null-terminated strings
//! via the [`AsName`] trait. The [`Access`] trait provides provides safe access
//! into the `_MALLCTL NAMESPACE_`.
//!
//! # Example
//!
//! ```
//! #[global_allocator]
//! static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
//!
//! fn main() {
//!     use tikv_jemalloc_ctl::{Access, AsName, Name, Mib};
//!     use libc::{c_uint, c_char};
//!     let name = b"arenas.nbins\0".name();
//!     let nbins: c_uint = name.read().unwrap();
//!     let mut mib: Mib<[usize; 4]> = b"arenas.bin.0.size\0".name().mib().unwrap();
//!     for i in 0..4 {
//!         mib[2] = i;
//!         let bin_size: usize = mib.read().unwrap();
//!         println!("arena bin {} has size {}", i, bin_size);
//!     }
//! }
//! ```

#![allow(clippy::uninlined_format_args)]

use crate::error::Result;
use crate::std::str;
use crate::{fmt, ops, raw};

/// A `Name` in the _MALLCTL NAMESPACE_.
#[repr(transparent)]
#[derive(PartialEq, Eq)]
pub struct Name([u8]);

/// Converts a null-terminated byte-string into a [`Name`].
pub trait AsName {
    /// Converts a null-terminated byte-string into a [`Name`].
    fn name(&self) -> &Name;
}

impl AsName for [u8] {
    fn name(&self) -> &Name {
        assert!(
            !self.is_empty(),
            "cannot create Name from empty byte-string"
        );
        assert_eq!(
            *self.last().unwrap(),
            b'\0',
            "cannot create Name from non-null-terminated byte-string \"{}\"",
            str::from_utf8(self).unwrap()
        );
        unsafe { &*(self as *const Self as *const Name) }
    }
}

impl AsName for str {
    fn name(&self) -> &Name {
        self.as_bytes().name()
    }
}

impl Name {
    /// Returns the [`Mib`] of `self`.
    pub fn mib<T: MibArg>(&self) -> Result<Mib<T>> {
        let mut mib: Mib<T> = Mib::default();
        raw::name_to_mib(&self.0, mib.0.as_mut())?;
        Ok(mib)
    }

    /// Returns the [`MibStr`] of `self` which is a key whose value is a string.
    pub fn mib_str<T: MibArg>(&self) -> Result<MibStr<T>> {
        assert!(
            self.value_type_str(),
            "key \"{}\" does not refer to a string",
            self
        );
        let mut mib: MibStr<T> = MibStr::default();
        raw::name_to_mib(&self.0, mib.0.as_mut())?;
        Ok(mib)
    }

    /// Returns `true` if `self` is a key in the _MALLCTL NAMESPCE_ referring to
    /// a null-terminated string.
    pub fn value_type_str(&self) -> bool {
        // remove the null-terminator:
        let name = self.0.split_at(self.0.len() - 1).0;
        if name.is_empty() {
            return false;
        }
        debug_assert_ne!(*name.last().unwrap(), b'\0');

        match name {
            b"version"
            | b"config.malloc_conf"
            | b"opt.metadata_thp"
            | b"opt.dss"
            | b"opt.percpu_arena"
            | b"opt.stats_print_opts"
            | b"opt.junk"
            | b"opt.thp"
            | b"opt.prof_prefix"
            | b"thread.prof.name"
            | b"prof.dump" => true,
            v if v.starts_with(b"arena.") && v.ends_with(b".dss") => true,
            v if v.starts_with(b"stats.arenas.") && v.ends_with(b".dss") => {
                true
            }
            _ => false,
        }
    }

    /// Returns the name as null-terminated byte-string.
    pub fn as_bytes(&self) -> &'static [u8] {
        unsafe { &*(self as *const Self as *const [u8]) }
    }
}

impl fmt::Debug for Name {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", str::from_utf8(&self.0).unwrap())
    }
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", str::from_utf8(&self.0).unwrap())
    }
}

/// Management Information Base of a non-string value.
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Mib<T: MibArg>(T);

/// Management Information Base of a string value.
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct MibStr<T: MibArg>(T);

impl<T: MibArg> AsRef<[usize]> for Mib<T> {
    fn as_ref(&self) -> &[usize] {
        self.0.as_ref()
    }
}

impl<T: MibArg> AsMut<[usize]> for Mib<T> {
    fn as_mut(&mut self) -> &mut [usize] {
        self.0.as_mut()
    }
}

impl<T: MibArg> ops::Index<usize> for Mib<T> {
    type Output = usize;
    fn index(&self, idx: usize) -> &Self::Output {
        &self.0.as_ref()[idx]
    }
}

impl<T: MibArg> ops::IndexMut<usize> for Mib<T> {
    fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
        &mut self.0.as_mut()[idx]
    }
}

impl<T: MibArg> ops::Index<usize> for MibStr<T> {
    type Output = usize;
    fn index(&self, idx: usize) -> &Self::Output {
        &self.0.as_ref()[idx]
    }
}

impl<T: MibArg> ops::IndexMut<usize> for MibStr<T> {
    fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
        &mut self.0.as_mut()[idx]
    }
}

/// Safe read access to the _MALLCTL NAMESPACE_.
pub trait Access<T> {
    /// Read the key at `self`.
    fn read(&self) -> Result<T>;
    /// Write `value` at the key `self`.
    fn write(&self, value: T) -> Result<()>;
    /// Write `value` at the key `self` returning its previous value.
    fn update(&self, value: T) -> Result<T>;
}

macro_rules! impl_access {
    ($id:ty) => {
        impl<T: MibArg> Access<$id> for Mib<T> {
            fn read(&self) -> Result<$id> {
                unsafe { raw::read_mib(self.0.as_ref()) }
            }
            fn write(&self, value: $id) -> Result<()> {
                unsafe { raw::write_mib(self.0.as_ref(), value) }
            }
            fn update(&self, value: $id) -> Result<$id> {
                unsafe { raw::update_mib(self.0.as_ref(), value) }
            }
        }
        impl Access<$id> for Name {
            fn read(&self) -> Result<$id> {
                unsafe { raw::read(&self.0) }
            }
            fn write(&self, value: $id) -> Result<()> {
                unsafe { raw::write(&self.0, value) }
            }
            fn update(&self, value: $id) -> Result<$id> {
                unsafe { raw::update(&self.0, value) }
            }
        }
    };
}

impl_access!(u32);
impl_access!(u64);
impl_access!(isize);
impl_access!(usize);

impl<T: MibArg> Access<bool> for Mib<T> {
    fn read(&self) -> Result<bool> {
        unsafe {
            let v: u8 = raw::read_mib(self.0.as_ref())?;
            assert!(v == 0 || v == 1);
            Ok(v == 1)
        }
    }
    fn write(&self, value: bool) -> Result<()> {
        unsafe { raw::write_mib(self.0.as_ref(), value) }
    }
    fn update(&self, value: bool) -> Result<bool> {
        unsafe {
            let v: u8 = raw::update_mib(self.0.as_ref(), value as u8)?;
            Ok(v == 1)
        }
    }
}

impl Access<bool> for Name {
    fn read(&self) -> Result<bool> {
        unsafe {
            let v: u8 = raw::read(&self.0)?;
            assert!(v == 0 || v == 1);
            Ok(v == 1)
        }
    }
    fn write(&self, value: bool) -> Result<()> {
        unsafe { raw::write(&self.0, value) }
    }
    fn update(&self, value: bool) -> Result<bool> {
        unsafe {
            let v: u8 = raw::update(&self.0, value as u8)?;
            Ok(v == 1)
        }
    }
}

impl<T: MibArg> Access<&'static [u8]> for MibStr<T> {
    fn read(&self) -> Result<&'static [u8]> {
        // this is safe because the only safe way to construct a `MibStr` is by
        // validating that the key refers to a byte-string value
        unsafe { raw::read_str_mib(self.0.as_ref()) }
    }
    fn write(&self, value: &'static [u8]) -> Result<()> {
        raw::write_str_mib(self.0.as_ref(), value)
    }
    fn update(&self, value: &'static [u8]) -> Result<&'static [u8]> {
        // this is safe because the only safe way to construct a `MibStr` is by
        // validating that the key refers to a byte-string value
        unsafe { raw::update_str_mib(self.0.as_ref(), value) }
    }
}

impl Access<&'static [u8]> for Name {
    fn read(&self) -> Result<&'static [u8]> {
        assert!(
            self.value_type_str(),
            "the name \"{:?}\" does not refer to a byte string",
            self
        );
        // this is safe because the key refers to a byte string:
        unsafe { raw::read_str(&self.0) }
    }
    fn write(&self, value: &'static [u8]) -> Result<()> {
        assert!(
            self.value_type_str(),
            "the name \"{:?}\" does not refer to a byte string",
            self
        );
        raw::write_str(&self.0, value)
    }
    fn update(&self, value: &'static [u8]) -> Result<&'static [u8]> {
        assert!(
            self.value_type_str(),
            "the name \"{:?}\" does not refer to a byte string",
            self
        );
        // this is safe because the key refers to a byte string:
        unsafe { raw::update_str(&self.0, value) }
    }
}

impl<T: MibArg> Access<&'static str> for MibStr<T> {
    fn read(&self) -> Result<&'static str> {
        // this is safe because the only safe way to construct a `MibStr` is by
        // validating that the key refers to a byte-string value
        let s = unsafe { raw::read_str_mib(self.0.as_ref())? };
        Ok(str::from_utf8(s).unwrap())
    }
    fn write(&self, value: &'static str) -> Result<()> {
        raw::write_str_mib(self.0.as_ref(), value.as_bytes())
    }
    fn update(&self, value: &'static str) -> Result<&'static str> {
        // this is safe because the only safe way to construct a `MibStr` is by
        // validating that the key refers to a byte-string value
        let s =
            unsafe { raw::update_str_mib(self.0.as_ref(), value.as_bytes())? };
        Ok(str::from_utf8(s).unwrap())
    }
}

impl Access<&'static str> for Name {
    fn read(&self) -> Result<&'static str> {
        assert!(
            self.value_type_str(),
            "the name \"{:?}\" does not refer to a byte string",
            self
        );
        // this is safe because the key refers to a byte string:
        let s = unsafe { raw::read_str(&self.0)? };
        Ok(str::from_utf8(s).unwrap())
    }
    fn write(&self, value: &'static str) -> Result<()> {
        assert!(
            self.value_type_str(),
            "the name \"{:?}\" does not refer to a byte string",
            self
        );
        raw::write_str(&self.0, value.as_bytes())
    }
    fn update(&self, value: &'static str) -> Result<&'static str> {
        assert!(
            self.value_type_str(),
            "the name \"{:?}\" does not refer to a byte string",
            self
        );
        // this is safe because the key refers to a byte string:
        let s = unsafe { raw::update_str(&self.0, value.as_bytes())? };
        Ok(str::from_utf8(s).unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::{Access, AsName, Mib, MibStr};
    #[test]
    fn bool_rw() {
        let name = b"thread.tcache.enabled\0".name();
        let tcache: bool = name.read().unwrap();

        let new_tcache = !tcache;

        name.write(new_tcache).unwrap();

        let mib: Mib<[usize; 3]> = name.mib().unwrap();
        let r: bool = mib.read().unwrap();
        assert_eq!(r, new_tcache);
    }

    #[test]
    fn u32_r() {
        let name = b"arenas.bin.0.nregs\0".name();
        let v: u32 = name.read().unwrap();

        let mib: Mib<[usize; 4]> = name.mib().unwrap();
        let r: u32 = mib.read().unwrap();
        assert_eq!(r, v);
    }

    #[test]
    fn size_t_r() {
        let name = b"arenas.lextent.0.size\0".name();
        let v: libc::size_t = name.read().unwrap();

        let mib: Mib<[usize; 4]> = name.mib().unwrap();
        let r: libc::size_t = mib.read().unwrap();
        assert_eq!(r, v);
    }

    #[test]
    fn ssize_t_rw() {
        let name = b"arenas.dirty_decay_ms\0".name();
        let v: libc::ssize_t = name.read().unwrap();
        name.write(v).unwrap();

        let mib: Mib<[usize; 2]> = name.mib().unwrap();
        let r: libc::ssize_t = mib.read().unwrap();
        assert_eq!(r, v);
    }

    #[test]
    fn u64_rw() {
        let name = b"epoch\0".name();
        let epoch: u64 = name.read().unwrap();
        name.write(epoch).unwrap();

        let mib: Mib<[usize; 1]> = name.mib().unwrap();
        let epoch: u64 = mib.read().unwrap();
        mib.write(epoch).unwrap();
    }

    #[test]
    fn str_rw() {
        let name = b"arena.0.dss\0".name();
        let dss: &'static [u8] = name.read().unwrap();
        name.write(dss).unwrap();

        let mib: MibStr<[usize; 3]> = name.mib_str().unwrap();
        let dss2: &'static [u8] = mib.read().unwrap();
        mib.write(dss2).unwrap();

        assert_eq!(dss, dss2);
    }
}

pub trait MibArg:
    Copy
    + Clone
    + PartialEq
    + Default
    + fmt::Debug
    + AsRef<[usize]>
    + AsMut<[usize]>
{
}
impl<T> MibArg for T where
    T: Copy
        + Clone
        + PartialEq
        + Default
        + fmt::Debug
        + AsRef<[usize]>
        + AsMut<[usize]>
{
}