Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * mm/percpu-debug.c
0004  *
0005  * Copyright (C) 2017       Facebook Inc.
0006  * Copyright (C) 2017       Dennis Zhou <dennis@kernel.org>
0007  *
0008  * Prints statistics about the percpu allocator and backing chunks.
0009  */
0010 #include <linux/debugfs.h>
0011 #include <linux/list.h>
0012 #include <linux/percpu.h>
0013 #include <linux/seq_file.h>
0014 #include <linux/sort.h>
0015 #include <linux/vmalloc.h>
0016 
0017 #include "percpu-internal.h"
0018 
0019 #define P(X, Y) \
0020     seq_printf(m, "  %-20s: %12lld\n", X, (long long int)Y)
0021 
0022 struct percpu_stats pcpu_stats;
0023 struct pcpu_alloc_info pcpu_stats_ai;
0024 
0025 static int cmpint(const void *a, const void *b)
0026 {
0027     return *(int *)a - *(int *)b;
0028 }
0029 
0030 /*
0031  * Iterates over all chunks to find the max nr_alloc entries.
0032  */
0033 static int find_max_nr_alloc(void)
0034 {
0035     struct pcpu_chunk *chunk;
0036     int slot, max_nr_alloc;
0037 
0038     max_nr_alloc = 0;
0039     for (slot = 0; slot < pcpu_nr_slots; slot++)
0040         list_for_each_entry(chunk, &pcpu_chunk_lists[slot], list)
0041             max_nr_alloc = max(max_nr_alloc, chunk->nr_alloc);
0042 
0043     return max_nr_alloc;
0044 }
0045 
0046 /*
0047  * Prints out chunk state. Fragmentation is considered between
0048  * the beginning of the chunk to the last allocation.
0049  *
0050  * All statistics are in bytes unless stated otherwise.
0051  */
0052 static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
0053                 int *buffer)
0054 {
0055     struct pcpu_block_md *chunk_md = &chunk->chunk_md;
0056     int i, last_alloc, as_len, start, end;
0057     int *alloc_sizes, *p;
0058     /* statistics */
0059     int sum_frag = 0, max_frag = 0;
0060     int cur_min_alloc = 0, cur_med_alloc = 0, cur_max_alloc = 0;
0061 
0062     alloc_sizes = buffer;
0063 
0064     /*
0065      * find_last_bit returns the start value if nothing found.
0066      * Therefore, we must determine if it is a failure of find_last_bit
0067      * and set the appropriate value.
0068      */
0069     last_alloc = find_last_bit(chunk->alloc_map,
0070                    pcpu_chunk_map_bits(chunk) -
0071                    chunk->end_offset / PCPU_MIN_ALLOC_SIZE - 1);
0072     last_alloc = test_bit(last_alloc, chunk->alloc_map) ?
0073              last_alloc + 1 : 0;
0074 
0075     as_len = 0;
0076     start = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
0077 
0078     /*
0079      * If a bit is set in the allocation map, the bound_map identifies
0080      * where the allocation ends.  If the allocation is not set, the
0081      * bound_map does not identify free areas as it is only kept accurate
0082      * on allocation, not free.
0083      *
0084      * Positive values are allocations and negative values are free
0085      * fragments.
0086      */
0087     while (start < last_alloc) {
0088         if (test_bit(start, chunk->alloc_map)) {
0089             end = find_next_bit(chunk->bound_map, last_alloc,
0090                         start + 1);
0091             alloc_sizes[as_len] = 1;
0092         } else {
0093             end = find_next_bit(chunk->alloc_map, last_alloc,
0094                         start + 1);
0095             alloc_sizes[as_len] = -1;
0096         }
0097 
0098         alloc_sizes[as_len++] *= (end - start) * PCPU_MIN_ALLOC_SIZE;
0099 
0100         start = end;
0101     }
0102 
0103     /*
0104      * The negative values are free fragments and thus sorting gives the
0105      * free fragments at the beginning in largest first order.
0106      */
0107     if (as_len > 0) {
0108         sort(alloc_sizes, as_len, sizeof(int), cmpint, NULL);
0109 
0110         /* iterate through the unallocated fragments */
0111         for (i = 0, p = alloc_sizes; *p < 0 && i < as_len; i++, p++) {
0112             sum_frag -= *p;
0113             max_frag = max(max_frag, -1 * (*p));
0114         }
0115 
0116         cur_min_alloc = alloc_sizes[i];
0117         cur_med_alloc = alloc_sizes[(i + as_len - 1) / 2];
0118         cur_max_alloc = alloc_sizes[as_len - 1];
0119     }
0120 
0121     P("nr_alloc", chunk->nr_alloc);
0122     P("max_alloc_size", chunk->max_alloc_size);
0123     P("empty_pop_pages", chunk->nr_empty_pop_pages);
0124     P("first_bit", chunk_md->first_free);
0125     P("free_bytes", chunk->free_bytes);
0126     P("contig_bytes", chunk_md->contig_hint * PCPU_MIN_ALLOC_SIZE);
0127     P("sum_frag", sum_frag);
0128     P("max_frag", max_frag);
0129     P("cur_min_alloc", cur_min_alloc);
0130     P("cur_med_alloc", cur_med_alloc);
0131     P("cur_max_alloc", cur_max_alloc);
0132     seq_putc(m, '\n');
0133 }
0134 
0135 static int percpu_stats_show(struct seq_file *m, void *v)
0136 {
0137     struct pcpu_chunk *chunk;
0138     int slot, max_nr_alloc;
0139     int *buffer;
0140 
0141 alloc_buffer:
0142     spin_lock_irq(&pcpu_lock);
0143     max_nr_alloc = find_max_nr_alloc();
0144     spin_unlock_irq(&pcpu_lock);
0145 
0146     /* there can be at most this many free and allocated fragments */
0147     buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int));
0148     if (!buffer)
0149         return -ENOMEM;
0150 
0151     spin_lock_irq(&pcpu_lock);
0152 
0153     /* if the buffer allocated earlier is too small */
0154     if (max_nr_alloc < find_max_nr_alloc()) {
0155         spin_unlock_irq(&pcpu_lock);
0156         vfree(buffer);
0157         goto alloc_buffer;
0158     }
0159 
0160 #define PL(X)                               \
0161     seq_printf(m, "  %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
0162 
0163     seq_printf(m,
0164             "Percpu Memory Statistics\n"
0165             "Allocation Info:\n"
0166             "----------------------------------------\n");
0167     PL(unit_size);
0168     PL(static_size);
0169     PL(reserved_size);
0170     PL(dyn_size);
0171     PL(atom_size);
0172     PL(alloc_size);
0173     seq_putc(m, '\n');
0174 
0175 #undef PL
0176 
0177 #define PU(X) \
0178     seq_printf(m, "  %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
0179 
0180     seq_printf(m,
0181             "Global Stats:\n"
0182             "----------------------------------------\n");
0183     PU(nr_alloc);
0184     PU(nr_dealloc);
0185     PU(nr_cur_alloc);
0186     PU(nr_max_alloc);
0187     PU(nr_chunks);
0188     PU(nr_max_chunks);
0189     PU(min_alloc_size);
0190     PU(max_alloc_size);
0191     P("empty_pop_pages", pcpu_nr_empty_pop_pages);
0192     seq_putc(m, '\n');
0193 
0194 #undef PU
0195 
0196     seq_printf(m,
0197             "Per Chunk Stats:\n"
0198             "----------------------------------------\n");
0199 
0200     if (pcpu_reserved_chunk) {
0201         seq_puts(m, "Chunk: <- Reserved Chunk\n");
0202         chunk_map_stats(m, pcpu_reserved_chunk, buffer);
0203     }
0204 
0205     for (slot = 0; slot < pcpu_nr_slots; slot++) {
0206         list_for_each_entry(chunk, &pcpu_chunk_lists[slot], list) {
0207             if (chunk == pcpu_first_chunk)
0208                 seq_puts(m, "Chunk: <- First Chunk\n");
0209             else if (slot == pcpu_to_depopulate_slot)
0210                 seq_puts(m, "Chunk (to_depopulate)\n");
0211             else if (slot == pcpu_sidelined_slot)
0212                 seq_puts(m, "Chunk (sidelined):\n");
0213             else
0214                 seq_puts(m, "Chunk:\n");
0215             chunk_map_stats(m, chunk, buffer);
0216         }
0217     }
0218 
0219     spin_unlock_irq(&pcpu_lock);
0220 
0221     vfree(buffer);
0222 
0223     return 0;
0224 }
0225 DEFINE_SHOW_ATTRIBUTE(percpu_stats);
0226 
0227 static int __init init_percpu_stats_debugfs(void)
0228 {
0229     debugfs_create_file("percpu_stats", 0444, NULL, NULL,
0230             &percpu_stats_fops);
0231 
0232     return 0;
0233 }
0234 
0235 late_initcall(init_percpu_stats_debugfs);