vmstat.c 29 KB

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