vmstat.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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_HIGH_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_printf(m, "%s %lu\n", vmstat_text[off], *l);
  968. return 0;
  969. }
  970. static void vmstat_stop(struct seq_file *m, void *arg)
  971. {
  972. kfree(m->private);
  973. m->private = NULL;
  974. }
  975. static const struct seq_operations vmstat_op = {
  976. .start = vmstat_start,
  977. .next = vmstat_next,
  978. .stop = vmstat_stop,
  979. .show = vmstat_show,
  980. };
  981. static int vmstat_open(struct inode *inode, struct file *file)
  982. {
  983. return seq_open(file, &vmstat_op);
  984. }
  985. static const struct file_operations proc_vmstat_file_operations = {
  986. .open = vmstat_open,
  987. .read = seq_read,
  988. .llseek = seq_lseek,
  989. .release = seq_release,
  990. };
  991. #endif /* CONFIG_PROC_FS */
  992. #ifdef CONFIG_SMP
  993. static struct workqueue_struct *vmstat_wq;
  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. queue_delayed_work_on(smp_processor_id(),
  1000. vmstat_wq, &__get_cpu_var(vmstat_work),
  1001. round_jiffies_relative(sysctl_stat_interval));
  1002. }
  1003. static void __cpuinit start_cpu_timer(int cpu)
  1004. {
  1005. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  1006. INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
  1007. queue_delayed_work_on(cpu, vmstat_wq, work, __round_jiffies_relative(HZ, cpu));
  1008. }
  1009. /*
  1010. * Use the cpu notifier to insure that the thresholds are recalculated
  1011. * when necessary.
  1012. */
  1013. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  1014. unsigned long action,
  1015. void *hcpu)
  1016. {
  1017. long cpu = (long)hcpu;
  1018. switch (action) {
  1019. case CPU_ONLINE:
  1020. case CPU_ONLINE_FROZEN:
  1021. refresh_zone_stat_thresholds();
  1022. start_cpu_timer(cpu);
  1023. node_set_state(cpu_to_node(cpu), N_CPU);
  1024. break;
  1025. case CPU_DOWN_PREPARE:
  1026. case CPU_DOWN_PREPARE_FROZEN:
  1027. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1028. per_cpu(vmstat_work, cpu).work.func = NULL;
  1029. break;
  1030. case CPU_DOWN_FAILED:
  1031. case CPU_DOWN_FAILED_FROZEN:
  1032. start_cpu_timer(cpu);
  1033. break;
  1034. case CPU_DEAD:
  1035. case CPU_DEAD_FROZEN:
  1036. refresh_zone_stat_thresholds();
  1037. break;
  1038. default:
  1039. break;
  1040. }
  1041. return NOTIFY_OK;
  1042. }
  1043. static struct notifier_block __cpuinitdata vmstat_notifier =
  1044. { &vmstat_cpuup_callback, NULL, 0 };
  1045. #endif
  1046. static int __init setup_vmstat(void)
  1047. {
  1048. #ifdef CONFIG_SMP
  1049. int cpu;
  1050. register_cpu_notifier(&vmstat_notifier);
  1051. vmstat_wq = alloc_workqueue("vmstat", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1052. for_each_online_cpu(cpu)
  1053. start_cpu_timer(cpu);
  1054. #endif
  1055. #ifdef CONFIG_PROC_FS
  1056. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  1057. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  1058. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  1059. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  1060. #endif
  1061. return 0;
  1062. }
  1063. module_init(setup_vmstat)
  1064. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1065. #include <linux/debugfs.h>
  1066. static struct dentry *extfrag_debug_root;
  1067. /*
  1068. * Return an index indicating how much of the available free memory is
  1069. * unusable for an allocation of the requested size.
  1070. */
  1071. static int unusable_free_index(unsigned int order,
  1072. struct contig_page_info *info)
  1073. {
  1074. /* No free memory is interpreted as all free memory is unusable */
  1075. if (info->free_pages == 0)
  1076. return 1000;
  1077. /*
  1078. * Index should be a value between 0 and 1. Return a value to 3
  1079. * decimal places.
  1080. *
  1081. * 0 => no fragmentation
  1082. * 1 => high fragmentation
  1083. */
  1084. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1085. }
  1086. static void unusable_show_print(struct seq_file *m,
  1087. pg_data_t *pgdat, struct zone *zone)
  1088. {
  1089. unsigned int order;
  1090. int index;
  1091. struct contig_page_info info;
  1092. seq_printf(m, "Node %d, zone %8s ",
  1093. pgdat->node_id,
  1094. zone->name);
  1095. for (order = 0; order < MAX_ORDER; ++order) {
  1096. fill_contig_page_info(zone, order, &info);
  1097. index = unusable_free_index(order, &info);
  1098. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1099. }
  1100. seq_putc(m, '\n');
  1101. }
  1102. /*
  1103. * Display unusable free space index
  1104. *
  1105. * The unusable free space index measures how much of the available free
  1106. * memory cannot be used to satisfy an allocation of a given size and is a
  1107. * value between 0 and 1. The higher the value, the more of free memory is
  1108. * unusable and by implication, the worse the external fragmentation is. This
  1109. * can be expressed as a percentage by multiplying by 100.
  1110. */
  1111. static int unusable_show(struct seq_file *m, void *arg)
  1112. {
  1113. pg_data_t *pgdat = (pg_data_t *)arg;
  1114. /* check memoryless node */
  1115. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  1116. return 0;
  1117. walk_zones_in_node(m, pgdat, unusable_show_print);
  1118. return 0;
  1119. }
  1120. static const struct seq_operations unusable_op = {
  1121. .start = frag_start,
  1122. .next = frag_next,
  1123. .stop = frag_stop,
  1124. .show = unusable_show,
  1125. };
  1126. static int unusable_open(struct inode *inode, struct file *file)
  1127. {
  1128. return seq_open(file, &unusable_op);
  1129. }
  1130. static const struct file_operations unusable_file_ops = {
  1131. .open = unusable_open,
  1132. .read = seq_read,
  1133. .llseek = seq_lseek,
  1134. .release = seq_release,
  1135. };
  1136. static void extfrag_show_print(struct seq_file *m,
  1137. pg_data_t *pgdat, struct zone *zone)
  1138. {
  1139. unsigned int order;
  1140. int index;
  1141. /* Alloc on stack as interrupts are disabled for zone walk */
  1142. struct contig_page_info info;
  1143. seq_printf(m, "Node %d, zone %8s ",
  1144. pgdat->node_id,
  1145. zone->name);
  1146. for (order = 0; order < MAX_ORDER; ++order) {
  1147. fill_contig_page_info(zone, order, &info);
  1148. index = __fragmentation_index(order, &info);
  1149. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1150. }
  1151. seq_putc(m, '\n');
  1152. }
  1153. /*
  1154. * Display fragmentation index for orders that allocations would fail for
  1155. */
  1156. static int extfrag_show(struct seq_file *m, void *arg)
  1157. {
  1158. pg_data_t *pgdat = (pg_data_t *)arg;
  1159. walk_zones_in_node(m, pgdat, extfrag_show_print);
  1160. return 0;
  1161. }
  1162. static const struct seq_operations extfrag_op = {
  1163. .start = frag_start,
  1164. .next = frag_next,
  1165. .stop = frag_stop,
  1166. .show = extfrag_show,
  1167. };
  1168. static int extfrag_open(struct inode *inode, struct file *file)
  1169. {
  1170. return seq_open(file, &extfrag_op);
  1171. }
  1172. static const struct file_operations extfrag_file_ops = {
  1173. .open = extfrag_open,
  1174. .read = seq_read,
  1175. .llseek = seq_lseek,
  1176. .release = seq_release,
  1177. };
  1178. static int __init extfrag_debug_init(void)
  1179. {
  1180. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1181. if (!extfrag_debug_root)
  1182. return -ENOMEM;
  1183. if (!debugfs_create_file("unusable_index", 0444,
  1184. extfrag_debug_root, NULL, &unusable_file_ops))
  1185. return -ENOMEM;
  1186. if (!debugfs_create_file("extfrag_index", 0444,
  1187. extfrag_debug_root, NULL, &extfrag_file_ops))
  1188. return -ENOMEM;
  1189. return 0;
  1190. }
  1191. module_init(extfrag_debug_init);
  1192. #endif