xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision 231789093f91686ebe86b5ca109528cb461a173f)
1 /*      $NetBSD: procfs_linux.c,v 1.2 2001/01/18 16:39:43 tv 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/param.h>
39 #include <sys/systm.h>
40 #include <sys/time.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 
45 #include <miscfs/procfs/procfs.h>
46 
47 #include <uvm/uvm_extern.h>
48 
49 #define PGTOB(p)	((unsigned long)(p) << PAGE_SHIFT)
50 #define PGTOKB(p)	((unsigned long)(p) << (PAGE_SHIFT - 10))
51 
52 /*
53  * Linux compatible /proc/meminfo. Only active when the -o linux
54  * mountflag is used.
55  */
56 int
57 procfs_domeminfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
58 		 struct uio *uio)
59 {
60 	char buf[512], *cp;
61 	int len, error;
62 
63 	len = snprintf(buf, sizeof buf,
64 		"        total:    used:    free:  shared: buffers: cached:\n"
65 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
66 		"Swap: %8lu %8lu %8lu\n"
67 		"MemTotal:  %8lu kB\n"
68 		"MemFree:   %8lu kB\n"
69 		"MemShared: %8lu kB\n"
70 		"Buffers:   %8lu kB\n"
71 		"Cached:    %8lu kB\n"
72 		"SwapTotal: %8lu kB\n"
73 		"SwapFree:  %8lu kB\n",
74 		PGTOB(uvmexp.npages),
75 		PGTOB(uvmexp.npages - uvmexp.free),
76 		PGTOB(uvmexp.free),
77 		0L,
78 		PGTOB(uvmexp.vnodepages),
79 		PGTOB(uvmexp.anonpages + uvmexp.vnodepages + uvmexp.vtextpages),
80 		PGTOB(uvmexp.swpages),
81 		PGTOB(uvmexp.swpginuse),
82 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
83 		PGTOKB(uvmexp.npages),
84 		PGTOKB(uvmexp.free),
85 		0L,
86 		PGTOKB(uvmexp.vnodepages),
87 		PGTOKB(uvmexp.anonpages + uvmexp.vnodepages +uvmexp.vtextpages),
88 		PGTOKB(uvmexp.swpages),
89 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
90 
91 	if (len == 0)
92 		return 0;
93 
94 	len -= uio->uio_offset;
95 	cp = buf + uio->uio_offset;
96 	len = imin(len, uio->uio_resid);
97 	if (len <= 0)
98 		error = 0;
99 	else
100 		error = uiomove(cp, len, uio);
101 	return error;
102 }
103 
104 int
105 procfs_docpuinfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
106 		 struct uio *uio)
107 {
108 	char buf[512], *cp;
109 	int len, error;
110 
111 	len = sizeof buf;
112 	if (procfs_getcpuinfstr(buf, &len) < 0)
113 		return EIO;
114 
115 	if (len == 0)
116 		return 0;
117 
118 	len -= uio->uio_offset;
119 	cp = buf + uio->uio_offset;
120 	len = imin(len, uio->uio_resid);
121 	if (len <= 0)
122 		error = 0;
123 	else
124 		error = uiomove(cp, len, uio);
125 	return error;
126 }
127