10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*5331Samw * Common Development and Distribution License (the "License"). 6*5331Samw * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*5331Samw * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/t_lock.h> 430Sstevel@tonic-gate #include <sys/param.h> 440Sstevel@tonic-gate #include <sys/systm.h> 450Sstevel@tonic-gate #include <sys/mman.h> 460Sstevel@tonic-gate #include <sys/sysmacros.h> 470Sstevel@tonic-gate #include <sys/errno.h> 480Sstevel@tonic-gate #include <sys/signal.h> 490Sstevel@tonic-gate #include <sys/user.h> 500Sstevel@tonic-gate #include <sys/proc.h> 510Sstevel@tonic-gate #include <sys/cmn_err.h> 520Sstevel@tonic-gate #include <sys/debug.h> 530Sstevel@tonic-gate 540Sstevel@tonic-gate #include <vm/hat.h> 550Sstevel@tonic-gate #include <vm/as.h> 560Sstevel@tonic-gate #include <vm/seg_vn.h> 570Sstevel@tonic-gate #include <vm/rm.h> 580Sstevel@tonic-gate #include <vm/seg.h> 590Sstevel@tonic-gate #include <vm/page.h> 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* 620Sstevel@tonic-gate * Yield the size of an address space. 630Sstevel@tonic-gate * 640Sstevel@tonic-gate * The size can only be used as a hint since we cannot guarantee it 650Sstevel@tonic-gate * will stay the same size unless the as->a_lock is held by the caller. 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate size_t 680Sstevel@tonic-gate rm_assize(struct as *as) 690Sstevel@tonic-gate { 700Sstevel@tonic-gate size_t size = 0; 710Sstevel@tonic-gate struct seg *seg; 720Sstevel@tonic-gate struct segvn_data *svd; 730Sstevel@tonic-gate extern struct seg_ops segdev_ops; /* needs a header file */ 740Sstevel@tonic-gate 750Sstevel@tonic-gate ASSERT(as != NULL && AS_READ_HELD(as, &as->a_lock)); 760Sstevel@tonic-gate 770Sstevel@tonic-gate if (as == &kas) 780Sstevel@tonic-gate return (0); 790Sstevel@tonic-gate 800Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 810Sstevel@tonic-gate if (seg->s_ops == &segdev_ops && 820Sstevel@tonic-gate ((SEGOP_GETTYPE(seg, seg->s_base) & 830Sstevel@tonic-gate (MAP_SHARED | MAP_PRIVATE)) == 0)) { 840Sstevel@tonic-gate /* 850Sstevel@tonic-gate * Don't include mappings of /dev/null. These just 860Sstevel@tonic-gate * reserve address space ranges and have no memory. 870Sstevel@tonic-gate * We cheat by knowing that these segments come 880Sstevel@tonic-gate * from segdev and have no mapping type. 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate /* EMPTY */; 910Sstevel@tonic-gate } else if (seg->s_ops == &segvn_ops && 920Sstevel@tonic-gate (svd = (struct segvn_data *)seg->s_data) != NULL && 930Sstevel@tonic-gate (svd->vp == NULL || svd->vp->v_type != VREG) && 940Sstevel@tonic-gate (svd->flags & MAP_NORESERVE)) { 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * Don't include MAP_NORESERVE pages in the 970Sstevel@tonic-gate * address range unless their mappings have 980Sstevel@tonic-gate * actually materialized. We cheat by knowing 990Sstevel@tonic-gate * that segvn is the only segment driver that 1000Sstevel@tonic-gate * supports MAP_NORESERVE and that the actual 1010Sstevel@tonic-gate * number of bytes reserved is in the segment's 1020Sstevel@tonic-gate * private data structure. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate size += svd->swresv; 1050Sstevel@tonic-gate } else { 1060Sstevel@tonic-gate caddr_t addr = seg->s_base; 1070Sstevel@tonic-gate size_t segsize = seg->s_size; 1080Sstevel@tonic-gate vnode_t *vp; 1090Sstevel@tonic-gate vattr_t vattr; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * If the segment is mapped beyond the end of the 1130Sstevel@tonic-gate * underlying mapped file, if any, then limit the 1140Sstevel@tonic-gate * segment's size contribution to the file size. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 1170Sstevel@tonic-gate if (seg->s_ops == &segvn_ops && 1180Sstevel@tonic-gate SEGOP_GETVP(seg, addr, &vp) == 0 && 1190Sstevel@tonic-gate vp != NULL && vp->v_type == VREG && 120*5331Samw VOP_GETATTR(vp, &vattr, ATTR_HINT, 121*5331Samw CRED(), NULL) == 0) { 1220Sstevel@tonic-gate u_offset_t filesize = vattr.va_size; 1230Sstevel@tonic-gate u_offset_t offset = SEGOP_GETOFFSET(seg, addr); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate if (filesize < offset) 1260Sstevel@tonic-gate filesize = 0; 1270Sstevel@tonic-gate else 1280Sstevel@tonic-gate filesize -= offset; 1290Sstevel@tonic-gate filesize = P2ROUNDUP_TYPED(filesize, PAGESIZE, 1300Sstevel@tonic-gate u_offset_t); 1310Sstevel@tonic-gate if ((u_offset_t)segsize > filesize) 1320Sstevel@tonic-gate segsize = filesize; 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate size += segsize; 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate return (size); 1390Sstevel@tonic-gate } 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * Yield the memory claim requirement for an address space. 1430Sstevel@tonic-gate * 1440Sstevel@tonic-gate * This is currently implemented as the number of active hardware 1450Sstevel@tonic-gate * translations that have page structures. Therefore, it can 1460Sstevel@tonic-gate * underestimate the traditional resident set size, eg, if the 1470Sstevel@tonic-gate * physical page is present and the hardware translation is missing; 1480Sstevel@tonic-gate * and it can overestimate the rss, eg, if there are active 1490Sstevel@tonic-gate * translations to a frame buffer with page structs. 1500Sstevel@tonic-gate * Also, it does not take sharing and XHATs into account. 1510Sstevel@tonic-gate */ 1520Sstevel@tonic-gate size_t 1530Sstevel@tonic-gate rm_asrss(as) 1540Sstevel@tonic-gate register struct as *as; 1550Sstevel@tonic-gate { 1560Sstevel@tonic-gate if (as != (struct as *)NULL && as != &kas) 1570Sstevel@tonic-gate return ((size_t)btop(hat_get_mapped_size(as->a_hat))); 1580Sstevel@tonic-gate else 1590Sstevel@tonic-gate return (0); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * Return a 16-bit binary fraction representing the percent of total memory 1640Sstevel@tonic-gate * used by this address space. Binary point is to right of high-order bit. 1650Sstevel@tonic-gate * Defined as the ratio of a_rss for the process to total physical memory. 1660Sstevel@tonic-gate * This assumes 2s-complement arithmetic and that shorts and longs are 1670Sstevel@tonic-gate * 16 bits and 32 bits, respectively. 1680Sstevel@tonic-gate */ 1690Sstevel@tonic-gate ushort_t 1700Sstevel@tonic-gate rm_pctmemory(struct as *as) 1710Sstevel@tonic-gate { 1720Sstevel@tonic-gate /* This can't overflow */ 1730Sstevel@tonic-gate ulong_t num = (ulong_t)rm_asrss(as) << (PAGESHIFT-1); 1740Sstevel@tonic-gate int shift = 16 - PAGESHIFT; 1750Sstevel@tonic-gate ulong_t total = total_pages; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate if (shift < 0) { 1780Sstevel@tonic-gate num >>= (-shift); 1790Sstevel@tonic-gate shift = 0; 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate while (shift > 0 && (num & 0x80000000) == 0) { 1820Sstevel@tonic-gate shift--; 1830Sstevel@tonic-gate num <<= 1; 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate if (shift > 0) 1860Sstevel@tonic-gate total >>= shift; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate return (num / total); 1890Sstevel@tonic-gate } 190