vmstat.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*
  2. * linux/mm/vmstat.c
  3. *
  4. * Manages VM statistics
  5. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  6. *
  7. * zoned VM statistics
  8. * Copyright (C) 2006 Silicon Graphics, Inc.,
  9. * Christoph Lameter <christoph@lameter.com>
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/cpu.h>
  17. #include <linux/vmstat.h>
  18. #include <linux/sched.h>
  19. #ifdef CONFIG_VM_EVENT_COUNTERS
  20. DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
  21. EXPORT_PER_CPU_SYMBOL(vm_event_states);
  22. static void sum_vm_events(unsigned long *ret, const struct cpumask *cpumask)
  23. {
  24. int cpu;
  25. int i;
  26. memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
  27. for_each_cpu(cpu, cpumask) {
  28. struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
  29. for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
  30. ret[i] += this->event[i];
  31. }
  32. }
  33. /*
  34. * Accumulate the vm event counters across all CPUs.
  35. * The result is unavoidably approximate - it can change
  36. * during and after execution of this function.
  37. */
  38. void all_vm_events(unsigned long *ret)
  39. {
  40. get_online_cpus();
  41. sum_vm_events(ret, cpu_online_mask);
  42. put_online_cpus();
  43. }
  44. EXPORT_SYMBOL_GPL(all_vm_events);
  45. #ifdef CONFIG_HOTPLUG
  46. /*
  47. * Fold the foreign cpu events into our own.
  48. *
  49. * This is adding to the events on one processor
  50. * but keeps the global counts constant.
  51. */
  52. void vm_events_fold_cpu(int cpu)
  53. {
  54. struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
  55. int i;
  56. for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
  57. count_vm_events(i, fold_state->event[i]);
  58. fold_state->event[i] = 0;
  59. }
  60. }
  61. #endif /* CONFIG_HOTPLUG */
  62. #endif /* CONFIG_VM_EVENT_COUNTERS */
  63. /*
  64. * Manage combined zone based / global counters
  65. *
  66. * vm_stat contains the global counters
  67. */
  68. atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
  69. EXPORT_SYMBOL(vm_stat);
  70. #ifdef CONFIG_SMP
  71. static int calculate_threshold(struct zone *zone)
  72. {
  73. int threshold;
  74. int mem; /* memory in 128 MB units */
  75. /*
  76. * The threshold scales with the number of processors and the amount
  77. * of memory per zone. More memory means that we can defer updates for
  78. * longer, more processors could lead to more contention.
  79. * fls() is used to have a cheap way of logarithmic scaling.
  80. *
  81. * Some sample thresholds:
  82. *
  83. * Threshold Processors (fls) Zonesize fls(mem+1)
  84. * ------------------------------------------------------------------
  85. * 8 1 1 0.9-1 GB 4
  86. * 16 2 2 0.9-1 GB 4
  87. * 20 2 2 1-2 GB 5
  88. * 24 2 2 2-4 GB 6
  89. * 28 2 2 4-8 GB 7
  90. * 32 2 2 8-16 GB 8
  91. * 4 2 2 <128M 1
  92. * 30 4 3 2-4 GB 5
  93. * 48 4 3 8-16 GB 8
  94. * 32 8 4 1-2 GB 4
  95. * 32 8 4 0.9-1GB 4
  96. * 10 16 5 <128M 1
  97. * 40 16 5 900M 4
  98. * 70 64 7 2-4 GB 5
  99. * 84 64 7 4-8 GB 6
  100. * 108 512 9 4-8 GB 6
  101. * 125 1024 10 8-16 GB 8
  102. * 125 1024 10 16-32 GB 9
  103. */
  104. mem = zone->present_pages >> (27 - PAGE_SHIFT);
  105. threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
  106. /*
  107. * Maximum threshold is 125
  108. */
  109. threshold = min(125, threshold);
  110. return threshold;
  111. }
  112. /*
  113. * Refresh the thresholds for each zone.
  114. */
  115. static void refresh_zone_stat_thresholds(void)
  116. {
  117. struct zone *zone;
  118. int cpu;
  119. int threshold;
  120. for_each_populated_zone(zone) {
  121. threshold = calculate_threshold(zone);
  122. for_each_online_cpu(cpu)
  123. per_cpu_ptr(zone->pageset, cpu)->stat_threshold
  124. = threshold;
  125. }
  126. }
  127. /*
  128. * For use when we know that interrupts are disabled.
  129. */
  130. void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  131. int delta)
  132. {
  133. struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
  134. s8 *p = pcp->vm_stat_diff + item;
  135. long x;
  136. x = delta + *p;
  137. if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
  138. zone_page_state_add(x, zone, item);
  139. x = 0;
  140. }
  141. *p = x;
  142. }
  143. EXPORT_SYMBOL(__mod_zone_page_state);
  144. /*
  145. * For an unknown interrupt state
  146. */
  147. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  148. int delta)
  149. {
  150. unsigned long flags;
  151. local_irq_save(flags);
  152. __mod_zone_page_state(zone, item, delta);
  153. local_irq_restore(flags);
  154. }
  155. EXPORT_SYMBOL(mod_zone_page_state);
  156. /*
  157. * Optimized increment and decrement functions.
  158. *
  159. * These are only for a single page and therefore can take a struct page *
  160. * argument instead of struct zone *. This allows the inclusion of the code
  161. * generated for page_zone(page) into the optimized functions.
  162. *
  163. * No overflow check is necessary and therefore the differential can be
  164. * incremented or decremented in place which may allow the compilers to
  165. * generate better code.
  166. * The increment or decrement is known and therefore one boundary check can
  167. * be omitted.
  168. *
  169. * NOTE: These functions are very performance sensitive. Change only
  170. * with care.
  171. *
  172. * Some processors have inc/dec instructions that are atomic vs an interrupt.
  173. * However, the code must first determine the differential location in a zone
  174. * based on the processor number and then inc/dec the counter. There is no
  175. * guarantee without disabling preemption that the processor will not change
  176. * in between and therefore the atomicity vs. interrupt cannot be exploited
  177. * in a useful way here.
  178. */
  179. void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  180. {
  181. struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
  182. s8 *p = pcp->vm_stat_diff + item;
  183. (*p)++;
  184. if (unlikely(*p > pcp->stat_threshold)) {
  185. int overstep = pcp->stat_threshold / 2;
  186. zone_page_state_add(*p + overstep, zone, item);
  187. *p = -overstep;
  188. }
  189. }
  190. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  191. {
  192. __inc_zone_state(page_zone(page), item);
  193. }
  194. EXPORT_SYMBOL(__inc_zone_page_state);
  195. void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  196. {
  197. struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
  198. s8 *p = pcp->vm_stat_diff + item;
  199. (*p)--;
  200. if (unlikely(*p < - pcp->stat_threshold)) {
  201. int overstep = pcp->stat_threshold / 2;
  202. zone_page_state_add(*p - overstep, zone, item);
  203. *p = overstep;
  204. }
  205. }
  206. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  207. {
  208. __dec_zone_state(page_zone(page), item);
  209. }
  210. EXPORT_SYMBOL(__dec_zone_page_state);
  211. void inc_zone_state(struct zone *zone, enum zone_stat_item item)
  212. {
  213. unsigned long flags;
  214. local_irq_save(flags);
  215. __inc_zone_state(zone, item);
  216. local_irq_restore(flags);
  217. }
  218. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  219. {
  220. unsigned long flags;
  221. struct zone *zone;
  222. zone = page_zone(page);
  223. local_irq_save(flags);
  224. __inc_zone_state(zone, item);
  225. local_irq_restore(flags);
  226. }
  227. EXPORT_SYMBOL(inc_zone_page_state);
  228. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  229. {
  230. unsigned long flags;
  231. local_irq_save(flags);
  232. __dec_zone_page_state(page, item);
  233. local_irq_restore(flags);
  234. }
  235. EXPORT_SYMBOL(dec_zone_page_state);
  236. /*
  237. * Update the zone counters for one cpu.
  238. *
  239. * The cpu specified must be either the current cpu or a processor that
  240. * is not online. If it is the current cpu then the execution thread must
  241. * be pinned to the current cpu.
  242. *
  243. * Note that refresh_cpu_vm_stats strives to only access
  244. * node local memory. The per cpu pagesets on remote zones are placed
  245. * in the memory local to the processor using that pageset. So the
  246. * loop over all zones will access a series of cachelines local to
  247. * the processor.
  248. *
  249. * The call to zone_page_state_add updates the cachelines with the
  250. * statistics in the remote zone struct as well as the global cachelines
  251. * with the global counters. These could cause remote node cache line
  252. * bouncing and will have to be only done when necessary.
  253. */
  254. void refresh_cpu_vm_stats(int cpu)
  255. {
  256. struct zone *zone;
  257. int i;
  258. int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
  259. for_each_populated_zone(zone) {
  260. struct per_cpu_pageset *p;
  261. p = per_cpu_ptr(zone->pageset, cpu);
  262. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  263. if (p->vm_stat_diff[i]) {
  264. unsigned long flags;
  265. int v;
  266. local_irq_save(flags);
  267. v = p->vm_stat_diff[i];
  268. p->vm_stat_diff[i] = 0;
  269. local_irq_restore(flags);
  270. atomic_long_add(v, &zone->vm_stat[i]);
  271. global_diff[i] += v;
  272. #ifdef CONFIG_NUMA
  273. /* 3 seconds idle till flush */
  274. p->expire = 3;
  275. #endif
  276. }
  277. cond_resched();
  278. #ifdef CONFIG_NUMA
  279. /*
  280. * Deal with draining the remote pageset of this
  281. * processor
  282. *
  283. * Check if there are pages remaining in this pageset
  284. * if not then there is nothing to expire.
  285. */
  286. if (!p->expire || !p->pcp.count)
  287. continue;
  288. /*
  289. * We never drain zones local to this processor.
  290. */
  291. if (zone_to_nid(zone) == numa_node_id()) {
  292. p->expire = 0;
  293. continue;
  294. }
  295. p->expire--;
  296. if (p->expire)
  297. continue;
  298. if (p->pcp.count)
  299. drain_zone_pages(zone, &p->pcp);
  300. #endif
  301. }
  302. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  303. if (global_diff[i])
  304. atomic_long_add(global_diff[i], &vm_stat[i]);
  305. }
  306. #endif
  307. #ifdef CONFIG_NUMA
  308. /*
  309. * zonelist = the list of zones passed to the allocator
  310. * z = the zone from which the allocation occurred.
  311. *
  312. * Must be called with interrupts disabled.
  313. */
  314. void zone_statistics(struct zone *preferred_zone, struct zone *z)
  315. {
  316. if (z->zone_pgdat == preferred_zone->zone_pgdat) {
  317. __inc_zone_state(z, NUMA_HIT);
  318. } else {
  319. __inc_zone_state(z, NUMA_MISS);
  320. __inc_zone_state(preferred_zone, NUMA_FOREIGN);
  321. }
  322. if (z->node == numa_node_id())
  323. __inc_zone_state(z, NUMA_LOCAL);
  324. else
  325. __inc_zone_state(z, NUMA_OTHER);
  326. }
  327. #endif
  328. #ifdef CONFIG_PROC_FS
  329. #include <linux/proc_fs.h>
  330. #include <linux/seq_file.h>
  331. static char * const migratetype_names[MIGRATE_TYPES] = {
  332. "Unmovable",
  333. "Reclaimable",
  334. "Movable",
  335. "Reserve",
  336. "Isolate",
  337. };
  338. static void *frag_start(struct seq_file *m, loff_t *pos)
  339. {
  340. pg_data_t *pgdat;
  341. loff_t node = *pos;
  342. for (pgdat = first_online_pgdat();
  343. pgdat && node;
  344. pgdat = next_online_pgdat(pgdat))
  345. --node;
  346. return pgdat;
  347. }
  348. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  349. {
  350. pg_data_t *pgdat = (pg_data_t *)arg;
  351. (*pos)++;
  352. return next_online_pgdat(pgdat);
  353. }
  354. static void frag_stop(struct seq_file *m, void *arg)
  355. {
  356. }
  357. /* Walk all the zones in a node and print using a callback */
  358. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  359. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  360. {
  361. struct zone *zone;
  362. struct zone *node_zones = pgdat->node_zones;
  363. unsigned long flags;
  364. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  365. if (!populated_zone(zone))
  366. continue;
  367. spin_lock_irqsave(&zone->lock, flags);
  368. print(m, pgdat, zone);
  369. spin_unlock_irqrestore(&zone->lock, flags);
  370. }
  371. }
  372. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  373. struct zone *zone)
  374. {
  375. int order;
  376. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  377. for (order = 0; order < MAX_ORDER; ++order)
  378. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  379. seq_putc(m, '\n');
  380. }
  381. /*
  382. * This walks the free areas for each zone.
  383. */
  384. static int frag_show(struct seq_file *m, void *arg)
  385. {
  386. pg_data_t *pgdat = (pg_data_t *)arg;
  387. walk_zones_in_node(m, pgdat, frag_show_print);
  388. return 0;
  389. }
  390. static void pagetypeinfo_showfree_print(struct seq_file *m,
  391. pg_data_t *pgdat, struct zone *zone)
  392. {
  393. int order, mtype;
  394. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  395. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  396. pgdat->node_id,
  397. zone->name,
  398. migratetype_names[mtype]);
  399. for (order = 0; order < MAX_ORDER; ++order) {
  400. unsigned long freecount = 0;
  401. struct free_area *area;
  402. struct list_head *curr;
  403. area = &(zone->free_area[order]);
  404. list_for_each(curr, &area->free_list[mtype])
  405. freecount++;
  406. seq_printf(m, "%6lu ", freecount);
  407. }
  408. seq_putc(m, '\n');
  409. }
  410. }
  411. /* Print out the free pages at each order for each migatetype */
  412. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  413. {
  414. int order;
  415. pg_data_t *pgdat = (pg_data_t *)arg;
  416. /* Print header */
  417. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  418. for (order = 0; order < MAX_ORDER; ++order)
  419. seq_printf(m, "%6d ", order);
  420. seq_putc(m, '\n');
  421. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  422. return 0;
  423. }
  424. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  425. pg_data_t *pgdat, struct zone *zone)
  426. {
  427. int mtype;
  428. unsigned long pfn;
  429. unsigned long start_pfn = zone->zone_start_pfn;
  430. unsigned long end_pfn = start_pfn + zone->spanned_pages;
  431. unsigned long count[MIGRATE_TYPES] = { 0, };
  432. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  433. struct page *page;
  434. if (!pfn_valid(pfn))
  435. continue;
  436. page = pfn_to_page(pfn);
  437. /* Watch for unexpected holes punched in the memmap */
  438. if (!memmap_valid_within(pfn, page, zone))
  439. continue;
  440. mtype = get_pageblock_migratetype(page);
  441. if (mtype < MIGRATE_TYPES)
  442. count[mtype]++;
  443. }
  444. /* Print counts */
  445. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  446. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  447. seq_printf(m, "%12lu ", count[mtype]);
  448. seq_putc(m, '\n');
  449. }
  450. /* Print out the free pages at each order for each migratetype */
  451. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  452. {
  453. int mtype;
  454. pg_data_t *pgdat = (pg_data_t *)arg;
  455. seq_printf(m, "\n%-23s", "Number of blocks type ");
  456. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  457. seq_printf(m, "%12s ", migratetype_names[mtype]);
  458. seq_putc(m, '\n');
  459. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  460. return 0;
  461. }
  462. /*
  463. * This prints out statistics in relation to grouping pages by mobility.
  464. * It is expensive to collect so do not constantly read the file.
  465. */
  466. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  467. {
  468. pg_data_t *pgdat = (pg_data_t *)arg;
  469. /* check memoryless node */
  470. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  471. return 0;
  472. seq_printf(m, "Page block order: %d\n", pageblock_order);
  473. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  474. seq_putc(m, '\n');
  475. pagetypeinfo_showfree(m, pgdat);
  476. pagetypeinfo_showblockcount(m, pgdat);
  477. return 0;
  478. }
  479. static const struct seq_operations fragmentation_op = {
  480. .start = frag_start,
  481. .next = frag_next,
  482. .stop = frag_stop,
  483. .show = frag_show,
  484. };
  485. static int fragmentation_open(struct inode *inode, struct file *file)
  486. {
  487. return seq_open(file, &fragmentation_op);
  488. }
  489. static const struct file_operations fragmentation_file_operations = {
  490. .open = fragmentation_open,
  491. .read = seq_read,
  492. .llseek = seq_lseek,
  493. .release = seq_release,
  494. };
  495. static const struct seq_operations pagetypeinfo_op = {
  496. .start = frag_start,
  497. .next = frag_next,
  498. .stop = frag_stop,
  499. .show = pagetypeinfo_show,
  500. };
  501. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  502. {
  503. return seq_open(file, &pagetypeinfo_op);
  504. }
  505. static const struct file_operations pagetypeinfo_file_ops = {
  506. .open = pagetypeinfo_open,
  507. .read = seq_read,
  508. .llseek = seq_lseek,
  509. .release = seq_release,
  510. };
  511. #ifdef CONFIG_ZONE_DMA
  512. #define TEXT_FOR_DMA(xx) xx "_dma",
  513. #else
  514. #define TEXT_FOR_DMA(xx)
  515. #endif
  516. #ifdef CONFIG_ZONE_DMA32
  517. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  518. #else
  519. #define TEXT_FOR_DMA32(xx)
  520. #endif
  521. #ifdef CONFIG_HIGHMEM
  522. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  523. #else
  524. #define TEXT_FOR_HIGHMEM(xx)
  525. #endif
  526. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  527. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  528. static const char * const vmstat_text[] = {
  529. /* Zoned VM counters */
  530. "nr_free_pages",
  531. "nr_inactive_anon",
  532. "nr_active_anon",
  533. "nr_inactive_file",
  534. "nr_active_file",
  535. "nr_unevictable",
  536. "nr_mlock",
  537. "nr_anon_pages",
  538. "nr_mapped",
  539. "nr_file_pages",
  540. "nr_dirty",
  541. "nr_writeback",
  542. "nr_slab_reclaimable",
  543. "nr_slab_unreclaimable",
  544. "nr_page_table_pages",
  545. "nr_kernel_stack",
  546. "nr_unstable",
  547. "nr_bounce",
  548. "nr_vmscan_write",
  549. "nr_writeback_temp",
  550. "nr_isolated_anon",
  551. "nr_isolated_file",
  552. "nr_shmem",
  553. #ifdef CONFIG_NUMA
  554. "numa_hit",
  555. "numa_miss",
  556. "numa_foreign",
  557. "numa_interleave",
  558. "numa_local",
  559. "numa_other",
  560. #endif
  561. #ifdef CONFIG_VM_EVENT_COUNTERS
  562. "pgpgin",
  563. "pgpgout",
  564. "pswpin",
  565. "pswpout",
  566. TEXTS_FOR_ZONES("pgalloc")
  567. "pgfree",
  568. "pgactivate",
  569. "pgdeactivate",
  570. "pgfault",
  571. "pgmajfault",
  572. TEXTS_FOR_ZONES("pgrefill")
  573. TEXTS_FOR_ZONES("pgsteal")
  574. TEXTS_FOR_ZONES("pgscan_kswapd")
  575. TEXTS_FOR_ZONES("pgscan_direct")
  576. #ifdef CONFIG_NUMA
  577. "zone_reclaim_failed",
  578. #endif
  579. "pginodesteal",
  580. "slabs_scanned",
  581. "kswapd_steal",
  582. "kswapd_inodesteal",
  583. "kswapd_low_wmark_hit_quickly",
  584. "kswapd_high_wmark_hit_quickly",
  585. "kswapd_skip_congestion_wait",
  586. "pageoutrun",
  587. "allocstall",
  588. "pgrotated",
  589. #ifdef CONFIG_HUGETLB_PAGE
  590. "htlb_buddy_alloc_success",
  591. "htlb_buddy_alloc_fail",
  592. #endif
  593. "unevictable_pgs_culled",
  594. "unevictable_pgs_scanned",
  595. "unevictable_pgs_rescued",
  596. "unevictable_pgs_mlocked",
  597. "unevictable_pgs_munlocked",
  598. "unevictable_pgs_cleared",
  599. "unevictable_pgs_stranded",
  600. "unevictable_pgs_mlockfreed",
  601. #endif
  602. };
  603. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  604. struct zone *zone)
  605. {
  606. int i;
  607. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  608. seq_printf(m,
  609. "\n pages free %lu"
  610. "\n min %lu"
  611. "\n low %lu"
  612. "\n high %lu"
  613. "\n scanned %lu"
  614. "\n spanned %lu"
  615. "\n present %lu",
  616. zone_page_state(zone, NR_FREE_PAGES),
  617. min_wmark_pages(zone),
  618. low_wmark_pages(zone),
  619. high_wmark_pages(zone),
  620. zone->pages_scanned,
  621. zone->spanned_pages,
  622. zone->present_pages);
  623. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  624. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  625. zone_page_state(zone, i));
  626. seq_printf(m,
  627. "\n protection: (%lu",
  628. zone->lowmem_reserve[0]);
  629. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  630. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  631. seq_printf(m,
  632. ")"
  633. "\n pagesets");
  634. for_each_online_cpu(i) {
  635. struct per_cpu_pageset *pageset;
  636. pageset = per_cpu_ptr(zone->pageset, i);
  637. seq_printf(m,
  638. "\n cpu: %i"
  639. "\n count: %i"
  640. "\n high: %i"
  641. "\n batch: %i",
  642. i,
  643. pageset->pcp.count,
  644. pageset->pcp.high,
  645. pageset->pcp.batch);
  646. #ifdef CONFIG_SMP
  647. seq_printf(m, "\n vm stats threshold: %d",
  648. pageset->stat_threshold);
  649. #endif
  650. }
  651. seq_printf(m,
  652. "\n all_unreclaimable: %u"
  653. "\n prev_priority: %i"
  654. "\n start_pfn: %lu"
  655. "\n inactive_ratio: %u",
  656. zone->all_unreclaimable,
  657. zone->prev_priority,
  658. zone->zone_start_pfn,
  659. zone->inactive_ratio);
  660. seq_putc(m, '\n');
  661. }
  662. /*
  663. * Output information about zones in @pgdat.
  664. */
  665. static int zoneinfo_show(struct seq_file *m, void *arg)
  666. {
  667. pg_data_t *pgdat = (pg_data_t *)arg;
  668. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  669. return 0;
  670. }
  671. static const struct seq_operations zoneinfo_op = {
  672. .start = frag_start, /* iterate over all zones. The same as in
  673. * fragmentation. */
  674. .next = frag_next,
  675. .stop = frag_stop,
  676. .show = zoneinfo_show,
  677. };
  678. static int zoneinfo_open(struct inode *inode, struct file *file)
  679. {
  680. return seq_open(file, &zoneinfo_op);
  681. }
  682. static const struct file_operations proc_zoneinfo_file_operations = {
  683. .open = zoneinfo_open,
  684. .read = seq_read,
  685. .llseek = seq_lseek,
  686. .release = seq_release,
  687. };
  688. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  689. {
  690. unsigned long *v;
  691. #ifdef CONFIG_VM_EVENT_COUNTERS
  692. unsigned long *e;
  693. #endif
  694. int i;
  695. if (*pos >= ARRAY_SIZE(vmstat_text))
  696. return NULL;
  697. #ifdef CONFIG_VM_EVENT_COUNTERS
  698. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
  699. + sizeof(struct vm_event_state), GFP_KERNEL);
  700. #else
  701. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
  702. GFP_KERNEL);
  703. #endif
  704. m->private = v;
  705. if (!v)
  706. return ERR_PTR(-ENOMEM);
  707. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  708. v[i] = global_page_state(i);
  709. #ifdef CONFIG_VM_EVENT_COUNTERS
  710. e = v + NR_VM_ZONE_STAT_ITEMS;
  711. all_vm_events(e);
  712. e[PGPGIN] /= 2; /* sectors -> kbytes */
  713. e[PGPGOUT] /= 2;
  714. #endif
  715. return v + *pos;
  716. }
  717. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  718. {
  719. (*pos)++;
  720. if (*pos >= ARRAY_SIZE(vmstat_text))
  721. return NULL;
  722. return (unsigned long *)m->private + *pos;
  723. }
  724. static int vmstat_show(struct seq_file *m, void *arg)
  725. {
  726. unsigned long *l = arg;
  727. unsigned long off = l - (unsigned long *)m->private;
  728. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  729. return 0;
  730. }
  731. static void vmstat_stop(struct seq_file *m, void *arg)
  732. {
  733. kfree(m->private);
  734. m->private = NULL;
  735. }
  736. static const struct seq_operations vmstat_op = {
  737. .start = vmstat_start,
  738. .next = vmstat_next,
  739. .stop = vmstat_stop,
  740. .show = vmstat_show,
  741. };
  742. static int vmstat_open(struct inode *inode, struct file *file)
  743. {
  744. return seq_open(file, &vmstat_op);
  745. }
  746. static const struct file_operations proc_vmstat_file_operations = {
  747. .open = vmstat_open,
  748. .read = seq_read,
  749. .llseek = seq_lseek,
  750. .release = seq_release,
  751. };
  752. #endif /* CONFIG_PROC_FS */
  753. #ifdef CONFIG_SMP
  754. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  755. int sysctl_stat_interval __read_mostly = HZ;
  756. static void vmstat_update(struct work_struct *w)
  757. {
  758. refresh_cpu_vm_stats(smp_processor_id());
  759. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  760. round_jiffies_relative(sysctl_stat_interval));
  761. }
  762. static void __cpuinit start_cpu_timer(int cpu)
  763. {
  764. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  765. INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
  766. schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
  767. }
  768. /*
  769. * Use the cpu notifier to insure that the thresholds are recalculated
  770. * when necessary.
  771. */
  772. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  773. unsigned long action,
  774. void *hcpu)
  775. {
  776. long cpu = (long)hcpu;
  777. switch (action) {
  778. case CPU_ONLINE:
  779. case CPU_ONLINE_FROZEN:
  780. start_cpu_timer(cpu);
  781. node_set_state(cpu_to_node(cpu), N_CPU);
  782. break;
  783. case CPU_DOWN_PREPARE:
  784. case CPU_DOWN_PREPARE_FROZEN:
  785. cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
  786. per_cpu(vmstat_work, cpu).work.func = NULL;
  787. break;
  788. case CPU_DOWN_FAILED:
  789. case CPU_DOWN_FAILED_FROZEN:
  790. start_cpu_timer(cpu);
  791. break;
  792. case CPU_DEAD:
  793. case CPU_DEAD_FROZEN:
  794. refresh_zone_stat_thresholds();
  795. break;
  796. default:
  797. break;
  798. }
  799. return NOTIFY_OK;
  800. }
  801. static struct notifier_block __cpuinitdata vmstat_notifier =
  802. { &vmstat_cpuup_callback, NULL, 0 };
  803. #endif
  804. static int __init setup_vmstat(void)
  805. {
  806. #ifdef CONFIG_SMP
  807. int cpu;
  808. refresh_zone_stat_thresholds();
  809. register_cpu_notifier(&vmstat_notifier);
  810. for_each_online_cpu(cpu)
  811. start_cpu_timer(cpu);
  812. #endif
  813. #ifdef CONFIG_PROC_FS
  814. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  815. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  816. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  817. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  818. #endif
  819. return 0;
  820. }
  821. module_init(setup_vmstat)