percpu-stats.c 6.2 KB

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