2014-07-27 08:46:48 +04:00
|
|
|
/* $NetBSD: schedctl.c,v 1.16 2014/07/27 04:46:48 dholland Exp $ */
|
2008-01-15 06:37:10 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org>
|
|
|
|
* 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.
|
|
|
|
*
|
2008-10-18 07:40:18 +04:00
|
|
|
* 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.
|
2008-01-15 06:37:10 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* schedctl(8) - a program to control scheduling of processes and threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#ifndef lint
|
2014-07-27 08:46:48 +04:00
|
|
|
__RCSID("$NetBSD: schedctl.c,v 1.16 2014/07/27 04:46:48 dholland Exp $");
|
2008-01-15 06:37:10 +03:00
|
|
|
#endif
|
|
|
|
|
2014-07-27 08:46:48 +04:00
|
|
|
#include <stdbool.h>
|
2008-01-15 06:37:10 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/pset.h>
|
|
|
|
#include <sys/sched.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
static const char *class_str[] = {
|
|
|
|
"SCHED_OTHER",
|
|
|
|
"SCHED_FIFO",
|
2008-04-28 20:52:17 +04:00
|
|
|
"SCHED_RR",
|
|
|
|
NULL
|
2008-01-15 06:37:10 +03:00
|
|
|
};
|
|
|
|
|
2008-02-09 20:01:51 +03:00
|
|
|
static void sched_set(pid_t, lwpid_t, int, struct sched_param *, cpuset_t *);
|
2008-01-15 06:37:10 +03:00
|
|
|
static void thread_info(pid_t, lwpid_t);
|
|
|
|
static cpuset_t *makecpuset(char *);
|
2014-07-27 08:46:48 +04:00
|
|
|
static void printcpuset(cpuset_t *);
|
2011-08-31 17:32:36 +04:00
|
|
|
__dead static void usage(void);
|
2008-01-15 06:37:10 +03:00
|
|
|
|
2008-01-26 20:52:08 +03:00
|
|
|
static u_int ncpu;
|
|
|
|
|
2008-01-15 06:37:10 +03:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
kvm_t *kd;
|
|
|
|
struct kinfo_lwp *lwp_list, *lwp;
|
|
|
|
struct sched_param *sp;
|
|
|
|
cpuset_t *cpuset;
|
2008-02-09 20:01:51 +03:00
|
|
|
int i, count, ch, policy;
|
2008-01-15 06:37:10 +03:00
|
|
|
pid_t pid;
|
|
|
|
lwpid_t lid;
|
|
|
|
bool set;
|
|
|
|
|
2008-01-26 20:52:08 +03:00
|
|
|
ncpu = sysconf(_SC_NPROCESSORS_CONF);
|
|
|
|
|
2008-01-15 06:37:10 +03:00
|
|
|
pid = lid = 0;
|
|
|
|
cpuset = NULL;
|
|
|
|
set = false;
|
|
|
|
|
2008-03-24 13:22:41 +03:00
|
|
|
sp = calloc(1, sizeof(struct sched_param));
|
2008-01-15 06:37:10 +03:00
|
|
|
if (sp == NULL)
|
2008-03-24 13:22:41 +03:00
|
|
|
err(EXIT_FAILURE, "calloc");
|
2008-01-15 06:37:10 +03:00
|
|
|
|
|
|
|
sp->sched_priority = PRI_NONE;
|
2008-02-09 20:01:51 +03:00
|
|
|
policy = SCHED_NONE;
|
2008-01-15 06:37:10 +03:00
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "A:C:P:p:t:")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
|
|
|
/* PID */
|
|
|
|
pid = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
/* Thread (LWP) ID */
|
|
|
|
lid = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
/* Affinity */
|
|
|
|
cpuset = makecpuset(optarg);
|
|
|
|
if (cpuset == NULL) {
|
|
|
|
fprintf(stderr, "%s: invalid CPU value\n",
|
|
|
|
getprogname());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
/* Scheduling class */
|
2008-04-28 20:52:17 +04:00
|
|
|
for (policy = 0; class_str[policy] != NULL; policy++) {
|
|
|
|
if (strcasecmp(optarg, class_str[policy]) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (class_str[policy] == NULL)
|
|
|
|
policy = atoi(optarg);
|
2008-02-09 20:01:51 +03:00
|
|
|
if (policy < SCHED_OTHER || policy > SCHED_RR) {
|
2008-01-15 06:37:10 +03:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: invalid scheduling class\n",
|
|
|
|
getprogname());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
set = true;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
/* Priority */
|
|
|
|
sp->sched_priority = atoi(optarg);
|
|
|
|
if (sp->sched_priority < sysconf(_SC_SCHED_PRI_MIN) ||
|
|
|
|
sp->sched_priority > sysconf(_SC_SCHED_PRI_MAX)) {
|
|
|
|
fprintf(stderr, "%s: invalid priority\n",
|
|
|
|
getprogname());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
set = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At least PID must be specified */
|
2008-05-26 03:22:16 +04:00
|
|
|
if (pid == 0) {
|
2008-05-29 15:32:07 +04:00
|
|
|
if (argv[optind] == NULL || lid != 0)
|
2008-05-26 03:22:16 +04:00
|
|
|
usage();
|
|
|
|
pid = getpid();
|
|
|
|
} else {
|
2008-05-29 15:32:07 +04:00
|
|
|
if (argv[optind] != NULL)
|
2008-05-26 03:22:16 +04:00
|
|
|
usage();
|
|
|
|
}
|
2008-01-15 06:37:10 +03:00
|
|
|
|
|
|
|
/* Set the scheduling information for thread/process */
|
2008-02-09 20:01:51 +03:00
|
|
|
sched_set(pid, lid, policy, set ? sp : NULL, cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
|
|
|
|
/* Show information about each thread */
|
2008-05-26 03:22:16 +04:00
|
|
|
if (pid != getpid()) {
|
|
|
|
kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
|
|
|
|
if (kd == NULL)
|
|
|
|
err(EXIT_FAILURE, "kvm_open");
|
|
|
|
lwp_list = kvm_getlwps(kd, pid, 0, sizeof(struct kinfo_lwp), &count);
|
|
|
|
if (lwp_list == NULL)
|
|
|
|
err(EXIT_FAILURE, "kvm_getlwps");
|
|
|
|
for (lwp = lwp_list, i = 0; i < count; lwp++, i++) {
|
|
|
|
if (lid && lid != lwp->l_lid)
|
|
|
|
continue;
|
2009-05-17 03:59:56 +04:00
|
|
|
if (lwp->l_stat == LSIDL || lwp->l_stat == LSZOMB)
|
|
|
|
continue;
|
2008-05-26 03:22:16 +04:00
|
|
|
thread_info(pid, lwp->l_lid);
|
|
|
|
}
|
|
|
|
kvm_close(kd);
|
|
|
|
free(sp);
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset_destroy(cpuset);
|
2008-05-26 03:22:16 +04:00
|
|
|
return 0;
|
2008-01-15 06:37:10 +03:00
|
|
|
}
|
|
|
|
|
2008-05-26 03:22:16 +04:00
|
|
|
(void)execvp(argv[optind], argv + optind);
|
|
|
|
err(EXIT_FAILURE, "execvp");
|
2008-01-15 06:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-02-09 20:01:51 +03:00
|
|
|
sched_set(pid_t pid, lwpid_t lid, int policy,
|
|
|
|
struct sched_param *sp, cpuset_t *cpuset)
|
2008-01-15 06:37:10 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (sp) {
|
|
|
|
/* Set the scheduling parameters for the thread */
|
2008-02-09 20:01:51 +03:00
|
|
|
error = _sched_setparam(pid, lid, policy, sp);
|
2008-01-15 06:37:10 +03:00
|
|
|
if (error < 0)
|
|
|
|
err(EXIT_FAILURE, "_sched_setparam");
|
|
|
|
}
|
|
|
|
if (cpuset) {
|
|
|
|
/* Set the CPU-set for affinity */
|
|
|
|
error = _sched_setaffinity(pid, lid,
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset_size(cpuset), cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
if (error < 0)
|
|
|
|
err(EXIT_FAILURE, "_sched_setaffinity");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
thread_info(pid_t pid, lwpid_t lid)
|
|
|
|
{
|
|
|
|
struct sched_param sp;
|
|
|
|
cpuset_t *cpuset;
|
2008-02-09 20:01:51 +03:00
|
|
|
int error, policy;
|
2008-01-15 06:37:10 +03:00
|
|
|
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset = cpuset_create();
|
2008-01-15 06:37:10 +03:00
|
|
|
if (cpuset == NULL)
|
2008-06-16 05:41:20 +04:00
|
|
|
err(EXIT_FAILURE, "cpuset_create");
|
2008-01-15 06:37:10 +03:00
|
|
|
|
2008-02-09 20:01:51 +03:00
|
|
|
error = _sched_getparam(pid, lid, &policy, &sp);
|
2008-01-15 06:37:10 +03:00
|
|
|
if (error < 0)
|
|
|
|
err(EXIT_FAILURE, "_sched_getparam");
|
|
|
|
|
2008-06-16 05:41:20 +04:00
|
|
|
error = _sched_getaffinity(pid, lid, cpuset_size(cpuset), cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
if (error < 0)
|
|
|
|
err(EXIT_FAILURE, "_sched_getaffinity");
|
|
|
|
|
|
|
|
printf(" LID: %d\n", lid);
|
|
|
|
printf(" Priority: %d\n", sp.sched_priority);
|
2008-02-09 20:01:51 +03:00
|
|
|
printf(" Class: %s\n", class_str[policy]);
|
2008-01-15 06:37:10 +03:00
|
|
|
|
2014-07-27 08:46:48 +04:00
|
|
|
printf(" Affinity (CPUs): ");
|
|
|
|
printcpuset(cpuset);
|
|
|
|
printf("\n");
|
2008-01-15 06:37:10 +03:00
|
|
|
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset_destroy(cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static cpuset_t *
|
|
|
|
makecpuset(char *str)
|
|
|
|
{
|
|
|
|
cpuset_t *cpuset;
|
|
|
|
char *cpustr, *s;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset = cpuset_create();
|
2008-01-15 06:37:10 +03:00
|
|
|
if (cpuset == NULL)
|
2008-06-16 05:41:20 +04:00
|
|
|
err(EXIT_FAILURE, "cpuset_create");
|
|
|
|
cpuset_zero(cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
|
|
|
|
cpustr = strdup(str);
|
|
|
|
if (cpustr == NULL)
|
|
|
|
err(EXIT_FAILURE, "strdup");
|
|
|
|
s = cpustr;
|
|
|
|
|
|
|
|
while (s != NULL) {
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get the CPU number and validate the range */
|
|
|
|
p = strsep(&s, ",");
|
|
|
|
if (p == NULL) {
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset_destroy(cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
cpuset = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = atoi(p);
|
|
|
|
if (i == -1) {
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset_zero(cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-01-26 20:52:08 +03:00
|
|
|
if ((unsigned int)i >= ncpu) {
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset_destroy(cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
cpuset = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the bit */
|
2008-06-16 05:41:20 +04:00
|
|
|
cpuset_set(i, cpuset);
|
2008-01-15 06:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
free(cpustr);
|
|
|
|
return cpuset;
|
|
|
|
}
|
|
|
|
|
2014-07-27 08:46:48 +04:00
|
|
|
static void
|
|
|
|
printcpuset(cpuset_t *cpuset)
|
2008-01-15 06:37:10 +03:00
|
|
|
{
|
2009-01-18 13:10:47 +03:00
|
|
|
unsigned int i;
|
2014-07-27 08:46:48 +04:00
|
|
|
bool seen;
|
2008-01-15 06:37:10 +03:00
|
|
|
|
2014-07-27 08:46:48 +04:00
|
|
|
seen = false;
|
|
|
|
for (i = 0; i < ncpu; i++) {
|
|
|
|
if (cpuset_isset(i, cpuset)) {
|
|
|
|
if (seen) {
|
|
|
|
putchar(',');
|
|
|
|
}
|
|
|
|
printf("%d", i);
|
|
|
|
seen = true;
|
|
|
|
}
|
2008-01-15 06:37:10 +03:00
|
|
|
}
|
|
|
|
|
2014-07-27 08:46:48 +04:00
|
|
|
if (!seen) {
|
|
|
|
printf("<none>");
|
|
|
|
}
|
2008-01-15 06:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2008-05-26 03:22:16 +04:00
|
|
|
|
2008-05-26 12:15:37 +04:00
|
|
|
fprintf(stderr, "usage: %s [-A processor] [-C class] "
|
|
|
|
"[-P priority] [-t lid] {-p pid|command}\n", getprogname());
|
2008-01-15 06:37:10 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|