meminfo.c 5.9 KB

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