vmstat.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. #endif
  694. #endif /* CONFIG_VM_EVENTS_COUNTERS */
  695. };
  696. #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
  697. #ifdef CONFIG_PROC_FS
  698. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  699. struct zone *zone)
  700. {
  701. int order;
  702. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  703. for (order = 0; order < MAX_ORDER; ++order)
  704. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  705. seq_putc(m, '\n');
  706. }
  707. /*
  708. * This walks the free areas for each zone.
  709. */
  710. static int frag_show(struct seq_file *m, void *arg)
  711. {
  712. pg_data_t *pgdat = (pg_data_t *)arg;
  713. walk_zones_in_node(m, pgdat, frag_show_print);
  714. return 0;
  715. }
  716. static void pagetypeinfo_showfree_print(struct seq_file *m,
  717. pg_data_t *pgdat, struct zone *zone)
  718. {
  719. int order, mtype;
  720. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  721. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  722. pgdat->node_id,
  723. zone->name,
  724. migratetype_names[mtype]);
  725. for (order = 0; order < MAX_ORDER; ++order) {
  726. unsigned long freecount = 0;
  727. struct free_area *area;
  728. struct list_head *curr;
  729. area = &(zone->free_area[order]);
  730. list_for_each(curr, &area->free_list[mtype])
  731. freecount++;
  732. seq_printf(m, "%6lu ", freecount);
  733. }
  734. seq_putc(m, '\n');
  735. }
  736. }
  737. /* Print out the free pages at each order for each migatetype */
  738. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  739. {
  740. int order;
  741. pg_data_t *pgdat = (pg_data_t *)arg;
  742. /* Print header */
  743. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  744. for (order = 0; order < MAX_ORDER; ++order)
  745. seq_printf(m, "%6d ", order);
  746. seq_putc(m, '\n');
  747. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  748. return 0;
  749. }
  750. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  751. pg_data_t *pgdat, struct zone *zone)
  752. {
  753. int mtype;
  754. unsigned long pfn;
  755. unsigned long start_pfn = zone->zone_start_pfn;
  756. unsigned long end_pfn = start_pfn + zone->spanned_pages;
  757. unsigned long count[MIGRATE_TYPES] = { 0, };
  758. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  759. struct page *page;
  760. if (!pfn_valid(pfn))
  761. continue;
  762. page = pfn_to_page(pfn);
  763. /* Watch for unexpected holes punched in the memmap */
  764. if (!memmap_valid_within(pfn, page, zone))
  765. continue;
  766. mtype = get_pageblock_migratetype(page);
  767. if (mtype < MIGRATE_TYPES)
  768. count[mtype]++;
  769. }
  770. /* Print counts */
  771. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  772. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  773. seq_printf(m, "%12lu ", count[mtype]);
  774. seq_putc(m, '\n');
  775. }
  776. /* Print out the free pages at each order for each migratetype */
  777. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  778. {
  779. int mtype;
  780. pg_data_t *pgdat = (pg_data_t *)arg;
  781. seq_printf(m, "\n%-23s", "Number of blocks type ");
  782. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  783. seq_printf(m, "%12s ", migratetype_names[mtype]);
  784. seq_putc(m, '\n');
  785. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  786. return 0;
  787. }
  788. /*
  789. * This prints out statistics in relation to grouping pages by mobility.
  790. * It is expensive to collect so do not constantly read the file.
  791. */
  792. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  793. {
  794. pg_data_t *pgdat = (pg_data_t *)arg;
  795. /* check memoryless node */
  796. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  797. return 0;
  798. seq_printf(m, "Page block order: %d\n", pageblock_order);
  799. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  800. seq_putc(m, '\n');
  801. pagetypeinfo_showfree(m, pgdat);
  802. pagetypeinfo_showblockcount(m, pgdat);
  803. return 0;
  804. }
  805. static const struct seq_operations fragmentation_op = {
  806. .start = frag_start,
  807. .next = frag_next,
  808. .stop = frag_stop,
  809. .show = frag_show,
  810. };
  811. static int fragmentation_open(struct inode *inode, struct file *file)
  812. {
  813. return seq_open(file, &fragmentation_op);
  814. }
  815. static const struct file_operations fragmentation_file_operations = {
  816. .open = fragmentation_open,
  817. .read = seq_read,
  818. .llseek = seq_lseek,
  819. .release = seq_release,
  820. };
  821. static const struct seq_operations pagetypeinfo_op = {
  822. .start = frag_start,
  823. .next = frag_next,
  824. .stop = frag_stop,
  825. .show = pagetypeinfo_show,
  826. };
  827. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  828. {
  829. return seq_open(file, &pagetypeinfo_op);
  830. }
  831. static const struct file_operations pagetypeinfo_file_ops = {
  832. .open = pagetypeinfo_open,
  833. .read = seq_read,
  834. .llseek = seq_lseek,
  835. .release = seq_release,
  836. };
  837. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  838. struct zone *zone)
  839. {
  840. int i;
  841. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  842. seq_printf(m,
  843. "\n pages free %lu"
  844. "\n min %lu"
  845. "\n low %lu"
  846. "\n high %lu"
  847. "\n scanned %lu"
  848. "\n spanned %lu"
  849. "\n present %lu",
  850. zone_page_state(zone, NR_FREE_PAGES),
  851. min_wmark_pages(zone),
  852. low_wmark_pages(zone),
  853. high_wmark_pages(zone),
  854. zone->pages_scanned,
  855. zone->spanned_pages,
  856. zone->present_pages);
  857. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  858. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  859. zone_page_state(zone, i));
  860. seq_printf(m,
  861. "\n protection: (%lu",
  862. zone->lowmem_reserve[0]);
  863. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  864. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  865. seq_printf(m,
  866. ")"
  867. "\n pagesets");
  868. for_each_online_cpu(i) {
  869. struct per_cpu_pageset *pageset;
  870. pageset = per_cpu_ptr(zone->pageset, i);
  871. seq_printf(m,
  872. "\n cpu: %i"
  873. "\n count: %i"
  874. "\n high: %i"
  875. "\n batch: %i",
  876. i,
  877. pageset->pcp.count,
  878. pageset->pcp.high,
  879. pageset->pcp.batch);
  880. #ifdef CONFIG_SMP
  881. seq_printf(m, "\n vm stats threshold: %d",
  882. pageset->stat_threshold);
  883. #endif
  884. }
  885. seq_printf(m,
  886. "\n all_unreclaimable: %u"
  887. "\n start_pfn: %lu"
  888. "\n inactive_ratio: %u",
  889. zone->all_unreclaimable,
  890. zone->zone_start_pfn,
  891. zone->inactive_ratio);
  892. seq_putc(m, '\n');
  893. }
  894. /*
  895. * Output information about zones in @pgdat.
  896. */
  897. static int zoneinfo_show(struct seq_file *m, void *arg)
  898. {
  899. pg_data_t *pgdat = (pg_data_t *)arg;
  900. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  901. return 0;
  902. }
  903. static const struct seq_operations zoneinfo_op = {
  904. .start = frag_start, /* iterate over all zones. The same as in
  905. * fragmentation. */
  906. .next = frag_next,
  907. .stop = frag_stop,
  908. .show = zoneinfo_show,
  909. };
  910. static int zoneinfo_open(struct inode *inode, struct file *file)
  911. {
  912. return seq_open(file, &zoneinfo_op);
  913. }
  914. static const struct file_operations proc_zoneinfo_file_operations = {
  915. .open = zoneinfo_open,
  916. .read = seq_read,
  917. .llseek = seq_lseek,
  918. .release = seq_release,
  919. };
  920. enum writeback_stat_item {
  921. NR_DIRTY_THRESHOLD,
  922. NR_DIRTY_BG_THRESHOLD,
  923. NR_VM_WRITEBACK_STAT_ITEMS,
  924. };
  925. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  926. {
  927. unsigned long *v;
  928. int i, stat_items_size;
  929. if (*pos >= ARRAY_SIZE(vmstat_text))
  930. return NULL;
  931. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  932. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  933. #ifdef CONFIG_VM_EVENT_COUNTERS
  934. stat_items_size += sizeof(struct vm_event_state);
  935. #endif
  936. v = kmalloc(stat_items_size, GFP_KERNEL);
  937. m->private = v;
  938. if (!v)
  939. return ERR_PTR(-ENOMEM);
  940. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  941. v[i] = global_page_state(i);
  942. v += NR_VM_ZONE_STAT_ITEMS;
  943. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  944. v + NR_DIRTY_THRESHOLD);
  945. v += NR_VM_WRITEBACK_STAT_ITEMS;
  946. #ifdef CONFIG_VM_EVENT_COUNTERS
  947. all_vm_events(v);
  948. v[PGPGIN] /= 2; /* sectors -> kbytes */
  949. v[PGPGOUT] /= 2;
  950. #endif
  951. return (unsigned long *)m->private + *pos;
  952. }
  953. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  954. {
  955. (*pos)++;
  956. if (*pos >= ARRAY_SIZE(vmstat_text))
  957. return NULL;
  958. return (unsigned long *)m->private + *pos;
  959. }
  960. static int vmstat_show(struct seq_file *m, void *arg)
  961. {
  962. unsigned long *l = arg;
  963. unsigned long off = l - (unsigned long *)m->private;
  964. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  965. return 0;
  966. }
  967. static void vmstat_stop(struct seq_file *m, void *arg)
  968. {
  969. kfree(m->private);
  970. m->private = NULL;
  971. }
  972. static const struct seq_operations vmstat_op = {
  973. .start = vmstat_start,
  974. .next = vmstat_next,
  975. .stop = vmstat_stop,
  976. .show = vmstat_show,
  977. };
  978. static int vmstat_open(struct inode *inode, struct file *file)
  979. {
  980. return seq_open(file, &vmstat_op);
  981. }
  982. static const struct file_operations proc_vmstat_file_operations = {
  983. .open = vmstat_open,
  984. .read = seq_read,
  985. .llseek = seq_lseek,
  986. .release = seq_release,
  987. };
  988. #endif /* CONFIG_PROC_FS */
  989. #ifdef CONFIG_SMP
  990. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  991. int sysctl_stat_interval __read_mostly = HZ;
  992. static void vmstat_update(struct work_struct *w)
  993. {
  994. refresh_cpu_vm_stats(smp_processor_id());
  995. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  996. round_jiffies_relative(sysctl_stat_interval));
  997. }
  998. static void __cpuinit start_cpu_timer(int cpu)
  999. {
  1000. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  1001. INIT_DEFERRABLE_WORK(work, vmstat_update);
  1002. schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
  1003. }
  1004. /*
  1005. * Use the cpu notifier to insure that the thresholds are recalculated
  1006. * when necessary.
  1007. */
  1008. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  1009. unsigned long action,
  1010. void *hcpu)
  1011. {
  1012. long cpu = (long)hcpu;
  1013. switch (action) {
  1014. case CPU_ONLINE:
  1015. case CPU_ONLINE_FROZEN:
  1016. refresh_zone_stat_thresholds();
  1017. start_cpu_timer(cpu);
  1018. node_set_state(cpu_to_node(cpu), N_CPU);
  1019. break;
  1020. case CPU_DOWN_PREPARE:
  1021. case CPU_DOWN_PREPARE_FROZEN:
  1022. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1023. per_cpu(vmstat_work, cpu).work.func = NULL;
  1024. break;
  1025. case CPU_DOWN_FAILED:
  1026. case CPU_DOWN_FAILED_FROZEN:
  1027. start_cpu_timer(cpu);
  1028. break;
  1029. case CPU_DEAD:
  1030. case CPU_DEAD_FROZEN:
  1031. refresh_zone_stat_thresholds();
  1032. break;
  1033. default:
  1034. break;
  1035. }
  1036. return NOTIFY_OK;
  1037. }
  1038. static struct notifier_block __cpuinitdata vmstat_notifier =
  1039. { &vmstat_cpuup_callback, NULL, 0 };
  1040. #endif
  1041. static int __init setup_vmstat(void)
  1042. {
  1043. #ifdef CONFIG_SMP
  1044. int cpu;
  1045. register_cpu_notifier(&vmstat_notifier);
  1046. for_each_online_cpu(cpu)
  1047. start_cpu_timer(cpu);
  1048. #endif
  1049. #ifdef CONFIG_PROC_FS
  1050. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  1051. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  1052. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  1053. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  1054. #endif
  1055. return 0;
  1056. }
  1057. module_init(setup_vmstat)
  1058. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1059. #include <linux/debugfs.h>
  1060. /*
  1061. * Return an index indicating how much of the available free memory is
  1062. * unusable for an allocation of the requested size.
  1063. */
  1064. static int unusable_free_index(unsigned int order,
  1065. struct contig_page_info *info)
  1066. {
  1067. /* No free memory is interpreted as all free memory is unusable */
  1068. if (info->free_pages == 0)
  1069. return 1000;
  1070. /*
  1071. * Index should be a value between 0 and 1. Return a value to 3
  1072. * decimal places.
  1073. *
  1074. * 0 => no fragmentation
  1075. * 1 => high fragmentation
  1076. */
  1077. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1078. }
  1079. static void unusable_show_print(struct seq_file *m,
  1080. pg_data_t *pgdat, struct zone *zone)
  1081. {
  1082. unsigned int order;
  1083. int index;
  1084. struct contig_page_info info;
  1085. seq_printf(m, "Node %d, zone %8s ",
  1086. pgdat->node_id,
  1087. zone->name);
  1088. for (order = 0; order < MAX_ORDER; ++order) {
  1089. fill_contig_page_info(zone, order, &info);
  1090. index = unusable_free_index(order, &info);
  1091. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1092. }
  1093. seq_putc(m, '\n');
  1094. }
  1095. /*
  1096. * Display unusable free space index
  1097. *
  1098. * The unusable free space index measures how much of the available free
  1099. * memory cannot be used to satisfy an allocation of a given size and is a
  1100. * value between 0 and 1. The higher the value, the more of free memory is
  1101. * unusable and by implication, the worse the external fragmentation is. This
  1102. * can be expressed as a percentage by multiplying by 100.
  1103. */
  1104. static int unusable_show(struct seq_file *m, void *arg)
  1105. {
  1106. pg_data_t *pgdat = (pg_data_t *)arg;
  1107. /* check memoryless node */
  1108. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  1109. return 0;
  1110. walk_zones_in_node(m, pgdat, unusable_show_print);
  1111. return 0;
  1112. }
  1113. static const struct seq_operations unusable_op = {
  1114. .start = frag_start,
  1115. .next = frag_next,
  1116. .stop = frag_stop,
  1117. .show = unusable_show,
  1118. };
  1119. static int unusable_open(struct inode *inode, struct file *file)
  1120. {
  1121. return seq_open(file, &unusable_op);
  1122. }
  1123. static const struct file_operations unusable_file_ops = {
  1124. .open = unusable_open,
  1125. .read = seq_read,
  1126. .llseek = seq_lseek,
  1127. .release = seq_release,
  1128. };
  1129. static void extfrag_show_print(struct seq_file *m,
  1130. pg_data_t *pgdat, struct zone *zone)
  1131. {
  1132. unsigned int order;
  1133. int index;
  1134. /* Alloc on stack as interrupts are disabled for zone walk */
  1135. struct contig_page_info info;
  1136. seq_printf(m, "Node %d, zone %8s ",
  1137. pgdat->node_id,
  1138. zone->name);
  1139. for (order = 0; order < MAX_ORDER; ++order) {
  1140. fill_contig_page_info(zone, order, &info);
  1141. index = __fragmentation_index(order, &info);
  1142. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1143. }
  1144. seq_putc(m, '\n');
  1145. }
  1146. /*
  1147. * Display fragmentation index for orders that allocations would fail for
  1148. */
  1149. static int extfrag_show(struct seq_file *m, void *arg)
  1150. {
  1151. pg_data_t *pgdat = (pg_data_t *)arg;
  1152. walk_zones_in_node(m, pgdat, extfrag_show_print);
  1153. return 0;
  1154. }
  1155. static const struct seq_operations extfrag_op = {
  1156. .start = frag_start,
  1157. .next = frag_next,
  1158. .stop = frag_stop,
  1159. .show = extfrag_show,
  1160. };
  1161. static int extfrag_open(struct inode *inode, struct file *file)
  1162. {
  1163. return seq_open(file, &extfrag_op);
  1164. }
  1165. static const struct file_operations extfrag_file_ops = {
  1166. .open = extfrag_open,
  1167. .read = seq_read,
  1168. .llseek = seq_lseek,
  1169. .release = seq_release,
  1170. };
  1171. static int __init extfrag_debug_init(void)
  1172. {
  1173. struct dentry *extfrag_debug_root;
  1174. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1175. if (!extfrag_debug_root)
  1176. return -ENOMEM;
  1177. if (!debugfs_create_file("unusable_index", 0444,
  1178. extfrag_debug_root, NULL, &unusable_file_ops))
  1179. goto fail;
  1180. if (!debugfs_create_file("extfrag_index", 0444,
  1181. extfrag_debug_root, NULL, &extfrag_file_ops))
  1182. goto fail;
  1183. return 0;
  1184. fail:
  1185. debugfs_remove_recursive(extfrag_debug_root);
  1186. return -ENOMEM;
  1187. }
  1188. module_init(extfrag_debug_init);
  1189. #endif