vmstat.c 34 KB

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