vmstat.c 17 KB

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