Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove malloc free test #190

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 0 additions & 109 deletions examples/malloc_hook.rs

This file was deleted.

87 changes: 0 additions & 87 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,90 +360,3 @@ mod tests {
}
}
}

#[cfg(test)]
#[cfg(target_os = "linux")]
mod malloc_free_test {
use super::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::ffi::c_void;

#[cfg(not(target_env = "gnu"))]
#[allow(clippy::wrong_self_convention)]
#[allow(non_upper_case_globals)]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void> = None;

#[cfg(target_arch = "riscv64")]
#[allow(clippy::wrong_self_convention)]
#[allow(non_upper_case_globals)]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void> = None;

extern "C" {
#[cfg(target_env = "gnu")]
#[cfg(not(target_arch = "riscv64"))]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void>;

fn malloc(size: usize) -> *mut c_void;
}

thread_local! {
static FLAG: RefCell<bool> = RefCell::new(false);
}

extern "C" fn malloc_hook(size: usize) -> *mut c_void {
unsafe {
__malloc_hook = None;
}

FLAG.with(|flag| {
flag.replace(true);
});
let p = unsafe { malloc(size) };

unsafe {
__malloc_hook = Some(malloc_hook);
}

p
}

#[test]
fn malloc_free() {
let mut collector = Collector::new().unwrap();
let mut real_map = BTreeMap::new();

unsafe {
__malloc_hook = Some(malloc_hook);
}

for item in 0..(1 << 10) * 4 {
for _ in 0..(item % 4) {
collector.add(item, 1).unwrap();
}
}
unsafe {
__malloc_hook = None;
}

FLAG.with(|flag| {
assert!(!*flag.borrow());
});

collector.try_iter().unwrap().for_each(|entry| {
test_utils::add_map(&mut real_map, entry);
});

for item in 0..(1 << 10) * 4 {
let count = (item % 4) as isize;
match real_map.get(&item) {
Some(value) => {
assert_eq!(count, *value);
}
None => {
assert_eq!(count, 0);
}
}
}
}
}
116 changes: 0 additions & 116 deletions src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,119 +464,3 @@ impl Profiler {
if let Ok(()) = self.data.add(frames, 1) {}
}
}

#[cfg(test)]
#[cfg(target_os = "linux")]
mod tests {
use super::*;

use std::cell::RefCell;
use std::ffi::c_void;
use std::ptr::null_mut;

#[cfg(not(target_env = "gnu"))]
#[allow(clippy::wrong_self_convention)]
#[allow(non_upper_case_globals)]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void> = None;

#[cfg(target_arch = "riscv64")]
#[allow(clippy::wrong_self_convention)]
#[allow(non_upper_case_globals)]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void> = None;

extern "C" {
#[cfg(target_env = "gnu")]
#[cfg(not(target_arch = "riscv64"))]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void>;

fn malloc(size: usize) -> *mut c_void;
}

thread_local! {
static FLAG: RefCell<bool> = RefCell::new(false);
}

extern "C" fn malloc_hook(size: usize) -> *mut c_void {
unsafe {
__malloc_hook = None;
}

FLAG.with(|flag| {
flag.replace(true);
});
let p = unsafe { malloc(size) };

unsafe {
__malloc_hook = Some(malloc_hook);
}

p
}

#[inline(never)]
fn is_prime_number(v: usize, prime_numbers: &[usize]) -> bool {
if v < 10000 {
let r = prime_numbers.binary_search(&v);
return r.is_ok();
}

for n in prime_numbers {
if v % n == 0 {
return false;
}
}

true
}

#[inline(never)]
fn prepare_prime_numbers() -> Vec<usize> {
// bootstrap: Generate a prime table of 0..10000
let mut prime_number_table: [bool; 10000] = [true; 10000];
prime_number_table[0] = false;
prime_number_table[1] = false;
for i in 2..10000 {
if prime_number_table[i] {
let mut v = i * 2;
while v < 10000 {
prime_number_table[v] = false;
v += i;
}
}
}
let mut prime_numbers = vec![];
for (i, item) in prime_number_table.iter().enumerate().skip(2) {
if *item {
prime_numbers.push(i);
}
}
prime_numbers
}

#[cfg(target_os = "linux")]
#[test]
fn malloc_free() {
trigger_lazy();

let prime_numbers = prepare_prime_numbers();

let mut _v = 0;

unsafe {
__malloc_hook = Some(malloc_hook);
}
for i in 2..50000 {
if is_prime_number(i, &prime_numbers) {
_v += 1;
perf_signal_handler(27, null_mut(), null_mut());
}
}
unsafe {
__malloc_hook = None;
}

FLAG.with(|flag| {
assert!(!*flag.borrow());
});
}
}