vmstat.c 41 KB

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