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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 /* 30 * University Copyright- Copyright (c) 1982, 1986, 1988 31 * The Regents of the University of California 32 * All Rights Reserved 33 * 34 * University Acknowledgment- Portions of this document are derived from 35 * software developed by the University of California, Berkeley, and its 36 * contributors. 37 */ 38 39 #pragma ident "%Z%%M% %I% %E% SMI" 40 41 #include <sys/types.h> 42 #include <sys/t_lock.h> 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/mman.h> 46 #include <sys/sysmacros.h> 47 #include <sys/errno.h> 48 #include <sys/signal.h> 49 #include <sys/user.h> 50 #include <sys/proc.h> 51 #include <sys/cmn_err.h> 52 #include <sys/debug.h> 53 54 #include <vm/hat.h> 55 #include <vm/as.h> 56 #include <vm/seg_vn.h> 57 #include <vm/rm.h> 58 #include <vm/seg.h> 59 #include <vm/page.h> 60 61 /* 62 * Yield the size of an address space. 63 * 64 * The size can only be used as a hint since we cannot guarantee it 65 * will stay the same size unless the as->a_lock is held by the caller. 66 */ 67 size_t 68 rm_assize(struct as *as) 69 { 70 size_t size = 0; 71 struct seg *seg; 72 struct segvn_data *svd; 73 extern struct seg_ops segdev_ops; /* needs a header file */ 74 75 ASSERT(as != NULL && AS_READ_HELD(as, &as->a_lock)); 76 77 if (as == &kas) 78 return (0); 79 80 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 81 if (seg->s_ops == &segdev_ops && 82 ((SEGOP_GETTYPE(seg, seg->s_base) & 83 (MAP_SHARED | MAP_PRIVATE)) == 0)) { 84 /* 85 * Don't include mappings of /dev/null. These just 86 * reserve address space ranges and have no memory. 87 * We cheat by knowing that these segments come 88 * from segdev and have no mapping type. 89 */ 90 /* EMPTY */; 91 } else if (seg->s_ops == &segvn_ops && 92 (svd = (struct segvn_data *)seg->s_data) != NULL && 93 (svd->vp == NULL || svd->vp->v_type != VREG) && 94 (svd->flags & MAP_NORESERVE)) { 95 /* 96 * Don't include MAP_NORESERVE pages in the 97 * address range unless their mappings have 98 * actually materialized. We cheat by knowing 99 * that segvn is the only segment driver that 100 * supports MAP_NORESERVE and that the actual 101 * number of bytes reserved is in the segment's 102 * private data structure. 103 */ 104 size += svd->swresv; 105 } else { 106 caddr_t addr = seg->s_base; 107 size_t segsize = seg->s_size; 108 vnode_t *vp; 109 vattr_t vattr; 110 111 /* 112 * If the segment is mapped beyond the end of the 113 * underlying mapped file, if any, then limit the 114 * segment's size contribution to the file size. 115 */ 116 vattr.va_mask = AT_SIZE; 117 if (seg->s_ops == &segvn_ops && 118 SEGOP_GETVP(seg, addr, &vp) == 0 && 119 vp != NULL && vp->v_type == VREG && 120 VOP_GETATTR(vp, &vattr, ATTR_HINT, 121 CRED(), NULL) == 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