Skip to content

Commit f2fd0e8

Browse files
Nicolas Pitrecarlescufi
Nicolas Pitre
authored andcommitted
lib/os/heap: make printed heap info more useful
Turn sys_heap_dump() into sys_heap_print_info() to better reflect what it actually does, and improve the information being printed. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent 8779a9e commit f2fd0e8

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

include/sys/sys_heap.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,14 @@ void sys_heap_stress(void *(*alloc)(void *arg, size_t bytes),
201201
int target_percent,
202202
struct z_heap_stress_result *result);
203203

204-
/** @brief Dump heap structure content for debugging to the console
204+
/** @brief Print heap internal structure information to the console
205205
*
206-
* Print information on the heap structure such as its size, chunk buckets
207-
* and chunk list.
206+
* Print information on the heap structure such as its size, chunk buckets,
207+
* chunk list and some statistics for debugging purpose.
208208
*
209209
* @param h Heap to print information about
210+
* @param dump_chunks True to print the entire heap chunk list
210211
*/
211-
void sys_heap_dump(struct sys_heap *h);
212+
void sys_heap_print_info(struct sys_heap *h, bool dump_chunks);
212213

213214
#endif /* ZEPHYR_INCLUDE_SYS_SYS_HEAP_H_ */

lib/os/heap-validate.c

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,41 +314,81 @@ void sys_heap_stress(void *(*alloc)(void *arg, size_t bytes),
314314
}
315315

316316
/*
317-
* Dump heap structure content for debugging / analysis purpose
317+
* Print heap info for debugging / analysis purpose
318318
*/
319-
void heap_dump(struct z_heap *h)
319+
void heap_print_info(struct z_heap *h, bool dump_chunks)
320320
{
321321
int i, nb_buckets = bucket_idx(h, h->len) + 1;
322+
size_t free_bytes, allocated_bytes, total, overhead;
322323

323-
printk("Heap at %p contains %d units\n", chunk_buf(h), h->len);
324+
printk("Heap at %p contains %d units in %d buckets\n\n",
325+
chunk_buf(h), h->len, nb_buckets);
324326

327+
printk(" bucket# min units total largest largest\n"
328+
" threshold chunks (units) (bytes)\n"
329+
" -----------------------------------------------------------\n");
325330
for (i = 0; i < nb_buckets; i++) {
326331
chunkid_t first = h->buckets[i].next;
332+
size_t largest = 0;
327333
int count = 0;
328334

329335
if (first) {
330336
chunkid_t curr = first;
331337
do {
332338
count++;
339+
largest = MAX(largest, chunk_size(h, curr));
333340
curr = next_free_chunk(h, curr);
334341
} while (curr != first);
335342
}
336-
337-
printk("bucket %d (min %d units): %d chunks\n", i,
338-
(1 << i) - 1 + min_chunk_size(h), count);
343+
if (count) {
344+
printk("%9d %12d %12d %12zd %12zd\n",
345+
i, (1 << i) - 1 + min_chunk_size(h), count,
346+
largest, largest * CHUNK_UNIT - chunk_header_bytes(h));
347+
}
339348
}
340349

350+
if (dump_chunks) {
351+
printk("\nChunk dump:\n");
352+
}
353+
free_bytes = allocated_bytes = 0;
341354
for (chunkid_t c = 0; ; c = right_chunk(h, c)) {
342-
printk("chunk %3zd: %c %3zd] %3zd [%zd\n",
343-
c, chunk_used(h, c) ? '*' : '-',
344-
left_chunk(h, c), chunk_size(h, c), right_chunk(h, c));
355+
if (c == 0 || c == h->len) {
356+
/* those are always allocated for internal purposes */
357+
} else if (chunk_used(h, c)) {
358+
allocated_bytes += chunk_size(h, c) * CHUNK_UNIT
359+
- chunk_header_bytes(h);
360+
} else if (!solo_free_header(h, c)) {
361+
free_bytes += chunk_size(h, c) * CHUNK_UNIT
362+
- chunk_header_bytes(h);
363+
}
364+
if (dump_chunks) {
365+
printk("chunk %4zd: [%c] size=%-4zd left=%-4zd right=%zd\n",
366+
c,
367+
chunk_used(h, c) ? '*'
368+
: solo_free_header(h, c) ? '.'
369+
: '-',
370+
chunk_size(h, c),
371+
left_chunk(h, c),
372+
right_chunk(h, c));
373+
}
345374
if (c == h->len) {
346375
break;
347376
}
348377
}
378+
379+
/*
380+
* The final chunk at h->len is just a header serving as a end
381+
* marker. It is part of the overhead.
382+
*/
383+
total = h->len * CHUNK_UNIT + chunk_header_bytes(h);
384+
overhead = total - free_bytes - allocated_bytes;
385+
printk("\n%zd free bytes, %zd allocated bytes, overhead = %zd bytes (%zd.%zd%%)\n",
386+
free_bytes, allocated_bytes, overhead,
387+
(1000 * overhead + total/2) / total / 10,
388+
(1000 * overhead + total/2) / total % 10);
349389
}
350390

351-
void sys_heap_dump(struct sys_heap *heap)
391+
void sys_heap_print_info(struct sys_heap *heap, bool dump_chunks)
352392
{
353-
heap_dump(heap->heap);
393+
heap_print_info(heap->heap, dump_chunks);
354394
}

lib/os/heap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,6 @@ static inline bool size_too_big(struct z_heap *h, size_t bytes)
240240
}
241241

242242
/* For debugging */
243-
void heap_dump(struct z_heap *h);
243+
void heap_print_info(struct z_heap *h, bool dump_chunks);
244244

245245
#endif /* ZEPHYR_INCLUDE_LIB_OS_HEAP_H_ */

0 commit comments

Comments
 (0)