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