vmstat.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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)
  24. {
  25. int cpu;
  26. int i;
  27. memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
  28. for_each_online_cpu(cpu) {
  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);
  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. static 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. /* Same as __fragmentation index but allocs contig_page_info on stack */
  388. int fragmentation_index(struct zone *zone, unsigned int order)
  389. {
  390. struct contig_page_info info;
  391. fill_contig_page_info(zone, order, &info);
  392. return __fragmentation_index(order, &info);
  393. }
  394. #endif
  395. #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
  396. #include <linux/proc_fs.h>
  397. #include <linux/seq_file.h>
  398. static char * const migratetype_names[MIGRATE_TYPES] = {
  399. "Unmovable",
  400. "Reclaimable",
  401. "Movable",
  402. "Reserve",
  403. "Isolate",
  404. };
  405. static void *frag_start(struct seq_file *m, loff_t *pos)
  406. {
  407. pg_data_t *pgdat;
  408. loff_t node = *pos;
  409. for (pgdat = first_online_pgdat();
  410. pgdat && node;
  411. pgdat = next_online_pgdat(pgdat))
  412. --node;
  413. return pgdat;
  414. }
  415. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  416. {
  417. pg_data_t *pgdat = (pg_data_t *)arg;
  418. (*pos)++;
  419. return next_online_pgdat(pgdat);
  420. }
  421. static void frag_stop(struct seq_file *m, void *arg)
  422. {
  423. }
  424. /* Walk all the zones in a node and print using a callback */
  425. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  426. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  427. {
  428. struct zone *zone;
  429. struct zone *node_zones = pgdat->node_zones;
  430. unsigned long flags;
  431. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  432. if (!populated_zone(zone))
  433. continue;
  434. spin_lock_irqsave(&zone->lock, flags);
  435. print(m, pgdat, zone);
  436. spin_unlock_irqrestore(&zone->lock, flags);
  437. }
  438. }
  439. #endif
  440. #ifdef CONFIG_PROC_FS
  441. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  442. struct zone *zone)
  443. {
  444. int order;
  445. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  446. for (order = 0; order < MAX_ORDER; ++order)
  447. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  448. seq_putc(m, '\n');
  449. }
  450. /*
  451. * This walks the free areas for each zone.
  452. */
  453. static int frag_show(struct seq_file *m, void *arg)
  454. {
  455. pg_data_t *pgdat = (pg_data_t *)arg;
  456. walk_zones_in_node(m, pgdat, frag_show_print);
  457. return 0;
  458. }
  459. static void pagetypeinfo_showfree_print(struct seq_file *m,
  460. pg_data_t *pgdat, struct zone *zone)
  461. {
  462. int order, mtype;
  463. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  464. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  465. pgdat->node_id,
  466. zone->name,
  467. migratetype_names[mtype]);
  468. for (order = 0; order < MAX_ORDER; ++order) {
  469. unsigned long freecount = 0;
  470. struct free_area *area;
  471. struct list_head *curr;
  472. area = &(zone->free_area[order]);
  473. list_for_each(curr, &area->free_list[mtype])
  474. freecount++;
  475. seq_printf(m, "%6lu ", freecount);
  476. }
  477. seq_putc(m, '\n');
  478. }
  479. }
  480. /* Print out the free pages at each order for each migatetype */
  481. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  482. {
  483. int order;
  484. pg_data_t *pgdat = (pg_data_t *)arg;
  485. /* Print header */
  486. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  487. for (order = 0; order < MAX_ORDER; ++order)
  488. seq_printf(m, "%6d ", order);
  489. seq_putc(m, '\n');
  490. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  491. return 0;
  492. }
  493. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  494. pg_data_t *pgdat, struct zone *zone)
  495. {
  496. int mtype;
  497. unsigned long pfn;
  498. unsigned long start_pfn = zone->zone_start_pfn;
  499. unsigned long end_pfn = start_pfn + zone->spanned_pages;
  500. unsigned long count[MIGRATE_TYPES] = { 0, };
  501. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  502. struct page *page;
  503. if (!pfn_valid(pfn))
  504. continue;
  505. page = pfn_to_page(pfn);
  506. /* Watch for unexpected holes punched in the memmap */
  507. if (!memmap_valid_within(pfn, page, zone))
  508. continue;
  509. mtype = get_pageblock_migratetype(page);
  510. if (mtype < MIGRATE_TYPES)
  511. count[mtype]++;
  512. }
  513. /* Print counts */
  514. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  515. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  516. seq_printf(m, "%12lu ", count[mtype]);
  517. seq_putc(m, '\n');
  518. }
  519. /* Print out the free pages at each order for each migratetype */
  520. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  521. {
  522. int mtype;
  523. pg_data_t *pgdat = (pg_data_t *)arg;
  524. seq_printf(m, "\n%-23s", "Number of blocks type ");
  525. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  526. seq_printf(m, "%12s ", migratetype_names[mtype]);
  527. seq_putc(m, '\n');
  528. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  529. return 0;
  530. }
  531. /*
  532. * This prints out statistics in relation to grouping pages by mobility.
  533. * It is expensive to collect so do not constantly read the file.
  534. */
  535. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  536. {
  537. pg_data_t *pgdat = (pg_data_t *)arg;
  538. /* check memoryless node */
  539. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  540. return 0;
  541. seq_printf(m, "Page block order: %d\n", pageblock_order);
  542. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  543. seq_putc(m, '\n');
  544. pagetypeinfo_showfree(m, pgdat);
  545. pagetypeinfo_showblockcount(m, pgdat);
  546. return 0;
  547. }
  548. static const struct seq_operations fragmentation_op = {
  549. .start = frag_start,
  550. .next = frag_next,
  551. .stop = frag_stop,
  552. .show = frag_show,
  553. };
  554. static int fragmentation_open(struct inode *inode, struct file *file)
  555. {
  556. return seq_open(file, &fragmentation_op);
  557. }
  558. static const struct file_operations fragmentation_file_operations = {
  559. .open = fragmentation_open,
  560. .read = seq_read,
  561. .llseek = seq_lseek,
  562. .release = seq_release,
  563. };
  564. static const struct seq_operations pagetypeinfo_op = {
  565. .start = frag_start,
  566. .next = frag_next,
  567. .stop = frag_stop,
  568. .show = pagetypeinfo_show,
  569. };
  570. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  571. {
  572. return seq_open(file, &pagetypeinfo_op);
  573. }
  574. static const struct file_operations pagetypeinfo_file_ops = {
  575. .open = pagetypeinfo_open,
  576. .read = seq_read,
  577. .llseek = seq_lseek,
  578. .release = seq_release,
  579. };
  580. #ifdef CONFIG_ZONE_DMA
  581. #define TEXT_FOR_DMA(xx) xx "_dma",
  582. #else
  583. #define TEXT_FOR_DMA(xx)
  584. #endif
  585. #ifdef CONFIG_ZONE_DMA32
  586. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  587. #else
  588. #define TEXT_FOR_DMA32(xx)
  589. #endif
  590. #ifdef CONFIG_HIGHMEM
  591. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  592. #else
  593. #define TEXT_FOR_HIGHMEM(xx)
  594. #endif
  595. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  596. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  597. static const char * const vmstat_text[] = {
  598. /* Zoned VM counters */
  599. "nr_free_pages",
  600. "nr_inactive_anon",
  601. "nr_active_anon",
  602. "nr_inactive_file",
  603. "nr_active_file",
  604. "nr_unevictable",
  605. "nr_mlock",
  606. "nr_anon_pages",
  607. "nr_mapped",
  608. "nr_file_pages",
  609. "nr_dirty",
  610. "nr_writeback",
  611. "nr_slab_reclaimable",
  612. "nr_slab_unreclaimable",
  613. "nr_page_table_pages",
  614. "nr_kernel_stack",
  615. "nr_unstable",
  616. "nr_bounce",
  617. "nr_vmscan_write",
  618. "nr_writeback_temp",
  619. "nr_isolated_anon",
  620. "nr_isolated_file",
  621. "nr_shmem",
  622. #ifdef CONFIG_NUMA
  623. "numa_hit",
  624. "numa_miss",
  625. "numa_foreign",
  626. "numa_interleave",
  627. "numa_local",
  628. "numa_other",
  629. #endif
  630. #ifdef CONFIG_VM_EVENT_COUNTERS
  631. "pgpgin",
  632. "pgpgout",
  633. "pswpin",
  634. "pswpout",
  635. TEXTS_FOR_ZONES("pgalloc")
  636. "pgfree",
  637. "pgactivate",
  638. "pgdeactivate",
  639. "pgfault",
  640. "pgmajfault",
  641. TEXTS_FOR_ZONES("pgrefill")
  642. TEXTS_FOR_ZONES("pgsteal")
  643. TEXTS_FOR_ZONES("pgscan_kswapd")
  644. TEXTS_FOR_ZONES("pgscan_direct")
  645. #ifdef CONFIG_NUMA
  646. "zone_reclaim_failed",
  647. #endif
  648. "pginodesteal",
  649. "slabs_scanned",
  650. "kswapd_steal",
  651. "kswapd_inodesteal",
  652. "kswapd_low_wmark_hit_quickly",
  653. "kswapd_high_wmark_hit_quickly",
  654. "kswapd_skip_congestion_wait",
  655. "pageoutrun",
  656. "allocstall",
  657. "pgrotated",
  658. #ifdef CONFIG_COMPACTION
  659. "compact_blocks_moved",
  660. "compact_pages_moved",
  661. "compact_pagemigrate_failed",
  662. "compact_stall",
  663. "compact_fail",
  664. "compact_success",
  665. #endif
  666. #ifdef CONFIG_HUGETLB_PAGE
  667. "htlb_buddy_alloc_success",
  668. "htlb_buddy_alloc_fail",
  669. #endif
  670. "unevictable_pgs_culled",
  671. "unevictable_pgs_scanned",
  672. "unevictable_pgs_rescued",
  673. "unevictable_pgs_mlocked",
  674. "unevictable_pgs_munlocked",
  675. "unevictable_pgs_cleared",
  676. "unevictable_pgs_stranded",
  677. "unevictable_pgs_mlockfreed",
  678. #endif
  679. };
  680. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  681. struct zone *zone)
  682. {
  683. int i;
  684. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  685. seq_printf(m,
  686. "\n pages free %lu"
  687. "\n min %lu"
  688. "\n low %lu"
  689. "\n high %lu"
  690. "\n scanned %lu"
  691. "\n spanned %lu"
  692. "\n present %lu",
  693. zone_page_state(zone, NR_FREE_PAGES),
  694. min_wmark_pages(zone),
  695. low_wmark_pages(zone),
  696. high_wmark_pages(zone),
  697. zone->pages_scanned,
  698. zone->spanned_pages,
  699. zone->present_pages);
  700. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  701. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  702. zone_page_state(zone, i));
  703. seq_printf(m,
  704. "\n protection: (%lu",
  705. zone->lowmem_reserve[0]);
  706. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  707. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  708. seq_printf(m,
  709. ")"
  710. "\n pagesets");
  711. for_each_online_cpu(i) {
  712. struct per_cpu_pageset *pageset;
  713. pageset = per_cpu_ptr(zone->pageset, i);
  714. seq_printf(m,
  715. "\n cpu: %i"
  716. "\n count: %i"
  717. "\n high: %i"
  718. "\n batch: %i",
  719. i,
  720. pageset->pcp.count,
  721. pageset->pcp.high,
  722. pageset->pcp.batch);
  723. #ifdef CONFIG_SMP
  724. seq_printf(m, "\n vm stats threshold: %d",
  725. pageset->stat_threshold);
  726. #endif
  727. }
  728. seq_printf(m,
  729. "\n all_unreclaimable: %u"
  730. "\n start_pfn: %lu"
  731. "\n inactive_ratio: %u",
  732. zone->all_unreclaimable,
  733. zone->zone_start_pfn,
  734. zone->inactive_ratio);
  735. seq_putc(m, '\n');
  736. }
  737. /*
  738. * Output information about zones in @pgdat.
  739. */
  740. static int zoneinfo_show(struct seq_file *m, void *arg)
  741. {
  742. pg_data_t *pgdat = (pg_data_t *)arg;
  743. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  744. return 0;
  745. }
  746. static const struct seq_operations zoneinfo_op = {
  747. .start = frag_start, /* iterate over all zones. The same as in
  748. * fragmentation. */
  749. .next = frag_next,
  750. .stop = frag_stop,
  751. .show = zoneinfo_show,
  752. };
  753. static int zoneinfo_open(struct inode *inode, struct file *file)
  754. {
  755. return seq_open(file, &zoneinfo_op);
  756. }
  757. static const struct file_operations proc_zoneinfo_file_operations = {
  758. .open = zoneinfo_open,
  759. .read = seq_read,
  760. .llseek = seq_lseek,
  761. .release = seq_release,
  762. };
  763. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  764. {
  765. unsigned long *v;
  766. #ifdef CONFIG_VM_EVENT_COUNTERS
  767. unsigned long *e;
  768. #endif
  769. int i;
  770. if (*pos >= ARRAY_SIZE(vmstat_text))
  771. return NULL;
  772. #ifdef CONFIG_VM_EVENT_COUNTERS
  773. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
  774. + sizeof(struct vm_event_state), GFP_KERNEL);
  775. #else
  776. v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
  777. GFP_KERNEL);
  778. #endif
  779. m->private = v;
  780. if (!v)
  781. return ERR_PTR(-ENOMEM);
  782. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  783. v[i] = global_page_state(i);
  784. #ifdef CONFIG_VM_EVENT_COUNTERS
  785. e = v + NR_VM_ZONE_STAT_ITEMS;
  786. all_vm_events(e);
  787. e[PGPGIN] /= 2; /* sectors -> kbytes */
  788. e[PGPGOUT] /= 2;
  789. #endif
  790. return v + *pos;
  791. }
  792. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  793. {
  794. (*pos)++;
  795. if (*pos >= ARRAY_SIZE(vmstat_text))
  796. return NULL;
  797. return (unsigned long *)m->private + *pos;
  798. }
  799. static int vmstat_show(struct seq_file *m, void *arg)
  800. {
  801. unsigned long *l = arg;
  802. unsigned long off = l - (unsigned long *)m->private;
  803. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  804. return 0;
  805. }
  806. static void vmstat_stop(struct seq_file *m, void *arg)
  807. {
  808. kfree(m->private);
  809. m->private = NULL;
  810. }
  811. static const struct seq_operations vmstat_op = {
  812. .start = vmstat_start,
  813. .next = vmstat_next,
  814. .stop = vmstat_stop,
  815. .show = vmstat_show,
  816. };
  817. static int vmstat_open(struct inode *inode, struct file *file)
  818. {
  819. return seq_open(file, &vmstat_op);
  820. }
  821. static const struct file_operations proc_vmstat_file_operations = {
  822. .open = vmstat_open,
  823. .read = seq_read,
  824. .llseek = seq_lseek,
  825. .release = seq_release,
  826. };
  827. #endif /* CONFIG_PROC_FS */
  828. #ifdef CONFIG_SMP
  829. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  830. int sysctl_stat_interval __read_mostly = HZ;
  831. static void vmstat_update(struct work_struct *w)
  832. {
  833. refresh_cpu_vm_stats(smp_processor_id());
  834. schedule_delayed_work(&__get_cpu_var(vmstat_work),
  835. round_jiffies_relative(sysctl_stat_interval));
  836. }
  837. static void __cpuinit start_cpu_timer(int cpu)
  838. {
  839. struct delayed_work *work = &per_cpu(vmstat_work, cpu);
  840. INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
  841. schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
  842. }
  843. /*
  844. * Use the cpu notifier to insure that the thresholds are recalculated
  845. * when necessary.
  846. */
  847. static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
  848. unsigned long action,
  849. void *hcpu)
  850. {
  851. long cpu = (long)hcpu;
  852. switch (action) {
  853. case CPU_ONLINE:
  854. case CPU_ONLINE_FROZEN:
  855. start_cpu_timer(cpu);
  856. node_set_state(cpu_to_node(cpu), N_CPU);
  857. break;
  858. case CPU_DOWN_PREPARE:
  859. case CPU_DOWN_PREPARE_FROZEN:
  860. cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
  861. per_cpu(vmstat_work, cpu).work.func = NULL;
  862. break;
  863. case CPU_DOWN_FAILED:
  864. case CPU_DOWN_FAILED_FROZEN:
  865. start_cpu_timer(cpu);
  866. break;
  867. case CPU_DEAD:
  868. case CPU_DEAD_FROZEN:
  869. refresh_zone_stat_thresholds();
  870. break;
  871. default:
  872. break;
  873. }
  874. return NOTIFY_OK;
  875. }
  876. static struct notifier_block __cpuinitdata vmstat_notifier =
  877. { &vmstat_cpuup_callback, NULL, 0 };
  878. #endif
  879. static int __init setup_vmstat(void)
  880. {
  881. #ifdef CONFIG_SMP
  882. int cpu;
  883. refresh_zone_stat_thresholds();
  884. register_cpu_notifier(&vmstat_notifier);
  885. for_each_online_cpu(cpu)
  886. start_cpu_timer(cpu);
  887. #endif
  888. #ifdef CONFIG_PROC_FS
  889. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  890. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  891. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  892. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  893. #endif
  894. return 0;
  895. }
  896. module_init(setup_vmstat)
  897. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  898. #include <linux/debugfs.h>
  899. static struct dentry *extfrag_debug_root;
  900. /*
  901. * Return an index indicating how much of the available free memory is
  902. * unusable for an allocation of the requested size.
  903. */
  904. static int unusable_free_index(unsigned int order,
  905. struct contig_page_info *info)
  906. {
  907. /* No free memory is interpreted as all free memory is unusable */
  908. if (info->free_pages == 0)
  909. return 1000;
  910. /*
  911. * Index should be a value between 0 and 1. Return a value to 3
  912. * decimal places.
  913. *
  914. * 0 => no fragmentation
  915. * 1 => high fragmentation
  916. */
  917. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  918. }
  919. static void unusable_show_print(struct seq_file *m,
  920. pg_data_t *pgdat, struct zone *zone)
  921. {
  922. unsigned int order;
  923. int index;
  924. struct contig_page_info info;
  925. seq_printf(m, "Node %d, zone %8s ",
  926. pgdat->node_id,
  927. zone->name);
  928. for (order = 0; order < MAX_ORDER; ++order) {
  929. fill_contig_page_info(zone, order, &info);
  930. index = unusable_free_index(order, &info);
  931. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  932. }
  933. seq_putc(m, '\n');
  934. }
  935. /*
  936. * Display unusable free space index
  937. *
  938. * The unusable free space index measures how much of the available free
  939. * memory cannot be used to satisfy an allocation of a given size and is a
  940. * value between 0 and 1. The higher the value, the more of free memory is
  941. * unusable and by implication, the worse the external fragmentation is. This
  942. * can be expressed as a percentage by multiplying by 100.
  943. */
  944. static int unusable_show(struct seq_file *m, void *arg)
  945. {
  946. pg_data_t *pgdat = (pg_data_t *)arg;
  947. /* check memoryless node */
  948. if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
  949. return 0;
  950. walk_zones_in_node(m, pgdat, unusable_show_print);
  951. return 0;
  952. }
  953. static const struct seq_operations unusable_op = {
  954. .start = frag_start,
  955. .next = frag_next,
  956. .stop = frag_stop,
  957. .show = unusable_show,
  958. };
  959. static int unusable_open(struct inode *inode, struct file *file)
  960. {
  961. return seq_open(file, &unusable_op);
  962. }
  963. static const struct file_operations unusable_file_ops = {
  964. .open = unusable_open,
  965. .read = seq_read,
  966. .llseek = seq_lseek,
  967. .release = seq_release,
  968. };
  969. static void extfrag_show_print(struct seq_file *m,
  970. pg_data_t *pgdat, struct zone *zone)
  971. {
  972. unsigned int order;
  973. int index;
  974. /* Alloc on stack as interrupts are disabled for zone walk */
  975. struct contig_page_info info;
  976. seq_printf(m, "Node %d, zone %8s ",
  977. pgdat->node_id,
  978. zone->name);
  979. for (order = 0; order < MAX_ORDER; ++order) {
  980. fill_contig_page_info(zone, order, &info);
  981. index = __fragmentation_index(order, &info);
  982. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  983. }
  984. seq_putc(m, '\n');
  985. }
  986. /*
  987. * Display fragmentation index for orders that allocations would fail for
  988. */
  989. static int extfrag_show(struct seq_file *m, void *arg)
  990. {
  991. pg_data_t *pgdat = (pg_data_t *)arg;
  992. walk_zones_in_node(m, pgdat, extfrag_show_print);
  993. return 0;
  994. }
  995. static const struct seq_operations extfrag_op = {
  996. .start = frag_start,
  997. .next = frag_next,
  998. .stop = frag_stop,
  999. .show = extfrag_show,
  1000. };
  1001. static int extfrag_open(struct inode *inode, struct file *file)
  1002. {
  1003. return seq_open(file, &extfrag_op);
  1004. }
  1005. static const struct file_operations extfrag_file_ops = {
  1006. .open = extfrag_open,
  1007. .read = seq_read,
  1008. .llseek = seq_lseek,
  1009. .release = seq_release,
  1010. };
  1011. static int __init extfrag_debug_init(void)
  1012. {
  1013. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1014. if (!extfrag_debug_root)
  1015. return -ENOMEM;
  1016. if (!debugfs_create_file("unusable_index", 0444,
  1017. extfrag_debug_root, NULL, &unusable_file_ops))
  1018. return -ENOMEM;
  1019. if (!debugfs_create_file("extfrag_index", 0444,
  1020. extfrag_debug_root, NULL, &extfrag_file_ops))
  1021. return -ENOMEM;
  1022. return 0;
  1023. }
  1024. module_init(extfrag_debug_init);
  1025. #endif