vmstat.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. #ifdef CONFIG_VM_EVENT_COUNTERS
  21. DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
  22. EXPORT_PER_CPU_SYMBOL(vm_event_states);
  23. static void sum_vm_events(unsigned long *ret, const struct cpumask *cpumask)
  24. {
  25. int cpu;
  26. int i;
  27. memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
  28. for_each_cpu(cpu, cpumask) {
  29. struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
  30. for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
  31. ret[i] += this->event[i];
  32. }
  33. }
  34. /*
  35. * Accumulate the vm event counters across all CPUs.
  36. * The result is unavoidably approximate - it can change
  37. * during and after execution of this function.
  38. */
  39. void all_vm_events(unsigned long *ret)
  40. {
  41. get_online_cpus();
  42. sum_vm_events(ret, cpu_online_mask);
  43. put_online_cpus();
  44. }
  45. EXPORT_SYMBOL_GPL(all_vm_events);
  46. #ifdef CONFIG_HOTPLUG
  47. /*
  48. * Fold the foreign cpu events into our own.
  49. *
  50. * This is adding to the events on one processor
  51. * but keeps the global counts constant.
  52. */
  53. void vm_events_fold_cpu(int cpu)
  54. {
  55. struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
  56. int i;
  57. for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
  58. count_vm_events(i, fold_state->event[i]);
  59. fold_state->event[i] = 0;
  60. }
  61. }
  62. #endif /* CONFIG_HOTPLUG */
  63. #endif /* CONFIG_VM_EVENT_COUNTERS */
  64. /*
  65. * Manage combined zone based / global counters
  66. *
  67. * vm_stat contains the global counters
  68. */
  69. atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
  70. EXPORT_SYMBOL(vm_stat);
  71. #ifdef CONFIG_SMP
  72. static int calculate_threshold(struct zone *zone)
  73. {
  74. int threshold;
  75. int mem; /* memory in 128 MB units */
  76. /*
  77. * The threshold scales with the number of processors and the amount
  78. * of memory per zone. More memory means that we can defer updates for
  79. * longer, more processors could lead to more contention.
  80. * fls() is used to have a cheap way of logarithmic scaling.
  81. *
  82. * Some sample thresholds:
  83. *
  84. * Threshold Processors (fls) Zonesize fls(mem+1)
  85. * ------------------------------------------------------------------
  86. * 8 1 1 0.9-1 GB 4
  87. * 16 2 2 0.9-1 GB 4
  88. * 20 2 2 1-2 GB 5
  89. * 24 2 2 2-4 GB 6
  90. * 28 2 2 4-8 GB 7
  91. * 32 2 2 8-16 GB 8
  92. * 4 2 2 <128M 1
  93. * 30 4 3 2-4 GB 5
  94. * 48 4 3 8-16 GB 8
  95. * 32 8 4 1-2 GB 4
  96. * 32 8 4 0.9-1GB 4
  97. * 10 16 5 <128M 1
  98. * 40 16 5 900M 4
  99. * 70 64 7 2-4 GB 5
  100. * 84 64 7 4-8 GB 6
  101. * 108 512 9 4-8 GB 6
  102. * 125 1024 10 8-16 GB 8
  103. * 125 1024 10 16-32 GB 9
  104. */
  105. mem = zone->present_pages >> (27 - PAGE_SHIFT);
  106. threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
  107. /*
  108. * Maximum threshold is 125
  109. */
  110. threshold = min(125, threshold);
  111. return threshold;
  112. }
  113. /*
  114. * Refresh the thresholds for each zone.
  115. */
  116. static void refresh_zone_stat_thresholds(void)
  117. {
  118. struct zone *zone;
  119. int cpu;
  120. int threshold;
  121. for_each_populated_zone(zone) {
  122. threshold = calculate_threshold(zone);
  123. for_each_online_cpu(cpu)
  124. per_cpu_ptr(zone->pageset, cpu)->stat_threshold
  125. = threshold;
  126. }
  127. }
  128. /*
  129. * For use when we know that interrupts are disabled.
  130. */
  131. void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  132. int delta)
  133. {
  134. struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
  135. s8 *p = pcp->vm_stat_diff + item;
  136. long x;
  137. x = delta + *p;
  138. if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
  139. zone_page_state_add(x, zone, item);
  140. x = 0;
  141. }
  142. *p = x;
  143. }
  144. EXPORT_SYMBOL(__mod_zone_page_state);
  145. /*
  146. * For an unknown interrupt state
  147. */
  148. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  149. int delta)
  150. {
  151. unsigned long flags;
  152. local_irq_save(flags);
  153. __mod_zone_page_state(zone, item, delta);
  154. local_irq_restore(flags);
  155. }
  156. EXPORT_SYMBOL(mod_zone_page_state);
  157. /*
  158. * Optimized increment and decrement functions.
  159. *
  160. * These are only for a single page and therefore can take a struct page *
  161. * argument instead of struct zone *. This allows the inclusion of the code
  162. * generated for page_zone(page) into the optimized functions.
  163. *
  164. * No overflow check is necessary and therefore the differential can be
  165. * incremented or decremented in place which may allow the compilers to
  166. * generate better code.
  167. * The increment or decrement is known and therefore one boundary check can
  168. * be omitted.
  169. *
  170. * NOTE: These functions are very performance sensitive. Change only
  171. * with care.
  172. *
  173. * Some processors have inc/dec instructions that are atomic vs an interrupt.
  174. * However, the code must first determine the differential location in a zone
  175. * based on the processor number and then inc/dec the counter. There is no
  176. * guarantee without disabling preemption that the processor will not change
  177. * in between and therefore the atomicity vs. interrupt cannot be exploited
  178. * in a useful way here.
  179. */
  180. void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  181. {
  182. struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
  183. s8 *p = pcp->vm_stat_diff + item;
  184. (*p)++;
  185. if (unlikely(*p > pcp->stat_threshold)) {
  186. int overstep = pcp->stat_threshold / 2;
  187. zone_page_state_add(*p + overstep, zone, item);
  188. *p = -overstep;
  189. }
  190. }
  191. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  192. {
  193. __inc_zone_state(page_zone(page), item);
  194. }
  195. EXPORT_SYMBOL(__inc_zone_page_state);
  196. void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  197. {
  198. struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
  199. s8 *p = pcp->vm_stat_diff + item;
  200. (*p)--;
  201. if (unlikely(*p < - pcp->stat_threshold)) {
  202. int overstep = pcp->stat_threshold / 2;
  203. zone_page_state_add(*p - overstep, zone, item);
  204. *p = overstep;
  205. }
  206. }
  207. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  208. {
  209. __dec_zone_state(page_zone(page), item);
  210. }
  211. EXPORT_SYMBOL(__dec_zone_page_state);
  212. void inc_zone_state(struct zone *zone, enum zone_stat_item item)
  213. {
  214. unsigned long flags;
  215. local_irq_save(flags);
  216. __inc_zone_state(zone, item);
  217. local_irq_restore(flags);
  218. }
  219. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  220. {
  221. unsigned long flags;
  222. struct zone *zone;
  223. zone = page_zone(page);
  224. local_irq_save(flags);
  225. __inc_zone_state(zone, item);
  226. local_irq_restore(flags);
  227. }
  228. EXPORT_SYMBOL(inc_zone_page_state);
  229. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  230. {
  231. unsigned long flags;
  232. local_irq_save(flags);
  233. __dec_zone_page_state(page, item);
  234. local_irq_restore(flags);
  235. }
  236. EXPORT_SYMBOL(dec_zone_page_state);
  237. /*
  238. * Update the zone counters for one cpu.
  239. *
  240. * The cpu specified must be either the current cpu or a processor that
  241. * is not online. If it is the current cpu then the execution thread must
  242. * be pinned to the current cpu.
  243. *
  244. * Note that refresh_cpu_vm_stats strives to only access
  245. * node local memory. The per cpu pagesets on remote zones are placed
  246. * in the memory local to the processor using that pageset. So the
  247. * loop over all zones will access a series of cachelines local to
  248. * the processor.
  249. *
  250. * The call to zone_page_state_add updates the cachelines with the
  251. * statistics in the remote zone struct as well as the global cachelines
  252. * with the global counters. These could cause remote node cache line
  253. * bouncing and will have to be only done when necessary.
  254. */
  255. void refresh_cpu_vm_stats(int cpu)
  256. {
  257. struct zone *zone;
  258. int i;
  259. int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
  260. for_each_populated_zone(zone) {
  261. struct per_cpu_pageset *p;
  262. p = per_cpu_ptr(zone->pageset, cpu);
  263. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  264. if (p->vm_stat_diff[i]) {
  265. unsigned long flags;
  266. int v;
  267. local_irq_save(flags);
  268. v = p->vm_stat_diff[i];
  269. p->vm_stat_diff[i] = 0;
  270. local_irq_restore(flags);
  271. atomic_long_add(v, &zone->vm_stat[i]);
  272. global_diff[i] += v;
  273. #ifdef CONFIG_NUMA
  274. /* 3 seconds idle till flush */
  275. p->expire = 3;
  276. #endif
  277. }
  278. cond_resched();
  279. #ifdef CONFIG_NUMA
  280. /*
  281. * Deal with draining the remote pageset of this
  282. * processor
  283. *
  284. * Check if there are pages remaining in this pageset
  285. * if not then there is nothing to expire.
  286. */
  287. if (!p->expire || !p->pcp.count)
  288. continue;
  289. /*
  290. * We never drain zones local to this processor.
  291. */
  292. if (zone_to_nid(zone) == numa_node_id()) {
  293. p->expire = 0;
  294. continue;
  295. }
  296. p->expire--;
  297. if (p->expire)
  298. continue;
  299. if (p->pcp.count)
  300. drain_zone_pages(zone, &p->pcp);
  301. #endif
  302. }
  303. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  304. if (global_diff[i])
  305. atomic_long_add(global_diff[i], &vm_stat[i]);
  306. }
  307. #endif
  308. #ifdef CONFIG_NUMA
  309. /*
  310. * zonelist = the list of zones passed to the allocator
  311. * z = the zone from which the allocation occurred.
  312. *
  313. * Must be called with interrupts disabled.
  314. */
  315. void zone_statistics(struct zone *preferred_zone, struct zone *z)
  316. {
  317. if (z->zone_pgdat == preferred_zone->zone_pgdat) {
  318. __inc_zone_state(z, NUMA_HIT);
  319. } else {
  320. __inc_zone_state(z, NUMA_MISS);
  321. __inc_zone_state(preferred_zone, NUMA_FOREIGN);
  322. }
  323. if (z->node == numa_node_id())
  324. __inc_zone_state(z, NUMA_LOCAL);
  325. else
  326. __inc_zone_state(z, NUMA_OTHER);
  327. }
  328. #endif
  329. #ifdef CONFIG_COMPACTION
  330. struct contig_page_info {
  331. unsigned long free_pages;
  332. unsigned long free_blocks_total;
  333. unsigned long free_blocks_suitable;
  334. };
  335. /*
  336. * Calculate the number of free pages in a zone, how many contiguous
  337. * pages are free and how many are large enough to satisfy an allocation of
  338. * the target size. Note that this function makes no attempt to estimate
  339. * how many suitable free blocks there *might* be if MOVABLE pages were
  340. * migrated. Calculating that is possible, but expensive and can be
  341. * figured out from userspace
  342. */
  343. static void fill_contig_page_info(struct zone *zone,
  344. unsigned int suitable_order,
  345. struct contig_page_info *info)
  346. {
  347. unsigned int order;
  348. info->free_pages = 0;
  349. info->free_blocks_total = 0;
  350. info->free_blocks_suitable = 0;
  351. for (order = 0; order < MAX_ORDER; order++) {
  352. unsigned long blocks;
  353. /* Count number of free blocks */
  354. blocks = zone->free_area[order].nr_free;
  355. info->free_blocks_total += blocks;
  356. /* Count free base pages */
  357. info->free_pages += blocks << order;
  358. /* Count the suitable free blocks */
  359. if (order >= suitable_order)
  360. info->free_blocks_suitable += blocks <<
  361. (order - suitable_order);
  362. }
  363. }
  364. /*
  365. * A fragmentation index only makes sense if an allocation of a requested
  366. * size would fail. If that is true, the fragmentation index indicates
  367. * whether external fragmentation or a lack of memory was the problem.
  368. * The value can be used to determine if page reclaim or compaction
  369. * should be used
  370. */
  371. int fragmentation_index(unsigned int order, struct contig_page_info *info)
  372. {
  373. unsigned long requested = 1UL << order;
  374. if (!info->free_blocks_total)
  375. return 0;
  376. /* Fragmentation index only makes sense when a request would fail */
  377. if (info->free_blocks_suitable)
  378. return -1000;
  379. /*
  380. * Index is between 0 and 1 so return within 3 decimal places
  381. *
  382. * 0 => allocation would fail due to lack of memory
  383. * 1 => allocation would fail due to fragmentation
  384. */
  385. return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
  386. }
  387. #endif
  388. #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
  389. #include <linux/proc_fs.h>
  390. #include <linux/seq_file.h>
  391. static char * const migratetype_names[MIGRATE_TYPES] = {
  392. "Unmovable",
  393. "Reclaimable",
  394. "Movable",
  395. "Reserve",
  396. "Isolate",
  397. };
  398. static void *frag_start(struct seq_file *m, loff_t *pos)
  399. {
  400. pg_data_t *pgdat;
  401. loff_t node = *pos;
  402. for (pgdat = first_online_pgdat();
  403. pgdat && node;
  404. pgdat = next_online_pgdat(pgdat))
  405. --node;
  406. return pgdat;
  407. }
  408. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  409. {
  410. pg_data_t *pgdat = (pg_data_t *)arg;
  411. (*pos)++;
  412. return next_online_pgdat(pgdat);
  413. }
  414. static void frag_stop(struct seq_file *m, void *arg)
  415. {
  416. }
  417. /* Walk all the zones in a node and print using a callback */
  418. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  419. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  420. {
  421. struct zone *zone;
  422. struct zone *node_zones = pgdat->node_zones;
  423. unsigned long flags;
  424. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  425. if (!populated_zone(zone))
  426. continue;
  427. spin_lock_irqsave(&zone->lock, flags);
  428. print(m, pgdat, zone);
  429. spin_unlock_irqrestore(&zone->lock, flags);
  430. }
  431. }
  432. #endif
  433. #ifdef CONFIG_PROC_FS
  434. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  435. struct zone *zone)
  436. {
  437. int order;
  438. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  439. for (order = 0; order < MAX_ORDER; ++order)
  440. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  441. seq_putc(m, '\n');
  442. }
  443. /*
  444. * This walks the free areas for each zone.
  445. */
  446. static int frag_show(struct seq_file *m, void *arg)
  447. {
  448. pg_data_t *pgdat = (pg_data_t *)arg;
  449. walk_zones_in_node(m, pgdat, frag_show_print);
  450. return 0;
  451. }
  452. static void pagetypeinfo_showfree_print(struct seq_file *m,
  453. pg_data_t *pgdat, struct zone *zone)
  454. {
  455. int order, mtype;
  456. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  457. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  458. pgdat->node_id,
  459. zone->name,
  460. migratetype_names[mtype]);
  461. for (order = 0; order < MAX_ORDER; ++order) {
  462. unsigned long freecount = 0;
  463. struct free_area *area;
  464. struct list_head *curr;
  465. area = &(zone->free_area[order]);
  466. list_for_each(curr, &area->free_list[mtype])
  467. freecount++;
  468. seq_printf(m, "%6lu ", freecount);
  469. }
  470. seq_putc(m, '\n');
  471. }
  472. }
  473. /* Print out the free pages at each order for each migatetype */
  474. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  475. {
  476. int order;
  477. pg_data_t *pgdat = (pg_data_t *)arg;
  478. /* Print header */
  479. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  480. for (order = 0; order < MAX_ORDER; ++order)
  481. seq_printf(m, "%6d ", order);
  482. seq_putc(m, '\n');
  483. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  484. return 0;
  485. }
  486. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  487. pg_data_t *pgdat, struct zone *zone)
  488. {
  489. int mtype;
  490. unsigned long pfn;
  491. unsigned long start_pfn = zone->zone_start_pfn;
  492. unsigned long end_pfn = start_pfn + zone->spanned_pages;
  493. unsigned long count[MIGRATE_TYPES] = { 0, };
  494. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  495. struct page *page;
  496. if (!pfn_valid(pfn))
  497. continue;
  498. page = pfn_to_page(pfn);
  499. /* Watch for unexpected holes punched in the memmap */
  500. if (!memmap_valid_within(pfn, page, zone))
  501. continue;
  502. mtype = get_pageblock_migratetype(page);
  503. if (mtype < MIGRATE_TYPES)
  504. count[mtype]++;
  505. }
  506. /* Print counts */
  507. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  508. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  509. seq_printf(m, "%12lu ", count[mtype]);
  510. seq_putc(m, '\n');
  511. }
  512. /* Print out the free pages at each order for each migratetype */
  513. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  514. {
  515. int mtype;
  516. pg_data_t *pgdat = (pg_data_t *)arg;
  517. seq_printf(m, "\n%-23s", "Number of blocks type ");
  518. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  519. seq_printf(m, "%12s ", migratetype_names[mtype]);
  520. seq_putc(m, '\n');
  521. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  522. return 0;
  523. }
  524. /*
  525. * This prints out statistics in relation to grouping pages by mobility.
  526. * It is expensive to collect so do not constantly read the file.
  527. */
  528. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  529. {
  530. pg_data_t *pgdat = (pg_data_t *)arg;
  531. /* check memoryless node */
  532. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  533. return 0;
  534. seq_printf(m, "Page block order: %d\n", pageblock_order);
  535. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  536. seq_putc(m, '\n');
  537. pagetypeinfo_showfree(m, pgdat);
  538. pagetypeinfo_showblockcount(m, pgdat);
  539. return 0;
  540. }
  541. static const struct seq_operations fragmentation_op = {
  542. .start = frag_start,
  543. .next = frag_next,
  544. .stop = frag_stop,
  545. .show = frag_show,
  546. };
  547. static int fragmentation_open(struct inode *inode, struct file *file)
  548. {
  549. return seq_open(file, &fragmentation_op);
  550. }
  551. static const struct file_operations fragmentation_file_operations = {
  552. .open = fragmentation_open,
  553. .read = seq_read,
  554. .llseek = seq_lseek,
  555. .release = seq_release,
  556. };
  557. static const struct seq_operations pagetypeinfo_op = {
  558. .start = frag_start,
  559. .next = frag_next,
  560. .stop = frag_stop,
  561. .show = pagetypeinfo_show,
  562. };
  563. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  564. {
  565. return seq_open(file, &pagetypeinfo_op);
  566. }
  567. static const struct file_operations pagetypeinfo_file_ops = {
  568. .open = pagetypeinfo_open,
  569. .read = seq_read,
  570. .llseek = seq_lseek,
  571. .release = seq_release,
  572. };
  573. #ifdef CONFIG_ZONE_DMA
  574. #define TEXT_FOR_DMA(xx) xx "_dma",
  575. #else
  576. #define TEXT_FOR_DMA(xx)
  577. #endif
  578. #ifdef CONFIG_ZONE_DMA32
  579. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  580. #else
  581. #define TEXT_FOR_DMA32(xx)
  582. #endif
  583. #ifdef CONFIG_HIGHMEM
  584. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  585. #else
  586. #define TEXT_FOR_HIGHMEM(xx)
  587. #endif
  588. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  589. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  590. static const char * const vmstat_text[] = {
  591. /* Zoned VM counters */
  592. "nr_free_pages",
  593. "nr_inactive_anon",
  594. "nr_active_anon",
  595. "nr_inactive_file",
  596. "nr_active_file",
  597. "nr_unevictable",
  598. "nr_mlock",
  599. "nr_anon_pages",
  600. "nr_mapped",
  601. "nr_file_pages",
  602. "nr_dirty",
  603. "nr_writeback",
  604. "nr_slab_reclaimable",
  605. "nr_slab_unreclaimable",
  606. "nr_page_table_pages",
  607. "nr_kernel_stack",
  608. "nr_unstable",
  609. "nr_bounce",
  610. "nr_vmscan_write",
  611. "nr_writeback_temp",
  612. "nr_isolated_anon",
  613. "nr_isolated_file",
  614. "nr_shmem",
  615. #ifdef CONFIG_NUMA
  616. "numa_hit",
  617. "numa_miss",
  618. "numa_foreign",
  619. "numa_interleave",
  620. "numa_local",
  621. "numa_other",
  622. #endif
  623. #ifdef CONFIG_VM_EVENT_COUNTERS
  624. "pgpgin",
  625. "pgpgout",
  626. "pswpin",
  627. "pswpout",
  628. TEXTS_FOR_ZONES("pgalloc")
  629. "pgfree",
  630. "pgactivate",
  631. "pgdeactivate",
  632. "pgfault",
  633. "pgmajfault",
  634. TEXTS_FOR_ZONES("pgrefill")
  635. TEXTS_FOR_ZONES("pgsteal")
  636. TEXTS_FOR_ZONES("pgscan_kswapd")
  637. TEXTS_FOR_ZONES("pgscan_direct")
  638. #ifdef CONFIG_NUMA
  639. "zone_reclaim_failed",
  640. #endif
  641. "pginodesteal",
  642. "slabs_scanned",
  643. "kswapd_steal",
  644. "kswapd_inodesteal",
  645. "kswapd_low_wmark_hit_quickly",
  646. "kswapd_high_wmark_hit_quickly",
  647. "kswapd_skip_congestion_wait",
  648. "pageoutrun",
  649. "allocstall",
  650. "pgrotated",
  651. #ifdef CONFIG_HUGETLB_PAGE
  652. "htlb_buddy_alloc_success",
  653. "htlb_buddy_alloc_fail",
  654. #endif
  655. "unevictable_pgs_culled",
  656. "unevictable_pgs_scanned",
  657. "unevictable_pgs_rescued",
  658. "unevictable_pgs_mlocked",
  659. "unevictable_pgs_munlocked",
  660. "unevictable_pgs_cleared",
  661. "unevictable_pgs_stranded",
  662. "unevictable_pgs_mlockfreed",
  663. #endif
  664. };
  665. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  666. struct zone *zone)
  667. {
  668. int i;
  669. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  670. seq_printf(m,
  671. "\n pages free %lu"
  672. "\n min %lu"
  673. "\n low %lu"
  674. "\n high %lu"
  675. "\n scanned %lu"
  676. "\n spanned %lu"
  677. "\n present %lu",
  678. zone_page_state(zone, NR_FREE_PAGES),
  679. min_wmark_pages(zone),
  680. low_wmark_pages(zone),
  681. high_wmark_pages(zone),
  682. zone->pages_scanned,
  683. zone->spanned_pages,
  684. zone->present_pages);
  685. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  686. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  687. zone_page_state(zone, i));
  688. seq_printf(m,
  689. "\n protection: (%lu",
  690. zone->lowmem_reserve[0]);
  691. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  692. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  693. seq_printf(m,
  694. ")"
  695. "\n pagesets");
  696. for_each_online_cpu(i) {
  697. struct per_cpu_pageset *pageset;
  698. pageset = per_cpu_ptr(zone->pageset, i);
  699. seq_printf(m,
  700. "\n cpu: %i"
  701. "\n count: %i"
  702. "\n high: %i"
  703. "\n batch: %i",
  704. i,
  705. pageset->pcp.count,
  706. pageset->pcp.high,
  707. pageset->pcp.batch);
  708. #ifdef CONFIG_SMP
  709. seq_printf(m, "\n vm stats threshold: %d",
  710. pageset->stat_threshold);
  711. #endif
  712. }
  713. seq_printf(m,
  714. "\n all_unreclaimable: %u"
  715. "\n prev_priority: %i"
  716. "\n start_pfn: %lu"
  717. "\n inactive_ratio: %u",
  718. zone->all_unreclaimable,
  719. zone->prev_priority,
  720. zone->zone_start_pfn,
  721. zone->inactive_ratio);
  722. seq_putc(m, '\n');
  723. }
  724. /*
  725. * Output information about zones in @pgdat.
  726. */
  727. static int zoneinfo_show(struct seq_file *m, void *arg)
  728. {
  729. pg_data_t *pgdat = (pg_data_t *)arg;
  730. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  731. return 0;
  732. }
  733. static const struct seq_operations zoneinfo_op = {
  734. .start = frag_start, /* iterate over all zones. The same as in
  735. * fragmentation. */
  736. .next = frag_next,
  737. .stop = frag_stop,
  738. .show = zoneinfo_show,
  739. };
  740. static int zoneinfo_open(struct inode *inode, struct file *file)
  741. {
  742. return seq_open(file, &zoneinfo_op);
  743. }
  744. static const struct file_operations proc_zoneinfo_file_operations = {
  745. .open = zoneinfo_open,
  746. .read = seq_read,
  747. .llseek = seq_lseek,
  748. .release = seq_release,
  749. };
  750. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  751. {
  752. unsigned long *v;
  753. #ifdef CONFIG_VM_EVENT_COUNTERS
  754. unsigned long *e;
  755. #endif
  756. int i;
  757. if (*pos >= ARRAY_SIZE(vmstat_text))
  758. return NULL;
  759. #ifdef CONFIG_VM_EVENT_COUNTERS
  760. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
  761. + sizeof(struct vm_event_state), GFP_KERNEL);
  762. #else
  763. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
  764. GFP_KERNEL);
  765. #endif
  766. m->private = v;
  767. if (!v)
  768. return ERR_PTR(-ENOMEM);
  769. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  770. v[i] = global_page_state(i);
  771. #ifdef CONFIG_VM_EVENT_COUNTERS
  772. e = v + NR_VM_ZONE_STAT_ITEMS;
  773. all_vm_events(e);
  774. e[PGPGIN] /= 2; /* sectors -> kbytes */
  775. e[PGPGOUT] /= 2;
  776. #endif
  777. return v + *pos;
  778. }
  779. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  780. {
  781. (*pos)++;
  782. if (*pos >= ARRAY_SIZE(vmstat_text))
  783. return NULL;
  784. return (unsigned long *)m->private + *pos;
  785. }
  786. static int vmstat_show(struct seq_file *m, void *arg)
  787. {
  788. unsigned long *l = arg;
  789. unsigned long off = l - (unsigned long *)m->private;
  790. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  791. return 0;
  792. }
  793. static void vmstat_stop(struct seq_file *m, void *arg)
  794. {
  795. kfree(m->private);
  796. m->private = NULL;
  797. }
  798. static const struct seq_operations vmstat_op = {
  799. .start = vmstat_start,
  800. .next = vmstat_next,
  801. .stop = vmstat_stop,
  802. .show = vmstat_show,
  803. };
  804. static int vmstat_open(struct inode *inode, struct file *file)
  805. {
  806. return seq_open(file, &vmstat_op);
  807. }
  808. static const struct file_operations proc_vmstat_file_operations = {
  809. .open = vmstat_open,
  810. .read = seq_read,
  811. .llseek = seq_lseek,
  812. .release = seq_release,
  813. };
  814. #endif /* CONFIG_PROC_FS */
  815. #ifdef CONFIG_SMP
  816. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  817. int sysctl_stat_interval __read_mostly = HZ;
  818. static void vmstat_update(struct work_struct *w)
  819. {
  820. refresh_cpu_vm_stats(smp_processor_id());
  821. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  822. round_jiffies_relative(sysctl_stat_interval));
  823. }
  824. static void __cpuinit start_cpu_timer(int cpu)
  825. {
  826. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  827. INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
  828. schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
  829. }
  830. /*
  831. * Use the cpu notifier to insure that the thresholds are recalculated
  832. * when necessary.
  833. */
  834. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  835. unsigned long action,
  836. void *hcpu)
  837. {
  838. long cpu = (long)hcpu;
  839. switch (action) {
  840. case CPU_ONLINE:
  841. case CPU_ONLINE_FROZEN:
  842. start_cpu_timer(cpu);
  843. node_set_state(cpu_to_node(cpu), N_CPU);
  844. break;
  845. case CPU_DOWN_PREPARE:
  846. case CPU_DOWN_PREPARE_FROZEN:
  847. cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
  848. per_cpu(vmstat_work, cpu).work.func = NULL;
  849. break;
  850. case CPU_DOWN_FAILED:
  851. case CPU_DOWN_FAILED_FROZEN:
  852. start_cpu_timer(cpu);
  853. break;
  854. case CPU_DEAD:
  855. case CPU_DEAD_FROZEN:
  856. refresh_zone_stat_thresholds();
  857. break;
  858. default:
  859. break;
  860. }
  861. return NOTIFY_OK;
  862. }
  863. static struct notifier_block __cpuinitdata vmstat_notifier =
  864. { &vmstat_cpuup_callback, NULL, 0 };
  865. #endif
  866. static int __init setup_vmstat(void)
  867. {
  868. #ifdef CONFIG_SMP
  869. int cpu;
  870. refresh_zone_stat_thresholds();
  871. register_cpu_notifier(&vmstat_notifier);
  872. for_each_online_cpu(cpu)
  873. start_cpu_timer(cpu);
  874. #endif
  875. #ifdef CONFIG_PROC_FS
  876. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  877. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  878. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  879. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  880. #endif
  881. return 0;
  882. }
  883. module_init(setup_vmstat)
  884. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  885. #include <linux/debugfs.h>
  886. static struct dentry *extfrag_debug_root;
  887. /*
  888. * Return an index indicating how much of the available free memory is
  889. * unusable for an allocation of the requested size.
  890. */
  891. static int unusable_free_index(unsigned int order,
  892. struct contig_page_info *info)
  893. {
  894. /* No free memory is interpreted as all free memory is unusable */
  895. if (info->free_pages == 0)
  896. return 1000;
  897. /*
  898. * Index should be a value between 0 and 1. Return a value to 3
  899. * decimal places.
  900. *
  901. * 0 => no fragmentation
  902. * 1 => high fragmentation
  903. */
  904. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  905. }
  906. static void unusable_show_print(struct seq_file *m,
  907. pg_data_t *pgdat, struct zone *zone)
  908. {
  909. unsigned int order;
  910. int index;
  911. struct contig_page_info info;
  912. seq_printf(m, "Node %d, zone %8s ",
  913. pgdat->node_id,
  914. zone->name);
  915. for (order = 0; order < MAX_ORDER; ++order) {
  916. fill_contig_page_info(zone, order, &info);
  917. index = unusable_free_index(order, &info);
  918. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  919. }
  920. seq_putc(m, '\n');
  921. }
  922. /*
  923. * Display unusable free space index
  924. *
  925. * The unusable free space index measures how much of the available free
  926. * memory cannot be used to satisfy an allocation of a given size and is a
  927. * value between 0 and 1. The higher the value, the more of free memory is
  928. * unusable and by implication, the worse the external fragmentation is. This
  929. * can be expressed as a percentage by multiplying by 100.
  930. */
  931. static int unusable_show(struct seq_file *m, void *arg)
  932. {
  933. pg_data_t *pgdat = (pg_data_t *)arg;
  934. /* check memoryless node */
  935. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  936. return 0;
  937. walk_zones_in_node(m, pgdat, unusable_show_print);
  938. return 0;
  939. }
  940. static const struct seq_operations unusable_op = {
  941. .start = frag_start,
  942. .next = frag_next,
  943. .stop = frag_stop,
  944. .show = unusable_show,
  945. };
  946. static int unusable_open(struct inode *inode, struct file *file)
  947. {
  948. return seq_open(file, &unusable_op);
  949. }
  950. static const struct file_operations unusable_file_ops = {
  951. .open = unusable_open,
  952. .read = seq_read,
  953. .llseek = seq_lseek,
  954. .release = seq_release,
  955. };
  956. static void extfrag_show_print(struct seq_file *m,
  957. pg_data_t *pgdat, struct zone *zone)
  958. {
  959. unsigned int order;
  960. int index;
  961. /* Alloc on stack as interrupts are disabled for zone walk */
  962. struct contig_page_info info;
  963. seq_printf(m, "Node %d, zone %8s ",
  964. pgdat->node_id,
  965. zone->name);
  966. for (order = 0; order < MAX_ORDER; ++order) {
  967. fill_contig_page_info(zone, order, &info);
  968. index = fragmentation_index(order, &info);
  969. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  970. }
  971. seq_putc(m, '\n');
  972. }
  973. /*
  974. * Display fragmentation index for orders that allocations would fail for
  975. */
  976. static int extfrag_show(struct seq_file *m, void *arg)
  977. {
  978. pg_data_t *pgdat = (pg_data_t *)arg;
  979. walk_zones_in_node(m, pgdat, extfrag_show_print);
  980. return 0;
  981. }
  982. static const struct seq_operations extfrag_op = {
  983. .start = frag_start,
  984. .next = frag_next,
  985. .stop = frag_stop,
  986. .show = extfrag_show,
  987. };
  988. static int extfrag_open(struct inode *inode, struct file *file)
  989. {
  990. return seq_open(file, &extfrag_op);
  991. }
  992. static const struct file_operations extfrag_file_ops = {
  993. .open = extfrag_open,
  994. .read = seq_read,
  995. .llseek = seq_lseek,
  996. .release = seq_release,
  997. };
  998. static int __init extfrag_debug_init(void)
  999. {
  1000. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1001. if (!extfrag_debug_root)
  1002. return -ENOMEM;
  1003. if (!debugfs_create_file("unusable_index", 0444,
  1004. extfrag_debug_root, NULL, &unusable_file_ops))
  1005. return -ENOMEM;
  1006. if (!debugfs_create_file("extfrag_index", 0444,
  1007. extfrag_debug_root, NULL, &extfrag_file_ops))
  1008. return -ENOMEM;
  1009. return 0;
  1010. }
  1011. module_init(extfrag_debug_init);
  1012. #endif