vmstat.c 34 KB

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