Skip to content

Commit

Permalink
update the prost generated file and remove malloc_free (#188)
Browse files Browse the repository at this point in the history
* update prost file

Signed-off-by: YangKeao <yangkeao@chunibyo.icu>

* remove malloc free test

Signed-off-by: YangKeao <yangkeao@chunibyo.icu>

Signed-off-by: YangKeao <yangkeao@chunibyo.icu>
  • Loading branch information
YangKeao authored Jan 19, 2023
1 parent 0760534 commit b771d52
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 312 deletions.
109 changes: 0 additions & 109 deletions examples/malloc_hook.rs

This file was deleted.

8 changes: 8 additions & 0 deletions proto/perftools.profiles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// f9f855b960d01b292a3c2642e263e6156d52631e78e0177fe51416ed5bbecc81 proto/profile.proto

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Profile {
/// A description of the samples associated with each Sample.value.
Expand Down Expand Up @@ -65,6 +66,7 @@ pub struct Profile {
pub default_sample_type: i64,
}
/// ValueType describes the semantics and measurement units of a value.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ValueType {
/// Rename it from type to ty to avoid using keyword in Rust.
Expand All @@ -80,6 +82,7 @@ pub struct ValueType {
/// context. The program context is typically a stack trace, perhaps
/// augmented with auxiliary information like the thread-id, some
/// indicator of a higher level request being handled etc.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Sample {
/// The ids recorded here correspond to a Profile.location.id.
Expand All @@ -99,6 +102,7 @@ pub struct Sample {
#[prost(message, repeated, tag = "3")]
pub label: ::prost::alloc::vec::Vec<Label>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Label {
/// Index into string table
Expand All @@ -123,6 +127,7 @@ pub struct Label {
#[prost(int64, tag = "4")]
pub num_unit: i64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Mapping {
/// Unique nonzero id for the mapping.
Expand Down Expand Up @@ -162,6 +167,7 @@ pub struct Mapping {
pub has_inline_frames: bool,
}
/// Describes function and line table debug information.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Location {
/// Unique nonzero id for the location. A profile could use
Expand Down Expand Up @@ -197,6 +203,7 @@ pub struct Location {
#[prost(bool, tag = "5")]
pub is_folded: bool,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Line {
/// The id of the corresponding profile.Function for this line.
Expand All @@ -206,6 +213,7 @@ pub struct Line {
#[prost(int64, tag = "2")]
pub line: i64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Function {
/// Unique nonzero id for the function.
Expand Down
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());
});
}
}

0 comments on commit b771d52

Please sign in to comment.