|
| 1 | +// Copyright 2024 TiKV Project Authors. Licensed under Apache-2.0. |
| 2 | + |
| 3 | +use collections::HashMap; |
| 4 | +use mock_engine_store::ThreadInfoJealloc; |
| 5 | +use more_asserts::assert_gt; |
| 6 | +use proxy_ffi::jemalloc_utils::{get_allocatep_on_thread_start, get_deallocatep_on_thread_start}; |
| 7 | + |
| 8 | +use crate::utils::v1::*; |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn test_alloc_dealloc() { |
| 12 | + let dummy: Vec<u64> = Vec::with_capacity(100000); |
| 13 | + let ptr_alloc = get_allocatep_on_thread_start(); |
| 14 | + let actual_alloc: &u64 = unsafe { &*(ptr_alloc as *const u64) }; |
| 15 | + let ptr_dealloc = get_deallocatep_on_thread_start(); |
| 16 | + let actual_dealloc: &u64 = unsafe { &*(ptr_dealloc as *const u64) }; |
| 17 | + assert_gt!(*actual_alloc, 100000 * std::mem::size_of::<u64>() as u64); |
| 18 | + let dummy2: Vec<u64> = Vec::with_capacity(100000); |
| 19 | + assert_gt!( |
| 20 | + *actual_alloc, |
| 21 | + 2 * 100000 * std::mem::size_of::<u64>() as u64 |
| 22 | + ); |
| 23 | + drop(dummy); |
| 24 | + assert_gt!(*actual_dealloc, 100000 * std::mem::size_of::<u64>() as u64); |
| 25 | + drop(dummy2); |
| 26 | + assert_gt!( |
| 27 | + *actual_dealloc, |
| 28 | + 2 * 100000 * std::mem::size_of::<u64>() as u64 |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +fn collect_thread_state( |
| 33 | + cluster_ext: &ClusterExt, |
| 34 | + store_id: u64, |
| 35 | +) -> HashMap<String, ThreadInfoJealloc> { |
| 36 | + let mut res: HashMap<String, ThreadInfoJealloc> = Default::default(); |
| 37 | + cluster_ext.iter_ffi_helpers(Some(vec![store_id]), &mut |_, ffi: &mut FFIHelperSet| { |
| 38 | + res = (*ffi.engine_store_server.thread_info_map.lock().expect("")).clone(); |
| 39 | + }); |
| 40 | + res |
| 41 | +} |
| 42 | + |
| 43 | +fn gather(m: &HashMap<String, ThreadInfoJealloc>, pattern: &str) -> i64 { |
| 44 | + m.iter() |
| 45 | + .filter(|(k, _)| k.contains(pattern)) |
| 46 | + .fold(0, |acc, e| acc + e.1.remaining()) |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn test_ffi() { |
| 51 | + let (mut cluster, _pd_client) = new_mock_cluster(0, 1); |
| 52 | + |
| 53 | + let _ = cluster.run(); |
| 54 | + |
| 55 | + let prev = collect_thread_state(&cluster.cluster_ext, 1); |
| 56 | + let before_raftstore = gather(&prev, "raftstore"); |
| 57 | + let before_apply = gather(&prev, "apply"); |
| 58 | + |
| 59 | + for i in 0..10 { |
| 60 | + let k = format!("k{}", i); |
| 61 | + let v = format!("v{}", i); |
| 62 | + cluster.must_put(k.as_bytes(), v.as_bytes()); |
| 63 | + } |
| 64 | + |
| 65 | + for i in 0..10 { |
| 66 | + let k = format!("k{}", i); |
| 67 | + let v = format!("v{}", i); |
| 68 | + check_key(&cluster, k.as_bytes(), v.as_bytes(), Some(true), None, None); |
| 69 | + } |
| 70 | + |
| 71 | + let after = collect_thread_state(&cluster.cluster_ext, 1); |
| 72 | + let after_raftstore = gather(&after, "raftstore"); |
| 73 | + let after_apply = gather(&after, "apply"); |
| 74 | + assert_gt!(after_raftstore, before_raftstore); |
| 75 | + assert_gt!(after_apply, before_apply); |
| 76 | + |
| 77 | + cluster.shutdown(); |
| 78 | +} |
0 commit comments