blob: ef5034a0464e5f3d7acdf07e6bd672804931de98 [file] [log] [blame]
Dennis Zhou30a5b532017-06-19 19:28:31 -04001/*
2 * mm/percpu-debug.c
3 *
4 * Copyright (C) 2017 Facebook Inc.
5 * Copyright (C) 2017 Dennis Zhou <dennisz@fb.com>
6 *
7 * This file is released under the GPLv2.
8 *
9 * Prints statistics about the percpu allocator and backing chunks.
10 */
11#include <linux/debugfs.h>
12#include <linux/list.h>
13#include <linux/percpu.h>
14#include <linux/seq_file.h>
15#include <linux/sort.h>
16#include <linux/vmalloc.h>
17
18#include "percpu-internal.h"
19
20#define P(X, Y) \
Dennis Zhou (Facebook)02459162017-07-15 22:23:07 -040021 seq_printf(m, " %-20s: %12lld\n", X, (long long int)Y)
Dennis Zhou30a5b532017-06-19 19:28:31 -040022
23struct percpu_stats pcpu_stats;
24struct pcpu_alloc_info pcpu_stats_ai;
25
26static int cmpint(const void *a, const void *b)
27{
28 return *(int *)a - *(int *)b;
29}
30
31/*
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070032 * Iterates over all chunks to find the max nr_alloc entries.
Dennis Zhou30a5b532017-06-19 19:28:31 -040033 */
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070034static int find_max_nr_alloc(void)
Dennis Zhou30a5b532017-06-19 19:28:31 -040035{
36 struct pcpu_chunk *chunk;
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070037 int slot, max_nr_alloc;
Dennis Zhou30a5b532017-06-19 19:28:31 -040038
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070039 max_nr_alloc = 0;
Dennis Zhou30a5b532017-06-19 19:28:31 -040040 for (slot = 0; slot < pcpu_nr_slots; slot++)
41 list_for_each_entry(chunk, &pcpu_slot[slot], list)
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070042 max_nr_alloc = max(max_nr_alloc, chunk->nr_alloc);
Dennis Zhou30a5b532017-06-19 19:28:31 -040043
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070044 return max_nr_alloc;
Dennis Zhou30a5b532017-06-19 19:28:31 -040045}
46
47/*
48 * Prints out chunk state. Fragmentation is considered between
49 * the beginning of the chunk to the last allocation.
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070050 *
51 * All statistics are in bytes unless stated otherwise.
Dennis Zhou30a5b532017-06-19 19:28:31 -040052 */
53static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
Dennis Zhou (Facebook)cd6a8842017-07-15 22:23:06 -040054 int *buffer)
Dennis Zhou30a5b532017-06-19 19:28:31 -040055{
Dennis Zhou92c14ca2019-02-26 10:00:08 -080056 struct pcpu_block_md *chunk_md = &chunk->chunk_md;
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070057 int i, last_alloc, as_len, start, end;
Dennis Zhou30a5b532017-06-19 19:28:31 -040058 int *alloc_sizes, *p;
59 /* statistics */
60 int sum_frag = 0, max_frag = 0;
61 int cur_min_alloc = 0, cur_med_alloc = 0, cur_max_alloc = 0;
62
63 alloc_sizes = buffer;
Dennis Zhou30a5b532017-06-19 19:28:31 -040064
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070065 /*
66 * find_last_bit returns the start value if nothing found.
67 * Therefore, we must determine if it is a failure of find_last_bit
68 * and set the appropriate value.
69 */
70 last_alloc = find_last_bit(chunk->alloc_map,
71 pcpu_chunk_map_bits(chunk) -
72 chunk->end_offset / PCPU_MIN_ALLOC_SIZE - 1);
73 last_alloc = test_bit(last_alloc, chunk->alloc_map) ?
74 last_alloc + 1 : 0;
75
76 as_len = 0;
Dennis Zhou2e08d202017-09-27 16:34:59 -050077 start = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070078
79 /*
80 * If a bit is set in the allocation map, the bound_map identifies
81 * where the allocation ends. If the allocation is not set, the
82 * bound_map does not identify free areas as it is only kept accurate
83 * on allocation, not free.
84 *
85 * Positive values are allocations and negative values are free
86 * fragments.
87 */
88 while (start < last_alloc) {
89 if (test_bit(start, chunk->alloc_map)) {
90 end = find_next_bit(chunk->bound_map, last_alloc,
91 start + 1);
92 alloc_sizes[as_len] = 1;
93 } else {
94 end = find_next_bit(chunk->alloc_map, last_alloc,
95 start + 1);
96 alloc_sizes[as_len] = -1;
Dennis Zhou30a5b532017-06-19 19:28:31 -040097 }
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070098
99 alloc_sizes[as_len++] *= (end - start) * PCPU_MIN_ALLOC_SIZE;
100
101 start = end;
Dennis Zhou30a5b532017-06-19 19:28:31 -0400102 }
103
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700104 /*
105 * The negative values are free fragments and thus sorting gives the
106 * free fragments at the beginning in largest first order.
107 */
108 if (as_len > 0) {
109 sort(alloc_sizes, as_len, sizeof(int), cmpint, NULL);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400110
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700111 /* iterate through the unallocated fragments */
Dennis Zhou30a5b532017-06-19 19:28:31 -0400112 for (i = 0, p = alloc_sizes; *p < 0 && i < as_len; i++, p++) {
113 sum_frag -= *p;
114 max_frag = max(max_frag, -1 * (*p));
115 }
116
117 cur_min_alloc = alloc_sizes[i];
118 cur_med_alloc = alloc_sizes[(i + as_len - 1) / 2];
119 cur_max_alloc = alloc_sizes[as_len - 1];
120 }
121
122 P("nr_alloc", chunk->nr_alloc);
123 P("max_alloc_size", chunk->max_alloc_size);
Dennis Zhou (Facebook)0cecf502017-07-24 19:02:08 -0400124 P("empty_pop_pages", chunk->nr_empty_pop_pages);
Dennis Zhou92c14ca2019-02-26 10:00:08 -0800125 P("first_bit", chunk_md->first_free);
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700126 P("free_bytes", chunk->free_bytes);
Dennis Zhou92c14ca2019-02-26 10:00:08 -0800127 P("contig_bytes", chunk_md->contig_hint * PCPU_MIN_ALLOC_SIZE);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400128 P("sum_frag", sum_frag);
129 P("max_frag", max_frag);
130 P("cur_min_alloc", cur_min_alloc);
131 P("cur_med_alloc", cur_med_alloc);
132 P("cur_max_alloc", cur_max_alloc);
133 seq_putc(m, '\n');
134}
135
136static int percpu_stats_show(struct seq_file *m, void *v)
137{
138 struct pcpu_chunk *chunk;
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700139 int slot, max_nr_alloc;
Dennis Zhou (Facebook)cd6a8842017-07-15 22:23:06 -0400140 int *buffer;
Dennis Zhou30a5b532017-06-19 19:28:31 -0400141
142alloc_buffer:
143 spin_lock_irq(&pcpu_lock);
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700144 max_nr_alloc = find_max_nr_alloc();
Dennis Zhou30a5b532017-06-19 19:28:31 -0400145 spin_unlock_irq(&pcpu_lock);
146
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700147 /* there can be at most this many free and allocated fragments */
Kees Cook42bc47b2018-06-12 14:27:11 -0700148 buffer = vmalloc(array_size(sizeof(int), (2 * max_nr_alloc + 1)));
Dennis Zhou30a5b532017-06-19 19:28:31 -0400149 if (!buffer)
150 return -ENOMEM;
151
152 spin_lock_irq(&pcpu_lock);
153
154 /* if the buffer allocated earlier is too small */
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700155 if (max_nr_alloc < find_max_nr_alloc()) {
Dennis Zhou30a5b532017-06-19 19:28:31 -0400156 spin_unlock_irq(&pcpu_lock);
157 vfree(buffer);
158 goto alloc_buffer;
159 }
160
161#define PL(X) \
Dennis Zhou (Facebook)02459162017-07-15 22:23:07 -0400162 seq_printf(m, " %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
Dennis Zhou30a5b532017-06-19 19:28:31 -0400163
164 seq_printf(m,
165 "Percpu Memory Statistics\n"
166 "Allocation Info:\n"
167 "----------------------------------------\n");
168 PL(unit_size);
169 PL(static_size);
170 PL(reserved_size);
171 PL(dyn_size);
172 PL(atom_size);
173 PL(alloc_size);
174 seq_putc(m, '\n');
175
176#undef PL
177
178#define PU(X) \
Dennis Zhou (Facebook)02459162017-07-15 22:23:07 -0400179 seq_printf(m, " %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
Dennis Zhou30a5b532017-06-19 19:28:31 -0400180
181 seq_printf(m,
182 "Global Stats:\n"
183 "----------------------------------------\n");
184 PU(nr_alloc);
185 PU(nr_dealloc);
186 PU(nr_cur_alloc);
187 PU(nr_max_alloc);
188 PU(nr_chunks);
189 PU(nr_max_chunks);
190 PU(min_alloc_size);
191 PU(max_alloc_size);
Dennis Zhou (Facebook)6b9b6f32017-07-15 22:23:08 -0400192 P("empty_pop_pages", pcpu_nr_empty_pop_pages);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400193 seq_putc(m, '\n');
194
195#undef PU
196
197 seq_printf(m,
198 "Per Chunk Stats:\n"
199 "----------------------------------------\n");
200
201 if (pcpu_reserved_chunk) {
202 seq_puts(m, "Chunk: <- Reserved Chunk\n");
203 chunk_map_stats(m, pcpu_reserved_chunk, buffer);
204 }
205
206 for (slot = 0; slot < pcpu_nr_slots; slot++) {
207 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
208 if (chunk == pcpu_first_chunk) {
209 seq_puts(m, "Chunk: <- First Chunk\n");
210 chunk_map_stats(m, chunk, buffer);
211
212
213 } else {
214 seq_puts(m, "Chunk:\n");
215 chunk_map_stats(m, chunk, buffer);
216 }
217
218 }
219 }
220
221 spin_unlock_irq(&pcpu_lock);
222
223 vfree(buffer);
224
225 return 0;
226}
Andy Shevchenko5ad35092018-04-05 16:23:16 -0700227DEFINE_SHOW_ATTRIBUTE(percpu_stats);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400228
229static int __init init_percpu_stats_debugfs(void)
230{
231 debugfs_create_file("percpu_stats", 0444, NULL, NULL,
232 &percpu_stats_fops);
233
234 return 0;
235}
236
237late_initcall(init_percpu_stats_debugfs);