vmstat.c 38 KB

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