vmstat.c 34 KB

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