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