1f08c3bdfSopenharmony_ci#include <stdio.h>
2f08c3bdfSopenharmony_ci#include "allocate.h"
3f08c3bdfSopenharmony_ci#include "linearize.h"
4f08c3bdfSopenharmony_ci#include "storage.h"
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci__DECLARE_ALLOCATOR(struct ptr_list, ptrlist);
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_citypedef void (*get_t)(struct allocator_stats*);
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_cistatic void show_stats(get_t get, struct allocator_stats * tot)
12f08c3bdfSopenharmony_ci{
13f08c3bdfSopenharmony_ci	struct allocator_stats x;
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci	if (get)
16f08c3bdfSopenharmony_ci		get(&x);
17f08c3bdfSopenharmony_ci	else
18f08c3bdfSopenharmony_ci		x = *tot;
19f08c3bdfSopenharmony_ci	fprintf(stderr, "%16s: %8d, %10ld, %10ld, %6.2f%%, %8.2f\n",
20f08c3bdfSopenharmony_ci		x.name, x.allocations, x.useful_bytes, x.total_bytes,
21f08c3bdfSopenharmony_ci		100 * (double) x.useful_bytes / (x.total_bytes ? : 1),
22f08c3bdfSopenharmony_ci		(double) x.useful_bytes / (x.allocations ? : 1));
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	tot->allocations += x.allocations;
25f08c3bdfSopenharmony_ci	tot->useful_bytes += x.useful_bytes;
26f08c3bdfSopenharmony_ci	tot->total_bytes += x.total_bytes;
27f08c3bdfSopenharmony_ci}
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_civoid show_allocation_stats(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	struct allocator_stats tot = { .name = "total", };
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	fprintf(stderr, "%16s: %8s, %10s, %10s, %7s, %8s\n", "allocator", "allocs",
34f08c3bdfSopenharmony_ci		"bytes", "total", "%usage", "average");
35f08c3bdfSopenharmony_ci	show_stats(get_token_stats, &tot);
36f08c3bdfSopenharmony_ci	show_stats(get_ident_stats, &tot);
37f08c3bdfSopenharmony_ci	show_stats(get_symbol_stats, &tot);
38f08c3bdfSopenharmony_ci	show_stats(get_expression_stats, &tot);
39f08c3bdfSopenharmony_ci	show_stats(get_statement_stats, &tot);
40f08c3bdfSopenharmony_ci	show_stats(get_scope_stats, &tot);
41f08c3bdfSopenharmony_ci	show_stats(get_basic_block_stats, &tot);
42f08c3bdfSopenharmony_ci	show_stats(get_instruction_stats, &tot);
43f08c3bdfSopenharmony_ci	show_stats(get_pseudo_stats, &tot);
44f08c3bdfSopenharmony_ci	show_stats(get_pseudo_user_stats, &tot);
45f08c3bdfSopenharmony_ci	show_stats(get_ptrlist_stats, &tot);
46f08c3bdfSopenharmony_ci	show_stats(get_multijmp_stats, &tot);
47f08c3bdfSopenharmony_ci	show_stats(get_asm_rules_stats, &tot);
48f08c3bdfSopenharmony_ci	show_stats(get_asm_constraint_stats, &tot);
49f08c3bdfSopenharmony_ci	show_stats(get_context_stats, &tot);
50f08c3bdfSopenharmony_ci	show_stats(get_string_stats, &tot);
51f08c3bdfSopenharmony_ci	show_stats(get_bytes_stats, &tot);
52f08c3bdfSopenharmony_ci	//show_stats(get_storage_stats, &tot);
53f08c3bdfSopenharmony_ci	//show_stats(get_storage_hash_stats, &tot);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	show_stats(NULL, &tot);
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_civoid report_stats(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	if (fmem_report)
61f08c3bdfSopenharmony_ci		show_allocation_stats();
62f08c3bdfSopenharmony_ci}
63