vmstat.c 20 KB

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