percpu-stats.c 5.8 KB

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