vmstat.c 35 KB

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