vmstat.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 = 0;
  22. int i;
  23. memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
  24. cpu = first_cpu(*cpumask);
  25. while (cpu < NR_CPUS) {
  26. struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
  27. cpu = next_cpu(cpu, *cpumask);
  28. if (cpu < NR_CPUS)
  29. prefetch(&per_cpu(vm_event_states, cpu));
  30. for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
  31. ret[i] += this->event[i];
  32. }
  33. }
  34. /*
  35. * Accumulate the vm event counters across all CPUs.
  36. * The result is unavoidably approximate - it can change
  37. * during and after execution of this function.
  38. */
  39. void all_vm_events(unsigned long *ret)
  40. {
  41. sum_vm_events(ret, &cpu_online_map);
  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_zone(zone) {
  120. if (!zone->present_pages)
  121. continue;
  122. threshold = calculate_threshold(zone);
  123. for_each_online_cpu(cpu)
  124. zone_pcp(zone, cpu)->stat_threshold = threshold;
  125. }
  126. }
  127. /*
  128. * For use when we know that interrupts are disabled.
  129. */
  130. void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  131. int delta)
  132. {
  133. struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
  134. s8 *p = pcp->vm_stat_diff + item;
  135. long x;
  136. x = delta + *p;
  137. if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
  138. zone_page_state_add(x, zone, item);
  139. x = 0;
  140. }
  141. *p = x;
  142. }
  143. EXPORT_SYMBOL(__mod_zone_page_state);
  144. /*
  145. * For an unknown interrupt state
  146. */
  147. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  148. int delta)
  149. {
  150. unsigned long flags;
  151. local_irq_save(flags);
  152. __mod_zone_page_state(zone, item, delta);
  153. local_irq_restore(flags);
  154. }
  155. EXPORT_SYMBOL(mod_zone_page_state);
  156. /*
  157. * Optimized increment and decrement functions.
  158. *
  159. * These are only for a single page and therefore can take a struct page *
  160. * argument instead of struct zone *. This allows the inclusion of the code
  161. * generated for page_zone(page) into the optimized functions.
  162. *
  163. * No overflow check is necessary and therefore the differential can be
  164. * incremented or decremented in place which may allow the compilers to
  165. * generate better code.
  166. * The increment or decrement is known and therefore one boundary check can
  167. * be omitted.
  168. *
  169. * NOTE: These functions are very performance sensitive. Change only
  170. * with care.
  171. *
  172. * Some processors have inc/dec instructions that are atomic vs an interrupt.
  173. * However, the code must first determine the differential location in a zone
  174. * based on the processor number and then inc/dec the counter. There is no
  175. * guarantee without disabling preemption that the processor will not change
  176. * in between and therefore the atomicity vs. interrupt cannot be exploited
  177. * in a useful way here.
  178. */
  179. void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  180. {
  181. struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
  182. s8 *p = pcp->vm_stat_diff + item;
  183. (*p)++;
  184. if (unlikely(*p > pcp->stat_threshold)) {
  185. int overstep = pcp->stat_threshold / 2;
  186. zone_page_state_add(*p + overstep, zone, item);
  187. *p = -overstep;
  188. }
  189. }
  190. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  191. {
  192. __inc_zone_state(page_zone(page), item);
  193. }
  194. EXPORT_SYMBOL(__inc_zone_page_state);
  195. void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  196. {
  197. struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
  198. s8 *p = pcp->vm_stat_diff + item;
  199. (*p)--;
  200. if (unlikely(*p < - pcp->stat_threshold)) {
  201. int overstep = pcp->stat_threshold / 2;
  202. zone_page_state_add(*p - overstep, zone, item);
  203. *p = overstep;
  204. }
  205. }
  206. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  207. {
  208. __dec_zone_state(page_zone(page), item);
  209. }
  210. EXPORT_SYMBOL(__dec_zone_page_state);
  211. void inc_zone_state(struct zone *zone, enum zone_stat_item item)
  212. {
  213. unsigned long flags;
  214. local_irq_save(flags);
  215. __inc_zone_state(zone, item);
  216. local_irq_restore(flags);
  217. }
  218. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  219. {
  220. unsigned long flags;
  221. struct zone *zone;
  222. zone = page_zone(page);
  223. local_irq_save(flags);
  224. __inc_zone_state(zone, item);
  225. local_irq_restore(flags);
  226. }
  227. EXPORT_SYMBOL(inc_zone_page_state);
  228. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  229. {
  230. unsigned long flags;
  231. local_irq_save(flags);
  232. __dec_zone_page_state(page, item);
  233. local_irq_restore(flags);
  234. }
  235. EXPORT_SYMBOL(dec_zone_page_state);
  236. /*
  237. * Update the zone counters for one 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. unsigned long flags;
  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. local_irq_save(flags);
  263. zone_page_state_add(p->vm_stat_diff[i],
  264. zone, i);
  265. p->vm_stat_diff[i] = 0;
  266. #ifdef CONFIG_NUMA
  267. /* 3 seconds idle till flush */
  268. p->expire = 3;
  269. #endif
  270. local_irq_restore(flags);
  271. }
  272. #ifdef CONFIG_NUMA
  273. /*
  274. * Deal with draining the remote pageset of this
  275. * processor
  276. *
  277. * Check if there are pages remaining in this pageset
  278. * if not then there is nothing to expire.
  279. */
  280. if (!p->expire || (!p->pcp[0].count && !p->pcp[1].count))
  281. continue;
  282. /*
  283. * We never drain zones local to this processor.
  284. */
  285. if (zone_to_nid(zone) == numa_node_id()) {
  286. p->expire = 0;
  287. continue;
  288. }
  289. p->expire--;
  290. if (p->expire)
  291. continue;
  292. if (p->pcp[0].count)
  293. drain_zone_pages(zone, p->pcp + 0);
  294. if (p->pcp[1].count)
  295. drain_zone_pages(zone, p->pcp + 1);
  296. #endif
  297. }
  298. }
  299. static void __refresh_cpu_vm_stats(void *dummy)
  300. {
  301. refresh_cpu_vm_stats(smp_processor_id());
  302. }
  303. /*
  304. * Consolidate all counters.
  305. *
  306. * Note that the result is less inaccurate but still inaccurate
  307. * if concurrent processes are allowed to run.
  308. */
  309. void refresh_vm_stats(void)
  310. {
  311. on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
  312. }
  313. EXPORT_SYMBOL(refresh_vm_stats);
  314. #endif
  315. #ifdef CONFIG_NUMA
  316. /*
  317. * zonelist = the list of zones passed to the allocator
  318. * z = the zone from which the allocation occurred.
  319. *
  320. * Must be called with interrupts disabled.
  321. */
  322. void zone_statistics(struct zonelist *zonelist, struct zone *z)
  323. {
  324. if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
  325. __inc_zone_state(z, NUMA_HIT);
  326. } else {
  327. __inc_zone_state(z, NUMA_MISS);
  328. __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
  329. }
  330. if (z->node == numa_node_id())
  331. __inc_zone_state(z, NUMA_LOCAL);
  332. else
  333. __inc_zone_state(z, NUMA_OTHER);
  334. }
  335. #endif
  336. #ifdef CONFIG_PROC_FS
  337. #include <linux/seq_file.h>
  338. static void *frag_start(struct seq_file *m, loff_t *pos)
  339. {
  340. pg_data_t *pgdat;
  341. loff_t node = *pos;
  342. for (pgdat = first_online_pgdat();
  343. pgdat && node;
  344. pgdat = next_online_pgdat(pgdat))
  345. --node;
  346. return pgdat;
  347. }
  348. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  349. {
  350. pg_data_t *pgdat = (pg_data_t *)arg;
  351. (*pos)++;
  352. return next_online_pgdat(pgdat);
  353. }
  354. static void frag_stop(struct seq_file *m, void *arg)
  355. {
  356. }
  357. /*
  358. * This walks the free areas for each zone.
  359. */
  360. static int frag_show(struct seq_file *m, void *arg)
  361. {
  362. pg_data_t *pgdat = (pg_data_t *)arg;
  363. struct zone *zone;
  364. struct zone *node_zones = pgdat->node_zones;
  365. unsigned long flags;
  366. int order;
  367. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  368. if (!populated_zone(zone))
  369. continue;
  370. spin_lock_irqsave(&zone->lock, flags);
  371. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  372. for (order = 0; order < MAX_ORDER; ++order)
  373. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  374. spin_unlock_irqrestore(&zone->lock, flags);
  375. seq_putc(m, '\n');
  376. }
  377. return 0;
  378. }
  379. const struct seq_operations fragmentation_op = {
  380. .start = frag_start,
  381. .next = frag_next,
  382. .stop = frag_stop,
  383. .show = frag_show,
  384. };
  385. #ifdef CONFIG_ZONE_DMA
  386. #define TEXT_FOR_DMA(xx) xx "_dma",
  387. #else
  388. #define TEXT_FOR_DMA(xx)
  389. #endif
  390. #ifdef CONFIG_ZONE_DMA32
  391. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  392. #else
  393. #define TEXT_FOR_DMA32(xx)
  394. #endif
  395. #ifdef CONFIG_HIGHMEM
  396. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  397. #else
  398. #define TEXT_FOR_HIGHMEM(xx)
  399. #endif
  400. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  401. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  402. static const char * const vmstat_text[] = {
  403. /* Zoned VM counters */
  404. "nr_free_pages",
  405. "nr_inactive",
  406. "nr_active",
  407. "nr_anon_pages",
  408. "nr_mapped",
  409. "nr_file_pages",
  410. "nr_dirty",
  411. "nr_writeback",
  412. "nr_slab_reclaimable",
  413. "nr_slab_unreclaimable",
  414. "nr_page_table_pages",
  415. "nr_unstable",
  416. "nr_bounce",
  417. "nr_vmscan_write",
  418. #ifdef CONFIG_NUMA
  419. "numa_hit",
  420. "numa_miss",
  421. "numa_foreign",
  422. "numa_interleave",
  423. "numa_local",
  424. "numa_other",
  425. #endif
  426. #ifdef CONFIG_VM_EVENT_COUNTERS
  427. "pgpgin",
  428. "pgpgout",
  429. "pswpin",
  430. "pswpout",
  431. TEXTS_FOR_ZONES("pgalloc")
  432. "pgfree",
  433. "pgactivate",
  434. "pgdeactivate",
  435. "pgfault",
  436. "pgmajfault",
  437. TEXTS_FOR_ZONES("pgrefill")
  438. TEXTS_FOR_ZONES("pgsteal")
  439. TEXTS_FOR_ZONES("pgscan_kswapd")
  440. TEXTS_FOR_ZONES("pgscan_direct")
  441. "pginodesteal",
  442. "slabs_scanned",
  443. "kswapd_steal",
  444. "kswapd_inodesteal",
  445. "pageoutrun",
  446. "allocstall",
  447. "pgrotated",
  448. #endif
  449. };
  450. /*
  451. * Output information about zones in @pgdat.
  452. */
  453. static int zoneinfo_show(struct seq_file *m, void *arg)
  454. {
  455. pg_data_t *pgdat = arg;
  456. struct zone *zone;
  457. struct zone *node_zones = pgdat->node_zones;
  458. unsigned long flags;
  459. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
  460. int i;
  461. if (!populated_zone(zone))
  462. continue;
  463. spin_lock_irqsave(&zone->lock, flags);
  464. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  465. seq_printf(m,
  466. "\n pages free %lu"
  467. "\n min %lu"
  468. "\n low %lu"
  469. "\n high %lu"
  470. "\n scanned %lu (a: %lu i: %lu)"
  471. "\n spanned %lu"
  472. "\n present %lu",
  473. zone_page_state(zone, NR_FREE_PAGES),
  474. zone->pages_min,
  475. zone->pages_low,
  476. zone->pages_high,
  477. zone->pages_scanned,
  478. zone->nr_scan_active, zone->nr_scan_inactive,
  479. zone->spanned_pages,
  480. zone->present_pages);
  481. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  482. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  483. zone_page_state(zone, i));
  484. seq_printf(m,
  485. "\n protection: (%lu",
  486. zone->lowmem_reserve[0]);
  487. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  488. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  489. seq_printf(m,
  490. ")"
  491. "\n pagesets");
  492. for_each_online_cpu(i) {
  493. struct per_cpu_pageset *pageset;
  494. int j;
  495. pageset = zone_pcp(zone, i);
  496. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  497. seq_printf(m,
  498. "\n cpu: %i pcp: %i"
  499. "\n count: %i"
  500. "\n high: %i"
  501. "\n batch: %i",
  502. i, j,
  503. pageset->pcp[j].count,
  504. pageset->pcp[j].high,
  505. pageset->pcp[j].batch);
  506. }
  507. #ifdef CONFIG_SMP
  508. seq_printf(m, "\n vm stats threshold: %d",
  509. pageset->stat_threshold);
  510. #endif
  511. }
  512. seq_printf(m,
  513. "\n all_unreclaimable: %u"
  514. "\n prev_priority: %i"
  515. "\n start_pfn: %lu",
  516. zone->all_unreclaimable,
  517. zone->prev_priority,
  518. zone->zone_start_pfn);
  519. spin_unlock_irqrestore(&zone->lock, flags);
  520. seq_putc(m, '\n');
  521. }
  522. return 0;
  523. }
  524. const struct seq_operations zoneinfo_op = {
  525. .start = frag_start, /* iterate over all zones. The same as in
  526. * fragmentation. */
  527. .next = frag_next,
  528. .stop = frag_stop,
  529. .show = zoneinfo_show,
  530. };
  531. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  532. {
  533. unsigned long *v;
  534. #ifdef CONFIG_VM_EVENT_COUNTERS
  535. unsigned long *e;
  536. #endif
  537. int i;
  538. if (*pos >= ARRAY_SIZE(vmstat_text))
  539. return NULL;
  540. #ifdef CONFIG_VM_EVENT_COUNTERS
  541. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
  542. + sizeof(struct vm_event_state), GFP_KERNEL);
  543. #else
  544. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
  545. GFP_KERNEL);
  546. #endif
  547. m->private = v;
  548. if (!v)
  549. return ERR_PTR(-ENOMEM);
  550. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  551. v[i] = global_page_state(i);
  552. #ifdef CONFIG_VM_EVENT_COUNTERS
  553. e = v + NR_VM_ZONE_STAT_ITEMS;
  554. all_vm_events(e);
  555. e[PGPGIN] /= 2; /* sectors -> kbytes */
  556. e[PGPGOUT] /= 2;
  557. #endif
  558. return v + *pos;
  559. }
  560. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  561. {
  562. (*pos)++;
  563. if (*pos >= ARRAY_SIZE(vmstat_text))
  564. return NULL;
  565. return (unsigned long *)m->private + *pos;
  566. }
  567. static int vmstat_show(struct seq_file *m, void *arg)
  568. {
  569. unsigned long *l = arg;
  570. unsigned long off = l - (unsigned long *)m->private;
  571. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  572. return 0;
  573. }
  574. static void vmstat_stop(struct seq_file *m, void *arg)
  575. {
  576. kfree(m->private);
  577. m->private = NULL;
  578. }
  579. const struct seq_operations vmstat_op = {
  580. .start = vmstat_start,
  581. .next = vmstat_next,
  582. .stop = vmstat_stop,
  583. .show = vmstat_show,
  584. };
  585. #endif /* CONFIG_PROC_FS */
  586. #ifdef CONFIG_SMP
  587. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  588. int sysctl_stat_interval __read_mostly = HZ;
  589. static void vmstat_update(struct work_struct *w)
  590. {
  591. refresh_cpu_vm_stats(smp_processor_id());
  592. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  593. sysctl_stat_interval);
  594. }
  595. static void __devinit start_cpu_timer(int cpu)
  596. {
  597. struct delayed_work *vmstat_work = &per_cpu(vmstat_work, cpu);
  598. INIT_DELAYED_WORK_DEFERRABLE(vmstat_work, vmstat_update);
  599. schedule_delayed_work_on(cpu, vmstat_work, HZ + cpu);
  600. }
  601. /*
  602. * Use the cpu notifier to insure that the thresholds are recalculated
  603. * when necessary.
  604. */
  605. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  606. unsigned long action,
  607. void *hcpu)
  608. {
  609. long cpu = (long)hcpu;
  610. switch (action) {
  611. case CPU_ONLINE:
  612. case CPU_ONLINE_FROZEN:
  613. start_cpu_timer(cpu);
  614. break;
  615. case CPU_DOWN_PREPARE:
  616. case CPU_DOWN_PREPARE_FROZEN:
  617. cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
  618. per_cpu(vmstat_work, cpu).work.func = NULL;
  619. break;
  620. case CPU_DOWN_FAILED:
  621. case CPU_DOWN_FAILED_FROZEN:
  622. start_cpu_timer(cpu);
  623. break;
  624. case CPU_DEAD:
  625. case CPU_DEAD_FROZEN:
  626. refresh_zone_stat_thresholds();
  627. break;
  628. default:
  629. break;
  630. }
  631. return NOTIFY_OK;
  632. }
  633. static struct notifier_block __cpuinitdata vmstat_notifier =
  634. { &vmstat_cpuup_callback, NULL, 0 };
  635. int __init setup_vmstat(void)
  636. {
  637. int cpu;
  638. refresh_zone_stat_thresholds();
  639. register_cpu_notifier(&vmstat_notifier);
  640. for_each_online_cpu(cpu)
  641. start_cpu_timer(cpu);
  642. return 0;
  643. }
  644. module_init(setup_vmstat)
  645. #endif