vmstat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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/config.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. /*
  15. * Accumulate the page_state information across all CPUs.
  16. * The result is unavoidably approximate - it can change
  17. * during and after execution of this function.
  18. */
  19. DEFINE_PER_CPU(struct page_state, page_states) = {0};
  20. atomic_t nr_pagecache = ATOMIC_INIT(0);
  21. EXPORT_SYMBOL(nr_pagecache);
  22. #ifdef CONFIG_SMP
  23. DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
  24. #endif
  25. static void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
  26. {
  27. unsigned cpu;
  28. memset(ret, 0, nr * sizeof(unsigned long));
  29. cpus_and(*cpumask, *cpumask, cpu_online_map);
  30. for_each_cpu_mask(cpu, *cpumask) {
  31. unsigned long *in;
  32. unsigned long *out;
  33. unsigned off;
  34. unsigned next_cpu;
  35. in = (unsigned long *)&per_cpu(page_states, cpu);
  36. next_cpu = next_cpu(cpu, *cpumask);
  37. if (likely(next_cpu < NR_CPUS))
  38. prefetch(&per_cpu(page_states, next_cpu));
  39. out = (unsigned long *)ret;
  40. for (off = 0; off < nr; off++)
  41. *out++ += *in++;
  42. }
  43. }
  44. void get_page_state_node(struct page_state *ret, int node)
  45. {
  46. int nr;
  47. cpumask_t mask = node_to_cpumask(node);
  48. nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
  49. nr /= sizeof(unsigned long);
  50. __get_page_state(ret, nr+1, &mask);
  51. }
  52. void get_page_state(struct page_state *ret)
  53. {
  54. int nr;
  55. cpumask_t mask = CPU_MASK_ALL;
  56. nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
  57. nr /= sizeof(unsigned long);
  58. __get_page_state(ret, nr + 1, &mask);
  59. }
  60. void get_full_page_state(struct page_state *ret)
  61. {
  62. cpumask_t mask = CPU_MASK_ALL;
  63. __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
  64. }
  65. unsigned long read_page_state_offset(unsigned long offset)
  66. {
  67. unsigned long ret = 0;
  68. int cpu;
  69. for_each_online_cpu(cpu) {
  70. unsigned long in;
  71. in = (unsigned long)&per_cpu(page_states, cpu) + offset;
  72. ret += *((unsigned long *)in);
  73. }
  74. return ret;
  75. }
  76. void __mod_page_state_offset(unsigned long offset, unsigned long delta)
  77. {
  78. void *ptr;
  79. ptr = &__get_cpu_var(page_states);
  80. *(unsigned long *)(ptr + offset) += delta;
  81. }
  82. EXPORT_SYMBOL(__mod_page_state_offset);
  83. void mod_page_state_offset(unsigned long offset, unsigned long delta)
  84. {
  85. unsigned long flags;
  86. void *ptr;
  87. local_irq_save(flags);
  88. ptr = &__get_cpu_var(page_states);
  89. *(unsigned long *)(ptr + offset) += delta;
  90. local_irq_restore(flags);
  91. }
  92. EXPORT_SYMBOL(mod_page_state_offset);
  93. void __get_zone_counts(unsigned long *active, unsigned long *inactive,
  94. unsigned long *free, struct pglist_data *pgdat)
  95. {
  96. struct zone *zones = pgdat->node_zones;
  97. int i;
  98. *active = 0;
  99. *inactive = 0;
  100. *free = 0;
  101. for (i = 0; i < MAX_NR_ZONES; i++) {
  102. *active += zones[i].nr_active;
  103. *inactive += zones[i].nr_inactive;
  104. *free += zones[i].free_pages;
  105. }
  106. }
  107. void get_zone_counts(unsigned long *active,
  108. unsigned long *inactive, unsigned long *free)
  109. {
  110. struct pglist_data *pgdat;
  111. *active = 0;
  112. *inactive = 0;
  113. *free = 0;
  114. for_each_online_pgdat(pgdat) {
  115. unsigned long l, m, n;
  116. __get_zone_counts(&l, &m, &n, pgdat);
  117. *active += l;
  118. *inactive += m;
  119. *free += n;
  120. }
  121. }
  122. /*
  123. * Manage combined zone based / global counters
  124. *
  125. * vm_stat contains the global counters
  126. */
  127. atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
  128. EXPORT_SYMBOL(vm_stat);
  129. #ifdef CONFIG_SMP
  130. #define STAT_THRESHOLD 32
  131. /*
  132. * Determine pointer to currently valid differential byte given a zone and
  133. * the item number.
  134. *
  135. * Preemption must be off
  136. */
  137. static inline s8 *diff_pointer(struct zone *zone, enum zone_stat_item item)
  138. {
  139. return &zone_pcp(zone, smp_processor_id())->vm_stat_diff[item];
  140. }
  141. /*
  142. * For use when we know that interrupts are disabled.
  143. */
  144. void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  145. int delta)
  146. {
  147. s8 *p;
  148. long x;
  149. p = diff_pointer(zone, item);
  150. x = delta + *p;
  151. if (unlikely(x > STAT_THRESHOLD || x < -STAT_THRESHOLD)) {
  152. zone_page_state_add(x, zone, item);
  153. x = 0;
  154. }
  155. *p = x;
  156. }
  157. EXPORT_SYMBOL(__mod_zone_page_state);
  158. /*
  159. * For an unknown interrupt state
  160. */
  161. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  162. int delta)
  163. {
  164. unsigned long flags;
  165. local_irq_save(flags);
  166. __mod_zone_page_state(zone, item, delta);
  167. local_irq_restore(flags);
  168. }
  169. EXPORT_SYMBOL(mod_zone_page_state);
  170. /*
  171. * Optimized increment and decrement functions.
  172. *
  173. * These are only for a single page and therefore can take a struct page *
  174. * argument instead of struct zone *. This allows the inclusion of the code
  175. * generated for page_zone(page) into the optimized functions.
  176. *
  177. * No overflow check is necessary and therefore the differential can be
  178. * incremented or decremented in place which may allow the compilers to
  179. * generate better code.
  180. *
  181. * The increment or decrement is known and therefore one boundary check can
  182. * be omitted.
  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_page_state(struct page *page, enum zone_stat_item item)
  192. {
  193. struct zone *zone = page_zone(page);
  194. s8 *p = diff_pointer(zone, item);
  195. (*p)++;
  196. if (unlikely(*p > STAT_THRESHOLD)) {
  197. zone_page_state_add(*p, zone, item);
  198. *p = 0;
  199. }
  200. }
  201. EXPORT_SYMBOL(__inc_zone_page_state);
  202. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  203. {
  204. struct zone *zone = page_zone(page);
  205. s8 *p = diff_pointer(zone, item);
  206. (*p)--;
  207. if (unlikely(*p < -STAT_THRESHOLD)) {
  208. zone_page_state_add(*p, zone, item);
  209. *p = 0;
  210. }
  211. }
  212. EXPORT_SYMBOL(__dec_zone_page_state);
  213. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  214. {
  215. unsigned long flags;
  216. struct zone *zone;
  217. s8 *p;
  218. zone = page_zone(page);
  219. local_irq_save(flags);
  220. p = diff_pointer(zone, item);
  221. (*p)++;
  222. if (unlikely(*p > STAT_THRESHOLD)) {
  223. zone_page_state_add(*p, zone, item);
  224. *p = 0;
  225. }
  226. local_irq_restore(flags);
  227. }
  228. EXPORT_SYMBOL(inc_zone_page_state);
  229. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  230. {
  231. unsigned long flags;
  232. struct zone *zone;
  233. s8 *p;
  234. zone = page_zone(page);
  235. local_irq_save(flags);
  236. p = diff_pointer(zone, item);
  237. (*p)--;
  238. if (unlikely(*p < -STAT_THRESHOLD)) {
  239. zone_page_state_add(*p, zone, item);
  240. *p = 0;
  241. }
  242. local_irq_restore(flags);
  243. }
  244. EXPORT_SYMBOL(dec_zone_page_state);
  245. /*
  246. * Update the zone counters for one cpu.
  247. */
  248. void refresh_cpu_vm_stats(int cpu)
  249. {
  250. struct zone *zone;
  251. int i;
  252. unsigned long flags;
  253. for_each_zone(zone) {
  254. struct per_cpu_pageset *pcp;
  255. pcp = zone_pcp(zone, cpu);
  256. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  257. if (pcp->vm_stat_diff[i]) {
  258. local_irq_save(flags);
  259. zone_page_state_add(pcp->vm_stat_diff[i],
  260. zone, i);
  261. pcp->vm_stat_diff[i] = 0;
  262. local_irq_restore(flags);
  263. }
  264. }
  265. }
  266. static void __refresh_cpu_vm_stats(void *dummy)
  267. {
  268. refresh_cpu_vm_stats(smp_processor_id());
  269. }
  270. /*
  271. * Consolidate all counters.
  272. *
  273. * Note that the result is less inaccurate but still inaccurate
  274. * if concurrent processes are allowed to run.
  275. */
  276. void refresh_vm_stats(void)
  277. {
  278. on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
  279. }
  280. EXPORT_SYMBOL(refresh_vm_stats);
  281. #endif
  282. #ifdef CONFIG_PROC_FS
  283. #include <linux/seq_file.h>
  284. static void *frag_start(struct seq_file *m, loff_t *pos)
  285. {
  286. pg_data_t *pgdat;
  287. loff_t node = *pos;
  288. for (pgdat = first_online_pgdat();
  289. pgdat && node;
  290. pgdat = next_online_pgdat(pgdat))
  291. --node;
  292. return pgdat;
  293. }
  294. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  295. {
  296. pg_data_t *pgdat = (pg_data_t *)arg;
  297. (*pos)++;
  298. return next_online_pgdat(pgdat);
  299. }
  300. static void frag_stop(struct seq_file *m, void *arg)
  301. {
  302. }
  303. /*
  304. * This walks the free areas for each zone.
  305. */
  306. static int frag_show(struct seq_file *m, void *arg)
  307. {
  308. pg_data_t *pgdat = (pg_data_t *)arg;
  309. struct zone *zone;
  310. struct zone *node_zones = pgdat->node_zones;
  311. unsigned long flags;
  312. int order;
  313. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  314. if (!populated_zone(zone))
  315. continue;
  316. spin_lock_irqsave(&zone->lock, flags);
  317. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  318. for (order = 0; order < MAX_ORDER; ++order)
  319. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  320. spin_unlock_irqrestore(&zone->lock, flags);
  321. seq_putc(m, '\n');
  322. }
  323. return 0;
  324. }
  325. struct seq_operations fragmentation_op = {
  326. .start = frag_start,
  327. .next = frag_next,
  328. .stop = frag_stop,
  329. .show = frag_show,
  330. };
  331. static char *vmstat_text[] = {
  332. /* Zoned VM counters */
  333. "nr_mapped",
  334. /* Page state */
  335. "nr_dirty",
  336. "nr_writeback",
  337. "nr_unstable",
  338. "nr_page_table_pages",
  339. "nr_slab",
  340. "pgpgin",
  341. "pgpgout",
  342. "pswpin",
  343. "pswpout",
  344. "pgalloc_high",
  345. "pgalloc_normal",
  346. "pgalloc_dma32",
  347. "pgalloc_dma",
  348. "pgfree",
  349. "pgactivate",
  350. "pgdeactivate",
  351. "pgfault",
  352. "pgmajfault",
  353. "pgrefill_high",
  354. "pgrefill_normal",
  355. "pgrefill_dma32",
  356. "pgrefill_dma",
  357. "pgsteal_high",
  358. "pgsteal_normal",
  359. "pgsteal_dma32",
  360. "pgsteal_dma",
  361. "pgscan_kswapd_high",
  362. "pgscan_kswapd_normal",
  363. "pgscan_kswapd_dma32",
  364. "pgscan_kswapd_dma",
  365. "pgscan_direct_high",
  366. "pgscan_direct_normal",
  367. "pgscan_direct_dma32",
  368. "pgscan_direct_dma",
  369. "pginodesteal",
  370. "slabs_scanned",
  371. "kswapd_steal",
  372. "kswapd_inodesteal",
  373. "pageoutrun",
  374. "allocstall",
  375. "pgrotated",
  376. "nr_bounce",
  377. };
  378. /*
  379. * Output information about zones in @pgdat.
  380. */
  381. static int zoneinfo_show(struct seq_file *m, void *arg)
  382. {
  383. pg_data_t *pgdat = arg;
  384. struct zone *zone;
  385. struct zone *node_zones = pgdat->node_zones;
  386. unsigned long flags;
  387. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
  388. int i;
  389. if (!populated_zone(zone))
  390. continue;
  391. spin_lock_irqsave(&zone->lock, flags);
  392. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  393. seq_printf(m,
  394. "\n pages free %lu"
  395. "\n min %lu"
  396. "\n low %lu"
  397. "\n high %lu"
  398. "\n active %lu"
  399. "\n inactive %lu"
  400. "\n scanned %lu (a: %lu i: %lu)"
  401. "\n spanned %lu"
  402. "\n present %lu",
  403. zone->free_pages,
  404. zone->pages_min,
  405. zone->pages_low,
  406. zone->pages_high,
  407. zone->nr_active,
  408. zone->nr_inactive,
  409. zone->pages_scanned,
  410. zone->nr_scan_active, zone->nr_scan_inactive,
  411. zone->spanned_pages,
  412. zone->present_pages);
  413. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  414. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  415. zone_page_state(zone, i));
  416. seq_printf(m,
  417. "\n protection: (%lu",
  418. zone->lowmem_reserve[0]);
  419. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  420. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  421. seq_printf(m,
  422. ")"
  423. "\n pagesets");
  424. for_each_online_cpu(i) {
  425. struct per_cpu_pageset *pageset;
  426. int j;
  427. pageset = zone_pcp(zone, i);
  428. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  429. if (pageset->pcp[j].count)
  430. break;
  431. }
  432. if (j == ARRAY_SIZE(pageset->pcp))
  433. continue;
  434. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  435. seq_printf(m,
  436. "\n cpu: %i pcp: %i"
  437. "\n count: %i"
  438. "\n high: %i"
  439. "\n batch: %i",
  440. i, j,
  441. pageset->pcp[j].count,
  442. pageset->pcp[j].high,
  443. pageset->pcp[j].batch);
  444. }
  445. #ifdef CONFIG_NUMA
  446. seq_printf(m,
  447. "\n numa_hit: %lu"
  448. "\n numa_miss: %lu"
  449. "\n numa_foreign: %lu"
  450. "\n interleave_hit: %lu"
  451. "\n local_node: %lu"
  452. "\n other_node: %lu",
  453. pageset->numa_hit,
  454. pageset->numa_miss,
  455. pageset->numa_foreign,
  456. pageset->interleave_hit,
  457. pageset->local_node,
  458. pageset->other_node);
  459. #endif
  460. }
  461. seq_printf(m,
  462. "\n all_unreclaimable: %u"
  463. "\n prev_priority: %i"
  464. "\n temp_priority: %i"
  465. "\n start_pfn: %lu",
  466. zone->all_unreclaimable,
  467. zone->prev_priority,
  468. zone->temp_priority,
  469. zone->zone_start_pfn);
  470. spin_unlock_irqrestore(&zone->lock, flags);
  471. seq_putc(m, '\n');
  472. }
  473. return 0;
  474. }
  475. struct seq_operations zoneinfo_op = {
  476. .start = frag_start, /* iterate over all zones. The same as in
  477. * fragmentation. */
  478. .next = frag_next,
  479. .stop = frag_stop,
  480. .show = zoneinfo_show,
  481. };
  482. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  483. {
  484. unsigned long *v;
  485. struct page_state *ps;
  486. int i;
  487. if (*pos >= ARRAY_SIZE(vmstat_text))
  488. return NULL;
  489. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
  490. + sizeof(*ps), GFP_KERNEL);
  491. m->private = v;
  492. if (!v)
  493. return ERR_PTR(-ENOMEM);
  494. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  495. v[i] = global_page_state(i);
  496. ps = (struct page_state *)(v + NR_VM_ZONE_STAT_ITEMS);
  497. get_full_page_state(ps);
  498. ps->pgpgin /= 2; /* sectors -> kbytes */
  499. ps->pgpgout /= 2;
  500. return v + *pos;
  501. }
  502. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  503. {
  504. (*pos)++;
  505. if (*pos >= ARRAY_SIZE(vmstat_text))
  506. return NULL;
  507. return (unsigned long *)m->private + *pos;
  508. }
  509. static int vmstat_show(struct seq_file *m, void *arg)
  510. {
  511. unsigned long *l = arg;
  512. unsigned long off = l - (unsigned long *)m->private;
  513. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  514. return 0;
  515. }
  516. static void vmstat_stop(struct seq_file *m, void *arg)
  517. {
  518. kfree(m->private);
  519. m->private = NULL;
  520. }
  521. struct seq_operations vmstat_op = {
  522. .start = vmstat_start,
  523. .next = vmstat_next,
  524. .stop = vmstat_stop,
  525. .show = vmstat_show,
  526. };
  527. #endif /* CONFIG_PROC_FS */