blob: ad03d73aa5fed3f1d042450433f3416df70fe678 [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 Zhou (Facebook)40064ae2017-07-12 11:27:32 -070056 int i, last_alloc, as_len, start, end;
Dennis Zhou30a5b532017-06-19 19:28:31 -040057 int *alloc_sizes, *p;
58 /* statistics */
59 int sum_frag = 0, max_frag = 0;
60 int cur_min_alloc = 0, cur_med_alloc = 0, cur_max_alloc = 0;
61
62 alloc_sizes = buffer;
Dennis Zhou30a5b532017-06-19 19:28:31 -040063
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070064 /*
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.
68 */
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) ?
73 last_alloc + 1 : 0;
74
75 as_len = 0;
76 start = chunk->start_offset;
77
78 /*
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.
83 *
84 * Positive values are allocations and negative values are free
85 * fragments.
86 */
87 while (start < last_alloc) {
88 if (test_bit(start, chunk->alloc_map)) {
89 end = find_next_bit(chunk->bound_map, last_alloc,
90 start + 1);
91 alloc_sizes[as_len] = 1;
92 } else {
93 end = find_next_bit(chunk->alloc_map, last_alloc,
94 start + 1);
95 alloc_sizes[as_len] = -1;
Dennis Zhou30a5b532017-06-19 19:28:31 -040096 }
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -070097
98 alloc_sizes[as_len++] *= (end - start) * PCPU_MIN_ALLOC_SIZE;
99
100 start = end;
Dennis Zhou30a5b532017-06-19 19:28:31 -0400101 }
102
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700103 /*
104 * The negative values are free fragments and thus sorting gives the
105 * free fragments at the beginning in largest first order.
106 */
107 if (as_len > 0) {
108 sort(alloc_sizes, as_len, sizeof(int), cmpint, NULL);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400109
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700110 /* iterate through the unallocated fragments */
Dennis Zhou30a5b532017-06-19 19:28:31 -0400111 for (i = 0, p = alloc_sizes; *p < 0 && i < as_len; i++, p++) {
112 sum_frag -= *p;
113 max_frag = max(max_frag, -1 * (*p));
114 }
115
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];
119 }
120
121 P("nr_alloc", chunk->nr_alloc);
122 P("max_alloc_size", chunk->max_alloc_size);
Dennis Zhou (Facebook)0cecf502017-07-24 19:02:08 -0400123 P("empty_pop_pages", chunk->nr_empty_pop_pages);
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700124 P("free_bytes", chunk->free_bytes);
125 P("contig_bytes", chunk->contig_bits * PCPU_MIN_ALLOC_SIZE);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400126 P("sum_frag", sum_frag);
127 P("max_frag", max_frag);
128 P("cur_min_alloc", cur_min_alloc);
129 P("cur_med_alloc", cur_med_alloc);
130 P("cur_max_alloc", cur_max_alloc);
131 seq_putc(m, '\n');
132}
133
134static int percpu_stats_show(struct seq_file *m, void *v)
135{
136 struct pcpu_chunk *chunk;
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700137 int slot, max_nr_alloc;
Dennis Zhou (Facebook)cd6a8842017-07-15 22:23:06 -0400138 int *buffer;
Dennis Zhou30a5b532017-06-19 19:28:31 -0400139
140alloc_buffer:
141 spin_lock_irq(&pcpu_lock);
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700142 max_nr_alloc = find_max_nr_alloc();
Dennis Zhou30a5b532017-06-19 19:28:31 -0400143 spin_unlock_irq(&pcpu_lock);
144
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700145 /* there can be at most this many free and allocated fragments */
146 buffer = vmalloc((2 * max_nr_alloc + 1) * sizeof(int));
Dennis Zhou30a5b532017-06-19 19:28:31 -0400147 if (!buffer)
148 return -ENOMEM;
149
150 spin_lock_irq(&pcpu_lock);
151
152 /* if the buffer allocated earlier is too small */
Dennis Zhou (Facebook)40064ae2017-07-12 11:27:32 -0700153 if (max_nr_alloc < find_max_nr_alloc()) {
Dennis Zhou30a5b532017-06-19 19:28:31 -0400154 spin_unlock_irq(&pcpu_lock);
155 vfree(buffer);
156 goto alloc_buffer;
157 }
158
159#define PL(X) \
Dennis Zhou (Facebook)02459162017-07-15 22:23:07 -0400160 seq_printf(m, " %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
Dennis Zhou30a5b532017-06-19 19:28:31 -0400161
162 seq_printf(m,
163 "Percpu Memory Statistics\n"
164 "Allocation Info:\n"
165 "----------------------------------------\n");
166 PL(unit_size);
167 PL(static_size);
168 PL(reserved_size);
169 PL(dyn_size);
170 PL(atom_size);
171 PL(alloc_size);
172 seq_putc(m, '\n');
173
174#undef PL
175
176#define PU(X) \
Dennis Zhou (Facebook)02459162017-07-15 22:23:07 -0400177 seq_printf(m, " %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
Dennis Zhou30a5b532017-06-19 19:28:31 -0400178
179 seq_printf(m,
180 "Global Stats:\n"
181 "----------------------------------------\n");
182 PU(nr_alloc);
183 PU(nr_dealloc);
184 PU(nr_cur_alloc);
185 PU(nr_max_alloc);
186 PU(nr_chunks);
187 PU(nr_max_chunks);
188 PU(min_alloc_size);
189 PU(max_alloc_size);
Dennis Zhou (Facebook)6b9b6f32017-07-15 22:23:08 -0400190 P("empty_pop_pages", pcpu_nr_empty_pop_pages);
Dennis Zhou30a5b532017-06-19 19:28:31 -0400191 seq_putc(m, '\n');
192
193#undef PU
194
195 seq_printf(m,
196 "Per Chunk Stats:\n"
197 "----------------------------------------\n");
198
199 if (pcpu_reserved_chunk) {
200 seq_puts(m, "Chunk: <- Reserved Chunk\n");
201 chunk_map_stats(m, pcpu_reserved_chunk, buffer);
202 }
203
204 for (slot = 0; slot < pcpu_nr_slots; slot++) {
205 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
206 if (chunk == pcpu_first_chunk) {
207 seq_puts(m, "Chunk: <- First Chunk\n");
208 chunk_map_stats(m, chunk, buffer);
209
210
211 } else {
212 seq_puts(m, "Chunk:\n");
213 chunk_map_stats(m, chunk, buffer);
214 }
215
216 }
217 }
218
219 spin_unlock_irq(&pcpu_lock);
220
221 vfree(buffer);
222
223 return 0;
224}
225
226static int percpu_stats_open(struct inode *inode, struct file *filp)
227{
228 return single_open(filp, percpu_stats_show, NULL);
229}
230
231static const struct file_operations percpu_stats_fops = {
232 .open = percpu_stats_open,
233 .read = seq_read,
234 .llseek = seq_lseek,
235 .release = single_release,
236};
237
238static int __init init_percpu_stats_debugfs(void)
239{
240 debugfs_create_file("percpu_stats", 0444, NULL, NULL,
241 &percpu_stats_fops);
242
243 return 0;
244}
245
246late_initcall(init_percpu_stats_debugfs);