vmstat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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/module.h>
  13. #include <linux/cpu.h>
  14. void __get_zone_counts(unsigned long *active, unsigned long *inactive,
  15. unsigned long *free, struct pglist_data *pgdat)
  16. {
  17. *active = node_page_state(pgdat->node_id, NR_ACTIVE);
  18. *inactive = node_page_state(pgdat->node_id, NR_INACTIVE);
  19. *free = node_page_state(pgdat->node_id, NR_FREE_PAGES);
  20. }
  21. void get_zone_counts(unsigned long *active,
  22. unsigned long *inactive, unsigned long *free)
  23. {
  24. *active = global_page_state(NR_ACTIVE);
  25. *inactive = global_page_state(NR_INACTIVE);
  26. *free = global_page_state(NR_FREE_PAGES);
  27. }
  28. #ifdef CONFIG_VM_EVENT_COUNTERS
  29. DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
  30. EXPORT_PER_CPU_SYMBOL(vm_event_states);
  31. static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
  32. {
  33. int cpu = 0;
  34. int i;
  35. memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
  36. cpu = first_cpu(*cpumask);
  37. while (cpu < NR_CPUS) {
  38. struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
  39. cpu = next_cpu(cpu, *cpumask);
  40. if (cpu < NR_CPUS)
  41. prefetch(&per_cpu(vm_event_states, cpu));
  42. for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
  43. ret[i] += this->event[i];
  44. }
  45. }
  46. /*
  47. * Accumulate the vm event counters across all CPUs.
  48. * The result is unavoidably approximate - it can change
  49. * during and after execution of this function.
  50. */
  51. void all_vm_events(unsigned long *ret)
  52. {
  53. sum_vm_events(ret, &cpu_online_map);
  54. }
  55. EXPORT_SYMBOL_GPL(all_vm_events);
  56. #ifdef CONFIG_HOTPLUG
  57. /*
  58. * Fold the foreign cpu events into our own.
  59. *
  60. * This is adding to the events on one processor
  61. * but keeps the global counts constant.
  62. */
  63. void vm_events_fold_cpu(int cpu)
  64. {
  65. struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
  66. int i;
  67. for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
  68. count_vm_events(i, fold_state->event[i]);
  69. fold_state->event[i] = 0;
  70. }
  71. }
  72. #endif /* CONFIG_HOTPLUG */
  73. #endif /* CONFIG_VM_EVENT_COUNTERS */
  74. /*
  75. * Manage combined zone based / global counters
  76. *
  77. * vm_stat contains the global counters
  78. */
  79. atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
  80. EXPORT_SYMBOL(vm_stat);
  81. #ifdef CONFIG_SMP
  82. static int calculate_threshold(struct zone *zone)
  83. {
  84. int threshold;
  85. int mem; /* memory in 128 MB units */
  86. /*
  87. * The threshold scales with the number of processors and the amount
  88. * of memory per zone. More memory means that we can defer updates for
  89. * longer, more processors could lead to more contention.
  90. * fls() is used to have a cheap way of logarithmic scaling.
  91. *
  92. * Some sample thresholds:
  93. *
  94. * Threshold Processors (fls) Zonesize fls(mem+1)
  95. * ------------------------------------------------------------------
  96. * 8 1 1 0.9-1 GB 4
  97. * 16 2 2 0.9-1 GB 4
  98. * 20 2 2 1-2 GB 5
  99. * 24 2 2 2-4 GB 6
  100. * 28 2 2 4-8 GB 7
  101. * 32 2 2 8-16 GB 8
  102. * 4 2 2 <128M 1
  103. * 30 4 3 2-4 GB 5
  104. * 48 4 3 8-16 GB 8
  105. * 32 8 4 1-2 GB 4
  106. * 32 8 4 0.9-1GB 4
  107. * 10 16 5 <128M 1
  108. * 40 16 5 900M 4
  109. * 70 64 7 2-4 GB 5
  110. * 84 64 7 4-8 GB 6
  111. * 108 512 9 4-8 GB 6
  112. * 125 1024 10 8-16 GB 8
  113. * 125 1024 10 16-32 GB 9
  114. */
  115. mem = zone->present_pages >> (27 - PAGE_SHIFT);
  116. threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
  117. /*
  118. * Maximum threshold is 125
  119. */
  120. threshold = min(125, threshold);
  121. return threshold;
  122. }
  123. /*
  124. * Refresh the thresholds for each zone.
  125. */
  126. static void refresh_zone_stat_thresholds(void)
  127. {
  128. struct zone *zone;
  129. int cpu;
  130. int threshold;
  131. for_each_zone(zone) {
  132. if (!zone->present_pages)
  133. continue;
  134. threshold = calculate_threshold(zone);
  135. for_each_online_cpu(cpu)
  136. zone_pcp(zone, cpu)->stat_threshold = threshold;
  137. }
  138. }
  139. /*
  140. * For use when we know that interrupts are disabled.
  141. */
  142. void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  143. int delta)
  144. {
  145. struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
  146. s8 *p = pcp->vm_stat_diff + item;
  147. long x;
  148. x = delta + *p;
  149. if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
  150. zone_page_state_add(x, zone, item);
  151. x = 0;
  152. }
  153. *p = x;
  154. }
  155. EXPORT_SYMBOL(__mod_zone_page_state);
  156. /*
  157. * For an unknown interrupt state
  158. */
  159. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  160. int delta)
  161. {
  162. unsigned long flags;
  163. local_irq_save(flags);
  164. __mod_zone_page_state(zone, item, delta);
  165. local_irq_restore(flags);
  166. }
  167. EXPORT_SYMBOL(mod_zone_page_state);
  168. /*
  169. * Optimized increment and decrement functions.
  170. *
  171. * These are only for a single page and therefore can take a struct page *
  172. * argument instead of struct zone *. This allows the inclusion of the code
  173. * generated for page_zone(page) into the optimized functions.
  174. *
  175. * No overflow check is necessary and therefore the differential can be
  176. * incremented or decremented in place which may allow the compilers to
  177. * generate better code.
  178. * The increment or decrement is known and therefore one boundary check can
  179. * be omitted.
  180. *
  181. * NOTE: These functions are very performance sensitive. Change only
  182. * with care.
  183. *
  184. * Some processors have inc/dec instructions that are atomic vs an interrupt.
  185. * However, the code must first determine the differential location in a zone
  186. * based on the processor number and then inc/dec the counter. There is no
  187. * guarantee without disabling preemption that the processor will not change
  188. * in between and therefore the atomicity vs. interrupt cannot be exploited
  189. * in a useful way here.
  190. */
  191. void __inc_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 __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  203. {
  204. __inc_zone_state(page_zone(page), item);
  205. }
  206. EXPORT_SYMBOL(__inc_zone_page_state);
  207. void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  208. {
  209. struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
  210. s8 *p = pcp->vm_stat_diff + item;
  211. (*p)--;
  212. if (unlikely(*p < - pcp->stat_threshold)) {
  213. int overstep = pcp->stat_threshold / 2;
  214. zone_page_state_add(*p - overstep, zone, item);
  215. *p = overstep;
  216. }
  217. }
  218. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  219. {
  220. __dec_zone_state(page_zone(page), item);
  221. }
  222. EXPORT_SYMBOL(__dec_zone_page_state);
  223. void inc_zone_state(struct zone *zone, enum zone_stat_item item)
  224. {
  225. unsigned long flags;
  226. local_irq_save(flags);
  227. __inc_zone_state(zone, item);
  228. local_irq_restore(flags);
  229. }
  230. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  231. {
  232. unsigned long flags;
  233. struct zone *zone;
  234. zone = page_zone(page);
  235. local_irq_save(flags);
  236. __inc_zone_state(zone, item);
  237. local_irq_restore(flags);
  238. }
  239. EXPORT_SYMBOL(inc_zone_page_state);
  240. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  241. {
  242. unsigned long flags;
  243. local_irq_save(flags);
  244. __dec_zone_page_state(page, item);
  245. local_irq_restore(flags);
  246. }
  247. EXPORT_SYMBOL(dec_zone_page_state);
  248. /*
  249. * Update the zone counters for one cpu.
  250. */
  251. void refresh_cpu_vm_stats(int cpu)
  252. {
  253. struct zone *zone;
  254. int i;
  255. unsigned long flags;
  256. for_each_zone(zone) {
  257. struct per_cpu_pageset *pcp;
  258. if (!populated_zone(zone))
  259. continue;
  260. pcp = zone_pcp(zone, cpu);
  261. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  262. if (pcp->vm_stat_diff[i]) {
  263. local_irq_save(flags);
  264. zone_page_state_add(pcp->vm_stat_diff[i],
  265. zone, i);
  266. pcp->vm_stat_diff[i] = 0;
  267. local_irq_restore(flags);
  268. }
  269. }
  270. }
  271. static void __refresh_cpu_vm_stats(void *dummy)
  272. {
  273. refresh_cpu_vm_stats(smp_processor_id());
  274. }
  275. /*
  276. * Consolidate all counters.
  277. *
  278. * Note that the result is less inaccurate but still inaccurate
  279. * if concurrent processes are allowed to run.
  280. */
  281. void refresh_vm_stats(void)
  282. {
  283. on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
  284. }
  285. EXPORT_SYMBOL(refresh_vm_stats);
  286. #endif
  287. #ifdef CONFIG_NUMA
  288. /*
  289. * zonelist = the list of zones passed to the allocator
  290. * z = the zone from which the allocation occurred.
  291. *
  292. * Must be called with interrupts disabled.
  293. */
  294. void zone_statistics(struct zonelist *zonelist, struct zone *z)
  295. {
  296. if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
  297. __inc_zone_state(z, NUMA_HIT);
  298. } else {
  299. __inc_zone_state(z, NUMA_MISS);
  300. __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
  301. }
  302. if (z->node == numa_node_id())
  303. __inc_zone_state(z, NUMA_LOCAL);
  304. else
  305. __inc_zone_state(z, NUMA_OTHER);
  306. }
  307. #endif
  308. #ifdef CONFIG_PROC_FS
  309. #include <linux/seq_file.h>
  310. static void *frag_start(struct seq_file *m, loff_t *pos)
  311. {
  312. pg_data_t *pgdat;
  313. loff_t node = *pos;
  314. for (pgdat = first_online_pgdat();
  315. pgdat && node;
  316. pgdat = next_online_pgdat(pgdat))
  317. --node;
  318. return pgdat;
  319. }
  320. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  321. {
  322. pg_data_t *pgdat = (pg_data_t *)arg;
  323. (*pos)++;
  324. return next_online_pgdat(pgdat);
  325. }
  326. static void frag_stop(struct seq_file *m, void *arg)
  327. {
  328. }
  329. /*
  330. * This walks the free areas for each zone.
  331. */
  332. static int frag_show(struct seq_file *m, void *arg)
  333. {
  334. pg_data_t *pgdat = (pg_data_t *)arg;
  335. struct zone *zone;
  336. struct zone *node_zones = pgdat->node_zones;
  337. unsigned long flags;
  338. int order;
  339. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  340. if (!populated_zone(zone))
  341. continue;
  342. spin_lock_irqsave(&zone->lock, flags);
  343. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  344. for (order = 0; order < MAX_ORDER; ++order)
  345. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  346. spin_unlock_irqrestore(&zone->lock, flags);
  347. seq_putc(m, '\n');
  348. }
  349. return 0;
  350. }
  351. const struct seq_operations fragmentation_op = {
  352. .start = frag_start,
  353. .next = frag_next,
  354. .stop = frag_stop,
  355. .show = frag_show,
  356. };
  357. #ifdef CONFIG_ZONE_DMA32
  358. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  359. #else
  360. #define TEXT_FOR_DMA32(xx)
  361. #endif
  362. #ifdef CONFIG_HIGHMEM
  363. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  364. #else
  365. #define TEXT_FOR_HIGHMEM(xx)
  366. #endif
  367. #define TEXTS_FOR_ZONES(xx) xx "_dma", TEXT_FOR_DMA32(xx) xx "_normal", \
  368. TEXT_FOR_HIGHMEM(xx)
  369. static const char * const vmstat_text[] = {
  370. /* Zoned VM counters */
  371. "nr_free_pages",
  372. "nr_active",
  373. "nr_inactive",
  374. "nr_anon_pages",
  375. "nr_mapped",
  376. "nr_file_pages",
  377. "nr_slab_reclaimable",
  378. "nr_slab_unreclaimable",
  379. "nr_page_table_pages",
  380. "nr_dirty",
  381. "nr_writeback",
  382. "nr_unstable",
  383. "nr_bounce",
  384. "nr_vmscan_write",
  385. #ifdef CONFIG_NUMA
  386. "numa_hit",
  387. "numa_miss",
  388. "numa_foreign",
  389. "numa_interleave",
  390. "numa_local",
  391. "numa_other",
  392. #endif
  393. #ifdef CONFIG_VM_EVENT_COUNTERS
  394. "pgpgin",
  395. "pgpgout",
  396. "pswpin",
  397. "pswpout",
  398. TEXTS_FOR_ZONES("pgalloc")
  399. "pgfree",
  400. "pgactivate",
  401. "pgdeactivate",
  402. "pgfault",
  403. "pgmajfault",
  404. TEXTS_FOR_ZONES("pgrefill")
  405. TEXTS_FOR_ZONES("pgsteal")
  406. TEXTS_FOR_ZONES("pgscan_kswapd")
  407. TEXTS_FOR_ZONES("pgscan_direct")
  408. "pginodesteal",
  409. "slabs_scanned",
  410. "kswapd_steal",
  411. "kswapd_inodesteal",
  412. "pageoutrun",
  413. "allocstall",
  414. "pgrotated",
  415. #endif
  416. };
  417. /*
  418. * Output information about zones in @pgdat.
  419. */
  420. static int zoneinfo_show(struct seq_file *m, void *arg)
  421. {
  422. pg_data_t *pgdat = arg;
  423. struct zone *zone;
  424. struct zone *node_zones = pgdat->node_zones;
  425. unsigned long flags;
  426. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
  427. int i;
  428. if (!populated_zone(zone))
  429. continue;
  430. spin_lock_irqsave(&zone->lock, flags);
  431. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  432. seq_printf(m,
  433. "\n pages free %lu"
  434. "\n min %lu"
  435. "\n low %lu"
  436. "\n high %lu"
  437. "\n scanned %lu (a: %lu i: %lu)"
  438. "\n spanned %lu"
  439. "\n present %lu",
  440. zone_page_state(zone, NR_FREE_PAGES),
  441. zone->pages_min,
  442. zone->pages_low,
  443. zone->pages_high,
  444. zone->pages_scanned,
  445. zone->nr_scan_active, zone->nr_scan_inactive,
  446. zone->spanned_pages,
  447. zone->present_pages);
  448. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  449. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  450. zone_page_state(zone, i));
  451. seq_printf(m,
  452. "\n protection: (%lu",
  453. zone->lowmem_reserve[0]);
  454. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  455. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  456. seq_printf(m,
  457. ")"
  458. "\n pagesets");
  459. for_each_online_cpu(i) {
  460. struct per_cpu_pageset *pageset;
  461. int j;
  462. pageset = zone_pcp(zone, i);
  463. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  464. seq_printf(m,
  465. "\n cpu: %i pcp: %i"
  466. "\n count: %i"
  467. "\n high: %i"
  468. "\n batch: %i",
  469. i, j,
  470. pageset->pcp[j].count,
  471. pageset->pcp[j].high,
  472. pageset->pcp[j].batch);
  473. }
  474. #ifdef CONFIG_SMP
  475. seq_printf(m, "\n vm stats threshold: %d",
  476. pageset->stat_threshold);
  477. #endif
  478. }
  479. seq_printf(m,
  480. "\n all_unreclaimable: %u"
  481. "\n prev_priority: %i"
  482. "\n start_pfn: %lu",
  483. zone->all_unreclaimable,
  484. zone->prev_priority,
  485. zone->zone_start_pfn);
  486. spin_unlock_irqrestore(&zone->lock, flags);
  487. seq_putc(m, '\n');
  488. }
  489. return 0;
  490. }
  491. const struct seq_operations zoneinfo_op = {
  492. .start = frag_start, /* iterate over all zones. The same as in
  493. * fragmentation. */
  494. .next = frag_next,
  495. .stop = frag_stop,
  496. .show = zoneinfo_show,
  497. };
  498. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  499. {
  500. unsigned long *v;
  501. #ifdef CONFIG_VM_EVENT_COUNTERS
  502. unsigned long *e;
  503. #endif
  504. int i;
  505. if (*pos >= ARRAY_SIZE(vmstat_text))
  506. return NULL;
  507. #ifdef CONFIG_VM_EVENT_COUNTERS
  508. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
  509. + sizeof(struct vm_event_state), GFP_KERNEL);
  510. #else
  511. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
  512. GFP_KERNEL);
  513. #endif
  514. m->private = v;
  515. if (!v)
  516. return ERR_PTR(-ENOMEM);
  517. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  518. v[i] = global_page_state(i);
  519. #ifdef CONFIG_VM_EVENT_COUNTERS
  520. e = v + NR_VM_ZONE_STAT_ITEMS;
  521. all_vm_events(e);
  522. e[PGPGIN] /= 2; /* sectors -> kbytes */
  523. e[PGPGOUT] /= 2;
  524. #endif
  525. return v + *pos;
  526. }
  527. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  528. {
  529. (*pos)++;
  530. if (*pos >= ARRAY_SIZE(vmstat_text))
  531. return NULL;
  532. return (unsigned long *)m->private + *pos;
  533. }
  534. static int vmstat_show(struct seq_file *m, void *arg)
  535. {
  536. unsigned long *l = arg;
  537. unsigned long off = l - (unsigned long *)m->private;
  538. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  539. return 0;
  540. }
  541. static void vmstat_stop(struct seq_file *m, void *arg)
  542. {
  543. kfree(m->private);
  544. m->private = NULL;
  545. }
  546. const struct seq_operations vmstat_op = {
  547. .start = vmstat_start,
  548. .next = vmstat_next,
  549. .stop = vmstat_stop,
  550. .show = vmstat_show,
  551. };
  552. #endif /* CONFIG_PROC_FS */
  553. #ifdef CONFIG_SMP
  554. /*
  555. * Use the cpu notifier to insure that the thresholds are recalculated
  556. * when necessary.
  557. */
  558. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  559. unsigned long action,
  560. void *hcpu)
  561. {
  562. switch (action) {
  563. case CPU_UP_PREPARE:
  564. case CPU_UP_CANCELED:
  565. case CPU_DEAD:
  566. refresh_zone_stat_thresholds();
  567. break;
  568. default:
  569. break;
  570. }
  571. return NOTIFY_OK;
  572. }
  573. static struct notifier_block __cpuinitdata vmstat_notifier =
  574. { &vmstat_cpuup_callback, NULL, 0 };
  575. int __init setup_vmstat(void)
  576. {
  577. refresh_zone_stat_thresholds();
  578. register_cpu_notifier(&vmstat_notifier);
  579. return 0;
  580. }
  581. module_init(setup_vmstat)
  582. #endif