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, " %-24s: %8lld\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 # of map entries used.
34 static int find_max_map_used(void)
36 struct pcpu_chunk *chunk;
37 int slot, max_map_used;
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);
48 * Prints out chunk state. Fragmentation is considered between
49 * the beginning of the chunk to the last allocation.
51 static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
54 int i, s_index, last_alloc, alloc_sign, as_len;
57 int sum_frag = 0, max_frag = 0;
58 int cur_min_alloc = 0, cur_med_alloc = 0, cur_max_alloc = 0;
61 s_index = chunk->has_reserved ? 1 : 0;
63 /* find last allocation */
65 for (i = chunk->map_used - 1; i >= s_index; i--) {
66 if (chunk->map[i] & 1) {
72 /* if the chunk is not empty - ignoring reserve */
73 if (last_alloc >= s_index) {
74 as_len = last_alloc + 1 - s_index;
77 * Iterate through chunk map computing size info.
78 * The first bit is overloaded to be a used flag.
79 * negative = free space, positive = allocated
81 for (i = 0, p = chunk->map + s_index; i < as_len; i++, p++) {
82 alloc_sign = (*p & 1) ? 1 : -1;
83 alloc_sizes[i] = alloc_sign *
84 ((p[1] & ~1) - (p[0] & ~1));
87 sort(alloc_sizes, as_len, sizeof(chunk->map[0]), cmpint, NULL);
89 /* Iterate through the unallocated fragements. */
90 for (i = 0, p = alloc_sizes; *p < 0 && i < as_len; i++, p++) {
92 max_frag = max(max_frag, -1 * (*p));
95 cur_min_alloc = alloc_sizes[i];
96 cur_med_alloc = alloc_sizes[(i + as_len - 1) / 2];
97 cur_max_alloc = alloc_sizes[as_len - 1];
100 P("nr_alloc", chunk->nr_alloc);
101 P("max_alloc_size", chunk->max_alloc_size);
102 P("free_size", chunk->free_size);
103 P("contig_hint", chunk->contig_hint);
104 P("sum_frag", sum_frag);
105 P("max_frag", max_frag);
106 P("cur_min_alloc", cur_min_alloc);
107 P("cur_med_alloc", cur_med_alloc);
108 P("cur_max_alloc", cur_max_alloc);
112 static int percpu_stats_show(struct seq_file *m, void *v)
114 struct pcpu_chunk *chunk;
115 int slot, max_map_used;
119 spin_lock_irq(&pcpu_lock);
120 max_map_used = find_max_map_used();
121 spin_unlock_irq(&pcpu_lock);
123 buffer = vmalloc(max_map_used * sizeof(pcpu_first_chunk->map[0]));
127 spin_lock_irq(&pcpu_lock);
129 /* if the buffer allocated earlier is too small */
130 if (max_map_used < find_max_map_used()) {
131 spin_unlock_irq(&pcpu_lock);
137 seq_printf(m, " %-24s: %8lld\n", #X, (long long int)pcpu_stats_ai.X)
140 "Percpu Memory Statistics\n"
142 "----------------------------------------\n");
154 seq_printf(m, " %-18s: %14llu\n", #X, (unsigned long long)pcpu_stats.X)
158 "----------------------------------------\n");
173 "----------------------------------------\n");
175 if (pcpu_reserved_chunk) {
176 seq_puts(m, "Chunk: <- Reserved Chunk\n");
177 chunk_map_stats(m, pcpu_reserved_chunk, buffer);
180 for (slot = 0; slot < pcpu_nr_slots; slot++) {
181 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
182 if (chunk == pcpu_first_chunk) {
183 seq_puts(m, "Chunk: <- First Chunk\n");
184 chunk_map_stats(m, chunk, buffer);
188 seq_puts(m, "Chunk:\n");
189 chunk_map_stats(m, chunk, buffer);
195 spin_unlock_irq(&pcpu_lock);
202 static int percpu_stats_open(struct inode *inode, struct file *filp)
204 return single_open(filp, percpu_stats_show, NULL);
207 static const struct file_operations percpu_stats_fops = {
208 .open = percpu_stats_open,
211 .release = single_release,
214 static int __init init_percpu_stats_debugfs(void)
216 debugfs_create_file("percpu_stats", 0444, NULL, NULL,
222 late_initcall(init_percpu_stats_debugfs);