vmstat.c 35 KB

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