Add a dumb, mostly Solaris-compatible psrset command. It's missing some

options. Without it there's no easy way to test or use processor sets.
This commit is contained in:
ad 2008-06-22 13:53:59 +00:00
parent 2d315ce390
commit 44eb18fda8
9 changed files with 532 additions and 8 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.754 2008/06/18 09:06:26 yamt Exp $
# $NetBSD: mi,v 1.755 2008/06/22 13:54:00 ad Exp $
#
# Note: Don't delete entries from here - mark them as "obsolete" instead,
# unless otherwise stated below.
@ -1269,6 +1269,7 @@
./usr/sbin/pppoectl base-obsolete obsolete
./usr/sbin/pppstats base-ppp-bin
./usr/sbin/praliases base-obsolete obsolete
./usr/sbin/psrset base-sysutil-bin
./usr/sbin/pstat base-sysutil-bin
./usr/sbin/pvcsif base-netutil-bin
./usr/sbin/pvctxctl base-netutil-bin

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.1163 2008/06/20 14:39:28 christos Exp $
# $NetBSD: mi,v 1.1164 2008/06/22 13:54:00 ad Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@ -3148,6 +3148,7 @@
./usr/libdata/debug/usr/sbin/pppd.debug comp-ppp-debug debug
./usr/libdata/debug/usr/sbin/pppdump.debug comp-ppp-debug debug
./usr/libdata/debug/usr/sbin/pppstats.debug comp-ppp-debug debug
./usr/libdata/debug/usr/sbin/psrset.debug comp-sysutil-debug debug
./usr/libdata/debug/usr/sbin/pstat.debug comp-sysutil-debug debug
./usr/libdata/debug/usr/sbin/pvcsif.debug comp-netutil-debug debug
./usr/libdata/debug/usr/sbin/pvctxctl.debug comp-netutil-debug debug

View File

@ -1,4 +1,4 @@
LIST OF CHANGES FROM LAST RELEASE: <$Revision: 1.1072 $>
LIST OF CHANGES FROM LAST RELEASE: <$Revision: 1.1073 $>
[Note: This file does not mention every change made to the NetBSD source tree.
@ -548,3 +548,4 @@ Changes from NetBSD 4.0 to NetBSD 5.0:
port [christos 20080525]
pf(4): Updated to OpenBSD 4.2 PF [yamt,peter,christos,joerg 20080618]
bind: Import 9.5.0 [christos 20080621]
psrset(8): Utility to control processor sets [ad 20080622]

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.233 2008/05/14 16:49:47 reinoud Exp $
# $NetBSD: Makefile,v 1.234 2008/06/22 13:53:59 ad Exp $
# from: @(#)Makefile 5.20 (Berkeley) 6/12/93
.include <bsd.own.mk>
@ -15,8 +15,8 @@ SUBDIR= ac accton acpitools altq amd apm apmd arp bad144 bind bootp \
mrinfo mrouted mscdlabel mtrace \
mtree ndbootd ndiscvt netgroup_mkdb nfsd ntp ofctl paxctl pcictl \
pkg_install \
pppd pstat pwd_mkdb postinstall powerd quot quotacheck quotaon \
rarpd rbootd rdate \
pppd psrset pstat pwd_mkdb postinstall powerd quot quotacheck \
quotaon rarpd rbootd rdate \
repquota rmt rpc.bootparamd rpc.lockd rpc.pcnfsd \
rpc.statd rpcbind rwhod sa screenblank sdpd services_mkdb sesd \
schedctl sliplogin sntp \

View File

@ -1,4 +1,4 @@
.\" $NetBSD: cpuctl.8,v 1.4 2008/05/05 17:54:14 ad Exp $
.\" $NetBSD: cpuctl.8,v 1.5 2008/06/22 13:53:59 ad Exp $
.\"
.\" Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -77,6 +77,9 @@ Set the specified CPU on line, making it available to run unbound LWPs.
.It Pa /dev/cpuctl
control device
.El
.Sh SEE ALSO
.Xr psrset 8 ,
.Xr schedctl 8
.Sh HISTORY
The
.Nm

10
usr.sbin/psrset/Makefile Normal file
View File

@ -0,0 +1,10 @@
# $NetBSD: Makefile,v 1.1 2008/06/22 13:53:59 ad Exp $
PROG= psrset
MAN= psrset.8
LDADD+= -lrt
WARNS= 4
.include <bsd.prog.mk>

112
usr.sbin/psrset/psrset.8 Normal file
View File

@ -0,0 +1,112 @@
.\" $NetBSD: psrset.8,v 1.1 2008/06/22 13:53:59 ad Exp $
.\"
.\" Copyright (c) 2008 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
.\"
.Dd June 22, 2008
.Dt PSRSET 8
.Os
.Sh NAME
.Nm psrset
.Nd control CPU sets
.Sh SYNOPSIS
.Nm
.Op Ar setid ...
.Nm
.Fl a Ar setid Ar cpuid Ar ...
.Nm
.Fl b Ar setid Ar pid Ar ...
.Nm
.Fl c Op Ar cpuid Ar ...
.Nm
.Fl d Ar setid
.Nm
.Fl e Ar setid Ar command
.Nm
.Fl i Op Ar setid ...
.Nm
.Fl p
.Nm
.Fl r Ar cpuid Ar ...
.Nm
.Fl u Ar pid Ar ...
.Sh DESCRIPTION
The
.Nm
command can used to control and inspect CPU sets.
.Pp
The system always contains at least one CPU set: the default
set.
The default set must contain at least one CPU at all times.
.Pp
Available options:
.Bl -tag -width indent
.It Fl a
Assign one or more CPUs to the set
.Ar setid .
.It Fl b
Bind one or more processes to the set
.Ar setid .
All LWPs within the processes will be affected.
Bindings are inherited when new LWPs or processes are forked.
However, setting a new binding on a parent process does not affect the
bindings of its existing child processes.
.It Fl c
Create a new CPU set.
If successful, the ID of the new set will be printed.
If a list of CPU IDs is provided, those CPUs will be assigned to the set
upon creation.
Otherwise, the set will be created empty.
.It Fl d
Delete the CPU set specified by
.Ar setid .
Any LWPs bound to the set will be re-bound to the default CPU set.
.It Fl e
Execute a command within the CPU set specified by
.Ar setid .
.It Fl i
List all CPU sets.
For each set, print the member CPUs.
If
.Nm
is run without any options, it behaves as if
.Fl i
were given.
.It Fl p
List all CPUs.
For each CPU, print the associated CPU set.
.It Fl r
Remove a CPU from its current set, and return it back to the default CPU
set.
.It Fl u
Bind the specified processes to the system default CPU set.
.El
.Sh SEE ALSO
.Xr cpuctl 8 ,
.Xr schedctl 8
.Sh HISTORY
The
.Nm
command first appeared in
.Nx 5.0 .

395
usr.sbin/psrset/psrset.c Normal file
View File

@ -0,0 +1,395 @@
/* $NetBSD: psrset.c,v 1.1 2008/06/22 13:53:59 ad Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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 <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: psrset.c,v 1.1 2008/06/22 13:53:59 ad Exp $");
#endif
#include <sys/types.h>
#include <sys/pset.h>
#include <sys/sched.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <err.h>
#include <unistd.h>
#include <ctype.h>
static void usage(void);
static int eatopt(char **);
static int cmd_a(char **, int);
static int cmd_b(char **, int);
static int cmd_c(char **, int);
static int cmd_d(char **, int);
static int cmd_e(char **, int);
static int cmd_i(char **, int);
static int cmd_p(char **, int);
static int cmd_r(char **, int);
static int cmd_u(char **, int);
static int cmd__(char **, int);
static psetid_t psid;
static int ncpu;
static cpuset_t *cpuset;
int
main(int argc, char **argv)
{
int (*cmd)(char **, int);
int off;
ncpu = sysconf(_SC_NPROCESSORS_CONF);
cpuset = cpuset_create();
if (cpuset == NULL)
err(EXIT_FAILURE, "cpuset_create");
cpuset_zero(cpuset);
if (argc == 1 || argv[1][0] != '-') {
cmd = cmd_i;
off = 1;
} else if (strncmp(argv[1], "-a", 2) == 0) {
cmd = cmd_a;
off = eatopt(argv);
} else if (strncmp(argv[1], "-b", 2) == 0) {
cmd = cmd_b;
off = eatopt(argv);
} else if (strncmp(argv[1], "-c", 2) == 0) {
cmd = cmd_c;
off = eatopt(argv);
} else if (strncmp(argv[1], "-d", 2) == 0) {
cmd = cmd_d;
off = eatopt(argv);
} else if (strncmp(argv[1], "-e", 2) == 0) {
cmd = cmd_e;
off = eatopt(argv);
} else if (strncmp(argv[1], "-i", 2) == 0) {
cmd = cmd_i;
off = eatopt(argv);
} else if (strncmp(argv[1], "-p", 2) == 0) {
cmd = cmd_p;
off = eatopt(argv);
} else if (strncmp(argv[1], "-r", 2) == 0) {
cmd = cmd_r;
off = eatopt(argv);
} else if (strncmp(argv[1], "-u", 2) == 0) {
cmd = cmd_u;
off = eatopt(argv);
} else {
cmd = cmd__;
off = 0;
}
return (*cmd)(argv + off, argc - off);
}
static int
eatopt(char **argv)
{
if (argv[1][2] != '\0') {
argv[1] += 2;
return 1;
}
return 2;
}
static int
getint(char *p)
{
char *q;
int rv;
rv = (int)strtol(p, &q, 10);
if (q == p || *q != '\0')
usage();
return rv;
}
static void
usage(void)
{
fprintf(stderr, "usage:\n"
"\tpsrset [setid ...]\n"
"\tpsrset -a setid cpuid ...\n"
"\tpsrset -b setid pid ...\n"
"\tpsrset -c [cpuid ...]\n"
"\tpsrset -d setid\n"
"\tpsrset -e setid command\n"
"\tpsrset -i [setid ...]\n"
"\tpsrset -p\n"
"\tpsrset -r cpuid ...\n"
"\tpsrset -u pid ...\n");
exit(EXIT_FAILURE);
}
static void
makecpuset(char **argv)
{
char *p, *q;
int i, j;
if (*argv == NULL) {
for (i = 0; i < ncpu; i++)
cpuset_set(i, cpuset);
return;
}
for (; *argv != NULL; argv++) {
if (!isdigit((unsigned)**argv))
usage();
i = (int)strtol(*argv, &p, 10);
if ((*p != '\0' && *p != '-') || p == *argv)
usage();
if (*p == '-') {
if (!isdigit((unsigned)p[1]))
usage();
j = (int)strtol(p + 1, &q, 10);
if (q == p || *q != '\0')
usage();
} else {
j = i;
}
if ((unsigned int)i >= ncpu) {
errx(EXIT_FAILURE, "value out of range");
}
while (i <= j)
cpuset_set(i++, cpuset);
}
}
/*
* Assign processors to set.
*/
static int
cmd_a(char **argv, int argc)
{
int i;
if (argc < 2)
usage();
psid = getint(argv[0]);
makecpuset(argv + 1);
for (i = 0; i < ncpu; i++) {
if (!cpuset_isset(i, cpuset))
continue;
if (pset_assign(psid, i, NULL))
err(EXIT_FAILURE, "pset_assign");
}
return 0;
}
/*
* Bind LWPs within processes to set.
*/
static int
cmd_b(char **argv, int argc)
{
if (argc < 2)
usage();
psid = getint(*argv);
for (argv++; *argv != NULL; argv++) {
if (pset_bind(psid, P_PID, (idtype_t)getint(*argv), NULL))
err(EXIT_FAILURE, "pset_bind");
}
return 0;
}
/*
* Create set.
*/
static int
cmd_c(char **argv, int argc)
{
int i;
if (pset_create(&psid))
err(EXIT_FAILURE, "pset_create");
printf("%d\n", (int)psid);
if (argc != 0)
makecpuset(argv);
for (i = 0; i < ncpu; i++) {
if (!cpuset_isset(i, cpuset))
continue;
if (pset_assign(psid, i, NULL))
err(EXIT_FAILURE, "pset_assign");
}
return 0;
}
/*
* Destroy set.
*/
static int
cmd_d(char **argv, int argc)
{
if (argc != 1)
usage();
if (pset_destroy(getint(argv[0])))
err(EXIT_FAILURE, "pset_destroy");
return 0;
}
/*
* Execute command in set.
*/
static int
cmd_e(char **argv, int argc)
{
if (argc < 2)
usage();
if (pset_bind(getint(argv[0]), P_PID, getpid(), NULL))
err(EXIT_FAILURE, "pset_bind");
(void)execvp(argv[1], argv + 1);
return 1;
}
/*
* Print info about each set.
*/
static int
cmd_i(char **argv, int argc)
{
char buf[1024];
size_t len;
char *p, *q;
int i, j, k;
len = sizeof(buf);
if (sysctlbyname("kern.pset.list", buf, &len, NULL, 0) == -1)
err(EXIT_FAILURE, "kern.pset.list");
p = buf;
while ((q = strsep(&p, ",")) != NULL) {
i = (int)strtol(q, &q, 10);
if (*q != ':')
errx(EXIT_FAILURE, "bad kern.pset.list");
printf("%s processor set %d: ",
(atoi(q + 1) == 1 ? "system" : "user"), i);
for (j = 0, k = 0; j < ncpu; j++) {
if (pset_assign(PS_QUERY, j, &psid))
err(EXIT_FAILURE, "pset_assign");
if (psid == i) {
if (k++ == 0)
printf("processor(s)");
printf(" %d", j);
}
}
if (k == 0)
printf("empty");
putchar('\n');
}
return 0;
}
/*
* Print set ID for each processor.
*/
static int
cmd_p(char **argv, int argc)
{
int i;
makecpuset(argv);
for (i = 0; i < ncpu; i++) {
if (!cpuset_isset(i, cpuset))
continue;
if (pset_assign(PS_QUERY, i, &psid))
err(EXIT_FAILURE, "ioctl");
printf("processor %d: ", i);
if (psid == PS_NONE)
printf("not assigned\n");
else
printf("%d\n", (int)psid);
}
return 0;
}
/*
* Remove CPU from set and return to system.
*/
static int
cmd_r(char **argv, int argc)
{
int i;
makecpuset(argv);
for (i = 0; i < ncpu; i++) {
if (!cpuset_isset(i, cpuset))
continue;
if (pset_assign(PS_NONE, i, NULL))
err(EXIT_FAILURE, "ioctl");
}
return 0;
}
/*
* Unbind LWPs within process.
*/
static int
cmd_u(char **argv, int argc)
{
if (argc < 1)
usage();
for (; *argv != NULL; argv++) {
if (pset_bind(PS_NONE, P_PID, (idtype_t)getint(*argv), NULL))
err(EXIT_FAILURE, "pset_bind");
}
return 0;
}
/*
* Dummy.
*/
static int
cmd__(char **argv, int argc)
{
usage();
return 0;
}

View File

@ -1,4 +1,4 @@
.\" $NetBSD: schedctl.8,v 1.6 2008/05/25 23:22:16 ad Exp $
.\" $NetBSD: schedctl.8,v 1.7 2008/06/22 13:54:00 ad Exp $
.\"
.\" Copyright (c) 2008 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -120,6 +120,7 @@ command with real-time priority:
.Xr nice 1 ,
.Xr getpriority 2 ,
.Xr setpriority 2 ,
.Xr psrset 8 ,
.Xr renice 8
.Sh HISTORY
The