percpu-stats.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mm/percpu-debug.c
  4. *
  5. * Copyright (C) 2017 Facebook Inc.
  6. * Copyright (C) 2017 Dennis Zhou <[email protected]>
  7. *
  8. * Prints statistics about the percpu allocator and backing chunks.
  9. */
  10. #include <linux/debugfs.h>
  11. #include <linux/list.h>
  12. #include <linux/percpu.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/sort.h>
  15. #include <linux/vmalloc.h>
  16. #include "percpu-internal.h"
  17. #define P(X, Y) \
  18. seq_printf(m, " %-20s: %12lld\n", X, (long long int)Y)
  19. struct percpu_stats pcpu_stats;
  20. struct pcpu_alloc_info pcpu_stats_ai;
  21. static int cmpint(const void *a, const void *b)
  22. {
  23. return *(int *)a - *(int *)b;
  24. }
  25. /*
  26. * Iterates over all chunks to find the max nr_alloc entries.
  27. */
  28. static int find_max_nr_alloc(void)
  29. {
  30. struct pcpu_chunk *chunk;
  31. int slot, max_nr_alloc;
  32. max_nr_alloc = 0;
  33. for (slot = 0; slot < pcpu_nr_slots; slot++)
  34. list_for_each_entry(chunk, &pcpu_chunk_lists[slot], list)
  35. max_nr_alloc = max(max_nr_alloc, chunk->nr_alloc);
  36. return max_nr_alloc;
  37. }
  38. /*
  39. * Prints out chunk state. Fragmentation is considered between
  40. * the beginning of the chunk to the last allocation.
  41. *
  42. * All statistics are in bytes unless stated otherwise.
  43. */
  44. static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
  45. int *buffer)
  46. {
  47. struct pcpu_block_md *chunk_md = &chunk->chunk_md;
  48. int i, last_alloc, as_len, start, end;
  49. int *alloc_sizes, *p;
  50. /* statistics */
  51. int sum_frag = 0, max_frag = 0;
  52. int cur_min_alloc = 0, cur_med_alloc = 0, cur_max_alloc = 0;
  53. alloc_sizes = buffer;
  54. /*
  55. * find_last_bit returns the start value if nothing found.
  56. * Therefore, we must determine if it is a failure of find_last_bit
  57. * and set the appropriate value.
  58. */
  59. last_alloc = find_last_bit(chunk->alloc_map,
  60. pcpu_chunk_map_bits(chunk) -
  61. chunk->end_offset / PCPU_MIN_ALLOC_SIZE - 1);
  62. last_alloc = test_bit(last_alloc, chunk->alloc_map) ?
  63. last_alloc + 1 : 0;
  64. as_len = 0;
  65. start = chunk->start_offset / PCPU_MIN_ALLOC_SIZE;
  66. /*
  67. * If a bit is set in the allocation map, the bound_map identifies
  68. * where the allocation ends. If the allocation is not set, the
  69. * bound_map does not identify free areas as it is only kept accurate
  70. * on allocation, not free.
  71. *
  72. * Positive values are allocations and negative values are free
  73. * fragments.
  74. */
  75. while (start < last_alloc) {
  76. if (test_bit(start, chunk->alloc_map)) {
  77. end = find_next_bit(chunk->bound_map, last_alloc,
  78. start + 1);
  79. alloc_sizes[as_len] = 1;
  80. } else {
  81. end = find_next_bit(chunk->alloc_map, last_alloc,
  82. start + 1);
  83. alloc_sizes[as_len] = -1;
  84. }
  85. alloc_sizes[as_len++] *= (end - start) * PCPU_MIN_ALLOC_SIZE;
  86. start = end;
  87. }
  88. /*
  89. * The negative values are free fragments and thus sorting gives the
  90. * free fragments at the beginning in largest first order.
  91. */
  92. if (as_len > 0) {
  93. sort(alloc_sizes, as_len, sizeof(int), cmpint, NULL);
  94. /* iterate through the unallocated fragments */
  95. for (i = 0, p = alloc_sizes; *p < 0 && i < as_len; i++, p++) {
  96. sum_frag -= *p;
  97. max_frag = max(max_frag, -1 * (*p));
  98. }
  99. cur_min_alloc = alloc_sizes[i];
  100. cur_med_alloc = alloc_sizes[(i + as_len - 1) / 2];
  101. cur_max_alloc = alloc_sizes[as_len - 1];
  102. }
  103. P("nr_alloc", chunk->nr_alloc);
  104. P("max_alloc_size", chunk->max_alloc_size);
  105. P("empty_pop_pages", chunk->nr_empty_pop_pages);
  106. P("first_bit", chunk_md->first_free);
  107. P("free_bytes", chunk->free_bytes);
  108. P("contig_bytes", chunk_md->contig_hint * PCPU_MIN_ALLOC_SIZE);
  109. P("sum_frag", sum_frag);
  110. P("max_frag", max_frag);
  111. P("cur_min_alloc", cur_min_alloc);
  112. P("cur_med_alloc", cur_med_alloc);
  113. P("cur_max_alloc", cur_max_alloc);
  114. seq_putc(m, '\n');
  115. }
  116. static int percpu_stats_show(struct seq_file *m, void *v)
  117. {
  118. struct pcpu_chunk *chunk;
  119. int slot, max_nr_alloc;
  120. int *buffer;
  121. alloc_buffer:
  122. spin_lock_irq(&pcpu_lock);
  123. max_nr_alloc = find_max_nr_alloc();
  124. spin_unlock_irq(&pcpu_lock);
  125. /* there can be at most this many free and allocated fragments */
  126. buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int));
  127. if (!buffer)
  128. return -ENOMEM;
  129. spin_lock_irq(&pcpu_lock);
  130. /* if the buffer allocated earlier is too small */
  131. if (max_nr_alloc < find_max_nr_alloc()) {
  132. spin_unlock_irq(&pcpu_lock);
  133. vfree(buffer);
  134. goto alloc_buffer;
  135. }
  136. #define PL(X) \
  137. seq_printf(m, " %-20s: %12lld\n", #X, (long long int)pcpu_stats_ai.X)
  138. seq_printf(m,
  139. "Percpu Memory Statistics\n"
  140. "Allocation Info:\n"
  141. "----------------------------------------\n");
  142. PL(unit_size);
  143. PL(static_size);
  144. PL(reserved_size);
  145. PL(dyn_size);
  146. PL(atom_size);
  147. PL(alloc_size);
  148. seq_putc(m, '\n');
  149. #undef PL
  150. #define PU(X) \
  151. seq_printf(m, " %-20s: %12llu\n", #X, (unsigned long long)pcpu_stats.X)
  152. seq_printf(m,
  153. "Global Stats:\n"
  154. "----------------------------------------\n");
  155. PU(nr_alloc);
  156. PU(nr_dealloc);
  157. PU(nr_cur_alloc);
  158. PU(nr_max_alloc);
  159. PU(nr_chunks);
  160. PU(nr_max_chunks);
  161. PU(min_alloc_size);
  162. PU(max_alloc_size);
  163. P("empty_pop_pages", pcpu_nr_empty_pop_pages);
  164. seq_putc(m, '\n');
  165. #undef PU
  166. seq_printf(m,
  167. "Per Chunk Stats:\n"
  168. "----------------------------------------\n");
  169. if (pcpu_reserved_chunk) {
  170. seq_puts(m, "Chunk: <- Reserved Chunk\n");
  171. chunk_map_stats(m, pcpu_reserved_chunk, buffer);
  172. }
  173. for (slot = 0; slot < pcpu_nr_slots; slot++) {
  174. list_for_each_entry(chunk, &pcpu_chunk_lists[slot], list) {
  175. if (chunk == pcpu_first_chunk)
  176. seq_puts(m, "Chunk: <- First Chunk\n");
  177. else if (slot == pcpu_to_depopulate_slot)
  178. seq_puts(m, "Chunk (to_depopulate)\n");
  179. else if (slot == pcpu_sidelined_slot)
  180. seq_puts(m, "Chunk (sidelined):\n");
  181. else
  182. seq_puts(m, "Chunk:\n");
  183. chunk_map_stats(m, chunk, buffer);
  184. }
  185. }
  186. spin_unlock_irq(&pcpu_lock);
  187. vfree(buffer);
  188. return 0;
  189. }
  190. DEFINE_SHOW_ATTRIBUTE(percpu_stats);
  191. static int __init init_percpu_stats_debugfs(void)
  192. {
  193. debugfs_create_file("percpu_stats", 0444, NULL, NULL,
  194. &percpu_stats_fops);
  195. return 0;
  196. }
  197. late_initcall(init_percpu_stats_debugfs);