xref: /openbmc/linux/mm/percpu-stats.c (revision 1a59d1b8)
1 /*
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) \
21 	seq_printf(m, "  %-20s: %12lld\n", X, (long long int)Y)
22 
23 struct percpu_stats pcpu_stats;
24 struct pcpu_alloc_info pcpu_stats_ai;
25 
26 static int cmpint(const void *a, const void *b)
27 {
28 	return *(int *)a - *(int *)b;
29 }
30 
31 /*
32  * Iterates over all chunks to find the max nr_alloc entries.
33  */
34 static int find_max_nr_alloc(void)
35 {
36 	struct pcpu_chunk *chunk;
37 	int slot, max_nr_alloc;
38 
39 	max_nr_alloc = 0;
40 	for (slot = 0; slot < pcpu_nr_slots; slot++)
41 		list_for_each_entry(chunk, &pcpu_slot[slot], list)
42 			max_nr_alloc = max(max_nr_alloc, chunk->nr_alloc);
43 
44 	return max_nr_alloc;
45 }
46 
47 /*
48  * Prints out chunk state. Fragmentation is considered between
49  * the beginning of the chunk to the last allocation.
50  *
51  * All statistics are in bytes unless stated otherwise.
52  */
53 static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
54 			    int *buffer)
55 {
56 	struct pcpu_block_md *chunk_md = &chunk->chunk_md;
57 	int i, last_alloc, as_len, start, end;
58 	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;
64 
65 	/*
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;
77 	start = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
78 
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;
97 		}
98 
99 		alloc_sizes[as_len++] *= (end - start) * PCPU_MIN_ALLOC_SIZE;
100 
101 		start = end;
102 	}
103 
104 	/*
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);
110 
111 		/* iterate through the unallocated fragments */
112 		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);
124 	P("empty_pop_pages", chunk->nr_empty_pop_pages);
125 	P("first_bit", chunk_md->first_free);
126 	P("free_bytes", chunk->free_bytes);
127 	P("contig_bytes", chunk_md->contig_hint * PCPU_MIN_ALLOC_SIZE);
128 	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 
136 static int percpu_stats_show(struct seq_file *m, void *v)
137 {
138 	struct pcpu_chunk *chunk;
139 	int slot, max_nr_alloc;
140 	int *buffer;
141 
142 alloc_buffer:
143 	spin_lock_irq(&pcpu_lock);
144 	max_nr_alloc = find_max_nr_alloc();
145 	spin_unlock_irq(&pcpu_lock);
146 
147 	/* there can be at most this many free and allocated fragments */
148 	buffer = vmalloc(array_size(sizeof(int), (2 * max_nr_alloc + 1)));
149 	if (!buffer)
150 		return -ENOMEM;
151 
152 	spin_lock_irq(&pcpu_lock);
153 
154 	/* if the buffer allocated earlier is too small */
155 	if (max_nr_alloc < find_max_nr_alloc()) {
156 		spin_unlock_irq(&pcpu_lock);
157 		vfree(buffer);
158 		goto alloc_buffer;
159 	}
160 
161 #define PL(X) \
162 	seq_printf(m, "  %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
163 
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) \
179 	seq_printf(m, "  %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
180 
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);
192 	P("empty_pop_pages", pcpu_nr_empty_pop_pages);
193 	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 }
227 DEFINE_SHOW_ATTRIBUTE(percpu_stats);
228 
229 static 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 
237 late_initcall(init_percpu_stats_debugfs);
238