vmstat.c 22 KB

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