vmstat.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. 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. #endif
  388. #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
  389. #include <linux/proc_fs.h>
  390. #include <linux/seq_file.h>
  391. static char * const migratetype_names[MIGRATE_TYPES] = {
  392. "Unmovable",
  393. "Reclaimable",
  394. "Movable",
  395. "Reserve",
  396. "Isolate",
  397. };
  398. static void *frag_start(struct seq_file *m, loff_t *pos)
  399. {
  400. pg_data_t *pgdat;
  401. loff_t node = *pos;
  402. for (pgdat = first_online_pgdat();
  403. pgdat && node;
  404. pgdat = next_online_pgdat(pgdat))
  405. --node;
  406. return pgdat;
  407. }
  408. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  409. {
  410. pg_data_t *pgdat = (pg_data_t *)arg;
  411. (*pos)++;
  412. return next_online_pgdat(pgdat);
  413. }
  414. static void frag_stop(struct seq_file *m, void *arg)
  415. {
  416. }
  417. /* Walk all the zones in a node and print using a callback */
  418. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  419. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  420. {
  421. struct zone *zone;
  422. struct zone *node_zones = pgdat->node_zones;
  423. unsigned long flags;
  424. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  425. if (!populated_zone(zone))
  426. continue;
  427. spin_lock_irqsave(&zone->lock, flags);
  428. print(m, pgdat, zone);
  429. spin_unlock_irqrestore(&zone->lock, flags);
  430. }
  431. }
  432. #endif
  433. #ifdef CONFIG_PROC_FS
  434. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  435. struct zone *zone)
  436. {
  437. int order;
  438. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  439. for (order = 0; order < MAX_ORDER; ++order)
  440. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  441. seq_putc(m, '\n');
  442. }
  443. /*
  444. * This walks the free areas for each zone.
  445. */
  446. static int frag_show(struct seq_file *m, void *arg)
  447. {
  448. pg_data_t *pgdat = (pg_data_t *)arg;
  449. walk_zones_in_node(m, pgdat, frag_show_print);
  450. return 0;
  451. }
  452. static void pagetypeinfo_showfree_print(struct seq_file *m,
  453. pg_data_t *pgdat, struct zone *zone)
  454. {
  455. int order, mtype;
  456. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  457. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  458. pgdat->node_id,
  459. zone->name,
  460. migratetype_names[mtype]);
  461. for (order = 0; order < MAX_ORDER; ++order) {
  462. unsigned long freecount = 0;
  463. struct free_area *area;
  464. struct list_head *curr;
  465. area = &(zone->free_area[order]);
  466. list_for_each(curr, &area->free_list[mtype])
  467. freecount++;
  468. seq_printf(m, "%6lu ", freecount);
  469. }
  470. seq_putc(m, '\n');
  471. }
  472. }
  473. /* Print out the free pages at each order for each migatetype */
  474. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  475. {
  476. int order;
  477. pg_data_t *pgdat = (pg_data_t *)arg;
  478. /* Print header */
  479. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  480. for (order = 0; order < MAX_ORDER; ++order)
  481. seq_printf(m, "%6d ", order);
  482. seq_putc(m, '\n');
  483. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  484. return 0;
  485. }
  486. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  487. pg_data_t *pgdat, struct zone *zone)
  488. {
  489. int mtype;
  490. unsigned long pfn;
  491. unsigned long start_pfn = zone->zone_start_pfn;
  492. unsigned long end_pfn = start_pfn + zone->spanned_pages;
  493. unsigned long count[MIGRATE_TYPES] = { 0, };
  494. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  495. struct page *page;
  496. if (!pfn_valid(pfn))
  497. continue;
  498. page = pfn_to_page(pfn);
  499. /* Watch for unexpected holes punched in the memmap */
  500. if (!memmap_valid_within(pfn, page, zone))
  501. continue;
  502. mtype = get_pageblock_migratetype(page);
  503. if (mtype < MIGRATE_TYPES)
  504. count[mtype]++;
  505. }
  506. /* Print counts */
  507. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  508. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  509. seq_printf(m, "%12lu ", count[mtype]);
  510. seq_putc(m, '\n');
  511. }
  512. /* Print out the free pages at each order for each migratetype */
  513. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  514. {
  515. int mtype;
  516. pg_data_t *pgdat = (pg_data_t *)arg;
  517. seq_printf(m, "\n%-23s", "Number of blocks type ");
  518. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  519. seq_printf(m, "%12s ", migratetype_names[mtype]);
  520. seq_putc(m, '\n');
  521. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  522. return 0;
  523. }
  524. /*
  525. * This prints out statistics in relation to grouping pages by mobility.
  526. * It is expensive to collect so do not constantly read the file.
  527. */
  528. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  529. {
  530. pg_data_t *pgdat = (pg_data_t *)arg;
  531. /* check memoryless node */
  532. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  533. return 0;
  534. seq_printf(m, "Page block order: %d\n", pageblock_order);
  535. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  536. seq_putc(m, '\n');
  537. pagetypeinfo_showfree(m, pgdat);
  538. pagetypeinfo_showblockcount(m, pgdat);
  539. return 0;
  540. }
  541. static const struct seq_operations fragmentation_op = {
  542. .start = frag_start,
  543. .next = frag_next,
  544. .stop = frag_stop,
  545. .show = frag_show,
  546. };
  547. static int fragmentation_open(struct inode *inode, struct file *file)
  548. {
  549. return seq_open(file, &fragmentation_op);
  550. }
  551. static const struct file_operations fragmentation_file_operations = {
  552. .open = fragmentation_open,
  553. .read = seq_read,
  554. .llseek = seq_lseek,
  555. .release = seq_release,
  556. };
  557. static const struct seq_operations pagetypeinfo_op = {
  558. .start = frag_start,
  559. .next = frag_next,
  560. .stop = frag_stop,
  561. .show = pagetypeinfo_show,
  562. };
  563. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  564. {
  565. return seq_open(file, &pagetypeinfo_op);
  566. }
  567. static const struct file_operations pagetypeinfo_file_ops = {
  568. .open = pagetypeinfo_open,
  569. .read = seq_read,
  570. .llseek = seq_lseek,
  571. .release = seq_release,
  572. };
  573. #ifdef CONFIG_ZONE_DMA
  574. #define TEXT_FOR_DMA(xx) xx "_dma",
  575. #else
  576. #define TEXT_FOR_DMA(xx)
  577. #endif
  578. #ifdef CONFIG_ZONE_DMA32
  579. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  580. #else
  581. #define TEXT_FOR_DMA32(xx)
  582. #endif
  583. #ifdef CONFIG_HIGHMEM
  584. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  585. #else
  586. #define TEXT_FOR_HIGHMEM(xx)
  587. #endif
  588. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  589. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  590. static const char * const vmstat_text[] = {
  591. /* Zoned VM counters */
  592. "nr_free_pages",
  593. "nr_inactive_anon",
  594. "nr_active_anon",
  595. "nr_inactive_file",
  596. "nr_active_file",
  597. "nr_unevictable",
  598. "nr_mlock",
  599. "nr_anon_pages",
  600. "nr_mapped",
  601. "nr_file_pages",
  602. "nr_dirty",
  603. "nr_writeback",
  604. "nr_slab_reclaimable",
  605. "nr_slab_unreclaimable",
  606. "nr_page_table_pages",
  607. "nr_kernel_stack",
  608. "nr_unstable",
  609. "nr_bounce",
  610. "nr_vmscan_write",
  611. "nr_writeback_temp",
  612. "nr_isolated_anon",
  613. "nr_isolated_file",
  614. "nr_shmem",
  615. #ifdef CONFIG_NUMA
  616. "numa_hit",
  617. "numa_miss",
  618. "numa_foreign",
  619. "numa_interleave",
  620. "numa_local",
  621. "numa_other",
  622. #endif
  623. #ifdef CONFIG_VM_EVENT_COUNTERS
  624. "pgpgin",
  625. "pgpgout",
  626. "pswpin",
  627. "pswpout",
  628. TEXTS_FOR_ZONES("pgalloc")
  629. "pgfree",
  630. "pgactivate",
  631. "pgdeactivate",
  632. "pgfault",
  633. "pgmajfault",
  634. TEXTS_FOR_ZONES("pgrefill")
  635. TEXTS_FOR_ZONES("pgsteal")
  636. TEXTS_FOR_ZONES("pgscan_kswapd")
  637. TEXTS_FOR_ZONES("pgscan_direct")
  638. #ifdef CONFIG_NUMA
  639. "zone_reclaim_failed",
  640. #endif
  641. "pginodesteal",
  642. "slabs_scanned",
  643. "kswapd_steal",
  644. "kswapd_inodesteal",
  645. "kswapd_low_wmark_hit_quickly",
  646. "kswapd_high_wmark_hit_quickly",
  647. "kswapd_skip_congestion_wait",
  648. "pageoutrun",
  649. "allocstall",
  650. "pgrotated",
  651. #ifdef CONFIG_COMPACTION
  652. "compact_blocks_moved",
  653. "compact_pages_moved",
  654. "compact_pagemigrate_failed",
  655. #endif
  656. #ifdef CONFIG_HUGETLB_PAGE
  657. "htlb_buddy_alloc_success",
  658. "htlb_buddy_alloc_fail",
  659. #endif
  660. "unevictable_pgs_culled",
  661. "unevictable_pgs_scanned",
  662. "unevictable_pgs_rescued",
  663. "unevictable_pgs_mlocked",
  664. "unevictable_pgs_munlocked",
  665. "unevictable_pgs_cleared",
  666. "unevictable_pgs_stranded",
  667. "unevictable_pgs_mlockfreed",
  668. #endif
  669. };
  670. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  671. struct zone *zone)
  672. {
  673. int i;
  674. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  675. seq_printf(m,
  676. "\n pages free %lu"
  677. "\n min %lu"
  678. "\n low %lu"
  679. "\n high %lu"
  680. "\n scanned %lu"
  681. "\n spanned %lu"
  682. "\n present %lu",
  683. zone_page_state(zone, NR_FREE_PAGES),
  684. min_wmark_pages(zone),
  685. low_wmark_pages(zone),
  686. high_wmark_pages(zone),
  687. zone->pages_scanned,
  688. zone->spanned_pages,
  689. zone->present_pages);
  690. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  691. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  692. zone_page_state(zone, i));
  693. seq_printf(m,
  694. "\n protection: (%lu",
  695. zone->lowmem_reserve[0]);
  696. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  697. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  698. seq_printf(m,
  699. ")"
  700. "\n pagesets");
  701. for_each_online_cpu(i) {
  702. struct per_cpu_pageset *pageset;
  703. pageset = per_cpu_ptr(zone->pageset, i);
  704. seq_printf(m,
  705. "\n cpu: %i"
  706. "\n count: %i"
  707. "\n high: %i"
  708. "\n batch: %i",
  709. i,
  710. pageset->pcp.count,
  711. pageset->pcp.high,
  712. pageset->pcp.batch);
  713. #ifdef CONFIG_SMP
  714. seq_printf(m, "\n vm stats threshold: %d",
  715. pageset->stat_threshold);
  716. #endif
  717. }
  718. seq_printf(m,
  719. "\n all_unreclaimable: %u"
  720. "\n prev_priority: %i"
  721. "\n start_pfn: %lu"
  722. "\n inactive_ratio: %u",
  723. zone->all_unreclaimable,
  724. zone->prev_priority,
  725. zone->zone_start_pfn,
  726. zone->inactive_ratio);
  727. seq_putc(m, '\n');
  728. }
  729. /*
  730. * Output information about zones in @pgdat.
  731. */
  732. static int zoneinfo_show(struct seq_file *m, void *arg)
  733. {
  734. pg_data_t *pgdat = (pg_data_t *)arg;
  735. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  736. return 0;
  737. }
  738. static const struct seq_operations zoneinfo_op = {
  739. .start = frag_start, /* iterate over all zones. The same as in
  740. * fragmentation. */
  741. .next = frag_next,
  742. .stop = frag_stop,
  743. .show = zoneinfo_show,
  744. };
  745. static int zoneinfo_open(struct inode *inode, struct file *file)
  746. {
  747. return seq_open(file, &zoneinfo_op);
  748. }
  749. static const struct file_operations proc_zoneinfo_file_operations = {
  750. .open = zoneinfo_open,
  751. .read = seq_read,
  752. .llseek = seq_lseek,
  753. .release = seq_release,
  754. };
  755. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  756. {
  757. unsigned long *v;
  758. #ifdef CONFIG_VM_EVENT_COUNTERS
  759. unsigned long *e;
  760. #endif
  761. int i;
  762. if (*pos >= ARRAY_SIZE(vmstat_text))
  763. return NULL;
  764. #ifdef CONFIG_VM_EVENT_COUNTERS
  765. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
  766. + sizeof(struct vm_event_state), GFP_KERNEL);
  767. #else
  768. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
  769. GFP_KERNEL);
  770. #endif
  771. m->private = v;
  772. if (!v)
  773. return ERR_PTR(-ENOMEM);
  774. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  775. v[i] = global_page_state(i);
  776. #ifdef CONFIG_VM_EVENT_COUNTERS
  777. e = v + NR_VM_ZONE_STAT_ITEMS;
  778. all_vm_events(e);
  779. e[PGPGIN] /= 2; /* sectors -> kbytes */
  780. e[PGPGOUT] /= 2;
  781. #endif
  782. return v + *pos;
  783. }
  784. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  785. {
  786. (*pos)++;
  787. if (*pos >= ARRAY_SIZE(vmstat_text))
  788. return NULL;
  789. return (unsigned long *)m->private + *pos;
  790. }
  791. static int vmstat_show(struct seq_file *m, void *arg)
  792. {
  793. unsigned long *l = arg;
  794. unsigned long off = l - (unsigned long *)m->private;
  795. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  796. return 0;
  797. }
  798. static void vmstat_stop(struct seq_file *m, void *arg)
  799. {
  800. kfree(m->private);
  801. m->private = NULL;
  802. }
  803. static const struct seq_operations vmstat_op = {
  804. .start = vmstat_start,
  805. .next = vmstat_next,
  806. .stop = vmstat_stop,
  807. .show = vmstat_show,
  808. };
  809. static int vmstat_open(struct inode *inode, struct file *file)
  810. {
  811. return seq_open(file, &vmstat_op);
  812. }
  813. static const struct file_operations proc_vmstat_file_operations = {
  814. .open = vmstat_open,
  815. .read = seq_read,
  816. .llseek = seq_lseek,
  817. .release = seq_release,
  818. };
  819. #endif /* CONFIG_PROC_FS */
  820. #ifdef CONFIG_SMP
  821. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  822. int sysctl_stat_interval __read_mostly = HZ;
  823. static void vmstat_update(struct work_struct *w)
  824. {
  825. refresh_cpu_vm_stats(smp_processor_id());
  826. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  827. round_jiffies_relative(sysctl_stat_interval));
  828. }
  829. static void __cpuinit start_cpu_timer(int cpu)
  830. {
  831. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  832. INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
  833. schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
  834. }
  835. /*
  836. * Use the cpu notifier to insure that the thresholds are recalculated
  837. * when necessary.
  838. */
  839. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  840. unsigned long action,
  841. void *hcpu)
  842. {
  843. long cpu = (long)hcpu;
  844. switch (action) {
  845. case CPU_ONLINE:
  846. case CPU_ONLINE_FROZEN:
  847. start_cpu_timer(cpu);
  848. node_set_state(cpu_to_node(cpu), N_CPU);
  849. break;
  850. case CPU_DOWN_PREPARE:
  851. case CPU_DOWN_PREPARE_FROZEN:
  852. cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
  853. per_cpu(vmstat_work, cpu).work.func = NULL;
  854. break;
  855. case CPU_DOWN_FAILED:
  856. case CPU_DOWN_FAILED_FROZEN:
  857. start_cpu_timer(cpu);
  858. break;
  859. case CPU_DEAD:
  860. case CPU_DEAD_FROZEN:
  861. refresh_zone_stat_thresholds();
  862. break;
  863. default:
  864. break;
  865. }
  866. return NOTIFY_OK;
  867. }
  868. static struct notifier_block __cpuinitdata vmstat_notifier =
  869. { &vmstat_cpuup_callback, NULL, 0 };
  870. #endif
  871. static int __init setup_vmstat(void)
  872. {
  873. #ifdef CONFIG_SMP
  874. int cpu;
  875. refresh_zone_stat_thresholds();
  876. register_cpu_notifier(&vmstat_notifier);
  877. for_each_online_cpu(cpu)
  878. start_cpu_timer(cpu);
  879. #endif
  880. #ifdef CONFIG_PROC_FS
  881. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  882. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  883. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  884. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  885. #endif
  886. return 0;
  887. }
  888. module_init(setup_vmstat)
  889. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  890. #include <linux/debugfs.h>
  891. static struct dentry *extfrag_debug_root;
  892. /*
  893. * Return an index indicating how much of the available free memory is
  894. * unusable for an allocation of the requested size.
  895. */
  896. static int unusable_free_index(unsigned int order,
  897. struct contig_page_info *info)
  898. {
  899. /* No free memory is interpreted as all free memory is unusable */
  900. if (info->free_pages == 0)
  901. return 1000;
  902. /*
  903. * Index should be a value between 0 and 1. Return a value to 3
  904. * decimal places.
  905. *
  906. * 0 => no fragmentation
  907. * 1 => high fragmentation
  908. */
  909. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  910. }
  911. static void unusable_show_print(struct seq_file *m,
  912. pg_data_t *pgdat, struct zone *zone)
  913. {
  914. unsigned int order;
  915. int index;
  916. struct contig_page_info info;
  917. seq_printf(m, "Node %d, zone %8s ",
  918. pgdat->node_id,
  919. zone->name);
  920. for (order = 0; order < MAX_ORDER; ++order) {
  921. fill_contig_page_info(zone, order, &info);
  922. index = unusable_free_index(order, &info);
  923. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  924. }
  925. seq_putc(m, '\n');
  926. }
  927. /*
  928. * Display unusable free space index
  929. *
  930. * The unusable free space index measures how much of the available free
  931. * memory cannot be used to satisfy an allocation of a given size and is a
  932. * value between 0 and 1. The higher the value, the more of free memory is
  933. * unusable and by implication, the worse the external fragmentation is. This
  934. * can be expressed as a percentage by multiplying by 100.
  935. */
  936. static int unusable_show(struct seq_file *m, void *arg)
  937. {
  938. pg_data_t *pgdat = (pg_data_t *)arg;
  939. /* check memoryless node */
  940. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  941. return 0;
  942. walk_zones_in_node(m, pgdat, unusable_show_print);
  943. return 0;
  944. }
  945. static const struct seq_operations unusable_op = {
  946. .start = frag_start,
  947. .next = frag_next,
  948. .stop = frag_stop,
  949. .show = unusable_show,
  950. };
  951. static int unusable_open(struct inode *inode, struct file *file)
  952. {
  953. return seq_open(file, &unusable_op);
  954. }
  955. static const struct file_operations unusable_file_ops = {
  956. .open = unusable_open,
  957. .read = seq_read,
  958. .llseek = seq_lseek,
  959. .release = seq_release,
  960. };
  961. static void extfrag_show_print(struct seq_file *m,
  962. pg_data_t *pgdat, struct zone *zone)
  963. {
  964. unsigned int order;
  965. int index;
  966. /* Alloc on stack as interrupts are disabled for zone walk */
  967. struct contig_page_info info;
  968. seq_printf(m, "Node %d, zone %8s ",
  969. pgdat->node_id,
  970. zone->name);
  971. for (order = 0; order < MAX_ORDER; ++order) {
  972. fill_contig_page_info(zone, order, &info);
  973. index = fragmentation_index(order, &info);
  974. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  975. }
  976. seq_putc(m, '\n');
  977. }
  978. /*
  979. * Display fragmentation index for orders that allocations would fail for
  980. */
  981. static int extfrag_show(struct seq_file *m, void *arg)
  982. {
  983. pg_data_t *pgdat = (pg_data_t *)arg;
  984. walk_zones_in_node(m, pgdat, extfrag_show_print);
  985. return 0;
  986. }
  987. static const struct seq_operations extfrag_op = {
  988. .start = frag_start,
  989. .next = frag_next,
  990. .stop = frag_stop,
  991. .show = extfrag_show,
  992. };
  993. static int extfrag_open(struct inode *inode, struct file *file)
  994. {
  995. return seq_open(file, &extfrag_op);
  996. }
  997. static const struct file_operations extfrag_file_ops = {
  998. .open = extfrag_open,
  999. .read = seq_read,
  1000. .llseek = seq_lseek,
  1001. .release = seq_release,
  1002. };
  1003. static int __init extfrag_debug_init(void)
  1004. {
  1005. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1006. if (!extfrag_debug_root)
  1007. return -ENOMEM;
  1008. if (!debugfs_create_file("unusable_index", 0444,
  1009. extfrag_debug_root, NULL, &unusable_file_ops))
  1010. return -ENOMEM;
  1011. if (!debugfs_create_file("extfrag_index", 0444,
  1012. extfrag_debug_root, NULL, &extfrag_file_ops))
  1013. return -ENOMEM;
  1014. return 0;
  1015. }
  1016. module_init(extfrag_debug_init);
  1017. #endif