percpu-stats.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * mm/percpu-debug.c
  3. *
  4. * Copyright (C) 2017 Facebook Inc.
  5. * Copyright (C) 2017 Dennis Zhou <dennisz@fb.com>
  6. *
  7. * This file is released under the GPLv2.
  8. *
  9. * Prints statistics about the percpu allocator and backing chunks.
  10. */
  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>
  17. #include "percpu-internal.h"
  18. #define P(X, Y) \
  19. seq_printf(m, " %-20s: %12lld\n", X, (long long int)Y)
  20. struct percpu_stats pcpu_stats;
  21. struct pcpu_alloc_info pcpu_stats_ai;
  22. static int cmpint(const void *a, const void *b)
  23. {
  24. return *(int *)a - *(int *)b;
  25. }
  26. /*
  27. * Iterates over all chunks to find the max nr_alloc entries.
  28. */
  29. static int find_max_nr_alloc(void)
  30. {
  31. struct pcpu_chunk *chunk;
  32. int slot, max_nr_alloc;
  33. max_nr_alloc = 0;
  34. for (slot = 0; slot < pcpu_nr_slots; slot++)
  35. list_for_each_entry(chunk, &pcpu_slot[slot], list)
  36. max_nr_alloc = max(max_nr_alloc, chunk->nr_alloc);
  37. return max_nr_alloc;
  38. }
  39. /*
  40. * Prints out chunk state. Fragmentation is considered between
  41. * the beginning of the chunk to the last allocation.
  42. *
  43. * All statistics are in bytes unless stated otherwise.
  44. */
  45. static void chunk_map_stats(struct seq_file *m, struct pcpu_chunk *chunk,
  46. int *buffer)
  47. {
  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->first_bit);
  107. P("free_bytes", chunk->free_bytes);
  108. P("contig_bytes", chunk->contig_bits * 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_size(sizeof(int), (2 * max_nr_alloc + 1)));
  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_slot[slot], list) {
  175. if (chunk == pcpu_first_chunk) {
  176. seq_puts(m, "Chunk: <- First Chunk\n");
  177. chunk_map_stats(m, chunk, buffer);
  178. } else {
  179. seq_puts(m, "Chunk:\n");
  180. chunk_map_stats(m, chunk, buffer);
  181. }
  182. }
  183. }
  184. spin_unlock_irq(&pcpu_lock);
  185. vfree(buffer);
  186. return 0;
  187. }
  188. DEFINE_SHOW_ATTRIBUTE(percpu_stats);
  189. static int __init init_percpu_stats_debugfs(void)
  190. {
  191. debugfs_create_file("percpu_stats", 0444, NULL, NULL,
  192. &percpu_stats_fops);
  193. return 0;
  194. }
  195. late_initcall(init_percpu_stats_debugfs);