pprof-alloc 0.1.0

Allocation profiling and Linux memory telemetry for Rust services.
Documentation
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
use human_bytes::human_bytes;
use std::fs;
use std::sync::atomic::{AtomicUsize, Ordering};

#[global_allocator]
static GLOBAL: pprof_alloc::PprofAlloc = pprof_alloc::PprofAlloc::new().with_pprof().with_stats();

fn main() {
	let runtime = tokio::runtime::Builder::new_multi_thread()
		.worker_threads(4)
		.thread_name_fn(|| {
			static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
			let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
			format!("test-{id}")
		})
		.enable_all()
		.build()
		.unwrap();
	runtime.block_on(async {
		tokio::task::spawn(main_inner()).await.unwrap();
	});
}

async fn main_inner() {
	println!("Starting allocation patterns example...");
	println!(
		"cgroup memory: {:?}",
		pprof_alloc::stats::cgroups::get_memory()
	);
	// 1. Small vector allocations
	println!("1. Allocating small vectors...");
	let _vectors = allocation_helpers::allocate_small_vectors();

	// 2. Large buffer allocation
	println!("2. Allocating large buffer...");
	let _large_buffer = allocation_helpers::allocate_large_buffer();

	// 3. String allocations
	println!("3. Allocating string data...");
	let _string_data = allocation_helpers::allocate_string_data();

	// 4. Nested structure allocations
	println!("4. Allocating nested structures...");
	let _nested = allocation_helpers::allocate_nested_structures();

	// 5. Immediate allocations and frees
	println!("5. Immediate allocate and free pattern...");
	for _ in 0..10 {
		allocation_helpers::allocate_and_immediately_free();
	}

	// 6. Recursive allocations
	println!("6. Recursive allocations...");
	let _recursive_data = recursive_allocations::recursive_allocation(10);
	let _fib_sequence = recursive_allocations::fibonacci_allocation(20);

	// 7. Concurrent allocations
	println!("7. Concurrent allocations...");
	concurrent_allocations::spawn_allocation_threads();

	// 9. Mixed allocation patterns
	println!("9. Mixed allocation patterns...");
	mixed_allocation_patterns();

	// 10. Long-lived allocations
	println!("10. Long-lived allocations...");
	let _long_lived = create_long_lived_allocations();

	// 11. Async allocations
	println!("11. Async allocations...");
	async_allocations().await;

	// 12. Box, Rc, Arc allocations
	println!("12. Smart pointer allocations...");
	smart_pointer_allocations();

	// 13. Nested call stacks
	println!("13. Nested call stacks...");
	nested_call_stacks();

	// 14. Varied size allocations
	println!("14. Varied size allocations...");
	varied_size_allocations();

	// 15. Spawned task allocations
	println!("15. Spawned task allocations...");
	spawned_task_allocations().await;

	// println!("{:#?}", pprof_alloc::malloc_info());

	drop(_large_buffer);
	// pprof_alloc::generate_fragmentation_map().unwrap();
	let by = pprof_alloc::generate_pprof().unwrap();
	fs::write("/tmp/pprof.memprof", by).unwrap();
	println!("Wrote /tmp/pprof.memprof");

	println!("Allocation patterns example completed.");
	println!("Press Enter to exit (keeping some allocations alive)...");
	let m = pprof_alloc::stats::malloc::info().unwrap();
	let s = pprof_alloc::stats::cgroups::get_stats().unwrap();
	let r = pprof_alloc::stats::smaps::rollup().unwrap();
	println!("malloc: {:#?}", m);
	println!(
		"cgroup memory: {:?}",
		pprof_alloc::stats::cgroups::get_memory()
	);
	println!(
		"cgroup stats: {:?}",
		pprof_alloc::stats::cgroups::get_stats()
	);
	println!("smaps memory: {:?}", pprof_alloc::stats::smaps::rollup());

	println!("malloc max:\t\t{}", bytes(m.system_max()));
	println!("malloc cur:\t\t{}", bytes(m.system_current()));
	println!("malloc total:\t\t{}", bytes(m.total()));
	println!("cg usage:\t\t{}", bytes(s.usage));
	println!("cg working_set:\t\t{}", bytes(s.working_set));
	println!("cg anon:\t\t{}", bytes(s.anon));
	println!("cg inactive_anon:\t{}", bytes(s.inactive_anon));
	println!("cg active_anon:\t\t{}", bytes(s.active_anon));
	println!("cg file:\t\t{}", bytes(s.file));
	println!("cg active_file:\t\t{}", bytes(s.active_file));
	println!("cg inactive_file:\t{}", bytes(s.inactive_file));
	println!("cg kernel:\t\t{}", bytes(s.kernel));

	println!("smaps size:\t\t{}", bytes(r.size));
	println!("smaps rss:\t\t{}", bytes(r.rss));
	println!("smaps pss:\t\t{}", bytes(r.pss));
	println!("smaps pss_dirty:\t{}", bytes(r.pss_dirty));
	println!("smaps shared_clean:\t{}", bytes(r.shared_clean));
	println!("smaps shared_dirty:\t{}", bytes(r.shared_dirty));
	println!("smaps private_clean:\t{}", bytes(r.private_clean));
	println!("smaps private_dirty:\t{}", bytes(r.private_dirty));
	println!("smaps referenced:\t{}", bytes(r.referenced));
	println!("smaps anonymous:\t{}", bytes(r.anonymous));
	let _ = std::io::stdin().read_line(&mut String::new());
}

fn bytes(u: u64) -> String {
	human_bytes(u as f64)
}

mod allocation_helpers {
	use std::collections::HashMap;

	pub fn allocate_small_vectors() -> Vec<Vec<u8>> {
		let mut vectors = Vec::new();
		for i in 0..100 {
			let mut vec = Vec::with_capacity(64);
			for j in 0..64 {
				vec.push((i * j) as u8);
			}
			vectors.push(vec);
		}
		vectors
	}

	pub fn allocate_large_buffer() -> Vec<u8> {
		vec![0u8; 1024 * 1024] // 1MB buffer
	}

	pub fn allocate_string_data() -> String {
		let mut result = String::new();
		for i in 0..1000 {
			result.push_str(&format!("Item {}: {}\n", i, i * 2));
		}
		result
	}

	pub fn allocate_nested_structures() -> HashMap<String, Vec<i32>> {
		let mut map = HashMap::new();
		for i in 0..50 {
			let key = format!("key_{}", i);
			let values: Vec<i32> = (0..100).map(|j| i * j).collect();
			map.insert(key, values);
		}
		map
	}

	pub fn allocate_and_immediately_free() {
		let _temp = vec![0u8; 8192];
		let _temp_string = "This will be freed immediately".to_string();
	}
}

mod recursive_allocations {
	use std::collections::VecDeque;

	pub fn recursive_allocation(depth: usize) -> Vec<i32> {
		if depth == 0 {
			return vec![1, 2, 3, 4, 5];
		}

		let mut result = recursive_allocation(depth - 1);
		result.extend_from_slice(&[depth as i32; 10]);
		result
	}

	pub fn fibonacci_allocation(n: usize) -> VecDeque<usize> {
		let mut sequence = VecDeque::new();
		let mut a = 0;
		let mut b = 1;

		for _ in 0..n {
			sequence.push_back(a);
			let temp = a + b;
			a = b;
			b = temp;
		}
		sequence
	}
}

mod concurrent_allocations {
	use std::sync::{Arc, Mutex};
	use std::thread;
	use std::time::Duration;

	pub fn spawn_allocation_threads() {
		let counter = Arc::new(Mutex::new(0));
		let mut handles = Vec::new();

		for i in 0..4 {
			let counter_clone = Arc::clone(&counter);
			let handle = thread::spawn(move || {
				worker_thread(i, counter_clone);
			});
			handles.push(handle);
		}

		for handle in handles {
			handle.join().unwrap();
		}
	}

	fn worker_thread(id: usize, counter: Arc<Mutex<usize>>) {
		for round in 0..10 {
			// Allocate different sizes based on thread ID and round
			let size = 1024 * (id + 1) * (round + 1);
			let _data = vec![0u8; size];

			// Update shared counter
			{
				let mut count = counter.lock().unwrap();
				*count += 1;
			}

			thread::sleep(Duration::from_millis(10));
		}
	}
}

fn mixed_allocation_patterns() {
	// Mix of different allocation types in the same call stack
	let mut string_map = std::collections::HashMap::new();

	for i in 0..50 {
		let key = format!("mixed_key_{}", i);
		let value = format!("value_with_some_data_{}", i);
		string_map.insert(key, value);

		// Allocate some binary data
		let _binary_data = vec![i as u8; 256];

		// Allocate a small struct
		let small_struct = SmallStruct {
			id: i,
			data: vec![i; 32],
		};
		let _ = small_struct.id + small_struct.data.len();
	}
}

fn create_long_lived_allocations() -> Vec<LongLivedData> {
	let mut data = Vec::new();

	for i in 0..10 {
		let item = LongLivedData {
			id: i,
			buffer: vec![0u8; 1024 * 100], // 10KB each
			metadata: format!("Metadata for item {}", i),
			counters: vec![0; 100],
		};
		let _ = item.id + item.buffer.len() + item.metadata.len() + item.counters.len();
		data.push(item);
	}

	data
}

async fn async_allocations() {
	// Allocate in async context
	let _async_vec = vec![0u8; 2048];

	// Spawn a task that allocates
	let handle = tokio::spawn(async {
		let _task_vec = vec![1u8; 1024];
		std::thread::sleep(std::time::Duration::from_millis(50));
	});

	// Another allocation
	let _another_vec = vec![2u8; 512];

	handle.await.unwrap();
}

fn smart_pointer_allocations() {
	use std::rc::Rc;
	use std::sync::Arc;

	// Box allocations
	let _boxed_int = Box::new(42);
	let _boxed_vec = Box::new(vec![0u8; 256]);

	// Rc allocations
	let _rc_vec = Rc::new(vec![1u8; 128]);
	let _rc_string = Rc::new("Rc string".to_string());

	// Arc allocations
	let _arc_vec = Arc::new(vec![2u8; 64]);
	let _arc_hashmap = Arc::new(std::collections::HashMap::<String, Vec<u8>>::new());
}

fn nested_call_stacks() {
	level1();
}

fn level1() {
	level2();
	let _alloc1 = vec![0u8; 100];
	let _ = _alloc1.capacity();
}

fn level2() {
	level3();
	let _alloc2 = vec![1u8; 200];
	let _ = _alloc2.capacity();
}

fn level3() {
	level4();
	let _alloc3 = vec![2u8; 300];
}

fn level4() {
	let _alloc4 = vec![3u8; 400];
}

fn varied_size_allocations() {
	// Powers of 2
	for i in 0..10 {
		let size = 1 << i; // 1, 2, 4, ..., 512
		let _vec = vec![0u8; size];
	}

	// Random sizes
	use rand::Rng;
	let mut rng = rand::thread_rng();
	for _ in 0..20 {
		let size = rng.gen_range(1..=1000);
		let _vec = vec![0u8; size];
	}

	// Large allocations
	let _large1 = vec![0u8; 10000];
	let _large2 = vec![0u8; 50000];
}

async fn spawned_task_allocations() {
	let mut handles = Vec::new();

	for i in 0..5 {
		let handle = tokio::spawn(async move {
			task_allocation_pattern(i).await;
		});
		handles.push(handle);
	}

	for handle in handles {
		handle.await.unwrap();
	}
}

async fn task_allocation_pattern(id: usize) {
	// Different patterns per task
	match id % 3 {
		0 => {
			let _vec = vec![id as u8; 1000];
			std::thread::sleep(std::time::Duration::from_millis(10));
		},
		1 => {
			let mut map = std::collections::HashMap::new();
			for j in 0..10 {
				map.insert(format!("key_{}_{}", id, j), vec![j as u8; 50]);
			}
		},
		2 => {
			let _string = (0..100).map(|_| "word ").collect::<String>();
		},
		_ => {},
	}
}

#[derive(Debug)]
struct SmallStruct {
	id: usize,
	data: Vec<usize>,
}

#[derive(Debug)]
struct LongLivedData {
	id: usize,
	buffer: Vec<u8>,
	metadata: String,
	counters: Vec<usize>,
}