1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2017 Facebook Inc.
6 * Copyright (C) 2017 Dennis Zhou <dennis@kernel.org>
8 * Prints statistics about the percpu allocator and backing chunks.
10 #include <linux/debugfs.h>
11 #include <linux/list.h>
12 #include <linux/percpu.h>
13 #include <linux/seq_file.h>
14 #include <linux/sort.h>
15 #include <linux/vmalloc.h>
17 #include "percpu-internal.h"
20 seq_printf(m, " %-20s: %12lld\n", X, (long long int)Y)
22 struct percpu_stats pcpu_stats
;
23 struct pcpu_alloc_info pcpu_stats_ai
;
25 static int cmpint(const void *a
, const void *b
)
27 return *(int *)a
- *(int *)b
;
31 * Iterates over all chunks to find the max nr_alloc entries.
33 static int find_max_nr_alloc(void)
35 struct pcpu_chunk
*chunk
;
36 int slot
, max_nr_alloc
;
39 for (slot
= 0; slot
< pcpu_nr_slots
; slot
++)
40 list_for_each_entry(chunk
, &pcpu_slot
[slot
], list
)
41 max_nr_alloc
= max(max_nr_alloc
, chunk
->nr_alloc
);
47 * Prints out chunk state. Fragmentation is considered between
48 * the beginning of the chunk to the last allocation.
50 * All statistics are in bytes unless stated otherwise.
52 static void chunk_map_stats(struct seq_file
*m
, struct pcpu_chunk
*chunk
,
55 struct pcpu_block_md
*chunk_md
= &chunk
->chunk_md
;
56 int i
, last_alloc
, as_len
, start
, end
;
59 int sum_frag
= 0, max_frag
= 0;
60 int cur_min_alloc
= 0, cur_med_alloc
= 0, cur_max_alloc
= 0;
65 * find_last_bit returns the start value if nothing found.
66 * Therefore, we must determine if it is a failure of find_last_bit
67 * and set the appropriate value.
69 last_alloc
= find_last_bit(chunk
->alloc_map
,
70 pcpu_chunk_map_bits(chunk
) -
71 chunk
->end_offset
/ PCPU_MIN_ALLOC_SIZE
- 1);
72 last_alloc
= test_bit(last_alloc
, chunk
->alloc_map
) ?
76 start
= chunk
->start_offset
/ PCPU_MIN_ALLOC_SIZE
;
79 * If a bit is set in the allocation map, the bound_map identifies
80 * where the allocation ends. If the allocation is not set, the
81 * bound_map does not identify free areas as it is only kept accurate
82 * on allocation, not free.
84 * Positive values are allocations and negative values are free
87 while (start
< last_alloc
) {
88 if (test_bit(start
, chunk
->alloc_map
)) {
89 end
= find_next_bit(chunk
->bound_map
, last_alloc
,
91 alloc_sizes
[as_len
] = 1;
93 end
= find_next_bit(chunk
->alloc_map
, last_alloc
,
95 alloc_sizes
[as_len
] = -1;
98 alloc_sizes
[as_len
++] *= (end
- start
) * PCPU_MIN_ALLOC_SIZE
;
104 * The negative values are free fragments and thus sorting gives the
105 * free fragments at the beginning in largest first order.
108 sort(alloc_sizes
, as_len
, sizeof(int), cmpint
, NULL
);
110 /* iterate through the unallocated fragments */
111 for (i
= 0, p
= alloc_sizes
; *p
< 0 && i
< as_len
; i
++, p
++) {
113 max_frag
= max(max_frag
, -1 * (*p
));
116 cur_min_alloc
= alloc_sizes
[i
];
117 cur_med_alloc
= alloc_sizes
[(i
+ as_len
- 1) / 2];
118 cur_max_alloc
= alloc_sizes
[as_len
- 1];
121 P("nr_alloc", chunk
->nr_alloc
);
122 P("max_alloc_size", chunk
->max_alloc_size
);
123 P("empty_pop_pages", chunk
->nr_empty_pop_pages
);
124 P("first_bit", chunk_md
->first_free
);
125 P("free_bytes", chunk
->free_bytes
);
126 P("contig_bytes", chunk_md
->contig_hint
* PCPU_MIN_ALLOC_SIZE
);
127 P("sum_frag", sum_frag
);
128 P("max_frag", max_frag
);
129 P("cur_min_alloc", cur_min_alloc
);
130 P("cur_med_alloc", cur_med_alloc
);
131 P("cur_max_alloc", cur_max_alloc
);
135 static int percpu_stats_show(struct seq_file
*m
, void *v
)
137 struct pcpu_chunk
*chunk
;
138 int slot
, max_nr_alloc
;
142 spin_lock_irq(&pcpu_lock
);
143 max_nr_alloc
= find_max_nr_alloc();
144 spin_unlock_irq(&pcpu_lock
);
146 /* there can be at most this many free and allocated fragments */
147 buffer
= vmalloc(array_size(sizeof(int), (2 * max_nr_alloc
+ 1)));
151 spin_lock_irq(&pcpu_lock
);
153 /* if the buffer allocated earlier is too small */
154 if (max_nr_alloc
< find_max_nr_alloc()) {
155 spin_unlock_irq(&pcpu_lock
);
161 seq_printf(m, " %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
164 "Percpu Memory Statistics\n"
166 "----------------------------------------\n");
178 seq_printf(m, " %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
182 "----------------------------------------\n");
191 P("empty_pop_pages", pcpu_nr_empty_pop_pages
);
198 "----------------------------------------\n");
200 if (pcpu_reserved_chunk
) {
201 seq_puts(m
, "Chunk: <- Reserved Chunk\n");
202 chunk_map_stats(m
, pcpu_reserved_chunk
, buffer
);
205 for (slot
= 0; slot
< pcpu_nr_slots
; slot
++) {
206 list_for_each_entry(chunk
, &pcpu_slot
[slot
], list
) {
207 if (chunk
== pcpu_first_chunk
) {
208 seq_puts(m
, "Chunk: <- First Chunk\n");
209 chunk_map_stats(m
, chunk
, buffer
);
213 seq_puts(m
, "Chunk:\n");
214 chunk_map_stats(m
, chunk
, buffer
);
220 spin_unlock_irq(&pcpu_lock
);
226 DEFINE_SHOW_ATTRIBUTE(percpu_stats
);
228 static int __init
init_percpu_stats_debugfs(void)
230 debugfs_create_file("percpu_stats", 0444, NULL
, NULL
,
236 late_initcall(init_percpu_stats_debugfs
);