vmstat.c 35 KB

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