]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/percpu-stats.c
percpu: generalize bitmap (un)populated iterators
[mirror_ubuntu-bionic-kernel.git] / mm / percpu-stats.c
CommitLineData
30a5b536
DZ
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) \
02459164 21 seq_printf(m, " %-20s: %12lld\n", X, (long long int)Y)
30a5b536
DZ
22
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/*
32 * Iterates over all chunks to find the max # of map entries used.
33 */
34static int find_max_map_used(void)
35{
36 struct pcpu_chunk *chunk;
37 int slot, max_map_used;
38
39 max_map_used = 0;
40 for (slot = 0; slot < pcpu_nr_slots; slot++)
41 list_for_each_entry(chunk, &pcpu_slot[slot], list)
42 max_map_used = max(max_map_used, chunk->map_used);
43
44 return max_map_used;
45}
46
47/*
48 * Prints out chunk state. Fragmentation is considered between
49 * the beginning of the chunk to the last allocation.
50 */
51static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
cd6a884d 52 int *buffer)
30a5b536 53{
6b9d7c8e 54 int i, s_index, e_index, last_alloc, alloc_sign, as_len;
30a5b536
DZ
55 int *alloc_sizes, *p;
56 /* statistics */
57 int sum_frag = 0, max_frag = 0;
58 int cur_min_alloc = 0, cur_med_alloc = 0, cur_max_alloc = 0;
59
60 alloc_sizes = buffer;
4af1e6fb 61 s_index = (chunk->start_offset) ? 1 : 0;
6b9d7c8e 62 e_index = chunk->map_used - ((chunk->end_offset) ? 1 : 0);
30a5b536
DZ
63
64 /* find last allocation */
65 last_alloc = -1;
6b9d7c8e 66 for (i = e_index - 1; i >= s_index; i--) {
30a5b536
DZ
67 if (chunk->map[i] & 1) {
68 last_alloc = i;
69 break;
70 }
71 }
72
73 /* if the chunk is not empty - ignoring reserve */
74 if (last_alloc >= s_index) {
75 as_len = last_alloc + 1 - s_index;
76
77 /*
78 * Iterate through chunk map computing size info.
79 * The first bit is overloaded to be a used flag.
80 * negative = free space, positive = allocated
81 */
82 for (i = 0, p = chunk->map + s_index; i < as_len; i++, p++) {
83 alloc_sign = (*p & 1) ? 1 : -1;
84 alloc_sizes[i] = alloc_sign *
85 ((p[1] & ~1) - (p[0] & ~1));
86 }
87
88 sort(alloc_sizes, as_len, sizeof(chunk->map[0]), cmpint, NULL);
89
90 /* Iterate through the unallocated fragements. */
91 for (i = 0, p = alloc_sizes; *p < 0 && i < as_len; i++, p++) {
92 sum_frag -= *p;
93 max_frag = max(max_frag, -1 * (*p));
94 }
95
96 cur_min_alloc = alloc_sizes[i];
97 cur_med_alloc = alloc_sizes[(i + as_len - 1) / 2];
98 cur_max_alloc = alloc_sizes[as_len - 1];
99 }
100
101 P("nr_alloc", chunk->nr_alloc);
102 P("max_alloc_size", chunk->max_alloc_size);
0cecf50c 103 P("empty_pop_pages", chunk->nr_empty_pop_pages);
30a5b536
DZ
104 P("free_size", chunk->free_size);
105 P("contig_hint", chunk->contig_hint);
106 P("sum_frag", sum_frag);
107 P("max_frag", max_frag);
108 P("cur_min_alloc", cur_min_alloc);
109 P("cur_med_alloc", cur_med_alloc);
110 P("cur_max_alloc", cur_max_alloc);
111 seq_putc(m, '\n');
112}
113
114static int percpu_stats_show(struct seq_file *m, void *v)
115{
116 struct pcpu_chunk *chunk;
117 int slot, max_map_used;
cd6a884d 118 int *buffer;
30a5b536
DZ
119
120alloc_buffer:
121 spin_lock_irq(&pcpu_lock);
122 max_map_used = find_max_map_used();
123 spin_unlock_irq(&pcpu_lock);
124
125 buffer = vmalloc(max_map_used * sizeof(pcpu_first_chunk->map[0]));
126 if (!buffer)
127 return -ENOMEM;
128
129 spin_lock_irq(&pcpu_lock);
130
131 /* if the buffer allocated earlier is too small */
132 if (max_map_used < find_max_map_used()) {
133 spin_unlock_irq(&pcpu_lock);
134 vfree(buffer);
135 goto alloc_buffer;
136 }
137
138#define PL(X) \
02459164 139 seq_printf(m, " %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
30a5b536
DZ
140
141 seq_printf(m,
142 "Percpu Memory Statistics\n"
143 "Allocation Info:\n"
144 "----------------------------------------\n");
145 PL(unit_size);
146 PL(static_size);
147 PL(reserved_size);
148 PL(dyn_size);
149 PL(atom_size);
150 PL(alloc_size);
151 seq_putc(m, '\n');
152
153#undef PL
154
155#define PU(X) \
02459164 156 seq_printf(m, " %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
30a5b536
DZ
157
158 seq_printf(m,
159 "Global Stats:\n"
160 "----------------------------------------\n");
161 PU(nr_alloc);
162 PU(nr_dealloc);
163 PU(nr_cur_alloc);
164 PU(nr_max_alloc);
165 PU(nr_chunks);
166 PU(nr_max_chunks);
167 PU(min_alloc_size);
168 PU(max_alloc_size);
6b9b6f39 169 P("empty_pop_pages", pcpu_nr_empty_pop_pages);
30a5b536
DZ
170 seq_putc(m, '\n');
171
172#undef PU
173
174 seq_printf(m,
175 "Per Chunk Stats:\n"
176 "----------------------------------------\n");
177
178 if (pcpu_reserved_chunk) {
179 seq_puts(m, "Chunk: <- Reserved Chunk\n");
180 chunk_map_stats(m, pcpu_reserved_chunk, buffer);
181 }
182
183 for (slot = 0; slot < pcpu_nr_slots; slot++) {
184 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
185 if (chunk == pcpu_first_chunk) {
186 seq_puts(m, "Chunk: <- First Chunk\n");
187 chunk_map_stats(m, chunk, buffer);
188
189
190 } else {
191 seq_puts(m, "Chunk:\n");
192 chunk_map_stats(m, chunk, buffer);
193 }
194
195 }
196 }
197
198 spin_unlock_irq(&pcpu_lock);
199
200 vfree(buffer);
201
202 return 0;
203}
204
205static int percpu_stats_open(struct inode *inode, struct file *filp)
206{
207 return single_open(filp, percpu_stats_show, NULL);
208}
209
210static const struct file_operations percpu_stats_fops = {
211 .open = percpu_stats_open,
212 .read = seq_read,
213 .llseek = seq_lseek,
214 .release = single_release,
215};
216
217static int __init init_percpu_stats_debugfs(void)
218{
219 debugfs_create_file("percpu_stats", 0444, NULL, NULL,
220 &percpu_stats_fops);
221
222 return 0;
223}
224
225late_initcall(init_percpu_stats_debugfs);