vmstat.c 31 KB

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