xref: /titanic_50/usr/src/uts/common/vm/vm_rm.c (revision 6dfee4834394825da35b977ca71cdc965bc7b6a4)
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 2004 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/t_lock.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mman.h>
47 #include <sys/sysmacros.h>
48 #include <sys/errno.h>
49 #include <sys/signal.h>
50 #include <sys/user.h>
51 #include <sys/proc.h>
52 #include <sys/cmn_err.h>
53 #include <sys/debug.h>
54 
55 #include <vm/hat.h>
56 #include <vm/as.h>
57 #include <vm/seg_vn.h>
58 #include <vm/rm.h>
59 #include <vm/seg.h>
60 #include <vm/page.h>
61 
62 /*
63  * Yield the size of an address space.
64  *
65  * The size can only be used as a hint since we cannot guarantee it
66  * will stay the same size unless the as->a_lock is held by the caller.
67  */
68 size_t
69 rm_assize(struct as *as)
70 {
71 	size_t size = 0;
72 	struct seg *seg;
73 	struct segvn_data *svd;
74 	extern struct seg_ops segdev_ops;	/* needs a header file */
75 
76 	ASSERT(as != NULL && AS_READ_HELD(as, &as->a_lock));
77 
78 	if (as == &kas)
79 		return (0);
80 
81 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
82 		if (seg->s_ops == &segdev_ops &&
83 		    ((SEGOP_GETTYPE(seg, seg->s_base) &
84 		    (MAP_SHARED | MAP_PRIVATE)) == 0)) {
85 			/*
86 			 * Don't include mappings of /dev/null.  These just
87 			 * reserve address space ranges and have no memory.
88 			 * We cheat by knowing that these segments come
89 			 * from segdev and have no mapping type.
90 			 */
91 			/* EMPTY */;
92 		} else if (seg->s_ops == &segvn_ops &&
93 		    (svd = (struct segvn_data *)seg->s_data) != NULL &&
94 		    (svd->vp == NULL || svd->vp->v_type != VREG) &&
95 		    (svd->flags & MAP_NORESERVE)) {
96 			/*
97 			 * Don't include MAP_NORESERVE pages in the
98 			 * address range unless their mappings have
99 			 * actually materialized.  We cheat by knowing
100 			 * that segvn is the only segment driver that
101 			 * supports MAP_NORESERVE and that the actual
102 			 * number of bytes reserved is in the segment's
103 			 * private data structure.
104 			 */
105 			size += svd->swresv;
106 		} else {
107 			caddr_t addr = seg->s_base;
108 			size_t segsize = seg->s_size;
109 			vnode_t *vp;
110 			vattr_t vattr;
111 
112 			/*
113 			 * If the segment is mapped beyond the end of the
114 			 * underlying mapped file, if any, then limit the
115 			 * segment's size contribution to the file size.
116 			 */
117 			vattr.va_mask = AT_SIZE;
118 			if (seg->s_ops == &segvn_ops &&
119 			    SEGOP_GETVP(seg, addr, &vp) == 0 &&
120 			    vp != NULL && vp->v_type == VREG &&
121 			    VOP_GETATTR(vp, &vattr, ATTR_HINT, CRED()) == 0) {
122 				u_offset_t filesize = vattr.va_size;
123 				u_offset_t offset = SEGOP_GETOFFSET(seg, addr);
124 
125 				if (filesize < offset)
126 					filesize = 0;
127 				else
128 					filesize -= offset;
129 				filesize = P2ROUNDUP_TYPED(filesize, PAGESIZE,
130 				    u_offset_t);
131 				if ((u_offset_t)segsize > filesize)
132 					segsize = filesize;
133 			}
134 			size += segsize;
135 		}
136 	}
137 
138 	return (size);
139 }
140 
141 /*
142  * Yield the memory claim requirement for an address space.
143  *
144  * This is currently implemented as the number of active hardware
145  * translations that have page structures.  Therefore, it can
146  * underestimate the traditional resident set size, eg, if the
147  * physical page is present and the hardware translation is missing;
148  * and it can overestimate the rss, eg, if there are active
149  * translations to a frame buffer with page structs.
150  * Also, it does not take sharing and XHATs into account.
151  */
152 size_t
153 rm_asrss(as)
154 	register struct as *as;
155 {
156 	if (as != (struct as *)NULL && as != &kas)
157 		return ((size_t)btop(hat_get_mapped_size(as->a_hat)));
158 	else
159 		return (0);
160 }
161 
162 /*
163  * Return a 16-bit binary fraction representing the percent of total memory
164  * used by this address space.  Binary point is to right of high-order bit.
165  * Defined as the ratio of a_rss for the process to total physical memory.
166  * This assumes 2s-complement arithmetic and that shorts and longs are
167  * 16 bits and 32 bits, respectively.
168  */
169 ushort_t
170 rm_pctmemory(struct as *as)
171 {
172 	/* This can't overflow */
173 	ulong_t num = (ulong_t)rm_asrss(as) << (PAGESHIFT-1);
174 	int shift = 16 - PAGESHIFT;
175 	ulong_t total = total_pages;
176 
177 	if (shift < 0) {
178 		num >>= (-shift);
179 		shift = 0;
180 	}
181 	while (shift > 0 && (num & 0x80000000) == 0) {
182 		shift--;
183 		num <<= 1;
184 	}
185 	if (shift > 0)
186 		total >>= shift;
187 
188 	return (num / total);
189 }
190