meminfo.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <linux/fs.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/mm.h>
  5. #include <linux/hugetlb.h>
  6. #include <linux/mman.h>
  7. #include <linux/mmzone.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/quicklist.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/swap.h>
  12. #include <linux/vmstat.h>
  13. #include <linux/atomic.h>
  14. #include <linux/vmalloc.h>
  15. #ifdef CONFIG_CMA
  16. #include <linux/cma.h>
  17. #endif
  18. #include <asm/page.h>
  19. #include <asm/pgtable.h>
  20. #include "internal.h"
  21. void __attribute__((weak)) arch_report_meminfo(struct seq_file *m)
  22. {
  23. }
  24. static int meminfo_proc_show(struct seq_file *m, void *v)
  25. {
  26. struct sysinfo i;
  27. unsigned long committed;
  28. long cached;
  29. long available;
  30. unsigned long pagecache;
  31. unsigned long wmark_low = 0;
  32. unsigned long pages[NR_LRU_LISTS];
  33. struct zone *zone;
  34. int lru;
  35. /*
  36. * display in kilobytes.
  37. */
  38. #define K(x) ((x) << (PAGE_SHIFT - 10))
  39. si_meminfo(&i);
  40. si_swapinfo(&i);
  41. committed = percpu_counter_read_positive(&vm_committed_as);
  42. cached = global_page_state(NR_FILE_PAGES) -
  43. total_swapcache_pages() - i.bufferram;
  44. if (cached < 0)
  45. cached = 0;
  46. for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
  47. pages[lru] = global_page_state(NR_LRU_BASE + lru);
  48. for_each_zone(zone)
  49. wmark_low += zone->watermark[WMARK_LOW];
  50. /*
  51. * Estimate the amount of memory available for userspace allocations,
  52. * without causing swapping.
  53. *
  54. * Free memory cannot be taken below the low watermark, before the
  55. * system starts swapping.
  56. */
  57. available = i.freeram - wmark_low;
  58. /*
  59. * Not all the page cache can be freed, otherwise the system will
  60. * start swapping. Assume at least half of the page cache, or the
  61. * low watermark worth of cache, needs to stay.
  62. */
  63. pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
  64. pagecache -= min(pagecache / 2, wmark_low);
  65. available += pagecache;
  66. /*
  67. * Part of the reclaimable slab consists of items that are in use,
  68. * and cannot be freed. Cap this estimate at the low watermark.
  69. */
  70. available += global_page_state(NR_SLAB_RECLAIMABLE) -
  71. min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low);
  72. if (available < 0)
  73. available = 0;
  74. /*
  75. * Tagged format, for easy grepping and expansion.
  76. */
  77. seq_printf(m,
  78. "MemTotal: %8lu kB\n"
  79. "MemFree: %8lu kB\n"
  80. "MemAvailable: %8lu kB\n"
  81. "Buffers: %8lu kB\n"
  82. "Cached: %8lu kB\n"
  83. "SwapCached: %8lu kB\n"
  84. "Active: %8lu kB\n"
  85. "Inactive: %8lu kB\n"
  86. "Active(anon): %8lu kB\n"
  87. "Inactive(anon): %8lu kB\n"
  88. "Active(file): %8lu kB\n"
  89. "Inactive(file): %8lu kB\n"
  90. "Unevictable: %8lu kB\n"
  91. "Mlocked: %8lu kB\n"
  92. #ifdef CONFIG_HIGHMEM
  93. "HighTotal: %8lu kB\n"
  94. "HighFree: %8lu kB\n"
  95. "LowTotal: %8lu kB\n"
  96. "LowFree: %8lu kB\n"
  97. #endif
  98. #ifndef CONFIG_MMU
  99. "MmapCopy: %8lu kB\n"
  100. #endif
  101. "SwapTotal: %8lu kB\n"
  102. "SwapFree: %8lu kB\n"
  103. "Dirty: %8lu kB\n"
  104. "Writeback: %8lu kB\n"
  105. "AnonPages: %8lu kB\n"
  106. "Mapped: %8lu kB\n"
  107. "Shmem: %8lu kB\n"
  108. "Slab: %8lu kB\n"
  109. "SReclaimable: %8lu kB\n"
  110. "SUnreclaim: %8lu kB\n"
  111. "KernelStack: %8lu kB\n"
  112. "PageTables: %8lu kB\n"
  113. #ifdef CONFIG_QUICKLIST
  114. "Quicklists: %8lu kB\n"
  115. #endif
  116. "NFS_Unstable: %8lu kB\n"
  117. "Bounce: %8lu kB\n"
  118. "WritebackTmp: %8lu kB\n"
  119. "CommitLimit: %8lu kB\n"
  120. "Committed_AS: %8lu kB\n"
  121. "VmallocTotal: %8lu kB\n"
  122. "VmallocUsed: %8lu kB\n"
  123. "VmallocChunk: %8lu kB\n"
  124. #ifdef CONFIG_MEMORY_FAILURE
  125. "HardwareCorrupted: %5lu kB\n"
  126. #endif
  127. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  128. "AnonHugePages: %8lu kB\n"
  129. #endif
  130. #ifdef CONFIG_CMA
  131. "CmaTotal: %8lu kB\n"
  132. "CmaFree: %8lu kB\n"
  133. #endif
  134. ,
  135. K(i.totalram),
  136. K(i.freeram),
  137. K(available),
  138. K(i.bufferram),
  139. K(cached),
  140. K(total_swapcache_pages()),
  141. K(pages[LRU_ACTIVE_ANON] + pages[LRU_ACTIVE_FILE]),
  142. K(pages[LRU_INACTIVE_ANON] + pages[LRU_INACTIVE_FILE]),
  143. K(pages[LRU_ACTIVE_ANON]),
  144. K(pages[LRU_INACTIVE_ANON]),
  145. K(pages[LRU_ACTIVE_FILE]),
  146. K(pages[LRU_INACTIVE_FILE]),
  147. K(pages[LRU_UNEVICTABLE]),
  148. K(global_page_state(NR_MLOCK)),
  149. #ifdef CONFIG_HIGHMEM
  150. K(i.totalhigh),
  151. K(i.freehigh),
  152. K(i.totalram-i.totalhigh),
  153. K(i.freeram-i.freehigh),
  154. #endif
  155. #ifndef CONFIG_MMU
  156. K((unsigned long) atomic_long_read(&mmap_pages_allocated)),
  157. #endif
  158. K(i.totalswap),
  159. K(i.freeswap),
  160. K(global_page_state(NR_FILE_DIRTY)),
  161. K(global_page_state(NR_WRITEBACK)),
  162. K(global_page_state(NR_ANON_PAGES)),
  163. K(global_page_state(NR_FILE_MAPPED)),
  164. K(i.sharedram),
  165. K(global_page_state(NR_SLAB_RECLAIMABLE) +
  166. global_page_state(NR_SLAB_UNRECLAIMABLE)),
  167. K(global_page_state(NR_SLAB_RECLAIMABLE)),
  168. K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
  169. global_page_state(NR_KERNEL_STACK) * THREAD_SIZE / 1024,
  170. K(global_page_state(NR_PAGETABLE)),
  171. #ifdef CONFIG_QUICKLIST
  172. K(quicklist_total_size()),
  173. #endif
  174. K(global_page_state(NR_UNSTABLE_NFS)),
  175. K(global_page_state(NR_BOUNCE)),
  176. K(global_page_state(NR_WRITEBACK_TEMP)),
  177. K(vm_commit_limit()),
  178. K(committed),
  179. (unsigned long)VMALLOC_TOTAL >> 10,
  180. 0ul, // used to be vmalloc 'used'
  181. 0ul // used to be vmalloc 'largest_chunk'
  182. #ifdef CONFIG_MEMORY_FAILURE
  183. , atomic_long_read(&num_poisoned_pages) << (PAGE_SHIFT - 10)
  184. #endif
  185. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  186. , K(global_page_state(NR_ANON_TRANSPARENT_HUGEPAGES) *
  187. HPAGE_PMD_NR)
  188. #endif
  189. #ifdef CONFIG_CMA
  190. , K(totalcma_pages)
  191. , K(global_page_state(NR_FREE_CMA_PAGES))
  192. #endif
  193. );
  194. hugetlb_report_meminfo(m);
  195. arch_report_meminfo(m);
  196. return 0;
  197. #undef K
  198. }
  199. static int meminfo_proc_open(struct inode *inode, struct file *file)
  200. {
  201. return single_open(file, meminfo_proc_show, NULL);
  202. }
  203. static const struct file_operations meminfo_proc_fops = {
  204. .open = meminfo_proc_open,
  205. .read = seq_read,
  206. .llseek = seq_lseek,
  207. .release = single_release,
  208. };
  209. static int __init proc_meminfo_init(void)
  210. {
  211. proc_create("meminfo", 0, NULL, &meminfo_proc_fops);
  212. return 0;
  213. }
  214. fs_initcall(proc_meminfo_init);