vmstat.c 35 KB

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