4 * Copyright (C) 2017 Facebook Inc.
5 * Copyright (C) 2017 Dennis Zhou <dennisz@fb.com>
7 * This file is released under the GPLv2.
9 * Prints statistics about the percpu allocator and backing chunks.
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>
18 #include "percpu-internal.h"
21 seq_printf(m, " %-20s: %12lld\n", X, (long long int)Y)
23 struct percpu_stats pcpu_stats;
24 struct pcpu_alloc_info pcpu_stats_ai;
26 static int cmpint(const void *a, const void *b)
28 return *(int *)a - *(int *)b;
32 * Iterates over all chunks to find the max nr_alloc entries.
34 static int find_max_nr_alloc(void)
36 struct pcpu_chunk *chunk;
37 int slot, max_nr_alloc;
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);
48 * Prints out chunk state. Fragmentation is considered between
49 * the beginning of the chunk to the last allocation.
51 * All statistics are in bytes unless stated otherwise.
53 static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
56 struct pcpu_block_md *chunk_md = &chunk->chunk_md;
57 int i, last_alloc, as_len, start, end;
60 int sum_frag = 0, max_frag = 0;
61 int cur_min_alloc = 0, cur_med_alloc = 0, cur_max_alloc = 0;
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.
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) ?
77 start = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
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.
85 * Positive values are allocations and negative values are free
88 while (start < last_alloc) {
89 if (test_bit(start, chunk->alloc_map)) {
90 end = find_next_bit(chunk->bound_map, last_alloc,
92 alloc_sizes[as_len] = 1;
94 end = find_next_bit(chunk->alloc_map, last_alloc,
96 alloc_sizes[as_len] = -1;
99 alloc_sizes[as_len++] *= (end - start) * PCPU_MIN_ALLOC_SIZE;
105 * The negative values are free fragments and thus sorting gives the
106 * free fragments at the beginning in largest first order.
109 sort(alloc_sizes, as_len, sizeof(int), cmpint, NULL);
111 /* iterate through the unallocated fragments */
112 for (i = 0, p = alloc_sizes; *p < 0 && i < as_len; i++, p++) {
114 max_frag = max(max_frag, -1 * (*p));
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];
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);
136 static int percpu_stats_show(struct seq_file *m, void *v)
138 struct pcpu_chunk *chunk;
139 int slot, max_nr_alloc;
143 spin_lock_irq(&pcpu_lock);
144 max_nr_alloc = find_max_nr_alloc();
145 spin_unlock_irq(&pcpu_lock);
147 /* there can be at most this many free and allocated fragments */
148 buffer = vmalloc(array_size(sizeof(int), (2 * max_nr_alloc + 1)));
152 spin_lock_irq(&pcpu_lock);
154 /* if the buffer allocated earlier is too small */
155 if (max_nr_alloc < find_max_nr_alloc()) {
156 spin_unlock_irq(&pcpu_lock);
162 seq_printf(m, " %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
165 "Percpu Memory Statistics\n"
167 "----------------------------------------\n");
179 seq_printf(m, " %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
183 "----------------------------------------\n");
192 P("empty_pop_pages", pcpu_nr_empty_pop_pages);
199 "----------------------------------------\n");
201 if (pcpu_reserved_chunk) {
202 seq_puts(m, "Chunk: <- Reserved Chunk\n");
203 chunk_map_stats(m, pcpu_reserved_chunk, buffer);
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);
214 seq_puts(m, "Chunk:\n");
215 chunk_map_stats(m, chunk, buffer);
221 spin_unlock_irq(&pcpu_lock);
227 DEFINE_SHOW_ATTRIBUTE(percpu_stats);
229 static int __init init_percpu_stats_debugfs(void)
231 debugfs_create_file("percpu_stats", 0444, NULL, NULL,
237 late_initcall(init_percpu_stats_debugfs);