vmstat.c 17 KB

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