vmstat.c 42 KB

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