linprocfs.c revision 65577
1/* 2 * Copyright (c) 2000 Dag-Erling Co�dan Sm�rgrav 3 * Copyright (c) 1999 Pierre Beyssac 4 * Copyright (c) 1993 Jan-Simon Pendry 5 * Copyright (c) 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)procfs_status.c 8.4 (Berkeley) 6/15/94 40 * 41 * $FreeBSD: head/sys/compat/linprocfs/linprocfs.c 65577 2000-09-07 16:44:26Z des $ 42 */ 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/proc.h> 47#include <sys/jail.h> 48#include <sys/vnode.h> 49#include <sys/blist.h> 50#include <sys/tty.h> 51#include <sys/resourcevar.h> 52#include <i386/linux/linprocfs/linprocfs.h> 53 54#include <vm/vm.h> 55#include <vm/pmap.h> 56#include <vm/vm_param.h> 57#include <vm/vm_object.h> 58#include <vm/swap_pager.h> 59#include <sys/vmmeter.h> 60#include <sys/exec.h> 61 62#include <machine/md_var.h> 63#include <machine/cputypes.h> 64 65struct proc; 66 67int 68linprocfs_domeminfo(curp, p, pfs, uio) 69 struct proc *curp; 70 struct proc *p; 71 struct pfsnode *pfs; 72 struct uio *uio; 73{ 74 char *ps; 75 int xlen; 76 int error; 77 char psbuf[512]; /* XXX - conservative */ 78 unsigned long memtotal; /* total memory in bytes */ 79 unsigned long memused; /* used memory in bytes */ 80 unsigned long memfree; /* free memory in bytes */ 81 unsigned long memshared; /* shared memory ??? */ 82 unsigned long buffers, cached; /* buffer / cache memory ??? */ 83 unsigned long swaptotal; /* total swap space in bytes */ 84 unsigned long swapused; /* used swap space in bytes */ 85 unsigned long swapfree; /* free swap space in bytes */ 86 vm_object_t object; 87 88 if (uio->uio_rw != UIO_READ) 89 return (EOPNOTSUPP); 90 91 memtotal = physmem * PAGE_SIZE; 92 /* 93 * The correct thing here would be: 94 * 95 memfree = cnt.v_free_count * PAGE_SIZE; 96 memused = memtotal - memfree; 97 * 98 * but it might mislead linux binaries into thinking there 99 * is very little memory left, so we cheat and tell them that 100 * all memory that isn't wired down is free. 101 */ 102 memused = cnt.v_wire_count * PAGE_SIZE; 103 memfree = memtotal - memused; 104 if (swapblist == NULL) { 105 swaptotal = 0; 106 swapfree = 0; 107 } else { 108 swaptotal = swapblist->bl_blocks * 1024; /* XXX why 1024? */ 109 swapfree = swapblist->bl_root->u.bmu_avail * PAGE_SIZE; 110 } 111 swapused = swaptotal - swapfree; 112 memshared = 0; 113 for (object = TAILQ_FIRST(&vm_object_list); object != NULL; 114 object = TAILQ_NEXT(object, object_list)) 115 if (object->shadow_count > 1) 116 memshared += object->resident_page_count; 117 memshared *= PAGE_SIZE; 118 /* 119 * We'd love to be able to write: 120 * 121 buffers = bufspace; 122 * 123 * but bufspace is internal to vfs_bio.c and we don't feel 124 * like unstaticizing it just for linprocfs's sake. 125 */ 126 buffers = 0; 127 cached = cnt.v_cache_count * PAGE_SIZE; 128 129 ps = psbuf; 130 ps += sprintf(ps, 131 " total: used: free: shared: buffers: cached:\n" 132 "Mem: %lu %lu %lu %lu %lu %lu\n" 133 "Swap: %lu %lu %lu\n" 134 "MemTotal: %9lu kB\n" 135 "MemFree: %9lu kB\n" 136 "MemShared:%9lu kB\n" 137 "Buffers: %9lu kB\n" 138 "Cached: %9lu kB\n" 139 "SwapTotal:%9lu kB\n" 140 "SwapFree: %9lu kB\n", 141 memtotal, memused, memfree, memshared, buffers, cached, 142 swaptotal, swapused, swapfree, 143 memtotal >> 10, memfree >> 10, 144 memshared >> 10, buffers >> 10, cached >> 10, 145 swaptotal >> 10, swapfree >> 10); 146 147 xlen = ps - psbuf; 148 xlen -= uio->uio_offset; 149 ps = psbuf + uio->uio_offset; 150 xlen = imin(xlen, uio->uio_resid); 151 if (xlen <= 0) 152 error = 0; 153 else 154 error = uiomove(ps, xlen, uio); 155 return (error); 156} 157 158int 159linprocfs_docpuinfo(curp, p, pfs, uio) 160 struct proc *curp; 161 struct proc *p; 162 struct pfsnode *pfs; 163 struct uio *uio; 164{ 165 char *ps; 166 int xlen; 167 int error; 168 char psbuf[512]; /* XXX - conservative */ 169 char *class; 170#if 0 171 extern char *cpu_model; /* Yuck */ 172#endif 173 174 if (uio->uio_rw != UIO_READ) 175 return (EOPNOTSUPP); 176 177 switch (cpu_class) { 178 case CPUCLASS_286: 179 class = "286"; 180 break; 181 case CPUCLASS_386: 182 class = "386"; 183 break; 184 case CPUCLASS_486: 185 class = "486"; 186 break; 187 case CPUCLASS_586: 188 class = "586"; 189 break; 190 case CPUCLASS_686: 191 class = "686"; 192 break; 193 default: 194 class = "unknown"; 195 break; 196 } 197 198 ps = psbuf; 199 ps += sprintf(ps, 200 "processor : %d\n" 201 "cpu : %.3s\n" 202 "model : %.20s\n" 203 "vendor_id : %.20s\n" 204 "stepping : %d\n", 205 0, class, "unknown", cpu_vendor, cpu_id); 206 207 xlen = ps - psbuf; 208 xlen -= uio->uio_offset; 209 ps = psbuf + uio->uio_offset; 210 xlen = imin(xlen, uio->uio_resid); 211 if (xlen <= 0) 212 error = 0; 213 else 214 error = uiomove(ps, xlen, uio); 215 return (error); 216} 217