vmstat.c 34 KB

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