vmstat.c 13 KB

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