vmstat.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  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. TEXTS_FOR_ZONES("pgrefill")
  703. TEXTS_FOR_ZONES("pgsteal_kswapd")
  704. TEXTS_FOR_ZONES("pgsteal_direct")
  705. TEXTS_FOR_ZONES("pgscan_kswapd")
  706. TEXTS_FOR_ZONES("pgscan_direct")
  707. "pgscan_direct_throttle",
  708. #ifdef CONFIG_NUMA
  709. "zone_reclaim_failed",
  710. #endif
  711. "pginodesteal",
  712. "slabs_scanned",
  713. "kswapd_inodesteal",
  714. "kswapd_low_wmark_hit_quickly",
  715. "kswapd_high_wmark_hit_quickly",
  716. "pageoutrun",
  717. "allocstall",
  718. "pgrotated",
  719. "drop_pagecache",
  720. "drop_slab",
  721. #ifdef CONFIG_NUMA_BALANCING
  722. "numa_pte_updates",
  723. "numa_huge_pte_updates",
  724. "numa_hint_faults",
  725. "numa_hint_faults_local",
  726. "numa_pages_migrated",
  727. #endif
  728. #ifdef CONFIG_MIGRATION
  729. "pgmigrate_success",
  730. "pgmigrate_fail",
  731. #endif
  732. #ifdef CONFIG_COMPACTION
  733. "compact_migrate_scanned",
  734. "compact_free_scanned",
  735. "compact_isolated",
  736. "compact_stall",
  737. "compact_fail",
  738. "compact_success",
  739. #endif
  740. #ifdef CONFIG_HUGETLB_PAGE
  741. "htlb_buddy_alloc_success",
  742. "htlb_buddy_alloc_fail",
  743. #endif
  744. "unevictable_pgs_culled",
  745. "unevictable_pgs_scanned",
  746. "unevictable_pgs_rescued",
  747. "unevictable_pgs_mlocked",
  748. "unevictable_pgs_munlocked",
  749. "unevictable_pgs_cleared",
  750. "unevictable_pgs_stranded",
  751. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  752. "thp_fault_alloc",
  753. "thp_fault_fallback",
  754. "thp_collapse_alloc",
  755. "thp_collapse_alloc_failed",
  756. "thp_split",
  757. "thp_zero_page_alloc",
  758. "thp_zero_page_alloc_failed",
  759. #endif
  760. #ifdef CONFIG_MEMORY_BALLOON
  761. "balloon_inflate",
  762. "balloon_deflate",
  763. #ifdef CONFIG_BALLOON_COMPACTION
  764. "balloon_migrate",
  765. #endif
  766. #endif /* CONFIG_MEMORY_BALLOON */
  767. #ifdef CONFIG_DEBUG_TLBFLUSH
  768. #ifdef CONFIG_SMP
  769. "nr_tlb_remote_flush",
  770. "nr_tlb_remote_flush_received",
  771. #endif /* CONFIG_SMP */
  772. "nr_tlb_local_flush_all",
  773. "nr_tlb_local_flush_one",
  774. #endif /* CONFIG_DEBUG_TLBFLUSH */
  775. #ifdef CONFIG_DEBUG_VM_VMACACHE
  776. "vmacache_find_calls",
  777. "vmacache_find_hits",
  778. #endif
  779. #endif /* CONFIG_VM_EVENTS_COUNTERS */
  780. };
  781. #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
  782. #ifdef CONFIG_PROC_FS
  783. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  784. struct zone *zone)
  785. {
  786. int order;
  787. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  788. for (order = 0; order < MAX_ORDER; ++order)
  789. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  790. seq_putc(m, '\n');
  791. }
  792. /*
  793. * This walks the free areas for each zone.
  794. */
  795. static int frag_show(struct seq_file *m, void *arg)
  796. {
  797. pg_data_t *pgdat = (pg_data_t *)arg;
  798. walk_zones_in_node(m, pgdat, frag_show_print);
  799. return 0;
  800. }
  801. static void pagetypeinfo_showfree_print(struct seq_file *m,
  802. pg_data_t *pgdat, struct zone *zone)
  803. {
  804. int order, mtype;
  805. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  806. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  807. pgdat->node_id,
  808. zone->name,
  809. migratetype_names[mtype]);
  810. for (order = 0; order < MAX_ORDER; ++order) {
  811. unsigned long freecount = 0;
  812. struct free_area *area;
  813. struct list_head *curr;
  814. area = &(zone->free_area[order]);
  815. list_for_each(curr, &area->free_list[mtype])
  816. freecount++;
  817. seq_printf(m, "%6lu ", freecount);
  818. }
  819. seq_putc(m, '\n');
  820. }
  821. }
  822. /* Print out the free pages at each order for each migatetype */
  823. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  824. {
  825. int order;
  826. pg_data_t *pgdat = (pg_data_t *)arg;
  827. /* Print header */
  828. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  829. for (order = 0; order < MAX_ORDER; ++order)
  830. seq_printf(m, "%6d ", order);
  831. seq_putc(m, '\n');
  832. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  833. return 0;
  834. }
  835. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  836. pg_data_t *pgdat, struct zone *zone)
  837. {
  838. int mtype;
  839. unsigned long pfn;
  840. unsigned long start_pfn = zone->zone_start_pfn;
  841. unsigned long end_pfn = zone_end_pfn(zone);
  842. unsigned long count[MIGRATE_TYPES] = { 0, };
  843. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  844. struct page *page;
  845. if (!pfn_valid(pfn))
  846. continue;
  847. page = pfn_to_page(pfn);
  848. /* Watch for unexpected holes punched in the memmap */
  849. if (!memmap_valid_within(pfn, page, zone))
  850. continue;
  851. mtype = get_pageblock_migratetype(page);
  852. if (mtype < MIGRATE_TYPES)
  853. count[mtype]++;
  854. }
  855. /* Print counts */
  856. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  857. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  858. seq_printf(m, "%12lu ", count[mtype]);
  859. seq_putc(m, '\n');
  860. }
  861. /* Print out the free pages at each order for each migratetype */
  862. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  863. {
  864. int mtype;
  865. pg_data_t *pgdat = (pg_data_t *)arg;
  866. seq_printf(m, "\n%-23s", "Number of blocks type ");
  867. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  868. seq_printf(m, "%12s ", migratetype_names[mtype]);
  869. seq_putc(m, '\n');
  870. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  871. return 0;
  872. }
  873. /*
  874. * This prints out statistics in relation to grouping pages by mobility.
  875. * It is expensive to collect so do not constantly read the file.
  876. */
  877. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  878. {
  879. pg_data_t *pgdat = (pg_data_t *)arg;
  880. /* check memoryless node */
  881. if (!node_state(pgdat->node_id, N_MEMORY))
  882. return 0;
  883. seq_printf(m, "Page block order: %d\n", pageblock_order);
  884. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  885. seq_putc(m, '\n');
  886. pagetypeinfo_showfree(m, pgdat);
  887. pagetypeinfo_showblockcount(m, pgdat);
  888. return 0;
  889. }
  890. static const struct seq_operations fragmentation_op = {
  891. .start = frag_start,
  892. .next = frag_next,
  893. .stop = frag_stop,
  894. .show = frag_show,
  895. };
  896. static int fragmentation_open(struct inode *inode, struct file *file)
  897. {
  898. return seq_open(file, &fragmentation_op);
  899. }
  900. static const struct file_operations fragmentation_file_operations = {
  901. .open = fragmentation_open,
  902. .read = seq_read,
  903. .llseek = seq_lseek,
  904. .release = seq_release,
  905. };
  906. static const struct seq_operations pagetypeinfo_op = {
  907. .start = frag_start,
  908. .next = frag_next,
  909. .stop = frag_stop,
  910. .show = pagetypeinfo_show,
  911. };
  912. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  913. {
  914. return seq_open(file, &pagetypeinfo_op);
  915. }
  916. static const struct file_operations pagetypeinfo_file_ops = {
  917. .open = pagetypeinfo_open,
  918. .read = seq_read,
  919. .llseek = seq_lseek,
  920. .release = seq_release,
  921. };
  922. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  923. struct zone *zone)
  924. {
  925. int i;
  926. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  927. seq_printf(m,
  928. "\n pages free %lu"
  929. "\n min %lu"
  930. "\n low %lu"
  931. "\n high %lu"
  932. "\n scanned %lu"
  933. "\n spanned %lu"
  934. "\n present %lu"
  935. "\n managed %lu",
  936. zone_page_state(zone, NR_FREE_PAGES),
  937. min_wmark_pages(zone),
  938. low_wmark_pages(zone),
  939. high_wmark_pages(zone),
  940. zone_page_state(zone, NR_PAGES_SCANNED),
  941. zone->spanned_pages,
  942. zone->present_pages,
  943. zone->managed_pages);
  944. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  945. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  946. zone_page_state(zone, i));
  947. seq_printf(m,
  948. "\n protection: (%ld",
  949. zone->lowmem_reserve[0]);
  950. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  951. seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
  952. seq_printf(m,
  953. ")"
  954. "\n pagesets");
  955. for_each_online_cpu(i) {
  956. struct per_cpu_pageset *pageset;
  957. pageset = per_cpu_ptr(zone->pageset, i);
  958. seq_printf(m,
  959. "\n cpu: %i"
  960. "\n count: %i"
  961. "\n high: %i"
  962. "\n batch: %i",
  963. i,
  964. pageset->pcp.count,
  965. pageset->pcp.high,
  966. pageset->pcp.batch);
  967. #ifdef CONFIG_SMP
  968. seq_printf(m, "\n vm stats threshold: %d",
  969. pageset->stat_threshold);
  970. #endif
  971. }
  972. seq_printf(m,
  973. "\n all_unreclaimable: %u"
  974. "\n start_pfn: %lu"
  975. "\n inactive_ratio: %u",
  976. !zone_reclaimable(zone),
  977. zone->zone_start_pfn,
  978. zone->inactive_ratio);
  979. seq_putc(m, '\n');
  980. }
  981. /*
  982. * Output information about zones in @pgdat.
  983. */
  984. static int zoneinfo_show(struct seq_file *m, void *arg)
  985. {
  986. pg_data_t *pgdat = (pg_data_t *)arg;
  987. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  988. return 0;
  989. }
  990. static const struct seq_operations zoneinfo_op = {
  991. .start = frag_start, /* iterate over all zones. The same as in
  992. * fragmentation. */
  993. .next = frag_next,
  994. .stop = frag_stop,
  995. .show = zoneinfo_show,
  996. };
  997. static int zoneinfo_open(struct inode *inode, struct file *file)
  998. {
  999. return seq_open(file, &zoneinfo_op);
  1000. }
  1001. static const struct file_operations proc_zoneinfo_file_operations = {
  1002. .open = zoneinfo_open,
  1003. .read = seq_read,
  1004. .llseek = seq_lseek,
  1005. .release = seq_release,
  1006. };
  1007. enum writeback_stat_item {
  1008. NR_DIRTY_THRESHOLD,
  1009. NR_DIRTY_BG_THRESHOLD,
  1010. NR_VM_WRITEBACK_STAT_ITEMS,
  1011. };
  1012. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  1013. {
  1014. unsigned long *v;
  1015. int i, stat_items_size;
  1016. if (*pos >= ARRAY_SIZE(vmstat_text))
  1017. return NULL;
  1018. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  1019. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  1020. #ifdef CONFIG_VM_EVENT_COUNTERS
  1021. stat_items_size += sizeof(struct vm_event_state);
  1022. #endif
  1023. v = kmalloc(stat_items_size, GFP_KERNEL);
  1024. m->private = v;
  1025. if (!v)
  1026. return ERR_PTR(-ENOMEM);
  1027. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1028. v[i] = global_page_state(i);
  1029. v += NR_VM_ZONE_STAT_ITEMS;
  1030. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  1031. v + NR_DIRTY_THRESHOLD);
  1032. v += NR_VM_WRITEBACK_STAT_ITEMS;
  1033. #ifdef CONFIG_VM_EVENT_COUNTERS
  1034. all_vm_events(v);
  1035. v[PGPGIN] /= 2; /* sectors -> kbytes */
  1036. v[PGPGOUT] /= 2;
  1037. #endif
  1038. return (unsigned long *)m->private + *pos;
  1039. }
  1040. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1041. {
  1042. (*pos)++;
  1043. if (*pos >= ARRAY_SIZE(vmstat_text))
  1044. return NULL;
  1045. return (unsigned long *)m->private + *pos;
  1046. }
  1047. static int vmstat_show(struct seq_file *m, void *arg)
  1048. {
  1049. unsigned long *l = arg;
  1050. unsigned long off = l - (unsigned long *)m->private;
  1051. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  1052. return 0;
  1053. }
  1054. static void vmstat_stop(struct seq_file *m, void *arg)
  1055. {
  1056. kfree(m->private);
  1057. m->private = NULL;
  1058. }
  1059. static const struct seq_operations vmstat_op = {
  1060. .start = vmstat_start,
  1061. .next = vmstat_next,
  1062. .stop = vmstat_stop,
  1063. .show = vmstat_show,
  1064. };
  1065. static int vmstat_open(struct inode *inode, struct file *file)
  1066. {
  1067. return seq_open(file, &vmstat_op);
  1068. }
  1069. static const struct file_operations proc_vmstat_file_operations = {
  1070. .open = vmstat_open,
  1071. .read = seq_read,
  1072. .llseek = seq_lseek,
  1073. .release = seq_release,
  1074. };
  1075. #endif /* CONFIG_PROC_FS */
  1076. #ifdef CONFIG_SMP
  1077. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  1078. int sysctl_stat_interval __read_mostly = HZ;
  1079. static cpumask_var_t cpu_stat_off;
  1080. static void vmstat_update(struct work_struct *w)
  1081. {
  1082. if (refresh_cpu_vm_stats())
  1083. /*
  1084. * Counters were updated so we expect more updates
  1085. * to occur in the future. Keep on running the
  1086. * update worker thread.
  1087. */
  1088. schedule_delayed_work(this_cpu_ptr(&vmstat_work),
  1089. round_jiffies_relative(sysctl_stat_interval));
  1090. else {
  1091. /*
  1092. * We did not update any counters so the app may be in
  1093. * a mode where it does not cause counter updates.
  1094. * We may be uselessly running vmstat_update.
  1095. * Defer the checking for differentials to the
  1096. * shepherd thread on a different processor.
  1097. */
  1098. int r;
  1099. /*
  1100. * Shepherd work thread does not race since it never
  1101. * changes the bit if its zero but the cpu
  1102. * online / off line code may race if
  1103. * worker threads are still allowed during
  1104. * shutdown / startup.
  1105. */
  1106. r = cpumask_test_and_set_cpu(smp_processor_id(),
  1107. cpu_stat_off);
  1108. VM_BUG_ON(r);
  1109. }
  1110. }
  1111. /*
  1112. * Check if the diffs for a certain cpu indicate that
  1113. * an update is needed.
  1114. */
  1115. static bool need_update(int cpu)
  1116. {
  1117. struct zone *zone;
  1118. for_each_populated_zone(zone) {
  1119. struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
  1120. BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
  1121. /*
  1122. * The fast way of checking if there are any vmstat diffs.
  1123. * This works because the diffs are byte sized items.
  1124. */
  1125. if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
  1126. return true;
  1127. }
  1128. return false;
  1129. }
  1130. /*
  1131. * Shepherd worker thread that checks the
  1132. * differentials of processors that have their worker
  1133. * threads for vm statistics updates disabled because of
  1134. * inactivity.
  1135. */
  1136. static void vmstat_shepherd(struct work_struct *w);
  1137. static DECLARE_DELAYED_WORK(shepherd, vmstat_shepherd);
  1138. static void vmstat_shepherd(struct work_struct *w)
  1139. {
  1140. int cpu;
  1141. get_online_cpus();
  1142. /* Check processors whose vmstat worker threads have been disabled */
  1143. for_each_cpu(cpu, cpu_stat_off)
  1144. if (need_update(cpu) &&
  1145. cpumask_test_and_clear_cpu(cpu, cpu_stat_off))
  1146. schedule_delayed_work_on(cpu, &per_cpu(vmstat_work, cpu),
  1147. __round_jiffies_relative(sysctl_stat_interval, cpu));
  1148. put_online_cpus();
  1149. schedule_delayed_work(&shepherd,
  1150. round_jiffies_relative(sysctl_stat_interval));
  1151. }
  1152. static void __init start_shepherd_timer(void)
  1153. {
  1154. int cpu;
  1155. for_each_possible_cpu(cpu)
  1156. INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
  1157. vmstat_update);
  1158. if (!alloc_cpumask_var(&cpu_stat_off, GFP_KERNEL))
  1159. BUG();
  1160. cpumask_copy(cpu_stat_off, cpu_online_mask);
  1161. schedule_delayed_work(&shepherd,
  1162. round_jiffies_relative(sysctl_stat_interval));
  1163. }
  1164. static void vmstat_cpu_dead(int node)
  1165. {
  1166. int cpu;
  1167. get_online_cpus();
  1168. for_each_online_cpu(cpu)
  1169. if (cpu_to_node(cpu) == node)
  1170. goto end;
  1171. node_clear_state(node, N_CPU);
  1172. end:
  1173. put_online_cpus();
  1174. }
  1175. /*
  1176. * Use the cpu notifier to insure that the thresholds are recalculated
  1177. * when necessary.
  1178. */
  1179. static int vmstat_cpuup_callback(struct notifier_block *nfb,
  1180. unsigned long action,
  1181. void *hcpu)
  1182. {
  1183. long cpu = (long)hcpu;
  1184. switch (action) {
  1185. case CPU_ONLINE:
  1186. case CPU_ONLINE_FROZEN:
  1187. refresh_zone_stat_thresholds();
  1188. node_set_state(cpu_to_node(cpu), N_CPU);
  1189. cpumask_set_cpu(cpu, cpu_stat_off);
  1190. break;
  1191. case CPU_DOWN_PREPARE:
  1192. case CPU_DOWN_PREPARE_FROZEN:
  1193. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1194. cpumask_clear_cpu(cpu, cpu_stat_off);
  1195. break;
  1196. case CPU_DOWN_FAILED:
  1197. case CPU_DOWN_FAILED_FROZEN:
  1198. cpumask_set_cpu(cpu, cpu_stat_off);
  1199. break;
  1200. case CPU_DEAD:
  1201. case CPU_DEAD_FROZEN:
  1202. refresh_zone_stat_thresholds();
  1203. vmstat_cpu_dead(cpu_to_node(cpu));
  1204. break;
  1205. default:
  1206. break;
  1207. }
  1208. return NOTIFY_OK;
  1209. }
  1210. static struct notifier_block vmstat_notifier =
  1211. { &vmstat_cpuup_callback, NULL, 0 };
  1212. #endif
  1213. static int __init setup_vmstat(void)
  1214. {
  1215. #ifdef CONFIG_SMP
  1216. cpu_notifier_register_begin();
  1217. __register_cpu_notifier(&vmstat_notifier);
  1218. start_shepherd_timer();
  1219. cpu_notifier_register_done();
  1220. #endif
  1221. #ifdef CONFIG_PROC_FS
  1222. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  1223. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  1224. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  1225. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  1226. #endif
  1227. return 0;
  1228. }
  1229. module_init(setup_vmstat)
  1230. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1231. #include <linux/debugfs.h>
  1232. /*
  1233. * Return an index indicating how much of the available free memory is
  1234. * unusable for an allocation of the requested size.
  1235. */
  1236. static int unusable_free_index(unsigned int order,
  1237. struct contig_page_info *info)
  1238. {
  1239. /* No free memory is interpreted as all free memory is unusable */
  1240. if (info->free_pages == 0)
  1241. return 1000;
  1242. /*
  1243. * Index should be a value between 0 and 1. Return a value to 3
  1244. * decimal places.
  1245. *
  1246. * 0 => no fragmentation
  1247. * 1 => high fragmentation
  1248. */
  1249. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1250. }
  1251. static void unusable_show_print(struct seq_file *m,
  1252. pg_data_t *pgdat, struct zone *zone)
  1253. {
  1254. unsigned int order;
  1255. int index;
  1256. struct contig_page_info info;
  1257. seq_printf(m, "Node %d, zone %8s ",
  1258. pgdat->node_id,
  1259. zone->name);
  1260. for (order = 0; order < MAX_ORDER; ++order) {
  1261. fill_contig_page_info(zone, order, &info);
  1262. index = unusable_free_index(order, &info);
  1263. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1264. }
  1265. seq_putc(m, '\n');
  1266. }
  1267. /*
  1268. * Display unusable free space index
  1269. *
  1270. * The unusable free space index measures how much of the available free
  1271. * memory cannot be used to satisfy an allocation of a given size and is a
  1272. * value between 0 and 1. The higher the value, the more of free memory is
  1273. * unusable and by implication, the worse the external fragmentation is. This
  1274. * can be expressed as a percentage by multiplying by 100.
  1275. */
  1276. static int unusable_show(struct seq_file *m, void *arg)
  1277. {
  1278. pg_data_t *pgdat = (pg_data_t *)arg;
  1279. /* check memoryless node */
  1280. if (!node_state(pgdat->node_id, N_MEMORY))
  1281. return 0;
  1282. walk_zones_in_node(m, pgdat, unusable_show_print);
  1283. return 0;
  1284. }
  1285. static const struct seq_operations unusable_op = {
  1286. .start = frag_start,
  1287. .next = frag_next,
  1288. .stop = frag_stop,
  1289. .show = unusable_show,
  1290. };
  1291. static int unusable_open(struct inode *inode, struct file *file)
  1292. {
  1293. return seq_open(file, &unusable_op);
  1294. }
  1295. static const struct file_operations unusable_file_ops = {
  1296. .open = unusable_open,
  1297. .read = seq_read,
  1298. .llseek = seq_lseek,
  1299. .release = seq_release,
  1300. };
  1301. static void extfrag_show_print(struct seq_file *m,
  1302. pg_data_t *pgdat, struct zone *zone)
  1303. {
  1304. unsigned int order;
  1305. int index;
  1306. /* Alloc on stack as interrupts are disabled for zone walk */
  1307. struct contig_page_info info;
  1308. seq_printf(m, "Node %d, zone %8s ",
  1309. pgdat->node_id,
  1310. zone->name);
  1311. for (order = 0; order < MAX_ORDER; ++order) {
  1312. fill_contig_page_info(zone, order, &info);
  1313. index = __fragmentation_index(order, &info);
  1314. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1315. }
  1316. seq_putc(m, '\n');
  1317. }
  1318. /*
  1319. * Display fragmentation index for orders that allocations would fail for
  1320. */
  1321. static int extfrag_show(struct seq_file *m, void *arg)
  1322. {
  1323. pg_data_t *pgdat = (pg_data_t *)arg;
  1324. walk_zones_in_node(m, pgdat, extfrag_show_print);
  1325. return 0;
  1326. }
  1327. static const struct seq_operations extfrag_op = {
  1328. .start = frag_start,
  1329. .next = frag_next,
  1330. .stop = frag_stop,
  1331. .show = extfrag_show,
  1332. };
  1333. static int extfrag_open(struct inode *inode, struct file *file)
  1334. {
  1335. return seq_open(file, &extfrag_op);
  1336. }
  1337. static const struct file_operations extfrag_file_ops = {
  1338. .open = extfrag_open,
  1339. .read = seq_read,
  1340. .llseek = seq_lseek,
  1341. .release = seq_release,
  1342. };
  1343. static int __init extfrag_debug_init(void)
  1344. {
  1345. struct dentry *extfrag_debug_root;
  1346. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1347. if (!extfrag_debug_root)
  1348. return -ENOMEM;
  1349. if (!debugfs_create_file("unusable_index", 0444,
  1350. extfrag_debug_root, NULL, &unusable_file_ops))
  1351. goto fail;
  1352. if (!debugfs_create_file("extfrag_index", 0444,
  1353. extfrag_debug_root, NULL, &extfrag_file_ops))
  1354. goto fail;
  1355. return 0;
  1356. fail:
  1357. debugfs_remove_recursive(extfrag_debug_root);
  1358. return -ENOMEM;
  1359. }
  1360. module_init(extfrag_debug_init);
  1361. #endif