blob: 1284f89fca082aa907973d16ceed3f09d316a780 [file] [log] [blame]
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001/*
2 * linux/mm/vmstat.c
3 *
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
Christoph Lameter2244b952006-06-30 01:55:33 -07006 *
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
Christoph Lameter7cc36bb2014-10-09 15:29:43 -070010 * Copyright (C) 2008-2014 Christoph Lameter
Christoph Lameterf6ac2352006-06-30 01:55:32 -070011 */
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +040012#include <linux/fs.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070013#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040014#include <linux/err.h>
Christoph Lameter2244b952006-06-30 01:55:33 -070015#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070017#include <linux/cpu.h>
Christoph Lameter7cc36bb2014-10-09 15:29:43 -070018#include <linux/cpumask.h>
Adrian Bunkc748e132008-07-23 21:27:03 -070019#include <linux/vmstat.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Mel Gormanf1a5ab12010-05-24 14:32:26 -070021#include <linux/math64.h>
Michael Rubin79da8262010-10-26 14:21:36 -070022#include <linux/writeback.h>
Namhyung Kim36deb0b2010-10-26 14:22:04 -070023#include <linux/compaction.h>
Lisa Du6e543d52013-09-11 14:22:36 -070024#include <linux/mm_inline.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -080025#include <linux/page_ext.h>
26#include <linux/page_owner.h>
Lisa Du6e543d52013-09-11 14:22:36 -070027
28#include "internal.h"
Christoph Lameterf6ac2352006-06-30 01:55:32 -070029
Christoph Lameterf8891e52006-06-30 01:55:45 -070030#ifdef CONFIG_VM_EVENT_COUNTERS
31DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
32EXPORT_PER_CPU_SYMBOL(vm_event_states);
33
Minchan Kim31f961a2010-08-09 17:18:59 -070034static void sum_vm_events(unsigned long *ret)
Christoph Lameterf8891e52006-06-30 01:55:45 -070035{
Christoph Lameter9eccf2a2008-02-04 22:29:22 -080036 int cpu;
Christoph Lameterf8891e52006-06-30 01:55:45 -070037 int i;
38
39 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
40
Minchan Kim31f961a2010-08-09 17:18:59 -070041 for_each_online_cpu(cpu) {
Christoph Lameterf8891e52006-06-30 01:55:45 -070042 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
43
Christoph Lameterf8891e52006-06-30 01:55:45 -070044 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
45 ret[i] += this->event[i];
46 }
47}
48
49/*
50 * Accumulate the vm event counters across all CPUs.
51 * The result is unavoidably approximate - it can change
52 * during and after execution of this function.
53*/
54void all_vm_events(unsigned long *ret)
55{
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070056 get_online_cpus();
Minchan Kim31f961a2010-08-09 17:18:59 -070057 sum_vm_events(ret);
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070058 put_online_cpus();
Christoph Lameterf8891e52006-06-30 01:55:45 -070059}
Heiko Carstens32dd66f2006-07-10 04:44:31 -070060EXPORT_SYMBOL_GPL(all_vm_events);
Christoph Lameterf8891e52006-06-30 01:55:45 -070061
Christoph Lameterf8891e52006-06-30 01:55:45 -070062/*
63 * Fold the foreign cpu events into our own.
64 *
65 * This is adding to the events on one processor
66 * but keeps the global counts constant.
67 */
68void vm_events_fold_cpu(int cpu)
69{
70 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
71 int i;
72
73 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
74 count_vm_events(i, fold_state->event[i]);
75 fold_state->event[i] = 0;
76 }
77}
Christoph Lameterf8891e52006-06-30 01:55:45 -070078
79#endif /* CONFIG_VM_EVENT_COUNTERS */
80
Christoph Lameter2244b952006-06-30 01:55:33 -070081/*
82 * Manage combined zone based / global counters
83 *
84 * vm_stat contains the global counters
85 */
Dimitri Sivanicha1cb2c62011-10-31 17:09:46 -070086atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
Christoph Lameter2244b952006-06-30 01:55:33 -070087EXPORT_SYMBOL(vm_stat);
88
89#ifdef CONFIG_SMP
90
Mel Gormanb44129b2011-01-13 15:45:43 -080091int calculate_pressure_threshold(struct zone *zone)
Mel Gorman88f5acf2011-01-13 15:45:41 -080092{
93 int threshold;
94 int watermark_distance;
95
96 /*
97 * As vmstats are not up to date, there is drift between the estimated
98 * and real values. For high thresholds and a high number of CPUs, it
99 * is possible for the min watermark to be breached while the estimated
100 * value looks fine. The pressure threshold is a reduced value such
101 * that even the maximum amount of drift will not accidentally breach
102 * the min watermark
103 */
104 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
105 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
106
107 /*
108 * Maximum threshold is 125
109 */
110 threshold = min(125, threshold);
111
112 return threshold;
113}
114
Mel Gormanb44129b2011-01-13 15:45:43 -0800115int calculate_normal_threshold(struct zone *zone)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700116{
117 int threshold;
118 int mem; /* memory in 128 MB units */
119
120 /*
121 * The threshold scales with the number of processors and the amount
122 * of memory per zone. More memory means that we can defer updates for
123 * longer, more processors could lead to more contention.
124 * fls() is used to have a cheap way of logarithmic scaling.
125 *
126 * Some sample thresholds:
127 *
128 * Threshold Processors (fls) Zonesize fls(mem+1)
129 * ------------------------------------------------------------------
130 * 8 1 1 0.9-1 GB 4
131 * 16 2 2 0.9-1 GB 4
132 * 20 2 2 1-2 GB 5
133 * 24 2 2 2-4 GB 6
134 * 28 2 2 4-8 GB 7
135 * 32 2 2 8-16 GB 8
136 * 4 2 2 <128M 1
137 * 30 4 3 2-4 GB 5
138 * 48 4 3 8-16 GB 8
139 * 32 8 4 1-2 GB 4
140 * 32 8 4 0.9-1GB 4
141 * 10 16 5 <128M 1
142 * 40 16 5 900M 4
143 * 70 64 7 2-4 GB 5
144 * 84 64 7 4-8 GB 6
145 * 108 512 9 4-8 GB 6
146 * 125 1024 10 8-16 GB 8
147 * 125 1024 10 16-32 GB 9
148 */
149
Jiang Liub40da042013-02-22 16:33:52 -0800150 mem = zone->managed_pages >> (27 - PAGE_SHIFT);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700151
152 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
153
154 /*
155 * Maximum threshold is 125
156 */
157 threshold = min(125, threshold);
158
159 return threshold;
160}
Christoph Lameter2244b952006-06-30 01:55:33 -0700161
162/*
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700163 * Refresh the thresholds for each zone.
Christoph Lameter2244b952006-06-30 01:55:33 -0700164 */
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -0700165void refresh_zone_stat_thresholds(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700166{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700167 struct zone *zone;
168 int cpu;
169 int threshold;
170
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700171 for_each_populated_zone(zone) {
Christoph Lameteraa454842010-09-09 16:38:17 -0700172 unsigned long max_drift, tolerate_drift;
173
Mel Gormanb44129b2011-01-13 15:45:43 -0800174 threshold = calculate_normal_threshold(zone);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700175
176 for_each_online_cpu(cpu)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900177 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
178 = threshold;
Christoph Lameteraa454842010-09-09 16:38:17 -0700179
180 /*
181 * Only set percpu_drift_mark if there is a danger that
182 * NR_FREE_PAGES reports the low watermark is ok when in fact
183 * the min watermark could be breached by an allocation
184 */
185 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
186 max_drift = num_online_cpus() * threshold;
187 if (max_drift > tolerate_drift)
188 zone->percpu_drift_mark = high_wmark_pages(zone) +
189 max_drift;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700190 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700191}
192
Mel Gormanb44129b2011-01-13 15:45:43 -0800193void set_pgdat_percpu_threshold(pg_data_t *pgdat,
194 int (*calculate_pressure)(struct zone *))
Mel Gorman88f5acf2011-01-13 15:45:41 -0800195{
196 struct zone *zone;
197 int cpu;
198 int threshold;
199 int i;
200
Mel Gorman88f5acf2011-01-13 15:45:41 -0800201 for (i = 0; i < pgdat->nr_zones; i++) {
202 zone = &pgdat->node_zones[i];
203 if (!zone->percpu_drift_mark)
204 continue;
205
Mel Gormanb44129b2011-01-13 15:45:43 -0800206 threshold = (*calculate_pressure)(zone);
Mel Gormanbb0b6df2014-08-06 16:07:18 -0700207 for_each_online_cpu(cpu)
Mel Gorman88f5acf2011-01-13 15:45:41 -0800208 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
209 = threshold;
210 }
Mel Gorman88f5acf2011-01-13 15:45:41 -0800211}
212
Christoph Lameter2244b952006-06-30 01:55:33 -0700213/*
Jianyu Zhanbea04b02014-06-04 16:09:51 -0700214 * For use when we know that interrupts are disabled,
215 * or when we know that preemption is disabled and that
216 * particular counter cannot be updated from interrupt context.
Christoph Lameter2244b952006-06-30 01:55:33 -0700217 */
218void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
219 int delta)
220{
Christoph Lameter12938a92010-12-06 11:16:20 -0600221 struct per_cpu_pageset __percpu *pcp = zone->pageset;
222 s8 __percpu *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700223 long x;
Christoph Lameter12938a92010-12-06 11:16:20 -0600224 long t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700225
Christoph Lameter12938a92010-12-06 11:16:20 -0600226 x = delta + __this_cpu_read(*p);
Christoph Lameter2244b952006-06-30 01:55:33 -0700227
Christoph Lameter12938a92010-12-06 11:16:20 -0600228 t = __this_cpu_read(pcp->stat_threshold);
229
230 if (unlikely(x > t || x < -t)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700231 zone_page_state_add(x, zone, item);
232 x = 0;
233 }
Christoph Lameter12938a92010-12-06 11:16:20 -0600234 __this_cpu_write(*p, x);
Christoph Lameter2244b952006-06-30 01:55:33 -0700235}
236EXPORT_SYMBOL(__mod_zone_page_state);
237
238/*
Christoph Lameter2244b952006-06-30 01:55:33 -0700239 * Optimized increment and decrement functions.
240 *
241 * These are only for a single page and therefore can take a struct page *
242 * argument instead of struct zone *. This allows the inclusion of the code
243 * generated for page_zone(page) into the optimized functions.
244 *
245 * No overflow check is necessary and therefore the differential can be
246 * incremented or decremented in place which may allow the compilers to
247 * generate better code.
Christoph Lameter2244b952006-06-30 01:55:33 -0700248 * The increment or decrement is known and therefore one boundary check can
249 * be omitted.
250 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700251 * NOTE: These functions are very performance sensitive. Change only
252 * with care.
253 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700254 * Some processors have inc/dec instructions that are atomic vs an interrupt.
255 * However, the code must first determine the differential location in a zone
256 * based on the processor number and then inc/dec the counter. There is no
257 * guarantee without disabling preemption that the processor will not change
258 * in between and therefore the atomicity vs. interrupt cannot be exploited
259 * in a useful way here.
260 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800261void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700262{
Christoph Lameter12938a92010-12-06 11:16:20 -0600263 struct per_cpu_pageset __percpu *pcp = zone->pageset;
264 s8 __percpu *p = pcp->vm_stat_diff + item;
265 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700266
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600267 v = __this_cpu_inc_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600268 t = __this_cpu_read(pcp->stat_threshold);
269 if (unlikely(v > t)) {
270 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700271
Christoph Lameter12938a92010-12-06 11:16:20 -0600272 zone_page_state_add(v + overstep, zone, item);
273 __this_cpu_write(*p, -overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700274 }
275}
Christoph Lameterca889e62006-06-30 01:55:44 -0700276
277void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
278{
279 __inc_zone_state(page_zone(page), item);
280}
Christoph Lameter2244b952006-06-30 01:55:33 -0700281EXPORT_SYMBOL(__inc_zone_page_state);
282
Christoph Lameterc8785382007-02-10 01:43:01 -0800283void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700284{
Christoph Lameter12938a92010-12-06 11:16:20 -0600285 struct per_cpu_pageset __percpu *pcp = zone->pageset;
286 s8 __percpu *p = pcp->vm_stat_diff + item;
287 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700288
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600289 v = __this_cpu_dec_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600290 t = __this_cpu_read(pcp->stat_threshold);
291 if (unlikely(v < - t)) {
292 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700293
Christoph Lameter12938a92010-12-06 11:16:20 -0600294 zone_page_state_add(v - overstep, zone, item);
295 __this_cpu_write(*p, overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700296 }
297}
Christoph Lameterc8785382007-02-10 01:43:01 -0800298
299void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
300{
301 __dec_zone_state(page_zone(page), item);
302}
Christoph Lameter2244b952006-06-30 01:55:33 -0700303EXPORT_SYMBOL(__dec_zone_page_state);
304
Heiko Carstens41561532012-01-12 17:17:30 -0800305#ifdef CONFIG_HAVE_CMPXCHG_LOCAL
Christoph Lameter7c839122010-12-14 10:28:46 -0600306/*
307 * If we have cmpxchg_local support then we do not need to incur the overhead
308 * that comes with local_irq_save/restore if we use this_cpu_cmpxchg.
309 *
310 * mod_state() modifies the zone counter state through atomic per cpu
311 * operations.
312 *
313 * Overstep mode specifies how overstep should handled:
314 * 0 No overstepping
315 * 1 Overstepping half of threshold
316 * -1 Overstepping minus half of threshold
317*/
318static inline void mod_state(struct zone *zone,
319 enum zone_stat_item item, int delta, int overstep_mode)
320{
321 struct per_cpu_pageset __percpu *pcp = zone->pageset;
322 s8 __percpu *p = pcp->vm_stat_diff + item;
323 long o, n, t, z;
324
325 do {
326 z = 0; /* overflow to zone counters */
327
328 /*
329 * The fetching of the stat_threshold is racy. We may apply
330 * a counter threshold to the wrong the cpu if we get
Christoph Lameterd3bc2362011-04-14 15:21:58 -0700331 * rescheduled while executing here. However, the next
332 * counter update will apply the threshold again and
333 * therefore bring the counter under the threshold again.
334 *
335 * Most of the time the thresholds are the same anyways
336 * for all cpus in a zone.
Christoph Lameter7c839122010-12-14 10:28:46 -0600337 */
338 t = this_cpu_read(pcp->stat_threshold);
339
340 o = this_cpu_read(*p);
341 n = delta + o;
342
343 if (n > t || n < -t) {
344 int os = overstep_mode * (t >> 1) ;
345
346 /* Overflow must be added to zone counters */
347 z = n + os;
348 n = -os;
349 }
350 } while (this_cpu_cmpxchg(*p, o, n) != o);
351
352 if (z)
353 zone_page_state_add(z, zone, item);
354}
355
356void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
357 int delta)
358{
359 mod_state(zone, item, delta, 0);
360}
361EXPORT_SYMBOL(mod_zone_page_state);
362
363void inc_zone_state(struct zone *zone, enum zone_stat_item item)
364{
365 mod_state(zone, item, 1, 1);
366}
367
368void inc_zone_page_state(struct page *page, enum zone_stat_item item)
369{
370 mod_state(page_zone(page), item, 1, 1);
371}
372EXPORT_SYMBOL(inc_zone_page_state);
373
374void dec_zone_page_state(struct page *page, enum zone_stat_item item)
375{
376 mod_state(page_zone(page), item, -1, -1);
377}
378EXPORT_SYMBOL(dec_zone_page_state);
379#else
380/*
381 * Use interrupt disable to serialize counter updates
382 */
383void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
384 int delta)
385{
386 unsigned long flags;
387
388 local_irq_save(flags);
389 __mod_zone_page_state(zone, item, delta);
390 local_irq_restore(flags);
391}
392EXPORT_SYMBOL(mod_zone_page_state);
393
Christoph Lameterca889e62006-06-30 01:55:44 -0700394void inc_zone_state(struct zone *zone, enum zone_stat_item item)
395{
396 unsigned long flags;
397
398 local_irq_save(flags);
399 __inc_zone_state(zone, item);
400 local_irq_restore(flags);
401}
402
Christoph Lameter2244b952006-06-30 01:55:33 -0700403void inc_zone_page_state(struct page *page, enum zone_stat_item item)
404{
405 unsigned long flags;
406 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700407
408 zone = page_zone(page);
409 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700410 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700411 local_irq_restore(flags);
412}
413EXPORT_SYMBOL(inc_zone_page_state);
414
415void dec_zone_page_state(struct page *page, enum zone_stat_item item)
416{
417 unsigned long flags;
Christoph Lameter2244b952006-06-30 01:55:33 -0700418
Christoph Lameter2244b952006-06-30 01:55:33 -0700419 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700420 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700421 local_irq_restore(flags);
422}
423EXPORT_SYMBOL(dec_zone_page_state);
Christoph Lameter7c839122010-12-14 10:28:46 -0600424#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700425
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700426
427/*
428 * Fold a differential into the global counters.
429 * Returns the number of counters updated.
430 */
431static int fold_diff(int *diff)
Christoph Lameter4edb0742013-09-11 14:21:31 -0700432{
433 int i;
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700434 int changes = 0;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700435
436 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700437 if (diff[i]) {
Christoph Lameter4edb0742013-09-11 14:21:31 -0700438 atomic_long_add(diff[i], &vm_stat[i]);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700439 changes++;
440 }
441 return changes;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700442}
443
Christoph Lameter2244b952006-06-30 01:55:33 -0700444/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700445 * Update the zone counters for the current cpu.
Christoph Lametera7f75e22008-02-04 22:29:16 -0800446 *
Christoph Lameter4037d452007-05-09 02:35:14 -0700447 * Note that refresh_cpu_vm_stats strives to only access
448 * node local memory. The per cpu pagesets on remote zones are placed
449 * in the memory local to the processor using that pageset. So the
450 * loop over all zones will access a series of cachelines local to
451 * the processor.
452 *
453 * The call to zone_page_state_add updates the cachelines with the
454 * statistics in the remote zone struct as well as the global cachelines
455 * with the global counters. These could cause remote node cache line
456 * bouncing and will have to be only done when necessary.
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700457 *
458 * The function returns the number of global counters updated.
Christoph Lameter2244b952006-06-30 01:55:33 -0700459 */
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700460static int refresh_cpu_vm_stats(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700461{
462 struct zone *zone;
463 int i;
Christoph Lametera7f75e22008-02-04 22:29:16 -0800464 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700465 int changes = 0;
Christoph Lameter2244b952006-06-30 01:55:33 -0700466
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700467 for_each_populated_zone(zone) {
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700468 struct per_cpu_pageset __percpu *p = zone->pageset;
Christoph Lameter2244b952006-06-30 01:55:33 -0700469
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700470 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
471 int v;
Christoph Lameter2244b952006-06-30 01:55:33 -0700472
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700473 v = this_cpu_xchg(p->vm_stat_diff[i], 0);
474 if (v) {
Christoph Lametera7f75e22008-02-04 22:29:16 -0800475
Christoph Lametera7f75e22008-02-04 22:29:16 -0800476 atomic_long_add(v, &zone->vm_stat[i]);
477 global_diff[i] += v;
Christoph Lameter4037d452007-05-09 02:35:14 -0700478#ifdef CONFIG_NUMA
479 /* 3 seconds idle till flush */
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700480 __this_cpu_write(p->expire, 3);
Christoph Lameter4037d452007-05-09 02:35:14 -0700481#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700482 }
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700483 }
Dimitri Sivanich468fd622008-04-28 02:13:37 -0700484 cond_resched();
Christoph Lameter4037d452007-05-09 02:35:14 -0700485#ifdef CONFIG_NUMA
486 /*
487 * Deal with draining the remote pageset of this
488 * processor
489 *
490 * Check if there are pages remaining in this pageset
491 * if not then there is nothing to expire.
492 */
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700493 if (!__this_cpu_read(p->expire) ||
494 !__this_cpu_read(p->pcp.count))
Christoph Lameter4037d452007-05-09 02:35:14 -0700495 continue;
496
497 /*
498 * We never drain zones local to this processor.
499 */
500 if (zone_to_nid(zone) == numa_node_id()) {
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700501 __this_cpu_write(p->expire, 0);
Christoph Lameter4037d452007-05-09 02:35:14 -0700502 continue;
503 }
504
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700505 if (__this_cpu_dec_return(p->expire))
Christoph Lameter4037d452007-05-09 02:35:14 -0700506 continue;
507
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700508 if (__this_cpu_read(p->pcp.count)) {
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700509 drain_zone_pages(zone, this_cpu_ptr(&p->pcp));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700510 changes++;
511 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700512#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700513 }
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700514 changes += fold_diff(global_diff);
515 return changes;
Christoph Lameter2244b952006-06-30 01:55:33 -0700516}
517
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700518/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700519 * Fold the data for an offline cpu into the global array.
520 * There cannot be any access by the offline cpu and therefore
521 * synchronization is simplified.
522 */
523void cpu_vm_stats_fold(int cpu)
524{
525 struct zone *zone;
526 int i;
527 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
528
529 for_each_populated_zone(zone) {
530 struct per_cpu_pageset *p;
531
532 p = per_cpu_ptr(zone->pageset, cpu);
533
534 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
535 if (p->vm_stat_diff[i]) {
536 int v;
537
538 v = p->vm_stat_diff[i];
539 p->vm_stat_diff[i] = 0;
540 atomic_long_add(v, &zone->vm_stat[i]);
541 global_diff[i] += v;
542 }
543 }
544
Christoph Lameter4edb0742013-09-11 14:21:31 -0700545 fold_diff(global_diff);
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700546}
547
548/*
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700549 * this is only called if !populated_zone(zone), which implies no other users of
550 * pset->vm_stat_diff[] exsist.
551 */
Minchan Kim5a883812012-10-08 16:33:39 -0700552void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
553{
554 int i;
555
556 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
557 if (pset->vm_stat_diff[i]) {
558 int v = pset->vm_stat_diff[i];
559 pset->vm_stat_diff[i] = 0;
560 atomic_long_add(v, &zone->vm_stat[i]);
561 atomic_long_add(v, &vm_stat[i]);
562 }
563}
Christoph Lameter2244b952006-06-30 01:55:33 -0700564#endif
565
Christoph Lameterca889e62006-06-30 01:55:44 -0700566#ifdef CONFIG_NUMA
567/*
568 * zonelist = the list of zones passed to the allocator
569 * z = the zone from which the allocation occurred.
570 *
571 * Must be called with interrupts disabled.
Andi Kleen78afd562011-03-22 16:33:12 -0700572 *
573 * When __GFP_OTHER_NODE is set assume the node of the preferred
574 * zone is the local node. This is useful for daemons who allocate
575 * memory on behalf of other processes.
Christoph Lameterca889e62006-06-30 01:55:44 -0700576 */
Andi Kleen78afd562011-03-22 16:33:12 -0700577void zone_statistics(struct zone *preferred_zone, struct zone *z, gfp_t flags)
Christoph Lameterca889e62006-06-30 01:55:44 -0700578{
Mel Gorman18ea7e72008-04-28 02:12:14 -0700579 if (z->zone_pgdat == preferred_zone->zone_pgdat) {
Christoph Lameterca889e62006-06-30 01:55:44 -0700580 __inc_zone_state(z, NUMA_HIT);
581 } else {
582 __inc_zone_state(z, NUMA_MISS);
Mel Gorman18ea7e72008-04-28 02:12:14 -0700583 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
Christoph Lameterca889e62006-06-30 01:55:44 -0700584 }
Andi Kleen78afd562011-03-22 16:33:12 -0700585 if (z->node == ((flags & __GFP_OTHER_NODE) ?
586 preferred_zone->node : numa_node_id()))
Christoph Lameterca889e62006-06-30 01:55:44 -0700587 __inc_zone_state(z, NUMA_LOCAL);
588 else
589 __inc_zone_state(z, NUMA_OTHER);
590}
591#endif
592
Mel Gormand7a57522010-05-24 14:32:25 -0700593#ifdef CONFIG_COMPACTION
Namhyung Kim36deb0b2010-10-26 14:22:04 -0700594
Mel Gormand7a57522010-05-24 14:32:25 -0700595struct contig_page_info {
596 unsigned long free_pages;
597 unsigned long free_blocks_total;
598 unsigned long free_blocks_suitable;
599};
600
601/*
602 * Calculate the number of free pages in a zone, how many contiguous
603 * pages are free and how many are large enough to satisfy an allocation of
604 * the target size. Note that this function makes no attempt to estimate
605 * how many suitable free blocks there *might* be if MOVABLE pages were
606 * migrated. Calculating that is possible, but expensive and can be
607 * figured out from userspace
608 */
609static void fill_contig_page_info(struct zone *zone,
610 unsigned int suitable_order,
611 struct contig_page_info *info)
612{
613 unsigned int order;
614
615 info->free_pages = 0;
616 info->free_blocks_total = 0;
617 info->free_blocks_suitable = 0;
618
619 for (order = 0; order < MAX_ORDER; order++) {
620 unsigned long blocks;
621
622 /* Count number of free blocks */
623 blocks = zone->free_area[order].nr_free;
624 info->free_blocks_total += blocks;
625
626 /* Count free base pages */
627 info->free_pages += blocks << order;
628
629 /* Count the suitable free blocks */
630 if (order >= suitable_order)
631 info->free_blocks_suitable += blocks <<
632 (order - suitable_order);
633 }
634}
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700635
636/*
637 * A fragmentation index only makes sense if an allocation of a requested
638 * size would fail. If that is true, the fragmentation index indicates
639 * whether external fragmentation or a lack of memory was the problem.
640 * The value can be used to determine if page reclaim or compaction
641 * should be used
642 */
Mel Gorman56de7262010-05-24 14:32:30 -0700643static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700644{
645 unsigned long requested = 1UL << order;
646
647 if (!info->free_blocks_total)
648 return 0;
649
650 /* Fragmentation index only makes sense when a request would fail */
651 if (info->free_blocks_suitable)
652 return -1000;
653
654 /*
655 * Index is between 0 and 1 so return within 3 decimal places
656 *
657 * 0 => allocation would fail due to lack of memory
658 * 1 => allocation would fail due to fragmentation
659 */
660 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
661}
Mel Gorman56de7262010-05-24 14:32:30 -0700662
663/* Same as __fragmentation index but allocs contig_page_info on stack */
664int fragmentation_index(struct zone *zone, unsigned int order)
665{
666 struct contig_page_info info;
667
668 fill_contig_page_info(zone, order, &info);
669 return __fragmentation_index(order, &info);
670}
Mel Gormand7a57522010-05-24 14:32:25 -0700671#endif
672
673#if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400674#include <linux/proc_fs.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700675#include <linux/seq_file.h>
676
Mel Gorman467c9962007-10-16 01:26:02 -0700677static char * const migratetype_names[MIGRATE_TYPES] = {
678 "Unmovable",
679 "Reclaimable",
680 "Movable",
681 "Reserve",
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100682#ifdef CONFIG_CMA
683 "CMA",
684#endif
Minchan Kim194159f2013-02-22 16:33:58 -0800685#ifdef CONFIG_MEMORY_ISOLATION
KOSAKI Motohiro91446b02008-04-15 14:34:42 -0700686 "Isolate",
Minchan Kim194159f2013-02-22 16:33:58 -0800687#endif
Mel Gorman467c9962007-10-16 01:26:02 -0700688};
689
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700690static void *frag_start(struct seq_file *m, loff_t *pos)
691{
692 pg_data_t *pgdat;
693 loff_t node = *pos;
694 for (pgdat = first_online_pgdat();
695 pgdat && node;
696 pgdat = next_online_pgdat(pgdat))
697 --node;
698
699 return pgdat;
700}
701
702static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
703{
704 pg_data_t *pgdat = (pg_data_t *)arg;
705
706 (*pos)++;
707 return next_online_pgdat(pgdat);
708}
709
710static void frag_stop(struct seq_file *m, void *arg)
711{
712}
713
Mel Gorman467c9962007-10-16 01:26:02 -0700714/* Walk all the zones in a node and print using a callback */
715static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
716 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700717{
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700718 struct zone *zone;
719 struct zone *node_zones = pgdat->node_zones;
720 unsigned long flags;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700721
722 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
723 if (!populated_zone(zone))
724 continue;
725
726 spin_lock_irqsave(&zone->lock, flags);
Mel Gorman467c9962007-10-16 01:26:02 -0700727 print(m, pgdat, zone);
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700728 spin_unlock_irqrestore(&zone->lock, flags);
Mel Gorman467c9962007-10-16 01:26:02 -0700729 }
730}
Mel Gormand7a57522010-05-24 14:32:25 -0700731#endif
Mel Gorman467c9962007-10-16 01:26:02 -0700732
David Rientjes0d6617c2011-09-14 16:21:05 -0700733#if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700734#ifdef CONFIG_ZONE_DMA
735#define TEXT_FOR_DMA(xx) xx "_dma",
736#else
737#define TEXT_FOR_DMA(xx)
738#endif
739
740#ifdef CONFIG_ZONE_DMA32
741#define TEXT_FOR_DMA32(xx) xx "_dma32",
742#else
743#define TEXT_FOR_DMA32(xx)
744#endif
745
746#ifdef CONFIG_HIGHMEM
747#define TEXT_FOR_HIGHMEM(xx) xx "_high",
748#else
749#define TEXT_FOR_HIGHMEM(xx)
750#endif
751
752#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
753 TEXT_FOR_HIGHMEM(xx) xx "_movable",
754
755const char * const vmstat_text[] = {
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700756 /* enum zone_stat_item countes */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700757 "nr_free_pages",
Johannes Weiner81c0a2b2013-09-11 14:20:47 -0700758 "nr_alloc_batch",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700759 "nr_inactive_anon",
760 "nr_active_anon",
761 "nr_inactive_file",
762 "nr_active_file",
763 "nr_unevictable",
764 "nr_mlock",
765 "nr_anon_pages",
766 "nr_mapped",
767 "nr_file_pages",
768 "nr_dirty",
769 "nr_writeback",
770 "nr_slab_reclaimable",
771 "nr_slab_unreclaimable",
772 "nr_page_table_pages",
773 "nr_kernel_stack",
774 "nr_unstable",
775 "nr_bounce",
776 "nr_vmscan_write",
Mel Gorman49ea7eb2011-10-31 17:07:59 -0700777 "nr_vmscan_immediate_reclaim",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700778 "nr_writeback_temp",
779 "nr_isolated_anon",
780 "nr_isolated_file",
781 "nr_shmem",
782 "nr_dirtied",
783 "nr_written",
Mel Gorman0d5d8232014-08-06 16:07:16 -0700784 "nr_pages_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700785
786#ifdef CONFIG_NUMA
787 "numa_hit",
788 "numa_miss",
789 "numa_foreign",
790 "numa_interleave",
791 "numa_local",
792 "numa_other",
793#endif
Johannes Weinera5289102014-04-03 14:47:51 -0700794 "workingset_refault",
795 "workingset_activate",
Johannes Weiner449dd692014-04-03 14:47:56 -0700796 "workingset_nodereclaim",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700797 "nr_anon_transparent_hugepages",
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700798 "nr_free_cma",
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700799
800 /* enum writeback_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700801 "nr_dirty_threshold",
802 "nr_dirty_background_threshold",
803
804#ifdef CONFIG_VM_EVENT_COUNTERS
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700805 /* enum vm_event_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700806 "pgpgin",
807 "pgpgout",
808 "pswpin",
809 "pswpout",
810
811 TEXTS_FOR_ZONES("pgalloc")
812
813 "pgfree",
814 "pgactivate",
815 "pgdeactivate",
816
817 "pgfault",
818 "pgmajfault",
819
820 TEXTS_FOR_ZONES("pgrefill")
Ying Han904249a2012-04-25 16:01:48 -0700821 TEXTS_FOR_ZONES("pgsteal_kswapd")
822 TEXTS_FOR_ZONES("pgsteal_direct")
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700823 TEXTS_FOR_ZONES("pgscan_kswapd")
824 TEXTS_FOR_ZONES("pgscan_direct")
Mel Gorman68243e72012-07-31 16:44:39 -0700825 "pgscan_direct_throttle",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700826
827#ifdef CONFIG_NUMA
828 "zone_reclaim_failed",
829#endif
830 "pginodesteal",
831 "slabs_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700832 "kswapd_inodesteal",
833 "kswapd_low_wmark_hit_quickly",
834 "kswapd_high_wmark_hit_quickly",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700835 "pageoutrun",
836 "allocstall",
837
838 "pgrotated",
839
Dave Hansen5509a5d2014-04-03 14:48:19 -0700840 "drop_pagecache",
841 "drop_slab",
842
Mel Gorman03c5a6e2012-11-02 14:52:48 +0000843#ifdef CONFIG_NUMA_BALANCING
844 "numa_pte_updates",
Mel Gorman72403b42013-11-12 15:08:32 -0800845 "numa_huge_pte_updates",
Mel Gorman03c5a6e2012-11-02 14:52:48 +0000846 "numa_hint_faults",
847 "numa_hint_faults_local",
848 "numa_pages_migrated",
849#endif
Mel Gorman5647bc22012-10-19 10:46:20 +0100850#ifdef CONFIG_MIGRATION
851 "pgmigrate_success",
852 "pgmigrate_fail",
853#endif
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700854#ifdef CONFIG_COMPACTION
Mel Gorman397487d2012-10-19 12:00:10 +0100855 "compact_migrate_scanned",
856 "compact_free_scanned",
857 "compact_isolated",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700858 "compact_stall",
859 "compact_fail",
860 "compact_success",
861#endif
862
863#ifdef CONFIG_HUGETLB_PAGE
864 "htlb_buddy_alloc_success",
865 "htlb_buddy_alloc_fail",
866#endif
867 "unevictable_pgs_culled",
868 "unevictable_pgs_scanned",
869 "unevictable_pgs_rescued",
870 "unevictable_pgs_mlocked",
871 "unevictable_pgs_munlocked",
872 "unevictable_pgs_cleared",
873 "unevictable_pgs_stranded",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700874
875#ifdef CONFIG_TRANSPARENT_HUGEPAGE
876 "thp_fault_alloc",
877 "thp_fault_fallback",
878 "thp_collapse_alloc",
879 "thp_collapse_alloc_failed",
880 "thp_split",
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800881 "thp_zero_page_alloc",
882 "thp_zero_page_alloc_failed",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700883#endif
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700884#ifdef CONFIG_MEMORY_BALLOON
885 "balloon_inflate",
886 "balloon_deflate",
887#ifdef CONFIG_BALLOON_COMPACTION
888 "balloon_migrate",
889#endif
890#endif /* CONFIG_MEMORY_BALLOON */
Mel Gormanec659932014-01-21 14:33:16 -0800891#ifdef CONFIG_DEBUG_TLBFLUSH
Dave Hansen6df46862013-09-11 14:20:24 -0700892#ifdef CONFIG_SMP
Dave Hansen9824cf92013-09-11 14:20:23 -0700893 "nr_tlb_remote_flush",
894 "nr_tlb_remote_flush_received",
Mel Gormanec659932014-01-21 14:33:16 -0800895#endif /* CONFIG_SMP */
Dave Hansen9824cf92013-09-11 14:20:23 -0700896 "nr_tlb_local_flush_all",
897 "nr_tlb_local_flush_one",
Mel Gormanec659932014-01-21 14:33:16 -0800898#endif /* CONFIG_DEBUG_TLBFLUSH */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700899
Davidlohr Bueso4f115142014-06-04 16:06:46 -0700900#ifdef CONFIG_DEBUG_VM_VMACACHE
901 "vmacache_find_calls",
902 "vmacache_find_hits",
Davidlohr Buesof5f302e2014-12-12 16:56:10 -0800903 "vmacache_full_flushes",
Davidlohr Bueso4f115142014-06-04 16:06:46 -0700904#endif
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700905#endif /* CONFIG_VM_EVENTS_COUNTERS */
906};
David Rientjes0d6617c2011-09-14 16:21:05 -0700907#endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700908
909
Mel Gormand7a57522010-05-24 14:32:25 -0700910#ifdef CONFIG_PROC_FS
Mel Gorman467c9962007-10-16 01:26:02 -0700911static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
912 struct zone *zone)
913{
914 int order;
915
916 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
917 for (order = 0; order < MAX_ORDER; ++order)
918 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
919 seq_putc(m, '\n');
920}
921
922/*
923 * This walks the free areas for each zone.
924 */
925static int frag_show(struct seq_file *m, void *arg)
926{
927 pg_data_t *pgdat = (pg_data_t *)arg;
928 walk_zones_in_node(m, pgdat, frag_show_print);
929 return 0;
930}
931
932static void pagetypeinfo_showfree_print(struct seq_file *m,
933 pg_data_t *pgdat, struct zone *zone)
934{
935 int order, mtype;
936
937 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
938 seq_printf(m, "Node %4d, zone %8s, type %12s ",
939 pgdat->node_id,
940 zone->name,
941 migratetype_names[mtype]);
942 for (order = 0; order < MAX_ORDER; ++order) {
943 unsigned long freecount = 0;
944 struct free_area *area;
945 struct list_head *curr;
946
947 area = &(zone->free_area[order]);
948
949 list_for_each(curr, &area->free_list[mtype])
950 freecount++;
951 seq_printf(m, "%6lu ", freecount);
952 }
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700953 seq_putc(m, '\n');
954 }
Mel Gorman467c9962007-10-16 01:26:02 -0700955}
956
957/* Print out the free pages at each order for each migatetype */
958static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
959{
960 int order;
961 pg_data_t *pgdat = (pg_data_t *)arg;
962
963 /* Print header */
964 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
965 for (order = 0; order < MAX_ORDER; ++order)
966 seq_printf(m, "%6d ", order);
967 seq_putc(m, '\n');
968
969 walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
970
971 return 0;
972}
973
974static void pagetypeinfo_showblockcount_print(struct seq_file *m,
975 pg_data_t *pgdat, struct zone *zone)
976{
977 int mtype;
978 unsigned long pfn;
979 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800980 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gorman467c9962007-10-16 01:26:02 -0700981 unsigned long count[MIGRATE_TYPES] = { 0, };
982
983 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
984 struct page *page;
985
986 if (!pfn_valid(pfn))
987 continue;
988
989 page = pfn_to_page(pfn);
Mel Gormaneb335752009-05-13 17:34:48 +0100990
991 /* Watch for unexpected holes punched in the memmap */
992 if (!memmap_valid_within(pfn, page, zone))
Mel Gormane80d6a22008-08-14 11:10:14 +0100993 continue;
Mel Gormaneb335752009-05-13 17:34:48 +0100994
Mel Gorman467c9962007-10-16 01:26:02 -0700995 mtype = get_pageblock_migratetype(page);
996
Mel Gormane80d6a22008-08-14 11:10:14 +0100997 if (mtype < MIGRATE_TYPES)
998 count[mtype]++;
Mel Gorman467c9962007-10-16 01:26:02 -0700999 }
1000
1001 /* Print counts */
1002 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1003 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1004 seq_printf(m, "%12lu ", count[mtype]);
1005 seq_putc(m, '\n');
1006}
1007
1008/* Print out the free pages at each order for each migratetype */
1009static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
1010{
1011 int mtype;
1012 pg_data_t *pgdat = (pg_data_t *)arg;
1013
1014 seq_printf(m, "\n%-23s", "Number of blocks type ");
1015 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1016 seq_printf(m, "%12s ", migratetype_names[mtype]);
1017 seq_putc(m, '\n');
1018 walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
1019
1020 return 0;
1021}
1022
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001023#ifdef CONFIG_PAGE_OWNER
1024static void pagetypeinfo_showmixedcount_print(struct seq_file *m,
1025 pg_data_t *pgdat,
1026 struct zone *zone)
1027{
1028 struct page *page;
1029 struct page_ext *page_ext;
1030 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
1031 unsigned long end_pfn = pfn + zone->spanned_pages;
1032 unsigned long count[MIGRATE_TYPES] = { 0, };
1033 int pageblock_mt, page_mt;
1034 int i;
1035
1036 /* Scan block by block. First and last block may be incomplete */
1037 pfn = zone->zone_start_pfn;
1038
1039 /*
1040 * Walk the zone in pageblock_nr_pages steps. If a page block spans
1041 * a zone boundary, it will be double counted between zones. This does
1042 * not matter as the mixed block count will still be correct
1043 */
1044 for (; pfn < end_pfn; ) {
1045 if (!pfn_valid(pfn)) {
1046 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
1047 continue;
1048 }
1049
1050 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
1051 block_end_pfn = min(block_end_pfn, end_pfn);
1052
1053 page = pfn_to_page(pfn);
1054 pageblock_mt = get_pfnblock_migratetype(page, pfn);
1055
1056 for (; pfn < block_end_pfn; pfn++) {
1057 if (!pfn_valid_within(pfn))
1058 continue;
1059
1060 page = pfn_to_page(pfn);
1061 if (PageBuddy(page)) {
1062 pfn += (1UL << page_order(page)) - 1;
1063 continue;
1064 }
1065
1066 if (PageReserved(page))
1067 continue;
1068
1069 page_ext = lookup_page_ext(page);
1070
1071 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
1072 continue;
1073
1074 page_mt = gfpflags_to_migratetype(page_ext->gfp_mask);
1075 if (pageblock_mt != page_mt) {
1076 if (is_migrate_cma(pageblock_mt))
1077 count[MIGRATE_MOVABLE]++;
1078 else
1079 count[pageblock_mt]++;
1080
1081 pfn = block_end_pfn;
1082 break;
1083 }
1084 pfn += (1UL << page_ext->order) - 1;
1085 }
1086 }
1087
1088 /* Print counts */
1089 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1090 for (i = 0; i < MIGRATE_TYPES; i++)
1091 seq_printf(m, "%12lu ", count[i]);
1092 seq_putc(m, '\n');
1093}
1094#endif /* CONFIG_PAGE_OWNER */
1095
1096/*
1097 * Print out the number of pageblocks for each migratetype that contain pages
1098 * of other types. This gives an indication of how well fallbacks are being
1099 * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
1100 * to determine what is going on
1101 */
1102static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
1103{
1104#ifdef CONFIG_PAGE_OWNER
1105 int mtype;
1106
1107 if (!page_owner_inited)
1108 return;
1109
1110 drain_all_pages(NULL);
1111
1112 seq_printf(m, "\n%-23s", "Number of mixed blocks ");
1113 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1114 seq_printf(m, "%12s ", migratetype_names[mtype]);
1115 seq_putc(m, '\n');
1116
1117 walk_zones_in_node(m, pgdat, pagetypeinfo_showmixedcount_print);
1118#endif /* CONFIG_PAGE_OWNER */
1119}
1120
Mel Gorman467c9962007-10-16 01:26:02 -07001121/*
1122 * This prints out statistics in relation to grouping pages by mobility.
1123 * It is expensive to collect so do not constantly read the file.
1124 */
1125static int pagetypeinfo_show(struct seq_file *m, void *arg)
1126{
1127 pg_data_t *pgdat = (pg_data_t *)arg;
1128
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001129 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08001130 if (!node_state(pgdat->node_id, N_MEMORY))
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001131 return 0;
1132
Mel Gorman467c9962007-10-16 01:26:02 -07001133 seq_printf(m, "Page block order: %d\n", pageblock_order);
1134 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
1135 seq_putc(m, '\n');
1136 pagetypeinfo_showfree(m, pgdat);
1137 pagetypeinfo_showblockcount(m, pgdat);
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001138 pagetypeinfo_showmixedcount(m, pgdat);
Mel Gorman467c9962007-10-16 01:26:02 -07001139
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001140 return 0;
1141}
1142
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001143static const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001144 .start = frag_start,
1145 .next = frag_next,
1146 .stop = frag_stop,
1147 .show = frag_show,
1148};
1149
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001150static int fragmentation_open(struct inode *inode, struct file *file)
1151{
1152 return seq_open(file, &fragmentation_op);
1153}
1154
1155static const struct file_operations fragmentation_file_operations = {
1156 .open = fragmentation_open,
1157 .read = seq_read,
1158 .llseek = seq_lseek,
1159 .release = seq_release,
1160};
1161
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001162static const struct seq_operations pagetypeinfo_op = {
Mel Gorman467c9962007-10-16 01:26:02 -07001163 .start = frag_start,
1164 .next = frag_next,
1165 .stop = frag_stop,
1166 .show = pagetypeinfo_show,
1167};
1168
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001169static int pagetypeinfo_open(struct inode *inode, struct file *file)
1170{
1171 return seq_open(file, &pagetypeinfo_op);
1172}
1173
1174static const struct file_operations pagetypeinfo_file_ops = {
1175 .open = pagetypeinfo_open,
1176 .read = seq_read,
1177 .llseek = seq_lseek,
1178 .release = seq_release,
1179};
1180
Mel Gorman467c9962007-10-16 01:26:02 -07001181static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
1182 struct zone *zone)
1183{
1184 int i;
1185 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
1186 seq_printf(m,
1187 "\n pages free %lu"
1188 "\n min %lu"
1189 "\n low %lu"
1190 "\n high %lu"
Wu Fengguang08d9ae72009-06-16 15:32:30 -07001191 "\n scanned %lu"
Mel Gorman467c9962007-10-16 01:26:02 -07001192 "\n spanned %lu"
Jiang Liu9feedc92012-12-12 13:52:12 -08001193 "\n present %lu"
1194 "\n managed %lu",
Mel Gorman88f5acf2011-01-13 15:45:41 -08001195 zone_page_state(zone, NR_FREE_PAGES),
Mel Gorman41858962009-06-16 15:32:12 -07001196 min_wmark_pages(zone),
1197 low_wmark_pages(zone),
1198 high_wmark_pages(zone),
Mel Gorman0d5d8232014-08-06 16:07:16 -07001199 zone_page_state(zone, NR_PAGES_SCANNED),
Mel Gorman467c9962007-10-16 01:26:02 -07001200 zone->spanned_pages,
Jiang Liu9feedc92012-12-12 13:52:12 -08001201 zone->present_pages,
1202 zone->managed_pages);
Mel Gorman467c9962007-10-16 01:26:02 -07001203
1204 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1205 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
1206 zone_page_state(zone, i));
1207
1208 seq_printf(m,
Mel Gorman3484b2d2014-08-06 16:07:14 -07001209 "\n protection: (%ld",
Mel Gorman467c9962007-10-16 01:26:02 -07001210 zone->lowmem_reserve[0]);
1211 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
Mel Gorman3484b2d2014-08-06 16:07:14 -07001212 seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
Mel Gorman467c9962007-10-16 01:26:02 -07001213 seq_printf(m,
1214 ")"
1215 "\n pagesets");
1216 for_each_online_cpu(i) {
1217 struct per_cpu_pageset *pageset;
Mel Gorman467c9962007-10-16 01:26:02 -07001218
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001219 pageset = per_cpu_ptr(zone->pageset, i);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001220 seq_printf(m,
1221 "\n cpu: %i"
1222 "\n count: %i"
1223 "\n high: %i"
1224 "\n batch: %i",
1225 i,
1226 pageset->pcp.count,
1227 pageset->pcp.high,
1228 pageset->pcp.batch);
Mel Gorman467c9962007-10-16 01:26:02 -07001229#ifdef CONFIG_SMP
1230 seq_printf(m, "\n vm stats threshold: %d",
1231 pageset->stat_threshold);
1232#endif
1233 }
1234 seq_printf(m,
1235 "\n all_unreclaimable: %u"
Rik van Riel556adec2008-10-18 20:26:34 -07001236 "\n start_pfn: %lu"
1237 "\n inactive_ratio: %u",
Lisa Du6e543d52013-09-11 14:22:36 -07001238 !zone_reclaimable(zone),
Rik van Riel556adec2008-10-18 20:26:34 -07001239 zone->zone_start_pfn,
1240 zone->inactive_ratio);
Mel Gorman467c9962007-10-16 01:26:02 -07001241 seq_putc(m, '\n');
1242}
1243
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001244/*
1245 * Output information about zones in @pgdat.
1246 */
1247static int zoneinfo_show(struct seq_file *m, void *arg)
1248{
Mel Gorman467c9962007-10-16 01:26:02 -07001249 pg_data_t *pgdat = (pg_data_t *)arg;
1250 walk_zones_in_node(m, pgdat, zoneinfo_show_print);
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001251 return 0;
1252}
1253
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001254static const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001255 .start = frag_start, /* iterate over all zones. The same as in
1256 * fragmentation. */
1257 .next = frag_next,
1258 .stop = frag_stop,
1259 .show = zoneinfo_show,
1260};
1261
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001262static int zoneinfo_open(struct inode *inode, struct file *file)
1263{
1264 return seq_open(file, &zoneinfo_op);
1265}
1266
1267static const struct file_operations proc_zoneinfo_file_operations = {
1268 .open = zoneinfo_open,
1269 .read = seq_read,
1270 .llseek = seq_lseek,
1271 .release = seq_release,
1272};
1273
Michael Rubin79da8262010-10-26 14:21:36 -07001274enum writeback_stat_item {
1275 NR_DIRTY_THRESHOLD,
1276 NR_DIRTY_BG_THRESHOLD,
1277 NR_VM_WRITEBACK_STAT_ITEMS,
1278};
1279
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001280static void *vmstat_start(struct seq_file *m, loff_t *pos)
1281{
Christoph Lameter2244b952006-06-30 01:55:33 -07001282 unsigned long *v;
Michael Rubin79da8262010-10-26 14:21:36 -07001283 int i, stat_items_size;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001284
1285 if (*pos >= ARRAY_SIZE(vmstat_text))
1286 return NULL;
Michael Rubin79da8262010-10-26 14:21:36 -07001287 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
1288 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001289
Christoph Lameterf8891e52006-06-30 01:55:45 -07001290#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -07001291 stat_items_size += sizeof(struct vm_event_state);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001292#endif
Michael Rubin79da8262010-10-26 14:21:36 -07001293
1294 v = kmalloc(stat_items_size, GFP_KERNEL);
Christoph Lameter2244b952006-06-30 01:55:33 -07001295 m->private = v;
1296 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001297 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -07001298 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1299 v[i] = global_page_state(i);
Michael Rubin79da8262010-10-26 14:21:36 -07001300 v += NR_VM_ZONE_STAT_ITEMS;
1301
1302 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
1303 v + NR_DIRTY_THRESHOLD);
1304 v += NR_VM_WRITEBACK_STAT_ITEMS;
1305
Christoph Lameterf8891e52006-06-30 01:55:45 -07001306#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -07001307 all_vm_events(v);
1308 v[PGPGIN] /= 2; /* sectors -> kbytes */
1309 v[PGPGOUT] /= 2;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001310#endif
Wu Fengguangff8b16d2010-11-04 01:56:49 +08001311 return (unsigned long *)m->private + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001312}
1313
1314static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1315{
1316 (*pos)++;
1317 if (*pos >= ARRAY_SIZE(vmstat_text))
1318 return NULL;
1319 return (unsigned long *)m->private + *pos;
1320}
1321
1322static int vmstat_show(struct seq_file *m, void *arg)
1323{
1324 unsigned long *l = arg;
1325 unsigned long off = l - (unsigned long *)m->private;
1326
1327 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
1328 return 0;
1329}
1330
1331static void vmstat_stop(struct seq_file *m, void *arg)
1332{
1333 kfree(m->private);
1334 m->private = NULL;
1335}
1336
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001337static const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001338 .start = vmstat_start,
1339 .next = vmstat_next,
1340 .stop = vmstat_stop,
1341 .show = vmstat_show,
1342};
1343
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001344static int vmstat_open(struct inode *inode, struct file *file)
1345{
1346 return seq_open(file, &vmstat_op);
1347}
1348
1349static const struct file_operations proc_vmstat_file_operations = {
1350 .open = vmstat_open,
1351 .read = seq_read,
1352 .llseek = seq_lseek,
1353 .release = seq_release,
1354};
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001355#endif /* CONFIG_PROC_FS */
1356
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001357#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001358static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
Christoph Lameter77461ab2007-05-09 02:35:13 -07001359int sysctl_stat_interval __read_mostly = HZ;
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001360static cpumask_var_t cpu_stat_off;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001361
1362static void vmstat_update(struct work_struct *w)
1363{
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001364 if (refresh_cpu_vm_stats())
1365 /*
1366 * Counters were updated so we expect more updates
1367 * to occur in the future. Keep on running the
1368 * update worker thread.
1369 */
1370 schedule_delayed_work(this_cpu_ptr(&vmstat_work),
1371 round_jiffies_relative(sysctl_stat_interval));
1372 else {
1373 /*
1374 * We did not update any counters so the app may be in
1375 * a mode where it does not cause counter updates.
1376 * We may be uselessly running vmstat_update.
1377 * Defer the checking for differentials to the
1378 * shepherd thread on a different processor.
1379 */
1380 int r;
1381 /*
1382 * Shepherd work thread does not race since it never
1383 * changes the bit if its zero but the cpu
1384 * online / off line code may race if
1385 * worker threads are still allowed during
1386 * shutdown / startup.
1387 */
1388 r = cpumask_test_and_set_cpu(smp_processor_id(),
1389 cpu_stat_off);
1390 VM_BUG_ON(r);
1391 }
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001392}
1393
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001394/*
1395 * Check if the diffs for a certain cpu indicate that
1396 * an update is needed.
1397 */
1398static bool need_update(int cpu)
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001399{
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001400 struct zone *zone;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001401
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001402 for_each_populated_zone(zone) {
1403 struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
1404
1405 BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
1406 /*
1407 * The fast way of checking if there are any vmstat diffs.
1408 * This works because the diffs are byte sized items.
1409 */
1410 if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
1411 return true;
1412
1413 }
1414 return false;
1415}
1416
1417
1418/*
1419 * Shepherd worker thread that checks the
1420 * differentials of processors that have their worker
1421 * threads for vm statistics updates disabled because of
1422 * inactivity.
1423 */
1424static void vmstat_shepherd(struct work_struct *w);
1425
1426static DECLARE_DELAYED_WORK(shepherd, vmstat_shepherd);
1427
1428static void vmstat_shepherd(struct work_struct *w)
1429{
1430 int cpu;
1431
1432 get_online_cpus();
1433 /* Check processors whose vmstat worker threads have been disabled */
1434 for_each_cpu(cpu, cpu_stat_off)
1435 if (need_update(cpu) &&
1436 cpumask_test_and_clear_cpu(cpu, cpu_stat_off))
1437
1438 schedule_delayed_work_on(cpu, &per_cpu(vmstat_work, cpu),
1439 __round_jiffies_relative(sysctl_stat_interval, cpu));
1440
1441 put_online_cpus();
1442
1443 schedule_delayed_work(&shepherd,
1444 round_jiffies_relative(sysctl_stat_interval));
1445
1446}
1447
1448static void __init start_shepherd_timer(void)
1449{
1450 int cpu;
1451
1452 for_each_possible_cpu(cpu)
1453 INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
1454 vmstat_update);
1455
1456 if (!alloc_cpumask_var(&cpu_stat_off, GFP_KERNEL))
1457 BUG();
1458 cpumask_copy(cpu_stat_off, cpu_online_mask);
1459
1460 schedule_delayed_work(&shepherd,
1461 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001462}
1463
Toshi Kani807a1bd2013-11-12 15:08:13 -08001464static void vmstat_cpu_dead(int node)
1465{
1466 int cpu;
1467
1468 get_online_cpus();
1469 for_each_online_cpu(cpu)
1470 if (cpu_to_node(cpu) == node)
1471 goto end;
1472
1473 node_clear_state(node, N_CPU);
1474end:
1475 put_online_cpus();
1476}
1477
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001478/*
1479 * Use the cpu notifier to insure that the thresholds are recalculated
1480 * when necessary.
1481 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04001482static int vmstat_cpuup_callback(struct notifier_block *nfb,
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001483 unsigned long action,
1484 void *hcpu)
1485{
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001486 long cpu = (long)hcpu;
1487
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001488 switch (action) {
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001489 case CPU_ONLINE:
1490 case CPU_ONLINE_FROZEN:
KAMEZAWA Hiroyuki5ee28a42010-09-09 16:38:14 -07001491 refresh_zone_stat_thresholds();
Christoph Lameterad596922010-01-05 15:34:51 +09001492 node_set_state(cpu_to_node(cpu), N_CPU);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001493 cpumask_set_cpu(cpu, cpu_stat_off);
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001494 break;
1495 case CPU_DOWN_PREPARE:
1496 case CPU_DOWN_PREPARE_FROZEN:
Tejun Heoafe2c512010-12-14 16:21:17 +01001497 cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001498 cpumask_clear_cpu(cpu, cpu_stat_off);
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001499 break;
1500 case CPU_DOWN_FAILED:
1501 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001502 cpumask_set_cpu(cpu, cpu_stat_off);
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001503 break;
Andy Whitcroftce421c72006-12-06 20:33:08 -08001504 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001505 case CPU_DEAD_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08001506 refresh_zone_stat_thresholds();
Toshi Kani807a1bd2013-11-12 15:08:13 -08001507 vmstat_cpu_dead(cpu_to_node(cpu));
Andy Whitcroftce421c72006-12-06 20:33:08 -08001508 break;
1509 default:
1510 break;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001511 }
1512 return NOTIFY_OK;
1513}
1514
Paul Gortmaker0db06282013-06-19 14:53:51 -04001515static struct notifier_block vmstat_notifier =
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001516 { &vmstat_cpuup_callback, NULL, 0 };
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001517#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001518
Adrian Bunke2fc88d2007-10-16 01:26:27 -07001519static int __init setup_vmstat(void)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001520{
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001521#ifdef CONFIG_SMP
Srivatsa S. Bhat0be94ba2014-03-11 02:12:27 +05301522 cpu_notifier_register_begin();
1523 __register_cpu_notifier(&vmstat_notifier);
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001524
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001525 start_shepherd_timer();
Srivatsa S. Bhat0be94ba2014-03-11 02:12:27 +05301526 cpu_notifier_register_done();
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001527#endif
1528#ifdef CONFIG_PROC_FS
1529 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001530 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001531 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001532 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001533#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001534 return 0;
1535}
1536module_init(setup_vmstat)
Mel Gormand7a57522010-05-24 14:32:25 -07001537
1538#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1539#include <linux/debugfs.h>
1540
Mel Gormand7a57522010-05-24 14:32:25 -07001541
1542/*
1543 * Return an index indicating how much of the available free memory is
1544 * unusable for an allocation of the requested size.
1545 */
1546static int unusable_free_index(unsigned int order,
1547 struct contig_page_info *info)
1548{
1549 /* No free memory is interpreted as all free memory is unusable */
1550 if (info->free_pages == 0)
1551 return 1000;
1552
1553 /*
1554 * Index should be a value between 0 and 1. Return a value to 3
1555 * decimal places.
1556 *
1557 * 0 => no fragmentation
1558 * 1 => high fragmentation
1559 */
1560 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1561
1562}
1563
1564static void unusable_show_print(struct seq_file *m,
1565 pg_data_t *pgdat, struct zone *zone)
1566{
1567 unsigned int order;
1568 int index;
1569 struct contig_page_info info;
1570
1571 seq_printf(m, "Node %d, zone %8s ",
1572 pgdat->node_id,
1573 zone->name);
1574 for (order = 0; order < MAX_ORDER; ++order) {
1575 fill_contig_page_info(zone, order, &info);
1576 index = unusable_free_index(order, &info);
1577 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1578 }
1579
1580 seq_putc(m, '\n');
1581}
1582
1583/*
1584 * Display unusable free space index
1585 *
1586 * The unusable free space index measures how much of the available free
1587 * memory cannot be used to satisfy an allocation of a given size and is a
1588 * value between 0 and 1. The higher the value, the more of free memory is
1589 * unusable and by implication, the worse the external fragmentation is. This
1590 * can be expressed as a percentage by multiplying by 100.
1591 */
1592static int unusable_show(struct seq_file *m, void *arg)
1593{
1594 pg_data_t *pgdat = (pg_data_t *)arg;
1595
1596 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08001597 if (!node_state(pgdat->node_id, N_MEMORY))
Mel Gormand7a57522010-05-24 14:32:25 -07001598 return 0;
1599
1600 walk_zones_in_node(m, pgdat, unusable_show_print);
1601
1602 return 0;
1603}
1604
1605static const struct seq_operations unusable_op = {
1606 .start = frag_start,
1607 .next = frag_next,
1608 .stop = frag_stop,
1609 .show = unusable_show,
1610};
1611
1612static int unusable_open(struct inode *inode, struct file *file)
1613{
1614 return seq_open(file, &unusable_op);
1615}
1616
1617static const struct file_operations unusable_file_ops = {
1618 .open = unusable_open,
1619 .read = seq_read,
1620 .llseek = seq_lseek,
1621 .release = seq_release,
1622};
1623
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001624static void extfrag_show_print(struct seq_file *m,
1625 pg_data_t *pgdat, struct zone *zone)
1626{
1627 unsigned int order;
1628 int index;
1629
1630 /* Alloc on stack as interrupts are disabled for zone walk */
1631 struct contig_page_info info;
1632
1633 seq_printf(m, "Node %d, zone %8s ",
1634 pgdat->node_id,
1635 zone->name);
1636 for (order = 0; order < MAX_ORDER; ++order) {
1637 fill_contig_page_info(zone, order, &info);
Mel Gorman56de7262010-05-24 14:32:30 -07001638 index = __fragmentation_index(order, &info);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001639 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1640 }
1641
1642 seq_putc(m, '\n');
1643}
1644
1645/*
1646 * Display fragmentation index for orders that allocations would fail for
1647 */
1648static int extfrag_show(struct seq_file *m, void *arg)
1649{
1650 pg_data_t *pgdat = (pg_data_t *)arg;
1651
1652 walk_zones_in_node(m, pgdat, extfrag_show_print);
1653
1654 return 0;
1655}
1656
1657static const struct seq_operations extfrag_op = {
1658 .start = frag_start,
1659 .next = frag_next,
1660 .stop = frag_stop,
1661 .show = extfrag_show,
1662};
1663
1664static int extfrag_open(struct inode *inode, struct file *file)
1665{
1666 return seq_open(file, &extfrag_op);
1667}
1668
1669static const struct file_operations extfrag_file_ops = {
1670 .open = extfrag_open,
1671 .read = seq_read,
1672 .llseek = seq_lseek,
1673 .release = seq_release,
1674};
1675
Mel Gormand7a57522010-05-24 14:32:25 -07001676static int __init extfrag_debug_init(void)
1677{
Sasikantha babubde8bd82012-05-29 15:06:22 -07001678 struct dentry *extfrag_debug_root;
1679
Mel Gormand7a57522010-05-24 14:32:25 -07001680 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
1681 if (!extfrag_debug_root)
1682 return -ENOMEM;
1683
1684 if (!debugfs_create_file("unusable_index", 0444,
1685 extfrag_debug_root, NULL, &unusable_file_ops))
Sasikantha babubde8bd82012-05-29 15:06:22 -07001686 goto fail;
Mel Gormand7a57522010-05-24 14:32:25 -07001687
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001688 if (!debugfs_create_file("extfrag_index", 0444,
1689 extfrag_debug_root, NULL, &extfrag_file_ops))
Sasikantha babubde8bd82012-05-29 15:06:22 -07001690 goto fail;
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001691
Mel Gormand7a57522010-05-24 14:32:25 -07001692 return 0;
Sasikantha babubde8bd82012-05-29 15:06:22 -07001693fail:
1694 debugfs_remove_recursive(extfrag_debug_root);
1695 return -ENOMEM;
Mel Gormand7a57522010-05-24 14:32:25 -07001696}
1697
1698module_init(extfrag_debug_init);
1699#endif