vmstat.c 29 KB

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