xref: /illumos-gate/usr/src/uts/common/os/vm_meter.c (revision 2d6eb4a5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/sysmacros.h>
43 #include <sys/systm.h>
44 #include <sys/vmsystm.h>
45 #include <sys/cpuvar.h>
46 #include <sys/sysinfo.h>
47 
48 /*
49  * This define represents the number of
50  * useful pages transferred per paging i/o operation, under the assumption
51  * that half of the total number is actually useful.  However, if there's
52  * only one page transferred per operation, we assume that it's useful.
53  */
54 
55 #ifdef	lint
56 #define	UsefulPagesPerIO	1
57 #else	/* lint */
58 #define	UsefulPagesPerIO	nz((MAXBSIZE/PAGESIZE)/2)
59 #endif	/* lint */
60 
61 extern int dopageout;
62 
63 /* Average new into old with aging factor time */
64 #define	ave(smooth, cnt, time) \
65 	smooth = ((time - 1) * (smooth) + (cnt)) / (time)
66 
67 /*
68  * pagein and pageout rates for use by swapper.
69  */
70 ulong_t pginrate;		/* 5 second average	*/
71 ulong_t pgoutrate;		/* 5 second average	*/
72 
73 static ulong_t ogpagein;	/* pagein rate a sec ago */
74 static ulong_t ogpageout;	/* pageout rate a sec ago */
75 
76 /*
77  * Called once a second to gather statistics.
78  */
79 void
vmmeter(void)80 vmmeter(void)
81 {
82 	cpu_t *cp;
83 	ulong_t gpagein, gpageout;
84 
85 	/*
86 	 * Compute 5 sec and 30 sec average free memory values.
87 	 */
88 	ave(avefree, freemem, 5);
89 	ave(avefree30, freemem, 30);
90 
91 	/*
92 	 * Compute the 5 secs average of pageins and pageouts.
93 	 */
94 	gpagein = gpageout = 0;
95 
96 	cp = cpu_list;
97 	do {
98 		gpagein += (ulong_t)CPU_STATS(cp, vm.pgin);
99 		gpageout += (ulong_t)CPU_STATS(cp, vm.pgout);
100 	} while ((cp = cp->cpu_next) != cpu_list);
101 
102 	if ((gpagein >= ogpagein) && (gpageout >= ogpageout)) {
103 		ave(pginrate, gpagein - ogpagein, 5);
104 		ave(pgoutrate, gpageout - ogpageout, 5);
105 	}
106 
107 	/*
108 	 * Save the current pagein/pageout values.
109 	 */
110 	ogpagein = gpagein;
111 	ogpageout = gpageout;
112 
113 	if (!lotsfree || !dopageout)
114 		return;
115 
116 	/*
117 	 * Decay deficit by the expected number of pages brought in since
118 	 * the last call (i.e., in the last second).  The calculation
119 	 * assumes that one half of the pages brought in are actually
120 	 * useful (see comment above), and that half of the overall
121 	 * paging i/o activity is pageins as opposed to pageouts (the
122 	 * trailing factor of 2)  It also assumes that paging i/o is done
123 	 * in units of MAXBSIZE bytes, which is a dubious assumption to
124 	 * apply to all file system types.
125 	 */
126 	deficit -= MIN(deficit,
127 	    MAX(deficit / 10, UsefulPagesPerIO * maxpgio / 2));
128 }
129