vmstat.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/cpu.h>
  17. #include <linux/vmstat.h>
  18. #include <linux/sched.h>
  19. #include <linux/math64.h>
  20. #include <linux/writeback.h>
  21. #include <linux/compaction.h>
  22. #ifdef CONFIG_VM_EVENT_COUNTERS
  23. DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
  24. EXPORT_PER_CPU_SYMBOL(vm_event_states);
  25. static void sum_vm_events(unsigned long *ret)
  26. {
  27. int cpu;
  28. int i;
  29. memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
  30. for_each_online_cpu(cpu) {
  31. struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
  32. for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
  33. ret[i] += this->event[i];
  34. }
  35. }
  36. /*
  37. * Accumulate the vm event counters across all CPUs.
  38. * The result is unavoidably approximate - it can change
  39. * during and after execution of this function.
  40. */
  41. void all_vm_events(unsigned long *ret)
  42. {
  43. get_online_cpus();
  44. sum_vm_events(ret);
  45. put_online_cpus();
  46. }
  47. EXPORT_SYMBOL_GPL(all_vm_events);
  48. #ifdef CONFIG_HOTPLUG
  49. /*
  50. * Fold the foreign cpu events into our own.
  51. *
  52. * This is adding to the events on one processor
  53. * but keeps the global counts constant.
  54. */
  55. void vm_events_fold_cpu(int cpu)
  56. {
  57. struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
  58. int i;
  59. for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
  60. count_vm_events(i, fold_state->event[i]);
  61. fold_state->event[i] = 0;
  62. }
  63. }
  64. #endif /* CONFIG_HOTPLUG */
  65. #endif /* CONFIG_VM_EVENT_COUNTERS */
  66. /*
  67. * Manage combined zone based / global counters
  68. *
  69. * vm_stat contains the global counters
  70. */
  71. atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
  72. EXPORT_SYMBOL(vm_stat);
  73. #ifdef CONFIG_SMP
  74. static int calculate_threshold(struct zone *zone)
  75. {
  76. int threshold;
  77. int mem; /* memory in 128 MB units */
  78. /*
  79. * The threshold scales with the number of processors and the amount
  80. * of memory per zone. More memory means that we can defer updates for
  81. * longer, more processors could lead to more contention.
  82. * fls() is used to have a cheap way of logarithmic scaling.
  83. *
  84. * Some sample thresholds:
  85. *
  86. * Threshold Processors (fls) Zonesize fls(mem+1)
  87. * ------------------------------------------------------------------
  88. * 8 1 1 0.9-1 GB 4
  89. * 16 2 2 0.9-1 GB 4
  90. * 20 2 2 1-2 GB 5
  91. * 24 2 2 2-4 GB 6
  92. * 28 2 2 4-8 GB 7
  93. * 32 2 2 8-16 GB 8
  94. * 4 2 2 <128M 1
  95. * 30 4 3 2-4 GB 5
  96. * 48 4 3 8-16 GB 8
  97. * 32 8 4 1-2 GB 4
  98. * 32 8 4 0.9-1GB 4
  99. * 10 16 5 <128M 1
  100. * 40 16 5 900M 4
  101. * 70 64 7 2-4 GB 5
  102. * 84 64 7 4-8 GB 6
  103. * 108 512 9 4-8 GB 6
  104. * 125 1024 10 8-16 GB 8
  105. * 125 1024 10 16-32 GB 9
  106. */
  107. mem = zone->present_pages >> (27 - PAGE_SHIFT);
  108. threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
  109. /*
  110. * Maximum threshold is 125
  111. */
  112. threshold = min(125, threshold);
  113. return threshold;
  114. }
  115. /*
  116. * Refresh the thresholds for each zone.
  117. */
  118. static void refresh_zone_stat_thresholds(void)
  119. {
  120. struct zone *zone;
  121. int cpu;
  122. int threshold;
  123. for_each_populated_zone(zone) {
  124. unsigned long max_drift, tolerate_drift;
  125. threshold = calculate_threshold(zone);
  126. for_each_online_cpu(cpu)
  127. per_cpu_ptr(zone->pageset, cpu)->stat_threshold
  128. = threshold;
  129. /*
  130. * Only set percpu_drift_mark if there is a danger that
  131. * NR_FREE_PAGES reports the low watermark is ok when in fact
  132. * the min watermark could be breached by an allocation
  133. */
  134. tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
  135. max_drift = num_online_cpus() * threshold;
  136. if (max_drift > tolerate_drift)
  137. zone->percpu_drift_mark = high_wmark_pages(zone) +
  138. max_drift;
  139. }
  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. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  148. s8 __percpu *p = pcp->vm_stat_diff + item;
  149. long x;
  150. long t;
  151. x = delta + __this_cpu_read(*p);
  152. t = __this_cpu_read(pcp->stat_threshold);
  153. if (unlikely(x > t || x < -t)) {
  154. zone_page_state_add(x, zone, item);
  155. x = 0;
  156. }
  157. __this_cpu_write(*p, x);
  158. }
  159. EXPORT_SYMBOL(__mod_zone_page_state);
  160. /*
  161. * For an unknown interrupt state
  162. */
  163. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  164. int delta)
  165. {
  166. unsigned long flags;
  167. local_irq_save(flags);
  168. __mod_zone_page_state(zone, item, delta);
  169. local_irq_restore(flags);
  170. }
  171. EXPORT_SYMBOL(mod_zone_page_state);
  172. /*
  173. * Optimized increment and decrement functions.
  174. *
  175. * These are only for a single page and therefore can take a struct page *
  176. * argument instead of struct zone *. This allows the inclusion of the code
  177. * generated for page_zone(page) into the optimized functions.
  178. *
  179. * No overflow check is necessary and therefore the differential can be
  180. * incremented or decremented in place which may allow the compilers to
  181. * generate better code.
  182. * The increment or decrement is known and therefore one boundary check can
  183. * be omitted.
  184. *
  185. * NOTE: These functions are very performance sensitive. Change only
  186. * with care.
  187. *
  188. * Some processors have inc/dec instructions that are atomic vs an interrupt.
  189. * However, the code must first determine the differential location in a zone
  190. * based on the processor number and then inc/dec the counter. There is no
  191. * guarantee without disabling preemption that the processor will not change
  192. * in between and therefore the atomicity vs. interrupt cannot be exploited
  193. * in a useful way here.
  194. */
  195. void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  196. {
  197. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  198. s8 __percpu *p = pcp->vm_stat_diff + item;
  199. s8 v, t;
  200. __this_cpu_inc(*p);
  201. v = __this_cpu_read(*p);
  202. t = __this_cpu_read(pcp->stat_threshold);
  203. if (unlikely(v > t)) {
  204. s8 overstep = t >> 1;
  205. zone_page_state_add(v + overstep, zone, item);
  206. __this_cpu_write(*p, -overstep);
  207. }
  208. }
  209. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  210. {
  211. __inc_zone_state(page_zone(page), item);
  212. }
  213. EXPORT_SYMBOL(__inc_zone_page_state);
  214. void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  215. {
  216. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  217. s8 __percpu *p = pcp->vm_stat_diff + item;
  218. s8 v, t;
  219. __this_cpu_dec(*p);
  220. v = __this_cpu_read(*p);
  221. t = __this_cpu_read(pcp->stat_threshold);
  222. if (unlikely(v < - t)) {
  223. s8 overstep = t >> 1;
  224. zone_page_state_add(v - overstep, zone, item);
  225. __this_cpu_write(*p, overstep);
  226. }
  227. }
  228. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  229. {
  230. __dec_zone_state(page_zone(page), item);
  231. }
  232. EXPORT_SYMBOL(__dec_zone_page_state);
  233. void inc_zone_state(struct zone *zone, enum zone_stat_item item)
  234. {
  235. unsigned long flags;
  236. local_irq_save(flags);
  237. __inc_zone_state(zone, item);
  238. local_irq_restore(flags);
  239. }
  240. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  241. {
  242. unsigned long flags;
  243. struct zone *zone;
  244. zone = page_zone(page);
  245. local_irq_save(flags);
  246. __inc_zone_state(zone, item);
  247. local_irq_restore(flags);
  248. }
  249. EXPORT_SYMBOL(inc_zone_page_state);
  250. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  251. {
  252. unsigned long flags;
  253. local_irq_save(flags);
  254. __dec_zone_page_state(page, item);
  255. local_irq_restore(flags);
  256. }
  257. EXPORT_SYMBOL(dec_zone_page_state);
  258. /*
  259. * Update the zone counters for one cpu.
  260. *
  261. * The cpu specified must be either the current cpu or a processor that
  262. * is not online. If it is the current cpu then the execution thread must
  263. * be pinned to the current cpu.
  264. *
  265. * Note that refresh_cpu_vm_stats strives to only access
  266. * node local memory. The per cpu pagesets on remote zones are placed
  267. * in the memory local to the processor using that pageset. So the
  268. * loop over all zones will access a series of cachelines local to
  269. * the processor.
  270. *
  271. * The call to zone_page_state_add updates the cachelines with the
  272. * statistics in the remote zone struct as well as the global cachelines
  273. * with the global counters. These could cause remote node cache line
  274. * bouncing and will have to be only done when necessary.
  275. */
  276. void refresh_cpu_vm_stats(int cpu)
  277. {
  278. struct zone *zone;
  279. int i;
  280. int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
  281. for_each_populated_zone(zone) {
  282. struct per_cpu_pageset *p;
  283. p = per_cpu_ptr(zone->pageset, cpu);
  284. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  285. if (p->vm_stat_diff[i]) {
  286. unsigned long flags;
  287. int v;
  288. local_irq_save(flags);
  289. v = p->vm_stat_diff[i];
  290. p->vm_stat_diff[i] = 0;
  291. local_irq_restore(flags);
  292. atomic_long_add(v, &zone->vm_stat[i]);
  293. global_diff[i] += v;
  294. #ifdef CONFIG_NUMA
  295. /* 3 seconds idle till flush */
  296. p->expire = 3;
  297. #endif
  298. }
  299. cond_resched();
  300. #ifdef CONFIG_NUMA
  301. /*
  302. * Deal with draining the remote pageset of this
  303. * processor
  304. *
  305. * Check if there are pages remaining in this pageset
  306. * if not then there is nothing to expire.
  307. */
  308. if (!p->expire || !p->pcp.count)
  309. continue;
  310. /*
  311. * We never drain zones local to this processor.
  312. */
  313. if (zone_to_nid(zone) == numa_node_id()) {
  314. p->expire = 0;
  315. continue;
  316. }
  317. p->expire--;
  318. if (p->expire)
  319. continue;
  320. if (p->pcp.count)
  321. drain_zone_pages(zone, &p->pcp);
  322. #endif
  323. }
  324. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  325. if (global_diff[i])
  326. atomic_long_add(global_diff[i], &vm_stat[i]);
  327. }
  328. #endif
  329. #ifdef CONFIG_NUMA
  330. /*
  331. * zonelist = the list of zones passed to the allocator
  332. * z = the zone from which the allocation occurred.
  333. *
  334. * Must be called with interrupts disabled.
  335. */
  336. void zone_statistics(struct zone *preferred_zone, struct zone *z)
  337. {
  338. if (z->zone_pgdat == preferred_zone->zone_pgdat) {
  339. __inc_zone_state(z, NUMA_HIT);
  340. } else {
  341. __inc_zone_state(z, NUMA_MISS);
  342. __inc_zone_state(preferred_zone, NUMA_FOREIGN);
  343. }
  344. if (z->node == numa_node_id())
  345. __inc_zone_state(z, NUMA_LOCAL);
  346. else
  347. __inc_zone_state(z, NUMA_OTHER);
  348. }
  349. #endif
  350. #ifdef CONFIG_COMPACTION
  351. struct contig_page_info {
  352. unsigned long free_pages;
  353. unsigned long free_blocks_total;
  354. unsigned long free_blocks_suitable;
  355. };
  356. /*
  357. * Calculate the number of free pages in a zone, how many contiguous
  358. * pages are free and how many are large enough to satisfy an allocation of
  359. * the target size. Note that this function makes no attempt to estimate
  360. * how many suitable free blocks there *might* be if MOVABLE pages were
  361. * migrated. Calculating that is possible, but expensive and can be
  362. * figured out from userspace
  363. */
  364. static void fill_contig_page_info(struct zone *zone,
  365. unsigned int suitable_order,
  366. struct contig_page_info *info)
  367. {
  368. unsigned int order;
  369. info->free_pages = 0;
  370. info->free_blocks_total = 0;
  371. info->free_blocks_suitable = 0;
  372. for (order = 0; order < MAX_ORDER; order++) {
  373. unsigned long blocks;
  374. /* Count number of free blocks */
  375. blocks = zone->free_area[order].nr_free;
  376. info->free_blocks_total += blocks;
  377. /* Count free base pages */
  378. info->free_pages += blocks << order;
  379. /* Count the suitable free blocks */
  380. if (order >= suitable_order)
  381. info->free_blocks_suitable += blocks <<
  382. (order - suitable_order);
  383. }
  384. }
  385. /*
  386. * A fragmentation index only makes sense if an allocation of a requested
  387. * size would fail. If that is true, the fragmentation index indicates
  388. * whether external fragmentation or a lack of memory was the problem.
  389. * The value can be used to determine if page reclaim or compaction
  390. * should be used
  391. */
  392. static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
  393. {
  394. unsigned long requested = 1UL << order;
  395. if (!info->free_blocks_total)
  396. return 0;
  397. /* Fragmentation index only makes sense when a request would fail */
  398. if (info->free_blocks_suitable)
  399. return -1000;
  400. /*
  401. * Index is between 0 and 1 so return within 3 decimal places
  402. *
  403. * 0 => allocation would fail due to lack of memory
  404. * 1 => allocation would fail due to fragmentation
  405. */
  406. return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
  407. }
  408. /* Same as __fragmentation index but allocs contig_page_info on stack */
  409. int fragmentation_index(struct zone *zone, unsigned int order)
  410. {
  411. struct contig_page_info info;
  412. fill_contig_page_info(zone, order, &info);
  413. return __fragmentation_index(order, &info);
  414. }
  415. #endif
  416. #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
  417. #include <linux/proc_fs.h>
  418. #include <linux/seq_file.h>
  419. static char * const migratetype_names[MIGRATE_TYPES] = {
  420. "Unmovable",
  421. "Reclaimable",
  422. "Movable",
  423. "Reserve",
  424. "Isolate",
  425. };
  426. static void *frag_start(struct seq_file *m, loff_t *pos)
  427. {
  428. pg_data_t *pgdat;
  429. loff_t node = *pos;
  430. for (pgdat = first_online_pgdat();
  431. pgdat && node;
  432. pgdat = next_online_pgdat(pgdat))
  433. --node;
  434. return pgdat;
  435. }
  436. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  437. {
  438. pg_data_t *pgdat = (pg_data_t *)arg;
  439. (*pos)++;
  440. return next_online_pgdat(pgdat);
  441. }
  442. static void frag_stop(struct seq_file *m, void *arg)
  443. {
  444. }
  445. /* Walk all the zones in a node and print using a callback */
  446. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  447. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  448. {
  449. struct zone *zone;
  450. struct zone *node_zones = pgdat->node_zones;
  451. unsigned long flags;
  452. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  453. if (!populated_zone(zone))
  454. continue;
  455. spin_lock_irqsave(&zone->lock, flags);
  456. print(m, pgdat, zone);
  457. spin_unlock_irqrestore(&zone->lock, flags);
  458. }
  459. }
  460. #endif
  461. #ifdef CONFIG_PROC_FS
  462. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  463. struct zone *zone)
  464. {
  465. int order;
  466. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  467. for (order = 0; order < MAX_ORDER; ++order)
  468. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  469. seq_putc(m, '\n');
  470. }
  471. /*
  472. * This walks the free areas for each zone.
  473. */
  474. static int frag_show(struct seq_file *m, void *arg)
  475. {
  476. pg_data_t *pgdat = (pg_data_t *)arg;
  477. walk_zones_in_node(m, pgdat, frag_show_print);
  478. return 0;
  479. }
  480. static void pagetypeinfo_showfree_print(struct seq_file *m,
  481. pg_data_t *pgdat, struct zone *zone)
  482. {
  483. int order, mtype;
  484. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  485. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  486. pgdat->node_id,
  487. zone->name,
  488. migratetype_names[mtype]);
  489. for (order = 0; order < MAX_ORDER; ++order) {
  490. unsigned long freecount = 0;
  491. struct free_area *area;
  492. struct list_head *curr;
  493. area = &(zone->free_area[order]);
  494. list_for_each(curr, &area->free_list[mtype])
  495. freecount++;
  496. seq_printf(m, "%6lu ", freecount);
  497. }
  498. seq_putc(m, '\n');
  499. }
  500. }
  501. /* Print out the free pages at each order for each migatetype */
  502. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  503. {
  504. int order;
  505. pg_data_t *pgdat = (pg_data_t *)arg;
  506. /* Print header */
  507. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  508. for (order = 0; order < MAX_ORDER; ++order)
  509. seq_printf(m, "%6d ", order);
  510. seq_putc(m, '\n');
  511. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  512. return 0;
  513. }
  514. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  515. pg_data_t *pgdat, struct zone *zone)
  516. {
  517. int mtype;
  518. unsigned long pfn;
  519. unsigned long start_pfn = zone->zone_start_pfn;
  520. unsigned long end_pfn = start_pfn + zone->spanned_pages;
  521. unsigned long count[MIGRATE_TYPES] = { 0, };
  522. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  523. struct page *page;
  524. if (!pfn_valid(pfn))
  525. continue;
  526. page = pfn_to_page(pfn);
  527. /* Watch for unexpected holes punched in the memmap */
  528. if (!memmap_valid_within(pfn, page, zone))
  529. continue;
  530. mtype = get_pageblock_migratetype(page);
  531. if (mtype < MIGRATE_TYPES)
  532. count[mtype]++;
  533. }
  534. /* Print counts */
  535. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  536. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  537. seq_printf(m, "%12lu ", count[mtype]);
  538. seq_putc(m, '\n');
  539. }
  540. /* Print out the free pages at each order for each migratetype */
  541. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  542. {
  543. int mtype;
  544. pg_data_t *pgdat = (pg_data_t *)arg;
  545. seq_printf(m, "\n%-23s", "Number of blocks type ");
  546. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  547. seq_printf(m, "%12s ", migratetype_names[mtype]);
  548. seq_putc(m, '\n');
  549. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  550. return 0;
  551. }
  552. /*
  553. * This prints out statistics in relation to grouping pages by mobility.
  554. * It is expensive to collect so do not constantly read the file.
  555. */
  556. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  557. {
  558. pg_data_t *pgdat = (pg_data_t *)arg;
  559. /* check memoryless node */
  560. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  561. return 0;
  562. seq_printf(m, "Page block order: %d\n", pageblock_order);
  563. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  564. seq_putc(m, '\n');
  565. pagetypeinfo_showfree(m, pgdat);
  566. pagetypeinfo_showblockcount(m, pgdat);
  567. return 0;
  568. }
  569. static const struct seq_operations fragmentation_op = {
  570. .start = frag_start,
  571. .next = frag_next,
  572. .stop = frag_stop,
  573. .show = frag_show,
  574. };
  575. static int fragmentation_open(struct inode *inode, struct file *file)
  576. {
  577. return seq_open(file, &fragmentation_op);
  578. }
  579. static const struct file_operations fragmentation_file_operations = {
  580. .open = fragmentation_open,
  581. .read = seq_read,
  582. .llseek = seq_lseek,
  583. .release = seq_release,
  584. };
  585. static const struct seq_operations pagetypeinfo_op = {
  586. .start = frag_start,
  587. .next = frag_next,
  588. .stop = frag_stop,
  589. .show = pagetypeinfo_show,
  590. };
  591. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  592. {
  593. return seq_open(file, &pagetypeinfo_op);
  594. }
  595. static const struct file_operations pagetypeinfo_file_ops = {
  596. .open = pagetypeinfo_open,
  597. .read = seq_read,
  598. .llseek = seq_lseek,
  599. .release = seq_release,
  600. };
  601. #ifdef CONFIG_ZONE_DMA
  602. #define TEXT_FOR_DMA(xx) xx "_dma",
  603. #else
  604. #define TEXT_FOR_DMA(xx)
  605. #endif
  606. #ifdef CONFIG_ZONE_DMA32
  607. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  608. #else
  609. #define TEXT_FOR_DMA32(xx)
  610. #endif
  611. #ifdef CONFIG_HIGHMEM
  612. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  613. #else
  614. #define TEXT_FOR_HIGHMEM(xx)
  615. #endif
  616. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  617. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  618. static const char * const vmstat_text[] = {
  619. /* Zoned VM counters */
  620. "nr_free_pages",
  621. "nr_inactive_anon",
  622. "nr_active_anon",
  623. "nr_inactive_file",
  624. "nr_active_file",
  625. "nr_unevictable",
  626. "nr_mlock",
  627. "nr_anon_pages",
  628. "nr_mapped",
  629. "nr_file_pages",
  630. "nr_dirty",
  631. "nr_writeback",
  632. "nr_slab_reclaimable",
  633. "nr_slab_unreclaimable",
  634. "nr_page_table_pages",
  635. "nr_kernel_stack",
  636. "nr_unstable",
  637. "nr_bounce",
  638. "nr_vmscan_write",
  639. "nr_writeback_temp",
  640. "nr_isolated_anon",
  641. "nr_isolated_file",
  642. "nr_shmem",
  643. "nr_dirtied",
  644. "nr_written",
  645. #ifdef CONFIG_NUMA
  646. "numa_hit",
  647. "numa_miss",
  648. "numa_foreign",
  649. "numa_interleave",
  650. "numa_local",
  651. "numa_other",
  652. #endif
  653. "nr_dirty_threshold",
  654. "nr_dirty_background_threshold",
  655. #ifdef CONFIG_VM_EVENT_COUNTERS
  656. "pgpgin",
  657. "pgpgout",
  658. "pswpin",
  659. "pswpout",
  660. TEXTS_FOR_ZONES("pgalloc")
  661. "pgfree",
  662. "pgactivate",
  663. "pgdeactivate",
  664. "pgfault",
  665. "pgmajfault",
  666. TEXTS_FOR_ZONES("pgrefill")
  667. TEXTS_FOR_ZONES("pgsteal")
  668. TEXTS_FOR_ZONES("pgscan_kswapd")
  669. TEXTS_FOR_ZONES("pgscan_direct")
  670. #ifdef CONFIG_NUMA
  671. "zone_reclaim_failed",
  672. #endif
  673. "pginodesteal",
  674. "slabs_scanned",
  675. "kswapd_steal",
  676. "kswapd_inodesteal",
  677. "kswapd_low_wmark_hit_quickly",
  678. "kswapd_high_wmark_hit_quickly",
  679. "kswapd_skip_congestion_wait",
  680. "pageoutrun",
  681. "allocstall",
  682. "pgrotated",
  683. #ifdef CONFIG_COMPACTION
  684. "compact_blocks_moved",
  685. "compact_pages_moved",
  686. "compact_pagemigrate_failed",
  687. "compact_stall",
  688. "compact_fail",
  689. "compact_success",
  690. #endif
  691. #ifdef CONFIG_HUGETLB_PAGE
  692. "htlb_buddy_alloc_success",
  693. "htlb_buddy_alloc_fail",
  694. #endif
  695. "unevictable_pgs_culled",
  696. "unevictable_pgs_scanned",
  697. "unevictable_pgs_rescued",
  698. "unevictable_pgs_mlocked",
  699. "unevictable_pgs_munlocked",
  700. "unevictable_pgs_cleared",
  701. "unevictable_pgs_stranded",
  702. "unevictable_pgs_mlockfreed",
  703. #endif
  704. };
  705. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  706. struct zone *zone)
  707. {
  708. int i;
  709. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  710. seq_printf(m,
  711. "\n pages free %lu"
  712. "\n min %lu"
  713. "\n low %lu"
  714. "\n high %lu"
  715. "\n scanned %lu"
  716. "\n spanned %lu"
  717. "\n present %lu",
  718. zone_nr_free_pages(zone),
  719. min_wmark_pages(zone),
  720. low_wmark_pages(zone),
  721. high_wmark_pages(zone),
  722. zone->pages_scanned,
  723. zone->spanned_pages,
  724. zone->present_pages);
  725. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  726. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  727. zone_page_state(zone, i));
  728. seq_printf(m,
  729. "\n protection: (%lu",
  730. zone->lowmem_reserve[0]);
  731. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  732. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  733. seq_printf(m,
  734. ")"
  735. "\n pagesets");
  736. for_each_online_cpu(i) {
  737. struct per_cpu_pageset *pageset;
  738. pageset = per_cpu_ptr(zone->pageset, i);
  739. seq_printf(m,
  740. "\n cpu: %i"
  741. "\n count: %i"
  742. "\n high: %i"
  743. "\n batch: %i",
  744. i,
  745. pageset->pcp.count,
  746. pageset->pcp.high,
  747. pageset->pcp.batch);
  748. #ifdef CONFIG_SMP
  749. seq_printf(m, "\n vm stats threshold: %d",
  750. pageset->stat_threshold);
  751. #endif
  752. }
  753. seq_printf(m,
  754. "\n all_unreclaimable: %u"
  755. "\n start_pfn: %lu"
  756. "\n inactive_ratio: %u",
  757. zone->all_unreclaimable,
  758. zone->zone_start_pfn,
  759. zone->inactive_ratio);
  760. seq_putc(m, '\n');
  761. }
  762. /*
  763. * Output information about zones in @pgdat.
  764. */
  765. static int zoneinfo_show(struct seq_file *m, void *arg)
  766. {
  767. pg_data_t *pgdat = (pg_data_t *)arg;
  768. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  769. return 0;
  770. }
  771. static const struct seq_operations zoneinfo_op = {
  772. .start = frag_start, /* iterate over all zones. The same as in
  773. * fragmentation. */
  774. .next = frag_next,
  775. .stop = frag_stop,
  776. .show = zoneinfo_show,
  777. };
  778. static int zoneinfo_open(struct inode *inode, struct file *file)
  779. {
  780. return seq_open(file, &zoneinfo_op);
  781. }
  782. static const struct file_operations proc_zoneinfo_file_operations = {
  783. .open = zoneinfo_open,
  784. .read = seq_read,
  785. .llseek = seq_lseek,
  786. .release = seq_release,
  787. };
  788. enum writeback_stat_item {
  789. NR_DIRTY_THRESHOLD,
  790. NR_DIRTY_BG_THRESHOLD,
  791. NR_VM_WRITEBACK_STAT_ITEMS,
  792. };
  793. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  794. {
  795. unsigned long *v;
  796. int i, stat_items_size;
  797. if (*pos >= ARRAY_SIZE(vmstat_text))
  798. return NULL;
  799. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  800. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  801. #ifdef CONFIG_VM_EVENT_COUNTERS
  802. stat_items_size += sizeof(struct vm_event_state);
  803. #endif
  804. v = kmalloc(stat_items_size, GFP_KERNEL);
  805. m->private = v;
  806. if (!v)
  807. return ERR_PTR(-ENOMEM);
  808. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  809. v[i] = global_page_state(i);
  810. v += NR_VM_ZONE_STAT_ITEMS;
  811. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  812. v + NR_DIRTY_THRESHOLD);
  813. v += NR_VM_WRITEBACK_STAT_ITEMS;
  814. #ifdef CONFIG_VM_EVENT_COUNTERS
  815. all_vm_events(v);
  816. v[PGPGIN] /= 2; /* sectors -> kbytes */
  817. v[PGPGOUT] /= 2;
  818. #endif
  819. return (unsigned long *)m->private + *pos;
  820. }
  821. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  822. {
  823. (*pos)++;
  824. if (*pos >= ARRAY_SIZE(vmstat_text))
  825. return NULL;
  826. return (unsigned long *)m->private + *pos;
  827. }
  828. static int vmstat_show(struct seq_file *m, void *arg)
  829. {
  830. unsigned long *l = arg;
  831. unsigned long off = l - (unsigned long *)m->private;
  832. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  833. return 0;
  834. }
  835. static void vmstat_stop(struct seq_file *m, void *arg)
  836. {
  837. kfree(m->private);
  838. m->private = NULL;
  839. }
  840. static const struct seq_operations vmstat_op = {
  841. .start = vmstat_start,
  842. .next = vmstat_next,
  843. .stop = vmstat_stop,
  844. .show = vmstat_show,
  845. };
  846. static int vmstat_open(struct inode *inode, struct file *file)
  847. {
  848. return seq_open(file, &vmstat_op);
  849. }
  850. static const struct file_operations proc_vmstat_file_operations = {
  851. .open = vmstat_open,
  852. .read = seq_read,
  853. .llseek = seq_lseek,
  854. .release = seq_release,
  855. };
  856. #endif /* CONFIG_PROC_FS */
  857. #ifdef CONFIG_SMP
  858. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  859. int sysctl_stat_interval __read_mostly = HZ;
  860. static void vmstat_update(struct work_struct *w)
  861. {
  862. refresh_cpu_vm_stats(smp_processor_id());
  863. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  864. round_jiffies_relative(sysctl_stat_interval));
  865. }
  866. static void __cpuinit start_cpu_timer(int cpu)
  867. {
  868. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  869. INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
  870. schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
  871. }
  872. /*
  873. * Use the cpu notifier to insure that the thresholds are recalculated
  874. * when necessary.
  875. */
  876. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  877. unsigned long action,
  878. void *hcpu)
  879. {
  880. long cpu = (long)hcpu;
  881. switch (action) {
  882. case CPU_ONLINE:
  883. case CPU_ONLINE_FROZEN:
  884. refresh_zone_stat_thresholds();
  885. start_cpu_timer(cpu);
  886. node_set_state(cpu_to_node(cpu), N_CPU);
  887. break;
  888. case CPU_DOWN_PREPARE:
  889. case CPU_DOWN_PREPARE_FROZEN:
  890. cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
  891. per_cpu(vmstat_work, cpu).work.func = NULL;
  892. break;
  893. case CPU_DOWN_FAILED:
  894. case CPU_DOWN_FAILED_FROZEN:
  895. start_cpu_timer(cpu);
  896. break;
  897. case CPU_DEAD:
  898. case CPU_DEAD_FROZEN:
  899. refresh_zone_stat_thresholds();
  900. break;
  901. default:
  902. break;
  903. }
  904. return NOTIFY_OK;
  905. }
  906. static struct notifier_block __cpuinitdata vmstat_notifier =
  907. { &vmstat_cpuup_callback, NULL, 0 };
  908. #endif
  909. static int __init setup_vmstat(void)
  910. {
  911. #ifdef CONFIG_SMP
  912. int cpu;
  913. refresh_zone_stat_thresholds();
  914. register_cpu_notifier(&vmstat_notifier);
  915. for_each_online_cpu(cpu)
  916. start_cpu_timer(cpu);
  917. #endif
  918. #ifdef CONFIG_PROC_FS
  919. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  920. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  921. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  922. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  923. #endif
  924. return 0;
  925. }
  926. module_init(setup_vmstat)
  927. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  928. #include <linux/debugfs.h>
  929. static struct dentry *extfrag_debug_root;
  930. /*
  931. * Return an index indicating how much of the available free memory is
  932. * unusable for an allocation of the requested size.
  933. */
  934. static int unusable_free_index(unsigned int order,
  935. struct contig_page_info *info)
  936. {
  937. /* No free memory is interpreted as all free memory is unusable */
  938. if (info->free_pages == 0)
  939. return 1000;
  940. /*
  941. * Index should be a value between 0 and 1. Return a value to 3
  942. * decimal places.
  943. *
  944. * 0 => no fragmentation
  945. * 1 => high fragmentation
  946. */
  947. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  948. }
  949. static void unusable_show_print(struct seq_file *m,
  950. pg_data_t *pgdat, struct zone *zone)
  951. {
  952. unsigned int order;
  953. int index;
  954. struct contig_page_info info;
  955. seq_printf(m, "Node %d, zone %8s ",
  956. pgdat->node_id,
  957. zone->name);
  958. for (order = 0; order < MAX_ORDER; ++order) {
  959. fill_contig_page_info(zone, order, &info);
  960. index = unusable_free_index(order, &info);
  961. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  962. }
  963. seq_putc(m, '\n');
  964. }
  965. /*
  966. * Display unusable free space index
  967. *
  968. * The unusable free space index measures how much of the available free
  969. * memory cannot be used to satisfy an allocation of a given size and is a
  970. * value between 0 and 1. The higher the value, the more of free memory is
  971. * unusable and by implication, the worse the external fragmentation is. This
  972. * can be expressed as a percentage by multiplying by 100.
  973. */
  974. static int unusable_show(struct seq_file *m, void *arg)
  975. {
  976. pg_data_t *pgdat = (pg_data_t *)arg;
  977. /* check memoryless node */
  978. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  979. return 0;
  980. walk_zones_in_node(m, pgdat, unusable_show_print);
  981. return 0;
  982. }
  983. static const struct seq_operations unusable_op = {
  984. .start = frag_start,
  985. .next = frag_next,
  986. .stop = frag_stop,
  987. .show = unusable_show,
  988. };
  989. static int unusable_open(struct inode *inode, struct file *file)
  990. {
  991. return seq_open(file, &unusable_op);
  992. }
  993. static const struct file_operations unusable_file_ops = {
  994. .open = unusable_open,
  995. .read = seq_read,
  996. .llseek = seq_lseek,
  997. .release = seq_release,
  998. };
  999. static void extfrag_show_print(struct seq_file *m,
  1000. pg_data_t *pgdat, struct zone *zone)
  1001. {
  1002. unsigned int order;
  1003. int index;
  1004. /* Alloc on stack as interrupts are disabled for zone walk */
  1005. struct contig_page_info info;
  1006. seq_printf(m, "Node %d, zone %8s ",
  1007. pgdat->node_id,
  1008. zone->name);
  1009. for (order = 0; order < MAX_ORDER; ++order) {
  1010. fill_contig_page_info(zone, order, &info);
  1011. index = __fragmentation_index(order, &info);
  1012. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1013. }
  1014. seq_putc(m, '\n');
  1015. }
  1016. /*
  1017. * Display fragmentation index for orders that allocations would fail for
  1018. */
  1019. static int extfrag_show(struct seq_file *m, void *arg)
  1020. {
  1021. pg_data_t *pgdat = (pg_data_t *)arg;
  1022. walk_zones_in_node(m, pgdat, extfrag_show_print);
  1023. return 0;
  1024. }
  1025. static const struct seq_operations extfrag_op = {
  1026. .start = frag_start,
  1027. .next = frag_next,
  1028. .stop = frag_stop,
  1029. .show = extfrag_show,
  1030. };
  1031. static int extfrag_open(struct inode *inode, struct file *file)
  1032. {
  1033. return seq_open(file, &extfrag_op);
  1034. }
  1035. static const struct file_operations extfrag_file_ops = {
  1036. .open = extfrag_open,
  1037. .read = seq_read,
  1038. .llseek = seq_lseek,
  1039. .release = seq_release,
  1040. };
  1041. static int __init extfrag_debug_init(void)
  1042. {
  1043. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1044. if (!extfrag_debug_root)
  1045. return -ENOMEM;
  1046. if (!debugfs_create_file("unusable_index", 0444,
  1047. extfrag_debug_root, NULL, &unusable_file_ops))
  1048. return -ENOMEM;
  1049. if (!debugfs_create_file("extfrag_index", 0444,
  1050. extfrag_debug_root, NULL, &extfrag_file_ops))
  1051. return -ENOMEM;
  1052. return 0;
  1053. }
  1054. module_init(extfrag_debug_init);
  1055. #endif