Home | History | Annotate | Line # | Download | only in procfs
procfs_linux.c revision 1.5
      1  1.5    jrf /*      $NetBSD: procfs_linux.c,v 1.5 2003/02/25 21:00:31 jrf Exp $      */
      2  1.1   fvdl 
      3  1.1   fvdl /*
      4  1.1   fvdl  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  1.1   fvdl  * All rights reserved.
      6  1.1   fvdl  *
      7  1.1   fvdl  * Written by Frank van der Linden for Wasabi Systems, Inc.
      8  1.1   fvdl  *
      9  1.1   fvdl  * Redistribution and use in source and binary forms, with or without
     10  1.1   fvdl  * modification, are permitted provided that the following conditions
     11  1.1   fvdl  * are met:
     12  1.1   fvdl  * 1. Redistributions of source code must retain the above copyright
     13  1.1   fvdl  *    notice, this list of conditions and the following disclaimer.
     14  1.1   fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1   fvdl  *    notice, this list of conditions and the following disclaimer in the
     16  1.1   fvdl  *    documentation and/or other materials provided with the distribution.
     17  1.1   fvdl  * 3. All advertising materials mentioning features or use of this software
     18  1.1   fvdl  *    must display the following acknowledgement:
     19  1.1   fvdl  *      This product includes software developed for the NetBSD Project by
     20  1.1   fvdl  *      Wasabi Systems, Inc.
     21  1.1   fvdl  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1   fvdl  *    or promote products derived from this software without specific prior
     23  1.1   fvdl  *    written permission.
     24  1.1   fvdl  *
     25  1.1   fvdl  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1   fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1   fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1   fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1   fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1   fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1   fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1   fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1   fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1   fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1   fvdl  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1   fvdl  */
     37  1.3  lukem 
     38  1.3  lukem #include <sys/cdefs.h>
     39  1.5    jrf __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.5 2003/02/25 21:00:31 jrf Exp $");
     40  1.1   fvdl 
     41  1.1   fvdl #include <sys/param.h>
     42  1.1   fvdl #include <sys/systm.h>
     43  1.1   fvdl #include <sys/time.h>
     44  1.1   fvdl #include <sys/kernel.h>
     45  1.1   fvdl #include <sys/proc.h>
     46  1.1   fvdl #include <sys/vnode.h>
     47  1.1   fvdl 
     48  1.1   fvdl #include <miscfs/procfs/procfs.h>
     49  1.1   fvdl 
     50  1.1   fvdl #include <uvm/uvm_extern.h>
     51  1.1   fvdl 
     52  1.1   fvdl #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
     53  1.1   fvdl #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
     54  1.1   fvdl 
     55  1.1   fvdl /*
     56  1.1   fvdl  * Linux compatible /proc/meminfo. Only active when the -o linux
     57  1.1   fvdl  * mountflag is used.
     58  1.1   fvdl  */
     59  1.1   fvdl int
     60  1.1   fvdl procfs_domeminfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
     61  1.1   fvdl 		 struct uio *uio)
     62  1.1   fvdl {
     63  1.1   fvdl 	char buf[512], *cp;
     64  1.1   fvdl 	int len, error;
     65  1.1   fvdl 
     66  1.1   fvdl 	len = snprintf(buf, sizeof buf,
     67  1.1   fvdl 		"        total:    used:    free:  shared: buffers: cached:\n"
     68  1.1   fvdl 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
     69  1.1   fvdl 		"Swap: %8lu %8lu %8lu\n"
     70  1.1   fvdl 		"MemTotal:  %8lu kB\n"
     71  1.1   fvdl 		"MemFree:   %8lu kB\n"
     72  1.1   fvdl 		"MemShared: %8lu kB\n"
     73  1.1   fvdl 		"Buffers:   %8lu kB\n"
     74  1.1   fvdl 		"Cached:    %8lu kB\n"
     75  1.1   fvdl 		"SwapTotal: %8lu kB\n"
     76  1.1   fvdl 		"SwapFree:  %8lu kB\n",
     77  1.1   fvdl 		PGTOB(uvmexp.npages),
     78  1.1   fvdl 		PGTOB(uvmexp.npages - uvmexp.free),
     79  1.1   fvdl 		PGTOB(uvmexp.free),
     80  1.1   fvdl 		0L,
     81  1.4    chs 		PGTOB(uvmexp.filepages),
     82  1.4    chs 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
     83  1.1   fvdl 		PGTOB(uvmexp.swpages),
     84  1.1   fvdl 		PGTOB(uvmexp.swpginuse),
     85  1.1   fvdl 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
     86  1.1   fvdl 		PGTOKB(uvmexp.npages),
     87  1.1   fvdl 		PGTOKB(uvmexp.free),
     88  1.1   fvdl 		0L,
     89  1.4    chs 		PGTOKB(uvmexp.filepages),
     90  1.4    chs 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
     91  1.1   fvdl 		PGTOKB(uvmexp.swpages),
     92  1.1   fvdl 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
     93  1.1   fvdl 
     94  1.1   fvdl 	if (len == 0)
     95  1.1   fvdl 		return 0;
     96  1.1   fvdl 
     97  1.1   fvdl 	len -= uio->uio_offset;
     98  1.1   fvdl 	cp = buf + uio->uio_offset;
     99  1.1   fvdl 	len = imin(len, uio->uio_resid);
    100  1.1   fvdl 	if (len <= 0)
    101  1.1   fvdl 		error = 0;
    102  1.1   fvdl 	else
    103  1.1   fvdl 		error = uiomove(cp, len, uio);
    104  1.1   fvdl 	return error;
    105  1.1   fvdl }
    106  1.1   fvdl 
    107  1.1   fvdl int
    108  1.1   fvdl procfs_docpuinfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
    109  1.1   fvdl 		 struct uio *uio)
    110  1.1   fvdl {
    111  1.1   fvdl 	char buf[512], *cp;
    112  1.1   fvdl 	int len, error;
    113  1.1   fvdl 
    114  1.1   fvdl 	len = sizeof buf;
    115  1.1   fvdl 	if (procfs_getcpuinfstr(buf, &len) < 0)
    116  1.1   fvdl 		return EIO;
    117  1.5    jrf 
    118  1.5    jrf 	if (len == 0)
    119  1.5    jrf 		return 0;
    120  1.5    jrf 
    121  1.5    jrf 	len -= uio->uio_offset;
    122  1.5    jrf 	cp = buf + uio->uio_offset;
    123  1.5    jrf 	len = imin(len, uio->uio_resid);
    124  1.5    jrf 	if (len <= 0)
    125  1.5    jrf 		error = 0;
    126  1.5    jrf 	else
    127  1.5    jrf 		error = uiomove(cp, len, uio);
    128  1.5    jrf 	return error;
    129  1.5    jrf }
    130  1.5    jrf 
    131  1.5    jrf int
    132  1.5    jrf procfs_douptime(struct proc *curp, struct proc *p, struct pfsnode *pfs,
    133  1.5    jrf 		 struct uio *uio)
    134  1.5    jrf {
    135  1.5    jrf 	char buf[512], *cp;
    136  1.5    jrf 	int len, error;
    137  1.5    jrf 	struct timeval runtime;
    138  1.5    jrf 	u_int64_t idle;
    139  1.5    jrf 
    140  1.5    jrf 	timersub(&curcpu()->ci_schedstate.spc_runtime, &boottime, &runtime);
    141  1.5    jrf 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
    142  1.5    jrf 	len = sprintf(buf, "%lu.%02lu %llu.%02llu\n",
    143  1.5    jrf 		      runtime.tv_sec, runtime.tv_usec / 10000,
    144  1.5    jrf 		      idle / hz, (((idle % hz) * 100) / hz) % 100);
    145  1.1   fvdl 
    146  1.1   fvdl 	if (len == 0)
    147  1.1   fvdl 		return 0;
    148  1.1   fvdl 
    149  1.1   fvdl 	len -= uio->uio_offset;
    150  1.1   fvdl 	cp = buf + uio->uio_offset;
    151  1.1   fvdl 	len = imin(len, uio->uio_resid);
    152  1.1   fvdl 	if (len <= 0)
    153  1.1   fvdl 		error = 0;
    154  1.1   fvdl 	else
    155  1.1   fvdl 		error = uiomove(cp, len, uio);
    156  1.1   fvdl 	return error;
    157  1.1   fvdl }
    158