vmstat.c 33 KB

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