xref: /illumos-gate/usr/src/uts/common/os/vm_meter.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/sysmacros.h>
45 #include <sys/systm.h>
46 #include <sys/vmsystm.h>
47 #include <sys/cpuvar.h>
48 #include <sys/sysinfo.h>
49 
50 /*
51  * This define represents the number of
52  * useful pages transferred per paging i/o operation, under the assumption
53  * that half of the total number is actually useful.  However, if there's
54  * only one page transferred per operation, we assume that it's useful.
55  */
56 
57 #ifdef	lint
58 #define	UsefulPagesPerIO	1
59 #else	/* lint */
60 #define	UsefulPagesPerIO	nz((MAXBSIZE/PAGESIZE)/2)
61 #endif	/* lint */
62 
63 extern int dopageout;
64 
65 /* Average new into old with aging factor time */
66 #define	ave(smooth, cnt, time) \
67 	smooth = ((time - 1) * (smooth) + (cnt)) / (time)
68 
69 /*
70  * pagein and pageout rates for use by swapper.
71  */
72 ulong_t pginrate;		/* 5 second average	*/
73 ulong_t pgoutrate;		/* 5 second average	*/
74 
75 static ulong_t ogpagein;	/* pagein rate a sec ago */
76 static ulong_t ogpageout;	/* pageout rate a sec ago */
77 
78 /*
79  * Called once a second to gather statistics.
80  */
81 void
82 vmmeter(void)
83 {
84 	cpu_t *cp;
85 	ulong_t gpagein, gpageout;
86 
87 	/*
88 	 * Compute 5 sec and 30 sec average free memory values.
89 	 */
90 	ave(avefree, freemem, 5);
91 	ave(avefree30, freemem, 30);
92 
93 	/*
94 	 * Compute the 5 secs average of pageins and pageouts.
95 	 */
96 	gpagein = gpageout = 0;
97 
98 	cp = cpu_list;
99 	do {
100 		gpagein += (ulong_t)CPU_STATS(cp, vm.pgin);
101 		gpageout += (ulong_t)CPU_STATS(cp, vm.pgout);
102 	} while ((cp = cp->cpu_next) != cpu_list);
103 
104 	if ((gpagein >= ogpagein) && (gpageout >= ogpageout)) {
105 		ave(pginrate, gpagein - ogpagein, 5);
106 		ave(pgoutrate, gpageout - ogpageout, 5);
107 	}
108 
109 	/*
110 	 * Save the current pagein/pageout values.
111 	 */
112 	ogpagein = gpagein;
113 	ogpageout = gpageout;
114 
115 	if (!lotsfree || !dopageout)
116 		return;
117 
118 	/*
119 	 * Decay deficit by the expected number of pages brought in since
120 	 * the last call (i.e., in the last second).  The calculation
121 	 * assumes that one half of the pages brought in are actually
122 	 * useful (see comment above), and that half of the overall
123 	 * paging i/o activity is pageins as opposed to pageouts (the
124 	 * trailing factor of 2)  It also assumes that paging i/o is done
125 	 * in units of MAXBSIZE bytes, which is a dubious assumption to
126 	 * apply to all file system types.
127 	 */
128 	deficit -= MIN(deficit,
129 	    MAX(deficit / 10, UsefulPagesPerIO * maxpgio / 2));
130 }
131