vmstat.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. static inline void fold_diff(int *diff)
  358. {
  359. int i;
  360. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  361. if (diff[i])
  362. atomic_long_add(diff[i], &vm_stat[i]);
  363. }
  364. /*
  365. * Update the zone counters for the current cpu.
  366. *
  367. * Note that refresh_cpu_vm_stats strives to only access
  368. * node local memory. The per cpu pagesets on remote zones are placed
  369. * in the memory local to the processor using that pageset. So the
  370. * loop over all zones will access a series of cachelines local to
  371. * the processor.
  372. *
  373. * The call to zone_page_state_add updates the cachelines with the
  374. * statistics in the remote zone struct as well as the global cachelines
  375. * with the global counters. These could cause remote node cache line
  376. * bouncing and will have to be only done when necessary.
  377. */
  378. static void refresh_cpu_vm_stats(int cpu)
  379. {
  380. struct zone *zone;
  381. int i;
  382. int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
  383. for_each_populated_zone(zone) {
  384. struct per_cpu_pageset *p;
  385. p = per_cpu_ptr(zone->pageset, cpu);
  386. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  387. if (p->vm_stat_diff[i]) {
  388. unsigned long flags;
  389. int v;
  390. local_irq_save(flags);
  391. v = p->vm_stat_diff[i];
  392. p->vm_stat_diff[i] = 0;
  393. local_irq_restore(flags);
  394. atomic_long_add(v, &zone->vm_stat[i]);
  395. global_diff[i] += v;
  396. #ifdef CONFIG_NUMA
  397. /* 3 seconds idle till flush */
  398. p->expire = 3;
  399. #endif
  400. }
  401. cond_resched();
  402. #ifdef CONFIG_NUMA
  403. /*
  404. * Deal with draining the remote pageset of this
  405. * processor
  406. *
  407. * Check if there are pages remaining in this pageset
  408. * if not then there is nothing to expire.
  409. */
  410. if (!p->expire || !p->pcp.count)
  411. continue;
  412. /*
  413. * We never drain zones local to this processor.
  414. */
  415. if (zone_to_nid(zone) == numa_node_id()) {
  416. p->expire = 0;
  417. continue;
  418. }
  419. p->expire--;
  420. if (p->expire)
  421. continue;
  422. if (p->pcp.count)
  423. drain_zone_pages(zone, &p->pcp);
  424. #endif
  425. }
  426. fold_diff(global_diff);
  427. }
  428. /*
  429. * Fold the data for an offline cpu into the global array.
  430. * There cannot be any access by the offline cpu and therefore
  431. * synchronization is simplified.
  432. */
  433. void cpu_vm_stats_fold(int cpu)
  434. {
  435. struct zone *zone;
  436. int i;
  437. int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
  438. for_each_populated_zone(zone) {
  439. struct per_cpu_pageset *p;
  440. p = per_cpu_ptr(zone->pageset, cpu);
  441. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  442. if (p->vm_stat_diff[i]) {
  443. int v;
  444. v = p->vm_stat_diff[i];
  445. p->vm_stat_diff[i] = 0;
  446. atomic_long_add(v, &zone->vm_stat[i]);
  447. global_diff[i] += v;
  448. }
  449. }
  450. fold_diff(global_diff);
  451. }
  452. /*
  453. * this is only called if !populated_zone(zone), which implies no other users of
  454. * pset->vm_stat_diff[] exsist.
  455. */
  456. void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
  457. {
  458. int i;
  459. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  460. if (pset->vm_stat_diff[i]) {
  461. int v = pset->vm_stat_diff[i];
  462. pset->vm_stat_diff[i] = 0;
  463. atomic_long_add(v, &zone->vm_stat[i]);
  464. atomic_long_add(v, &vm_stat[i]);
  465. }
  466. }
  467. #endif
  468. #ifdef CONFIG_NUMA
  469. /*
  470. * zonelist = the list of zones passed to the allocator
  471. * z = the zone from which the allocation occurred.
  472. *
  473. * Must be called with interrupts disabled.
  474. *
  475. * When __GFP_OTHER_NODE is set assume the node of the preferred
  476. * zone is the local node. This is useful for daemons who allocate
  477. * memory on behalf of other processes.
  478. */
  479. void zone_statistics(struct zone *preferred_zone, struct zone *z, gfp_t flags)
  480. {
  481. if (z->zone_pgdat == preferred_zone->zone_pgdat) {
  482. __inc_zone_state(z, NUMA_HIT);
  483. } else {
  484. __inc_zone_state(z, NUMA_MISS);
  485. __inc_zone_state(preferred_zone, NUMA_FOREIGN);
  486. }
  487. if (z->node == ((flags & __GFP_OTHER_NODE) ?
  488. preferred_zone->node : numa_node_id()))
  489. __inc_zone_state(z, NUMA_LOCAL);
  490. else
  491. __inc_zone_state(z, NUMA_OTHER);
  492. }
  493. #endif
  494. #ifdef CONFIG_COMPACTION
  495. struct contig_page_info {
  496. unsigned long free_pages;
  497. unsigned long free_blocks_total;
  498. unsigned long free_blocks_suitable;
  499. };
  500. /*
  501. * Calculate the number of free pages in a zone, how many contiguous
  502. * pages are free and how many are large enough to satisfy an allocation of
  503. * the target size. Note that this function makes no attempt to estimate
  504. * how many suitable free blocks there *might* be if MOVABLE pages were
  505. * migrated. Calculating that is possible, but expensive and can be
  506. * figured out from userspace
  507. */
  508. static void fill_contig_page_info(struct zone *zone,
  509. unsigned int suitable_order,
  510. struct contig_page_info *info)
  511. {
  512. unsigned int order;
  513. info->free_pages = 0;
  514. info->free_blocks_total = 0;
  515. info->free_blocks_suitable = 0;
  516. for (order = 0; order < MAX_ORDER; order++) {
  517. unsigned long blocks;
  518. /* Count number of free blocks */
  519. blocks = zone->free_area[order].nr_free;
  520. info->free_blocks_total += blocks;
  521. /* Count free base pages */
  522. info->free_pages += blocks << order;
  523. /* Count the suitable free blocks */
  524. if (order >= suitable_order)
  525. info->free_blocks_suitable += blocks <<
  526. (order - suitable_order);
  527. }
  528. }
  529. /*
  530. * A fragmentation index only makes sense if an allocation of a requested
  531. * size would fail. If that is true, the fragmentation index indicates
  532. * whether external fragmentation or a lack of memory was the problem.
  533. * The value can be used to determine if page reclaim or compaction
  534. * should be used
  535. */
  536. static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
  537. {
  538. unsigned long requested = 1UL << order;
  539. if (!info->free_blocks_total)
  540. return 0;
  541. /* Fragmentation index only makes sense when a request would fail */
  542. if (info->free_blocks_suitable)
  543. return -1000;
  544. /*
  545. * Index is between 0 and 1 so return within 3 decimal places
  546. *
  547. * 0 => allocation would fail due to lack of memory
  548. * 1 => allocation would fail due to fragmentation
  549. */
  550. return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
  551. }
  552. /* Same as __fragmentation index but allocs contig_page_info on stack */
  553. int fragmentation_index(struct zone *zone, unsigned int order)
  554. {
  555. struct contig_page_info info;
  556. fill_contig_page_info(zone, order, &info);
  557. return __fragmentation_index(order, &info);
  558. }
  559. #endif
  560. #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
  561. #include <linux/proc_fs.h>
  562. #include <linux/seq_file.h>
  563. static char * const migratetype_names[MIGRATE_TYPES] = {
  564. "Unmovable",
  565. "Reclaimable",
  566. "Movable",
  567. "Reserve",
  568. #ifdef CONFIG_CMA
  569. "CMA",
  570. #endif
  571. #ifdef CONFIG_MEMORY_ISOLATION
  572. "Isolate",
  573. #endif
  574. };
  575. static void *frag_start(struct seq_file *m, loff_t *pos)
  576. {
  577. pg_data_t *pgdat;
  578. loff_t node = *pos;
  579. for (pgdat = first_online_pgdat();
  580. pgdat && node;
  581. pgdat = next_online_pgdat(pgdat))
  582. --node;
  583. return pgdat;
  584. }
  585. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  586. {
  587. pg_data_t *pgdat = (pg_data_t *)arg;
  588. (*pos)++;
  589. return next_online_pgdat(pgdat);
  590. }
  591. static void frag_stop(struct seq_file *m, void *arg)
  592. {
  593. }
  594. /* Walk all the zones in a node and print using a callback */
  595. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  596. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  597. {
  598. struct zone *zone;
  599. struct zone *node_zones = pgdat->node_zones;
  600. unsigned long flags;
  601. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  602. if (!populated_zone(zone))
  603. continue;
  604. spin_lock_irqsave(&zone->lock, flags);
  605. print(m, pgdat, zone);
  606. spin_unlock_irqrestore(&zone->lock, flags);
  607. }
  608. }
  609. #endif
  610. #if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
  611. #ifdef CONFIG_ZONE_DMA
  612. #define TEXT_FOR_DMA(xx) xx "_dma",
  613. #else
  614. #define TEXT_FOR_DMA(xx)
  615. #endif
  616. #ifdef CONFIG_ZONE_DMA32
  617. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  618. #else
  619. #define TEXT_FOR_DMA32(xx)
  620. #endif
  621. #ifdef CONFIG_HIGHMEM
  622. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  623. #else
  624. #define TEXT_FOR_HIGHMEM(xx)
  625. #endif
  626. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  627. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  628. const char * const vmstat_text[] = {
  629. /* Zoned VM counters */
  630. "nr_free_pages",
  631. "nr_alloc_batch",
  632. "nr_inactive_anon",
  633. "nr_active_anon",
  634. "nr_inactive_file",
  635. "nr_active_file",
  636. "nr_unevictable",
  637. "nr_mlock",
  638. "nr_anon_pages",
  639. "nr_mapped",
  640. "nr_file_pages",
  641. "nr_dirty",
  642. "nr_writeback",
  643. "nr_slab_reclaimable",
  644. "nr_slab_unreclaimable",
  645. "nr_page_table_pages",
  646. "nr_kernel_stack",
  647. "nr_unstable",
  648. "nr_bounce",
  649. "nr_vmscan_write",
  650. "nr_vmscan_immediate_reclaim",
  651. "nr_writeback_temp",
  652. "nr_isolated_anon",
  653. "nr_isolated_file",
  654. "nr_shmem",
  655. "nr_dirtied",
  656. "nr_written",
  657. #ifdef CONFIG_NUMA
  658. "numa_hit",
  659. "numa_miss",
  660. "numa_foreign",
  661. "numa_interleave",
  662. "numa_local",
  663. "numa_other",
  664. #endif
  665. "nr_anon_transparent_hugepages",
  666. "nr_free_cma",
  667. "nr_dirty_threshold",
  668. "nr_dirty_background_threshold",
  669. #ifdef CONFIG_VM_EVENT_COUNTERS
  670. "pgpgin",
  671. "pgpgout",
  672. "pswpin",
  673. "pswpout",
  674. TEXTS_FOR_ZONES("pgalloc")
  675. "pgfree",
  676. "pgactivate",
  677. "pgdeactivate",
  678. "pgfault",
  679. "pgmajfault",
  680. TEXTS_FOR_ZONES("pgrefill")
  681. TEXTS_FOR_ZONES("pgsteal_kswapd")
  682. TEXTS_FOR_ZONES("pgsteal_direct")
  683. TEXTS_FOR_ZONES("pgscan_kswapd")
  684. TEXTS_FOR_ZONES("pgscan_direct")
  685. "pgscan_direct_throttle",
  686. #ifdef CONFIG_NUMA
  687. "zone_reclaim_failed",
  688. #endif
  689. "pginodesteal",
  690. "slabs_scanned",
  691. "kswapd_inodesteal",
  692. "kswapd_low_wmark_hit_quickly",
  693. "kswapd_high_wmark_hit_quickly",
  694. "pageoutrun",
  695. "allocstall",
  696. "pgrotated",
  697. #ifdef CONFIG_NUMA_BALANCING
  698. "numa_pte_updates",
  699. "numa_hint_faults",
  700. "numa_hint_faults_local",
  701. "numa_pages_migrated",
  702. #endif
  703. #ifdef CONFIG_MIGRATION
  704. "pgmigrate_success",
  705. "pgmigrate_fail",
  706. #endif
  707. #ifdef CONFIG_COMPACTION
  708. "compact_migrate_scanned",
  709. "compact_free_scanned",
  710. "compact_isolated",
  711. "compact_stall",
  712. "compact_fail",
  713. "compact_success",
  714. #endif
  715. #ifdef CONFIG_HUGETLB_PAGE
  716. "htlb_buddy_alloc_success",
  717. "htlb_buddy_alloc_fail",
  718. #endif
  719. "unevictable_pgs_culled",
  720. "unevictable_pgs_scanned",
  721. "unevictable_pgs_rescued",
  722. "unevictable_pgs_mlocked",
  723. "unevictable_pgs_munlocked",
  724. "unevictable_pgs_cleared",
  725. "unevictable_pgs_stranded",
  726. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  727. "thp_fault_alloc",
  728. "thp_fault_fallback",
  729. "thp_collapse_alloc",
  730. "thp_collapse_alloc_failed",
  731. "thp_split",
  732. "thp_zero_page_alloc",
  733. "thp_zero_page_alloc_failed",
  734. #endif
  735. #ifdef CONFIG_SMP
  736. "nr_tlb_remote_flush",
  737. "nr_tlb_remote_flush_received",
  738. #endif
  739. "nr_tlb_local_flush_all",
  740. "nr_tlb_local_flush_one",
  741. #endif /* CONFIG_VM_EVENTS_COUNTERS */
  742. };
  743. #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
  744. #ifdef CONFIG_PROC_FS
  745. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  746. struct zone *zone)
  747. {
  748. int order;
  749. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  750. for (order = 0; order < MAX_ORDER; ++order)
  751. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  752. seq_putc(m, '\n');
  753. }
  754. /*
  755. * This walks the free areas for each zone.
  756. */
  757. static int frag_show(struct seq_file *m, void *arg)
  758. {
  759. pg_data_t *pgdat = (pg_data_t *)arg;
  760. walk_zones_in_node(m, pgdat, frag_show_print);
  761. return 0;
  762. }
  763. static void pagetypeinfo_showfree_print(struct seq_file *m,
  764. pg_data_t *pgdat, struct zone *zone)
  765. {
  766. int order, mtype;
  767. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  768. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  769. pgdat->node_id,
  770. zone->name,
  771. migratetype_names[mtype]);
  772. for (order = 0; order < MAX_ORDER; ++order) {
  773. unsigned long freecount = 0;
  774. struct free_area *area;
  775. struct list_head *curr;
  776. area = &(zone->free_area[order]);
  777. list_for_each(curr, &area->free_list[mtype])
  778. freecount++;
  779. seq_printf(m, "%6lu ", freecount);
  780. }
  781. seq_putc(m, '\n');
  782. }
  783. }
  784. /* Print out the free pages at each order for each migatetype */
  785. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  786. {
  787. int order;
  788. pg_data_t *pgdat = (pg_data_t *)arg;
  789. /* Print header */
  790. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  791. for (order = 0; order < MAX_ORDER; ++order)
  792. seq_printf(m, "%6d ", order);
  793. seq_putc(m, '\n');
  794. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  795. return 0;
  796. }
  797. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  798. pg_data_t *pgdat, struct zone *zone)
  799. {
  800. int mtype;
  801. unsigned long pfn;
  802. unsigned long start_pfn = zone->zone_start_pfn;
  803. unsigned long end_pfn = zone_end_pfn(zone);
  804. unsigned long count[MIGRATE_TYPES] = { 0, };
  805. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  806. struct page *page;
  807. if (!pfn_valid(pfn))
  808. continue;
  809. page = pfn_to_page(pfn);
  810. /* Watch for unexpected holes punched in the memmap */
  811. if (!memmap_valid_within(pfn, page, zone))
  812. continue;
  813. mtype = get_pageblock_migratetype(page);
  814. if (mtype < MIGRATE_TYPES)
  815. count[mtype]++;
  816. }
  817. /* Print counts */
  818. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  819. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  820. seq_printf(m, "%12lu ", count[mtype]);
  821. seq_putc(m, '\n');
  822. }
  823. /* Print out the free pages at each order for each migratetype */
  824. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  825. {
  826. int mtype;
  827. pg_data_t *pgdat = (pg_data_t *)arg;
  828. seq_printf(m, "\n%-23s", "Number of blocks type ");
  829. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  830. seq_printf(m, "%12s ", migratetype_names[mtype]);
  831. seq_putc(m, '\n');
  832. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  833. return 0;
  834. }
  835. /*
  836. * This prints out statistics in relation to grouping pages by mobility.
  837. * It is expensive to collect so do not constantly read the file.
  838. */
  839. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  840. {
  841. pg_data_t *pgdat = (pg_data_t *)arg;
  842. /* check memoryless node */
  843. if (!node_state(pgdat->node_id, N_MEMORY))
  844. return 0;
  845. seq_printf(m, "Page block order: %d\n", pageblock_order);
  846. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  847. seq_putc(m, '\n');
  848. pagetypeinfo_showfree(m, pgdat);
  849. pagetypeinfo_showblockcount(m, pgdat);
  850. return 0;
  851. }
  852. static const struct seq_operations fragmentation_op = {
  853. .start = frag_start,
  854. .next = frag_next,
  855. .stop = frag_stop,
  856. .show = frag_show,
  857. };
  858. static int fragmentation_open(struct inode *inode, struct file *file)
  859. {
  860. return seq_open(file, &fragmentation_op);
  861. }
  862. static const struct file_operations fragmentation_file_operations = {
  863. .open = fragmentation_open,
  864. .read = seq_read,
  865. .llseek = seq_lseek,
  866. .release = seq_release,
  867. };
  868. static const struct seq_operations pagetypeinfo_op = {
  869. .start = frag_start,
  870. .next = frag_next,
  871. .stop = frag_stop,
  872. .show = pagetypeinfo_show,
  873. };
  874. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  875. {
  876. return seq_open(file, &pagetypeinfo_op);
  877. }
  878. static const struct file_operations pagetypeinfo_file_ops = {
  879. .open = pagetypeinfo_open,
  880. .read = seq_read,
  881. .llseek = seq_lseek,
  882. .release = seq_release,
  883. };
  884. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  885. struct zone *zone)
  886. {
  887. int i;
  888. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  889. seq_printf(m,
  890. "\n pages free %lu"
  891. "\n min %lu"
  892. "\n low %lu"
  893. "\n high %lu"
  894. "\n scanned %lu"
  895. "\n spanned %lu"
  896. "\n present %lu"
  897. "\n managed %lu",
  898. zone_page_state(zone, NR_FREE_PAGES),
  899. min_wmark_pages(zone),
  900. low_wmark_pages(zone),
  901. high_wmark_pages(zone),
  902. zone->pages_scanned,
  903. zone->spanned_pages,
  904. zone->present_pages,
  905. zone->managed_pages);
  906. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  907. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  908. zone_page_state(zone, i));
  909. seq_printf(m,
  910. "\n protection: (%lu",
  911. zone->lowmem_reserve[0]);
  912. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  913. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  914. seq_printf(m,
  915. ")"
  916. "\n pagesets");
  917. for_each_online_cpu(i) {
  918. struct per_cpu_pageset *pageset;
  919. pageset = per_cpu_ptr(zone->pageset, i);
  920. seq_printf(m,
  921. "\n cpu: %i"
  922. "\n count: %i"
  923. "\n high: %i"
  924. "\n batch: %i",
  925. i,
  926. pageset->pcp.count,
  927. pageset->pcp.high,
  928. pageset->pcp.batch);
  929. #ifdef CONFIG_SMP
  930. seq_printf(m, "\n vm stats threshold: %d",
  931. pageset->stat_threshold);
  932. #endif
  933. }
  934. seq_printf(m,
  935. "\n all_unreclaimable: %u"
  936. "\n start_pfn: %lu"
  937. "\n inactive_ratio: %u",
  938. zone->all_unreclaimable,
  939. zone->zone_start_pfn,
  940. zone->inactive_ratio);
  941. seq_putc(m, '\n');
  942. }
  943. /*
  944. * Output information about zones in @pgdat.
  945. */
  946. static int zoneinfo_show(struct seq_file *m, void *arg)
  947. {
  948. pg_data_t *pgdat = (pg_data_t *)arg;
  949. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  950. return 0;
  951. }
  952. static const struct seq_operations zoneinfo_op = {
  953. .start = frag_start, /* iterate over all zones. The same as in
  954. * fragmentation. */
  955. .next = frag_next,
  956. .stop = frag_stop,
  957. .show = zoneinfo_show,
  958. };
  959. static int zoneinfo_open(struct inode *inode, struct file *file)
  960. {
  961. return seq_open(file, &zoneinfo_op);
  962. }
  963. static const struct file_operations proc_zoneinfo_file_operations = {
  964. .open = zoneinfo_open,
  965. .read = seq_read,
  966. .llseek = seq_lseek,
  967. .release = seq_release,
  968. };
  969. enum writeback_stat_item {
  970. NR_DIRTY_THRESHOLD,
  971. NR_DIRTY_BG_THRESHOLD,
  972. NR_VM_WRITEBACK_STAT_ITEMS,
  973. };
  974. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  975. {
  976. unsigned long *v;
  977. int i, stat_items_size;
  978. if (*pos >= ARRAY_SIZE(vmstat_text))
  979. return NULL;
  980. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  981. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  982. #ifdef CONFIG_VM_EVENT_COUNTERS
  983. stat_items_size += sizeof(struct vm_event_state);
  984. #endif
  985. v = kmalloc(stat_items_size, GFP_KERNEL);
  986. m->private = v;
  987. if (!v)
  988. return ERR_PTR(-ENOMEM);
  989. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  990. v[i] = global_page_state(i);
  991. v += NR_VM_ZONE_STAT_ITEMS;
  992. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  993. v + NR_DIRTY_THRESHOLD);
  994. v += NR_VM_WRITEBACK_STAT_ITEMS;
  995. #ifdef CONFIG_VM_EVENT_COUNTERS
  996. all_vm_events(v);
  997. v[PGPGIN] /= 2; /* sectors -> kbytes */
  998. v[PGPGOUT] /= 2;
  999. #endif
  1000. return (unsigned long *)m->private + *pos;
  1001. }
  1002. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1003. {
  1004. (*pos)++;
  1005. if (*pos >= ARRAY_SIZE(vmstat_text))
  1006. return NULL;
  1007. return (unsigned long *)m->private + *pos;
  1008. }
  1009. static int vmstat_show(struct seq_file *m, void *arg)
  1010. {
  1011. unsigned long *l = arg;
  1012. unsigned long off = l - (unsigned long *)m->private;
  1013. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  1014. return 0;
  1015. }
  1016. static void vmstat_stop(struct seq_file *m, void *arg)
  1017. {
  1018. kfree(m->private);
  1019. m->private = NULL;
  1020. }
  1021. static const struct seq_operations vmstat_op = {
  1022. .start = vmstat_start,
  1023. .next = vmstat_next,
  1024. .stop = vmstat_stop,
  1025. .show = vmstat_show,
  1026. };
  1027. static int vmstat_open(struct inode *inode, struct file *file)
  1028. {
  1029. return seq_open(file, &vmstat_op);
  1030. }
  1031. static const struct file_operations proc_vmstat_file_operations = {
  1032. .open = vmstat_open,
  1033. .read = seq_read,
  1034. .llseek = seq_lseek,
  1035. .release = seq_release,
  1036. };
  1037. #endif /* CONFIG_PROC_FS */
  1038. #ifdef CONFIG_SMP
  1039. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  1040. int sysctl_stat_interval __read_mostly = HZ;
  1041. static void vmstat_update(struct work_struct *w)
  1042. {
  1043. refresh_cpu_vm_stats(smp_processor_id());
  1044. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  1045. round_jiffies_relative(sysctl_stat_interval));
  1046. }
  1047. static void start_cpu_timer(int cpu)
  1048. {
  1049. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  1050. INIT_DEFERRABLE_WORK(work, vmstat_update);
  1051. schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
  1052. }
  1053. /*
  1054. * Use the cpu notifier to insure that the thresholds are recalculated
  1055. * when necessary.
  1056. */
  1057. static int vmstat_cpuup_callback(struct notifier_block *nfb,
  1058. unsigned long action,
  1059. void *hcpu)
  1060. {
  1061. long cpu = (long)hcpu;
  1062. switch (action) {
  1063. case CPU_ONLINE:
  1064. case CPU_ONLINE_FROZEN:
  1065. refresh_zone_stat_thresholds();
  1066. start_cpu_timer(cpu);
  1067. node_set_state(cpu_to_node(cpu), N_CPU);
  1068. break;
  1069. case CPU_DOWN_PREPARE:
  1070. case CPU_DOWN_PREPARE_FROZEN:
  1071. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1072. per_cpu(vmstat_work, cpu).work.func = NULL;
  1073. break;
  1074. case CPU_DOWN_FAILED:
  1075. case CPU_DOWN_FAILED_FROZEN:
  1076. start_cpu_timer(cpu);
  1077. break;
  1078. case CPU_DEAD:
  1079. case CPU_DEAD_FROZEN:
  1080. refresh_zone_stat_thresholds();
  1081. break;
  1082. default:
  1083. break;
  1084. }
  1085. return NOTIFY_OK;
  1086. }
  1087. static struct notifier_block vmstat_notifier =
  1088. { &vmstat_cpuup_callback, NULL, 0 };
  1089. #endif
  1090. static int __init setup_vmstat(void)
  1091. {
  1092. #ifdef CONFIG_SMP
  1093. int cpu;
  1094. register_cpu_notifier(&vmstat_notifier);
  1095. for_each_online_cpu(cpu)
  1096. start_cpu_timer(cpu);
  1097. #endif
  1098. #ifdef CONFIG_PROC_FS
  1099. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  1100. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  1101. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  1102. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  1103. #endif
  1104. return 0;
  1105. }
  1106. module_init(setup_vmstat)
  1107. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1108. #include <linux/debugfs.h>
  1109. /*
  1110. * Return an index indicating how much of the available free memory is
  1111. * unusable for an allocation of the requested size.
  1112. */
  1113. static int unusable_free_index(unsigned int order,
  1114. struct contig_page_info *info)
  1115. {
  1116. /* No free memory is interpreted as all free memory is unusable */
  1117. if (info->free_pages == 0)
  1118. return 1000;
  1119. /*
  1120. * Index should be a value between 0 and 1. Return a value to 3
  1121. * decimal places.
  1122. *
  1123. * 0 => no fragmentation
  1124. * 1 => high fragmentation
  1125. */
  1126. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1127. }
  1128. static void unusable_show_print(struct seq_file *m,
  1129. pg_data_t *pgdat, struct zone *zone)
  1130. {
  1131. unsigned int order;
  1132. int index;
  1133. struct contig_page_info info;
  1134. seq_printf(m, "Node %d, zone %8s ",
  1135. pgdat->node_id,
  1136. zone->name);
  1137. for (order = 0; order < MAX_ORDER; ++order) {
  1138. fill_contig_page_info(zone, order, &info);
  1139. index = unusable_free_index(order, &info);
  1140. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1141. }
  1142. seq_putc(m, '\n');
  1143. }
  1144. /*
  1145. * Display unusable free space index
  1146. *
  1147. * The unusable free space index measures how much of the available free
  1148. * memory cannot be used to satisfy an allocation of a given size and is a
  1149. * value between 0 and 1. The higher the value, the more of free memory is
  1150. * unusable and by implication, the worse the external fragmentation is. This
  1151. * can be expressed as a percentage by multiplying by 100.
  1152. */
  1153. static int unusable_show(struct seq_file *m, void *arg)
  1154. {
  1155. pg_data_t *pgdat = (pg_data_t *)arg;
  1156. /* check memoryless node */
  1157. if (!node_state(pgdat->node_id, N_MEMORY))
  1158. return 0;
  1159. walk_zones_in_node(m, pgdat, unusable_show_print);
  1160. return 0;
  1161. }
  1162. static const struct seq_operations unusable_op = {
  1163. .start = frag_start,
  1164. .next = frag_next,
  1165. .stop = frag_stop,
  1166. .show = unusable_show,
  1167. };
  1168. static int unusable_open(struct inode *inode, struct file *file)
  1169. {
  1170. return seq_open(file, &unusable_op);
  1171. }
  1172. static const struct file_operations unusable_file_ops = {
  1173. .open = unusable_open,
  1174. .read = seq_read,
  1175. .llseek = seq_lseek,
  1176. .release = seq_release,
  1177. };
  1178. static void extfrag_show_print(struct seq_file *m,
  1179. pg_data_t *pgdat, struct zone *zone)
  1180. {
  1181. unsigned int order;
  1182. int index;
  1183. /* Alloc on stack as interrupts are disabled for zone walk */
  1184. struct contig_page_info info;
  1185. seq_printf(m, "Node %d, zone %8s ",
  1186. pgdat->node_id,
  1187. zone->name);
  1188. for (order = 0; order < MAX_ORDER; ++order) {
  1189. fill_contig_page_info(zone, order, &info);
  1190. index = __fragmentation_index(order, &info);
  1191. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1192. }
  1193. seq_putc(m, '\n');
  1194. }
  1195. /*
  1196. * Display fragmentation index for orders that allocations would fail for
  1197. */
  1198. static int extfrag_show(struct seq_file *m, void *arg)
  1199. {
  1200. pg_data_t *pgdat = (pg_data_t *)arg;
  1201. walk_zones_in_node(m, pgdat, extfrag_show_print);
  1202. return 0;
  1203. }
  1204. static const struct seq_operations extfrag_op = {
  1205. .start = frag_start,
  1206. .next = frag_next,
  1207. .stop = frag_stop,
  1208. .show = extfrag_show,
  1209. };
  1210. static int extfrag_open(struct inode *inode, struct file *file)
  1211. {
  1212. return seq_open(file, &extfrag_op);
  1213. }
  1214. static const struct file_operations extfrag_file_ops = {
  1215. .open = extfrag_open,
  1216. .read = seq_read,
  1217. .llseek = seq_lseek,
  1218. .release = seq_release,
  1219. };
  1220. static int __init extfrag_debug_init(void)
  1221. {
  1222. struct dentry *extfrag_debug_root;
  1223. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1224. if (!extfrag_debug_root)
  1225. return -ENOMEM;
  1226. if (!debugfs_create_file("unusable_index", 0444,
  1227. extfrag_debug_root, NULL, &unusable_file_ops))
  1228. goto fail;
  1229. if (!debugfs_create_file("extfrag_index", 0444,
  1230. extfrag_debug_root, NULL, &extfrag_file_ops))
  1231. goto fail;
  1232. return 0;
  1233. fail:
  1234. debugfs_remove_recursive(extfrag_debug_root);
  1235. return -ENOMEM;
  1236. }
  1237. module_init(extfrag_debug_init);
  1238. #endif