vmstat.c 34 KB

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