2 * Copyright (c) 2000 Dag-Erling Coïdan Smørgrav
3 * Copyright (c) 1999 Pierre Beyssac
4 * Copyright (c) 1993 Jan-Simon Pendry
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * @(#)procfs_status.c 8.4 (Berkeley) 6/15/94
41 * $FreeBSD: src/sys/i386/linux/linprocfs/linprocfs_misc.c,v 1.3.2.8 2001/06/25 19:46:47 pirzyk Exp $
42 * $DragonFly: src/sys/emulation/linux/i386/linprocfs/linprocfs_misc.c,v 1.16 2007/01/06 19:37:20 dillon Exp $
45 #include <sys/param.h>
46 #include <sys/blist.h>
47 #include <sys/kernel.h>
48 #include <sys/kinfo.h>
51 #include <sys/resourcevar.h>
52 #include <sys/systm.h>
54 #include <sys/vnode.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_param.h>
61 #include <vm/vm_object.h>
62 #include <vm/swap_pager.h>
63 #include <sys/vmmeter.h>
66 #include <machine/clock.h>
67 #include <machine/cputypes.h>
68 #include <machine/inttypes.h>
69 #include <machine/md_var.h>
71 #include "linprocfs.h"
74 * Various conversion macros
76 #define T2J(x) (((x) * 100) / (stathz ? stathz : hz)) /* ticks to jiffies */
77 #define T2S(x) ((x) / (stathz ? stathz : hz)) /* ticks to seconds */
78 #define B2K(x) ((unsigned long)((x) >> 10)) /* bytes to kbytes */
79 #define P2B(x) ((x) << PAGE_SHIFT) /* pages to bytes */
80 #define P2K(x) ((x) << (PAGE_SHIFT - 10)) /* pages to kbytes */
83 linprocfs_domeminfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
87 char psbuf[512]; /* XXX - conservative */
88 unsigned long memtotal; /* total memory in bytes */
89 unsigned long memused; /* used memory in bytes */
90 unsigned long memfree; /* free memory in bytes */
91 unsigned long memshared; /* shared memory ??? */
92 unsigned long buffers, cached; /* buffer / cache memory ??? */
93 unsigned long long swaptotal; /* total swap space in bytes */
94 unsigned long long swapused; /* used swap space in bytes */
95 unsigned long long swapfree; /* free swap space in bytes */
98 if (uio->uio_rw != UIO_READ)
101 memtotal = Maxmem * PAGE_SIZE;
103 * The correct thing here would be:
105 memfree = vmstats.v_free_count * PAGE_SIZE;
106 memused = memtotal - memfree;
108 * but it might mislead linux binaries into thinking there
109 * is very little memory left, so we cheat and tell them that
110 * all memory that isn't wired down is free.
112 memused = vmstats.v_wire_count * PAGE_SIZE;
113 memfree = memtotal - memused;
114 if (swapblist == NULL) {
118 swaptotal = swapblist->bl_blocks * 1024LL; /* XXX why 1024? */
119 swapfree = (unsigned long long)swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
121 swapused = swaptotal - swapfree;
123 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
124 object = TAILQ_NEXT(object, object_list))
125 if (object->shadow_count > 1)
126 memshared += object->resident_page_count;
127 memshared *= PAGE_SIZE;
129 * We'd love to be able to write:
133 * but bufspace is internal to vfs_bio.c and we don't feel
134 * like unstaticizing it just for linprocfs's sake.
137 cached = vmstats.v_cache_count * PAGE_SIZE;
141 " total: used: free: shared: buffers: cached:\n"
142 "Mem: %lu %lu %lu %lu %lu %lu\n"
143 "Swap: %llu %llu %llu\n"
144 "MemTotal: %9lu kB\n"
146 "MemShared:%9lu kB\n"
149 "SwapTotal:%9lu kB\n"
150 "SwapFree: %9lu kB\n",
151 memtotal, memused, memfree, memshared, buffers, cached,
152 swaptotal, swapused, swapfree,
153 B2K(memtotal), B2K(memfree),
154 B2K(memshared), B2K(buffers), B2K(cached),
155 B2K(swaptotal), B2K(swapfree));
157 return (uiomove_frombuf(psbuf, ps - psbuf, uio));
161 linprocfs_docpuinfo(struct proc *curp, struct proc *p, struct pfsnode *pfs,
165 char psbuf[512]; /* XXX - conservative */
169 extern char *cpu_model; /* Yuck */
171 /* We default the flags to include all non-conflicting flags,
172 and the Intel versions of conflicting flags. Note the space
173 before each name; that is significant, and should be
176 static char *flags[] = {
177 "fpu", "vme", "de", "pse", "tsc",
178 "msr", "pae", "mce", "cx8", "apic",
179 "sep", "sep", "mtrr", "pge", "mca",
180 "cmov", "pat", "pse36", "pn", "b19",
181 "b20", "b21", "mmxext", "mmx", "fxsr",
182 "xmm", "b26", "b27", "b28", "b29",
186 if (uio->uio_rw != UIO_READ)
213 "vendor_id\t: %.20s\n"
217 0, cpu_vendor, class, cpu, cpu_id & 0xf);
222 if (!strcmp(cpu_vendor, "AuthenticAMD") && (class < 6)) {
224 } else if (!strcmp(cpu_vendor, "CyrixInstead")) {
228 for (i = 0; i < 32; i++)
229 if (cpu_feature & (1 << i))
230 ps += ksprintf(ps, " %s", flags[i]);
231 ps += ksprintf(ps, "\n");
234 "cpu MHz\t\t: %d.%02d\n"
235 "bogomips\t: %d.%02d\n",
236 (tsc_freq + 4999) / 1000000,
237 ((tsc_freq + 4999) / 10000) % 100,
238 (tsc_freq + 4999) / 1000000,
239 ((tsc_freq + 4999) / 10000) % 100);
242 return (uiomove_frombuf(psbuf, ps - psbuf, uio));
251 for (i = 0; i < ncpus; ++i) {
252 struct globaldata *gd = globaldata_find(i);
253 count += *(unsigned int *)((char *)&gd->gd_cnt + offset);
259 linprocfs_dostat(struct proc *curp, struct proc *p, struct pfsnode *pfs,
267 "cpu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64"\n"
274 T2J(cpu_time.cp_user),
275 T2J(cpu_time.cp_nice),
276 T2J(cpu_time.cp_sys /*+ cpu_time[CP_INTR]*/),
277 T2J(cpu_time.cp_idle),
278 cpucnt(offsetof(struct vmmeter, v_vnodepgsin)),
279 cpucnt(offsetof(struct vmmeter, v_vnodepgsout)),
280 cpucnt(offsetof(struct vmmeter, v_swappgsin)),
281 cpucnt(offsetof(struct vmmeter, v_swappgsout)),
282 cpucnt(offsetof(struct vmmeter, v_intr)),
283 cpucnt(offsetof(struct vmmeter, v_swtch)),
285 return (uiomove_frombuf(psbuf, ps - psbuf, uio));
289 linprocfs_douptime(struct proc *curp, struct proc *p, struct pfsnode *pfs,
298 ps += ksprintf(ps, "%ld.%02ld %"PRIu64".%02"PRIu64"\n",
299 tv.tv_sec, tv.tv_usec / 10000,
300 T2S(cpu_time.cp_idle), T2J(cpu_time.cp_idle) % 100);
301 return (uiomove_frombuf(psbuf, ps - psbuf, uio));
305 linprocfs_doversion(struct proc *curp, struct proc *p, struct pfsnode *pfs,
311 ps = version; /* XXX not entirely correct */
312 for (xlen = 0; ps[xlen] != '\n'; ++xlen)
315 return (uiomove_frombuf(ps, xlen, uio));
319 linprocfs_doprocstat(struct proc *curp, struct proc *p, struct pfsnode *pfs,
322 char *ps, psbuf[1024];
325 ps += ksprintf(ps, "%d", p->p_pid);
326 #define PS_ADD(name, fmt, arg) ps += ksprintf(ps, " " fmt, arg)
327 PS_ADD("comm", "(%s)", p->p_comm);
328 PS_ADD("statr", "%c", '0'); /* XXX */
329 PS_ADD("ppid", "%d", p->p_pptr ? p->p_pptr->p_pid : 0);
330 PS_ADD("pgrp", "%d", p->p_pgid);
331 PS_ADD("session", "%d", p->p_session->s_sid);
332 PS_ADD("tty", "%d", 0); /* XXX */
333 PS_ADD("tpgid", "%d", 0); /* XXX */
334 PS_ADD("flags", "%u", 0); /* XXX */
335 PS_ADD("minflt", "%u", 0); /* XXX */
336 PS_ADD("cminflt", "%u", 0); /* XXX */
337 PS_ADD("majflt", "%u", 0); /* XXX */
338 PS_ADD("cminflt", "%u", 0); /* XXX */
339 PS_ADD("utime", "%d", 0); /* XXX */
340 PS_ADD("stime", "%d", 0); /* XXX */
341 PS_ADD("cutime", "%d", 0); /* XXX */
342 PS_ADD("cstime", "%d", 0); /* XXX */
343 PS_ADD("counter", "%d", 0); /* XXX */
344 PS_ADD("priority", "%d", 0); /* XXX */
345 PS_ADD("timeout", "%u", 0); /* XXX */
346 PS_ADD("itrealvalue", "%u", 0); /* XXX */
347 PS_ADD("starttime", "%d", 0); /* XXX */
348 PS_ADD("vsize", "%u", 0); /* XXX */
349 PS_ADD("rss", "%u", 0); /* XXX */
350 PS_ADD("rlim", "%u", 0); /* XXX */
351 PS_ADD("startcode", "%u", 0); /* XXX */
352 PS_ADD("endcode", "%u", 0); /* XXX */
353 PS_ADD("startstack", "%u", 0); /* XXX */
354 PS_ADD("kstkesp", "%u", 0); /* XXX */
355 PS_ADD("kstkeip", "%u", 0); /* XXX */
356 PS_ADD("signal", "%d", 0); /* XXX */
357 PS_ADD("blocked", "%d", 0); /* XXX */
358 PS_ADD("sigignore", "%d", 0); /* XXX */
359 PS_ADD("sigcatch", "%d", 0); /* XXX */
360 PS_ADD("wchan", "%u", 0); /* XXX */
362 ps += ksprintf(ps, "\n");
364 return (uiomove_frombuf(psbuf, ps - psbuf, uio));
368 * Map process state to descriptive letter. Note that this does not
369 * quite correspond to what Linux outputs, but it's close enough.
371 static char *state_str[] = {
383 linprocfs_doprocstatus(struct proc *curp, struct proc *p, struct pfsnode *pfs,
386 char *ps, psbuf[1024];
392 if (p->p_stat > sizeof state_str / sizeof *state_str)
393 state = state_str[0];
395 state = state_str[(int)p->p_stat];
397 #define PS_ADD ps += ksprintf
398 PS_ADD(ps, "Name:\t%s\n", p->p_comm); /* XXX escape */
399 PS_ADD(ps, "State:\t%s\n", state);
404 PS_ADD(ps, "Pid:\t%d\n", p->p_pid);
405 PS_ADD(ps, "PPid:\t%d\n", p->p_pptr ? p->p_pptr->p_pid : 0);
406 PS_ADD(ps, "Uid:\t%d %d %d %d\n", p->p_ucred->cr_ruid,
408 p->p_ucred->cr_svuid,
409 /* FreeBSD doesn't have fsuid */
411 PS_ADD(ps, "Gid:\t%d %d %d %d\n", p->p_ucred->cr_rgid,
413 p->p_ucred->cr_svgid,
414 /* FreeBSD doesn't have fsgid */
416 PS_ADD(ps, "Groups:\t");
417 for (i = 0; i < p->p_ucred->cr_ngroups; i++)
418 PS_ADD(ps, "%d ", p->p_ucred->cr_groups[i]);
424 PS_ADD(ps, "VmSize:\t%8lu kB\n", B2K(p->p_vmspace->vm_map.size));
425 PS_ADD(ps, "VmLck:\t%8u kB\n", P2K(0)); /* XXX */
426 /* XXX vm_rssize seems to always be zero, how can this be? */
427 PS_ADD(ps, "VmRss:\t%8u kB\n", P2K(p->p_vmspace->vm_rssize));
428 PS_ADD(ps, "VmData:\t%8u kB\n", P2K(p->p_vmspace->vm_dsize));
429 PS_ADD(ps, "VmStk:\t%8u kB\n", P2K(p->p_vmspace->vm_ssize));
430 PS_ADD(ps, "VmExe:\t%8u kB\n", P2K(p->p_vmspace->vm_tsize));
431 PS_ADD(ps, "VmLib:\t%8u kB\n", P2K(0)); /* XXX */
436 * We support up to 128 signals, while Linux supports 32,
437 * but we only define 32 (the same 32 as Linux, to boot), so
438 * just show the lower 32 bits of each mask. XXX hack.
440 * NB: on certain platforms (Sparc at least) Linux actually
441 * supports 64 signals, but this code is a long way from
442 * running on anything but i386, so ignore that for now.
444 PS_ADD(ps, "SigPnd:\t%08x\n", p->p_siglist.__bits[0]);
445 PS_ADD(ps, "SigBlk:\t%08x\n", 0); /* XXX */
446 PS_ADD(ps, "SigIgn:\t%08x\n", p->p_sigignore.__bits[0]);
447 PS_ADD(ps, "SigCgt:\t%08x\n", p->p_sigcatch.__bits[0]);
450 * Linux also prints the capability masks, but we don't have
451 * capabilities yet, and when we do get them they're likely to
452 * be meaningless to Linux programs, so we lie. XXX
454 PS_ADD(ps, "CapInh:\t%016x\n", 0);
455 PS_ADD(ps, "CapPrm:\t%016x\n", 0);
456 PS_ADD(ps, "CapEff:\t%016x\n", 0);
459 return (uiomove_frombuf(psbuf, ps - psbuf, uio));
463 linprocfs_doloadavg(struct proc *curp, struct proc *p,
464 struct pfsnode *pfs, struct uio *uio)
466 char *ps, psbuf[512];
469 ps += ksprintf(ps, "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
470 (int)(averunnable.ldavg[0] / averunnable.fscale),
471 (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
472 (int)(averunnable.ldavg[1] / averunnable.fscale),
473 (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
474 (int)(averunnable.ldavg[2] / averunnable.fscale),
475 (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
476 1, /* number of running tasks */
477 -1, /* number of tasks */
478 1 /* The last pid, just kidding */
480 return(uiomove_frombuf(psbuf, ps - psbuf, uio));