VMSTAT(8): Port to RTEMS

This commit is contained in:
Sebastian Huber 2016-11-10 13:52:08 +01:00
parent dba2dfc663
commit c4e89a9125
10 changed files with 120 additions and 5 deletions

View File

@ -41,6 +41,12 @@ static char sccsid[] = "@(#)vmstat.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */ #endif /* not lint */
#endif #endif
#ifdef __rtems__
#define __need_getopt_newlib
#include <getopt.h>
#include <machine/rtems-bsd-program.h>
#include <machine/rtems-bsd-commands.h>
#endif /* __rtems__ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
@ -82,6 +88,7 @@ __FBSDID("$FreeBSD$");
#define VMSTAT_XO_VERSION "1" #define VMSTAT_XO_VERSION "1"
#ifndef __rtems__
static char da[] = "da"; static char da[] = "da";
static struct nlist namelist[] = { static struct nlist namelist[] = {
@ -141,6 +148,9 @@ static int Pflag;
static int hflag; static int hflag;
static kvm_t *kd; static kvm_t *kd;
#else /* __rtems__ */
#define kd ((kvm_t *)0)
#endif /* __rtems__ */
#define FORKSTAT 0x01 #define FORKSTAT 0x01
#define INTRSTAT 0x02 #define INTRSTAT 0x02
@ -151,6 +161,7 @@ static kvm_t *kd;
#define ZMEMSTAT 0x40 #define ZMEMSTAT 0x40
#define OBJSTAT 0x80 #define OBJSTAT 0x80
#ifndef __rtems__
static void cpustats(void); static void cpustats(void);
static void pcpustats(int, u_long, int); static void pcpustats(int, u_long, int);
static void devstats(void); static void devstats(void);
@ -160,7 +171,9 @@ static void doobjstat(void);
static void dosum(void); static void dosum(void);
static void dovmstat(unsigned int, int); static void dovmstat(unsigned int, int);
static void domemstat_malloc(void); static void domemstat_malloc(void);
#endif /* __rtems__ */
static void domemstat_zone(void); static void domemstat_zone(void);
#ifndef __rtems__
static void kread(int, void *, size_t); static void kread(int, void *, size_t);
static void kreado(int, void *, size_t, size_t); static void kreado(int, void *, size_t, size_t);
static char *kgetstr(const char *); static char *kgetstr(const char *);
@ -168,13 +181,30 @@ static void needhdr(int);
static void needresize(int); static void needresize(int);
static void doresize(void); static void doresize(void);
static void printhdr(int, u_long); static void printhdr(int, u_long);
#endif /* __rtems__ */
static void usage(void); static void usage(void);
#ifndef __rtems__
static long pct(long, long); static long pct(long, long);
static long long getuptime(void); static long long getuptime(void);
static char **getdrivedata(char **); static char **getdrivedata(char **);
#endif /* __rtems__ */
#ifdef __rtems__
static int main(int , char *argv[]);
int
rtems_bsd_command_vmstat(int argc, char *argv[])
{
int exit_code;
rtems_bsd_program_lock();
exit_code = rtems_bsd_program_call_main("ping", main, argc, argv);
rtems_bsd_program_unlock();
return (exit_code);
}
#endif /* __rtems__ */
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -182,13 +212,28 @@ main(int argc, char *argv[])
unsigned int interval; unsigned int interval;
float f; float f;
int reps; int reps;
#ifndef __rtems__
char *memf, *nlistf; char *memf, *nlistf;
char errbuf[_POSIX2_LINE_MAX]; char errbuf[_POSIX2_LINE_MAX];
#endif /* __rtems__ */
#ifdef __rtems__
struct getopt_data getopt_data;
memset(&getopt_data, 0, sizeof(getopt_data));
#define optind getopt_data.optind
#define optarg getopt_data.optarg
#define opterr getopt_data.opterr
#define optopt getopt_data.optopt
#define getopt(argc, argv, opt) getopt_r(argc, argv, "+" opt, &getopt_data)
#endif /* __rtems__ */
#ifndef __rtems__
memf = nlistf = NULL; memf = nlistf = NULL;
#endif /* __rtems__ */
interval = reps = todo = 0; interval = reps = todo = 0;
#ifndef __rtems__
maxshowdevs = 2; maxshowdevs = 2;
hflag = isatty(1); hflag = isatty(1);
#endif /* __rtems__ */
argc = xo_parse_args(argc, argv); argc = xo_parse_args(argc, argv);
if (argc < 0) if (argc < 0)
@ -196,6 +241,7 @@ main(int argc, char *argv[])
while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:stw:z")) != -1) { while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:stw:z")) != -1) {
switch (c) { switch (c) {
#ifndef __rtems__
case 'a': case 'a':
aflag++; aflag++;
break; break;
@ -255,6 +301,7 @@ main(int argc, char *argv[])
f = atof(optarg); f = atof(optarg);
interval = f * 1000; interval = f * 1000;
break; break;
#endif /* __rtems__ */
case 'z': case 'z':
todo |= ZMEMSTAT; todo |= ZMEMSTAT;
break; break;
@ -270,6 +317,7 @@ main(int argc, char *argv[])
if (todo == 0) if (todo == 0)
todo = VMSTAT; todo = VMSTAT;
#ifndef __rtems__
if (memf != NULL) { if (memf != NULL) {
kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
if (kd == NULL) if (kd == NULL)
@ -331,6 +379,7 @@ retry_nlist:
argv = getdrivedata(argv); argv = getdrivedata(argv);
} }
#endif /* __rtems__ */
if (*argv) { if (*argv) {
f = atof(*argv); f = atof(*argv);
@ -345,12 +394,15 @@ retry_nlist:
} else if (reps) } else if (reps)
interval = 1 * 1000; interval = 1 * 1000;
#ifndef __rtems__
if (todo & FORKSTAT) if (todo & FORKSTAT)
doforkst(); doforkst();
if (todo & MEMSTAT) if (todo & MEMSTAT)
domemstat_malloc(); domemstat_malloc();
#endif /* __rtems__ */
if (todo & ZMEMSTAT) if (todo & ZMEMSTAT)
domemstat_zone(); domemstat_zone();
#ifndef __rtems__
if (todo & SUMSTAT) if (todo & SUMSTAT)
dosum(); dosum();
if (todo & OBJSTAT) if (todo & OBJSTAT)
@ -363,10 +415,12 @@ retry_nlist:
dointr(interval, reps); dointr(interval, reps);
if (todo & VMSTAT) if (todo & VMSTAT)
dovmstat(interval, reps); dovmstat(interval, reps);
#endif /* __rtems__ */
xo_finish(); xo_finish();
exit(0); exit(0);
} }
#ifndef __rtems__
static int static int
mysysctl(const char *name, void *oldp, size_t *oldlenp, mysysctl(const char *name, void *oldp, size_t *oldlenp,
void *newp, size_t newlen) void *newp, size_t newlen)
@ -1487,6 +1541,7 @@ domemstat_malloc(void)
xo_close_container("malloc-statistics"); xo_close_container("malloc-statistics");
memstat_mtl_free(mtlp); memstat_mtl_free(mtlp);
} }
#endif /* __rtems__ */
static void static void
domemstat_zone(void) domemstat_zone(void)
@ -1494,7 +1549,9 @@ domemstat_zone(void)
struct memory_type_list *mtlp; struct memory_type_list *mtlp;
struct memory_type *mtp; struct memory_type *mtp;
char name[MEMTYPE_MAXNAME + 1]; char name[MEMTYPE_MAXNAME + 1];
#ifndef __rtems__
int error; int error;
#endif /* __rtems__ */
mtlp = memstat_mtl_alloc(); mtlp = memstat_mtl_alloc();
if (mtlp == NULL) { if (mtlp == NULL) {
@ -1508,6 +1565,7 @@ domemstat_zone(void)
return; return;
} }
} else { } else {
#ifndef __rtems__
if (memstat_kvm_uma(mtlp, kd) < 0) { if (memstat_kvm_uma(mtlp, kd) < 0) {
error = memstat_mtl_geterror(mtlp); error = memstat_mtl_geterror(mtlp);
if (error == MEMSTAT_ERROR_KVM) if (error == MEMSTAT_ERROR_KVM)
@ -1517,6 +1575,10 @@ domemstat_zone(void)
xo_warnx("memstat_kvm_uma: %s", xo_warnx("memstat_kvm_uma: %s",
memstat_strerror(error)); memstat_strerror(error));
} }
#else /* __rtems__ */
xo_warn("memstat_kvm_uma");
return;
#endif /* __rtems__ */
} }
xo_open_container("memory-zone-statistics"); xo_open_container("memory-zone-statistics");
xo_emit("{T:/%-20s} {T:/%6s} {T:/%6s} {T:/%8s} {T:/%8s} {T:/%8s} " xo_emit("{T:/%-20s} {T:/%6s} {T:/%6s} {T:/%8s} {T:/%8s} {T:/%8s} "
@ -1545,6 +1607,7 @@ domemstat_zone(void)
xo_emit("\n"); xo_emit("\n");
} }
#ifndef __rtems__
static void static void
display_object(struct kinfo_vmobject *kvo) display_object(struct kinfo_vmobject *kvo)
{ {
@ -1719,6 +1782,7 @@ kgetstr(const char *strp)
} while (ret[n++] != '\0'); } while (ret[n++] != '\0');
return (ret); return (ret);
} }
#endif /* __rtems__ */
static void static void
usage(void) usage(void)

View File

@ -77,6 +77,7 @@ def rtems(mm):
'rtems/rtems-bsd-shell-route.c', 'rtems/rtems-bsd-shell-route.c',
'rtems/rtems-bsd-shell-sysctl.c', 'rtems/rtems-bsd-shell-sysctl.c',
'rtems/rtems-bsd-shell-tcpdump.c', 'rtems/rtems-bsd-shell-tcpdump.c',
'rtems/rtems-bsd-shell-vmstat.c',
'rtems/rtems-bsd-syscall-api.c', 'rtems/rtems-bsd-syscall-api.c',
'rtems/rtems-kernel-assert.c', 'rtems/rtems-kernel-assert.c',
'rtems/rtems-kernel-autoconf.c', 'rtems/rtems-kernel-autoconf.c',
@ -2216,6 +2217,7 @@ def user_space(mm):
'usr.bin/netstat/pfkey.c', 'usr.bin/netstat/pfkey.c',
'usr.bin/netstat/sctp.c', 'usr.bin/netstat/sctp.c',
'usr.bin/netstat/unix.c', 'usr.bin/netstat/unix.c',
'usr.bin/vmstat/vmstat.c',
], ],
mm.generator['source'](['-DINET6', '-DINET']) mm.generator['source'](['-DINET6', '-DINET'])
) )

View File

@ -446,7 +446,8 @@ def build(bld):
'freebsd/usr.bin/netstat/pfkey.c', 'freebsd/usr.bin/netstat/pfkey.c',
'freebsd/usr.bin/netstat/route.c', 'freebsd/usr.bin/netstat/route.c',
'freebsd/usr.bin/netstat/sctp.c', 'freebsd/usr.bin/netstat/sctp.c',
'freebsd/usr.bin/netstat/unix.c'] 'freebsd/usr.bin/netstat/unix.c',
'freebsd/usr.bin/vmstat/vmstat.c']
bld.objects(target = "objs01", bld.objects(target = "objs01",
features = "c", features = "c",
cflags = cflags, cflags = cflags,
@ -1105,6 +1106,7 @@ def build(bld):
'rtemsbsd/rtems/rtems-bsd-shell-route.c', 'rtemsbsd/rtems/rtems-bsd-shell-route.c',
'rtemsbsd/rtems/rtems-bsd-shell-sysctl.c', 'rtemsbsd/rtems/rtems-bsd-shell-sysctl.c',
'rtemsbsd/rtems/rtems-bsd-shell-tcpdump.c', 'rtemsbsd/rtems/rtems-bsd-shell-tcpdump.c',
'rtemsbsd/rtems/rtems-bsd-shell-vmstat.c',
'rtemsbsd/rtems/rtems-bsd-shell.c', 'rtemsbsd/rtems/rtems-bsd-shell.c',
'rtemsbsd/rtems/rtems-bsd-syscall-api.c', 'rtemsbsd/rtems/rtems-bsd-syscall-api.c',
'rtemsbsd/rtems/rtems-kernel-assert.c', 'rtemsbsd/rtems/rtems-kernel-assert.c',

View File

View File

@ -7,7 +7,7 @@
*/ */
/* /*
* Copyright (c) 2013-2014 embedded brains GmbH. All rights reserved. * Copyright (c) 2013, 2016 embedded brains GmbH. All rights reserved.
* *
* embedded brains GmbH * embedded brains GmbH
* Dornierstr. 4 * Dornierstr. 4
@ -64,6 +64,8 @@ int rtems_bsd_command_tcpdump(int argc, char **argv);
int rtems_bsd_command_sysctl(int argc, char **argv); int rtems_bsd_command_sysctl(int argc, char **argv);
int rtems_bsd_command_vmstat(int argc, char **argv);
__END_DECLS __END_DECLS
#endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_COMMANDS_H_ */ #endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_COMMANDS_H_ */

View File

@ -41,6 +41,8 @@ extern rtems_shell_cmd_t rtems_shell_TCPDUMP_Command;
extern rtems_shell_cmd_t rtems_shell_SYSCTL_Command; extern rtems_shell_cmd_t rtems_shell_SYSCTL_Command;
extern rtems_shell_cmd_t rtems_shell_VMSTAT_Command;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2016 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <rtems/netcmds-config.h>
#include <machine/rtems-bsd-commands.h>
rtems_shell_cmd_t rtems_shell_VMSTAT_Command = {
.name = "vmstat",
.usage = "vmstat [args]",
.topic = "net",
.command = rtems_bsd_command_vmstat
};

View File

@ -364,7 +364,8 @@ Init(rtems_task_argument arg)
&rtems_shell_NETSTAT_Command, \ &rtems_shell_NETSTAT_Command, \
&rtems_shell_IFCONFIG_Command, \ &rtems_shell_IFCONFIG_Command, \
&rtems_shell_TCPDUMP_Command, \ &rtems_shell_TCPDUMP_Command, \
&rtems_shell_SYSCTL_Command &rtems_shell_SYSCTL_Command, \
&rtems_shell_VMSTAT_Command
#define CONFIGURE_SHELL_COMMAND_CPUUSE #define CONFIGURE_SHELL_COMMAND_CPUUSE
#define CONFIGURE_SHELL_COMMAND_PERIODUSE #define CONFIGURE_SHELL_COMMAND_PERIODUSE

View File

@ -205,7 +205,8 @@ early_initialization(void)
&rtems_shell_ROUTE_Command, \ &rtems_shell_ROUTE_Command, \
&rtems_shell_NETSTAT_Command, \ &rtems_shell_NETSTAT_Command, \
&rtems_shell_SYSCTL_Command, \ &rtems_shell_SYSCTL_Command, \
&rtems_shell_IFCONFIG_Command &rtems_shell_IFCONFIG_Command, \
&rtems_shell_VMSTAT_Command
#define CONFIGURE_SHELL_COMMAND_CPUUSE #define CONFIGURE_SHELL_COMMAND_CPUUSE
#define CONFIGURE_SHELL_COMMAND_PERIODUSE #define CONFIGURE_SHELL_COMMAND_PERIODUSE

View File

@ -15,7 +15,8 @@
&rtems_shell_PING_Command, \ &rtems_shell_PING_Command, \
&rtems_shell_ROUTE_Command, \ &rtems_shell_ROUTE_Command, \
&rtems_shell_NETSTAT_Command, \ &rtems_shell_NETSTAT_Command, \
&rtems_shell_IFCONFIG_Command &rtems_shell_IFCONFIG_Command, \
&rtems_shell_VMSTAT_Command
#include <rtems/shellconfig.h> #include <rtems/shellconfig.h>