use std::hint::black_box;
use std::thread;
use alloc_tracker::{Allocator, Session};
#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();
fn multithreaded_allocations() {
let num_threads = 4_u32;
let allocations_per_thread = 100_u32;
let handles: Vec<_> = (0..num_threads)
.map(|thread_id| {
thread::spawn(move || {
for i in 0..allocations_per_thread {
let size = thread_id
.checked_add(1)
.expect("addition should not overflow for small test values")
.checked_mul(100)
.expect("multiplication should not overflow for small test values")
.checked_add(i)
.expect("addition should not overflow for small test values")
as usize;
let data = vec![42_u8; size];
black_box(data);
}
})
})
.collect();
for i in 0..50_u32 {
let data = vec![i; 200];
black_box(data);
}
for handle in handles {
handle.join().expect("worker threads do not panic");
}
}
fn main() {
println!("=== Thread vs Process Allocation Tracking ===");
println!();
let session = Session::new();
track_thread_local_allocations(&session);
track_process_wide_allocations(&session);
session.print_to_stdout();
println!();
println!("Note: measure_thread should show much lower allocation counts than measure_process.");
println!("This is because measure_thread only measures the main thread's allocations,");
println!("while measure_process measures allocations from all threads in the process.");
println!("The main thread only allocates a small amount compared to the worker threads.");
}
fn track_thread_local_allocations(session: &Session) {
println!("๐งต Tracking thread-local allocations (3 iterations)");
let thread_op = session.operation("thread_span_multithreaded");
let _span = thread_op.measure_thread().iterations(3);
for _i in 0..3 {
multithreaded_allocations();
}
}
fn track_process_wide_allocations(session: &Session) {
println!("๐ Tracking process-wide allocations (3 iterations)");
let process_op = session.operation("process_span_multithreaded");
let _span = process_op.measure_process().iterations(3);
for _i in 0..3 {
multithreaded_allocations();
}
}