1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/t_lock.h>
44*0Sstevel@tonic-gate #include <sys/param.h>
45*0Sstevel@tonic-gate #include <sys/systm.h>
46*0Sstevel@tonic-gate #include <sys/mman.h>
47*0Sstevel@tonic-gate #include <sys/sysmacros.h>
48*0Sstevel@tonic-gate #include <sys/errno.h>
49*0Sstevel@tonic-gate #include <sys/signal.h>
50*0Sstevel@tonic-gate #include <sys/user.h>
51*0Sstevel@tonic-gate #include <sys/proc.h>
52*0Sstevel@tonic-gate #include <sys/cmn_err.h>
53*0Sstevel@tonic-gate #include <sys/debug.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #include <vm/hat.h>
56*0Sstevel@tonic-gate #include <vm/as.h>
57*0Sstevel@tonic-gate #include <vm/seg_vn.h>
58*0Sstevel@tonic-gate #include <vm/rm.h>
59*0Sstevel@tonic-gate #include <vm/seg.h>
60*0Sstevel@tonic-gate #include <vm/page.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * Yield the size of an address space.
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * The size can only be used as a hint since we cannot guarantee it
66*0Sstevel@tonic-gate  * will stay the same size unless the as->a_lock is held by the caller.
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate size_t
69*0Sstevel@tonic-gate rm_assize(struct as *as)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	size_t size = 0;
72*0Sstevel@tonic-gate 	struct seg *seg;
73*0Sstevel@tonic-gate 	struct segvn_data *svd;
74*0Sstevel@tonic-gate 	extern struct seg_ops segdev_ops;	/* needs a header file */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	ASSERT(as != NULL && AS_READ_HELD(as, &as->a_lock));
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (as == &kas)
79*0Sstevel@tonic-gate 		return (0);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
82*0Sstevel@tonic-gate 		if (seg->s_ops == &segdev_ops &&
83*0Sstevel@tonic-gate 		    ((SEGOP_GETTYPE(seg, seg->s_base) &
84*0Sstevel@tonic-gate 		    (MAP_SHARED | MAP_PRIVATE)) == 0)) {
85*0Sstevel@tonic-gate 			/*
86*0Sstevel@tonic-gate 			 * Don't include mappings of /dev/null.  These just
87*0Sstevel@tonic-gate 			 * reserve address space ranges and have no memory.
88*0Sstevel@tonic-gate 			 * We cheat by knowing that these segments come
89*0Sstevel@tonic-gate 			 * from segdev and have no mapping type.
90*0Sstevel@tonic-gate 			 */
91*0Sstevel@tonic-gate 			/* EMPTY */;
92*0Sstevel@tonic-gate 		} else if (seg->s_ops == &segvn_ops &&
93*0Sstevel@tonic-gate 		    (svd = (struct segvn_data *)seg->s_data) != NULL &&
94*0Sstevel@tonic-gate 		    (svd->vp == NULL || svd->vp->v_type != VREG) &&
95*0Sstevel@tonic-gate 		    (svd->flags & MAP_NORESERVE)) {
96*0Sstevel@tonic-gate 			/*
97*0Sstevel@tonic-gate 			 * Don't include MAP_NORESERVE pages in the
98*0Sstevel@tonic-gate 			 * address range unless their mappings have
99*0Sstevel@tonic-gate 			 * actually materialized.  We cheat by knowing
100*0Sstevel@tonic-gate 			 * that segvn is the only segment driver that
101*0Sstevel@tonic-gate 			 * supports MAP_NORESERVE and that the actual
102*0Sstevel@tonic-gate 			 * number of bytes reserved is in the segment's
103*0Sstevel@tonic-gate 			 * private data structure.
104*0Sstevel@tonic-gate 			 */
105*0Sstevel@tonic-gate 			size += svd->swresv;
106*0Sstevel@tonic-gate 		} else {
107*0Sstevel@tonic-gate 			caddr_t addr = seg->s_base;
108*0Sstevel@tonic-gate 			size_t segsize = seg->s_size;
109*0Sstevel@tonic-gate 			vnode_t *vp;
110*0Sstevel@tonic-gate 			vattr_t vattr;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 			/*
113*0Sstevel@tonic-gate 			 * If the segment is mapped beyond the end of the
114*0Sstevel@tonic-gate 			 * underlying mapped file, if any, then limit the
115*0Sstevel@tonic-gate 			 * segment's size contribution to the file size.
116*0Sstevel@tonic-gate 			 */
117*0Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
118*0Sstevel@tonic-gate 			if (seg->s_ops == &segvn_ops &&
119*0Sstevel@tonic-gate 			    SEGOP_GETVP(seg, addr, &vp) == 0 &&
120*0Sstevel@tonic-gate 			    vp != NULL && vp->v_type == VREG &&
121*0Sstevel@tonic-gate 			    VOP_GETATTR(vp, &vattr, ATTR_HINT, CRED()) == 0) {
122*0Sstevel@tonic-gate 				u_offset_t filesize = vattr.va_size;
123*0Sstevel@tonic-gate 				u_offset_t offset = SEGOP_GETOFFSET(seg, addr);
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 				if (filesize < offset)
126*0Sstevel@tonic-gate 					filesize = 0;
127*0Sstevel@tonic-gate 				else
128*0Sstevel@tonic-gate 					filesize -= offset;
129*0Sstevel@tonic-gate 				filesize = P2ROUNDUP_TYPED(filesize, PAGESIZE,
130*0Sstevel@tonic-gate 				    u_offset_t);
131*0Sstevel@tonic-gate 				if ((u_offset_t)segsize > filesize)
132*0Sstevel@tonic-gate 					segsize = filesize;
133*0Sstevel@tonic-gate 			}
134*0Sstevel@tonic-gate 			size += segsize;
135*0Sstevel@tonic-gate 		}
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	return (size);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * Yield the memory claim requirement for an address space.
143*0Sstevel@tonic-gate  *
144*0Sstevel@tonic-gate  * This is currently implemented as the number of active hardware
145*0Sstevel@tonic-gate  * translations that have page structures.  Therefore, it can
146*0Sstevel@tonic-gate  * underestimate the traditional resident set size, eg, if the
147*0Sstevel@tonic-gate  * physical page is present and the hardware translation is missing;
148*0Sstevel@tonic-gate  * and it can overestimate the rss, eg, if there are active
149*0Sstevel@tonic-gate  * translations to a frame buffer with page structs.
150*0Sstevel@tonic-gate  * Also, it does not take sharing and XHATs into account.
151*0Sstevel@tonic-gate  */
152*0Sstevel@tonic-gate size_t
153*0Sstevel@tonic-gate rm_asrss(as)
154*0Sstevel@tonic-gate 	register struct as *as;
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	if (as != (struct as *)NULL && as != &kas)
157*0Sstevel@tonic-gate 		return ((size_t)btop(hat_get_mapped_size(as->a_hat)));
158*0Sstevel@tonic-gate 	else
159*0Sstevel@tonic-gate 		return (0);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate  * Return a 16-bit binary fraction representing the percent of total memory
164*0Sstevel@tonic-gate  * used by this address space.  Binary point is to right of high-order bit.
165*0Sstevel@tonic-gate  * Defined as the ratio of a_rss for the process to total physical memory.
166*0Sstevel@tonic-gate  * This assumes 2s-complement arithmetic and that shorts and longs are
167*0Sstevel@tonic-gate  * 16 bits and 32 bits, respectively.
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate ushort_t
170*0Sstevel@tonic-gate rm_pctmemory(struct as *as)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	/* This can't overflow */
173*0Sstevel@tonic-gate 	ulong_t num = (ulong_t)rm_asrss(as) << (PAGESHIFT-1);
174*0Sstevel@tonic-gate 	int shift = 16 - PAGESHIFT;
175*0Sstevel@tonic-gate 	ulong_t total = total_pages;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (shift < 0) {
178*0Sstevel@tonic-gate 		num >>= (-shift);
179*0Sstevel@tonic-gate 		shift = 0;
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 	while (shift > 0 && (num & 0x80000000) == 0) {
182*0Sstevel@tonic-gate 		shift--;
183*0Sstevel@tonic-gate 		num <<= 1;
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate 	if (shift > 0)
186*0Sstevel@tonic-gate 		total >>= shift;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	return (num / total);
189*0Sstevel@tonic-gate }
190