Home | History | Annotate | Line # | Download | only in schedctl
schedctl.c revision 1.12.4.1
      1  1.12.4.1   bouyer /*	$NetBSD: schedctl.c,v 1.12.4.1 2009/05/18 19:56:04 bouyer Exp $	*/
      2       1.1    rmind 
      3       1.1    rmind /*
      4       1.1    rmind  * Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org>
      5       1.1    rmind  * All rights reserved.
      6       1.1    rmind  *
      7       1.1    rmind  * Redistribution and use in source and binary forms, with or without
      8       1.1    rmind  * modification, are permitted provided that the following conditions
      9       1.1    rmind  * are met:
     10       1.1    rmind  * 1. Redistributions of source code must retain the above copyright
     11       1.1    rmind  *    notice, this list of conditions and the following disclaimer.
     12       1.1    rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    rmind  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    rmind  *    documentation and/or other materials provided with the distribution.
     15       1.1    rmind  *
     16      1.12    rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17      1.12    rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.12    rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.12    rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20      1.12    rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.12    rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.12    rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.12    rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.12    rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.12    rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.12    rmind  * SUCH DAMAGE.
     27       1.1    rmind  */
     28       1.1    rmind 
     29       1.1    rmind /*
     30       1.1    rmind  * schedctl(8) - a program to control scheduling of processes and threads.
     31       1.1    rmind  */
     32       1.1    rmind 
     33       1.1    rmind #include <sys/cdefs.h>
     34       1.1    rmind 
     35       1.1    rmind #ifndef lint
     36  1.12.4.1   bouyer __RCSID("$NetBSD: schedctl.c,v 1.12.4.1 2009/05/18 19:56:04 bouyer Exp $");
     37       1.1    rmind #endif
     38       1.1    rmind 
     39       1.1    rmind #include <stdio.h>
     40       1.1    rmind #include <stdlib.h>
     41       1.1    rmind #include <string.h>
     42       1.1    rmind 
     43       1.1    rmind #include <err.h>
     44       1.1    rmind #include <fcntl.h>
     45       1.1    rmind #include <kvm.h>
     46       1.1    rmind #include <unistd.h>
     47       1.1    rmind 
     48       1.1    rmind #include <sys/pset.h>
     49       1.1    rmind #include <sys/sched.h>
     50       1.1    rmind #include <sys/sysctl.h>
     51       1.1    rmind #include <sys/types.h>
     52       1.1    rmind 
     53       1.1    rmind static const char *class_str[] = {
     54       1.1    rmind 	"SCHED_OTHER",
     55       1.1    rmind 	"SCHED_FIFO",
     56       1.6       ad 	"SCHED_RR",
     57       1.6       ad 	NULL
     58       1.1    rmind };
     59       1.1    rmind 
     60       1.3     yamt static void	sched_set(pid_t, lwpid_t, int, struct sched_param *, cpuset_t *);
     61       1.1    rmind static void	thread_info(pid_t, lwpid_t);
     62       1.1    rmind static cpuset_t	*makecpuset(char *);
     63       1.1    rmind static char	*showcpuset(cpuset_t *);
     64       1.1    rmind static void	usage(void);
     65       1.1    rmind 
     66       1.2    rmind static u_int	ncpu;
     67       1.2    rmind 
     68       1.1    rmind int
     69       1.1    rmind main(int argc, char **argv)
     70       1.1    rmind {
     71       1.1    rmind 	kvm_t *kd;
     72       1.1    rmind 	struct kinfo_lwp *lwp_list, *lwp;
     73       1.1    rmind 	struct sched_param *sp;
     74       1.1    rmind 	cpuset_t *cpuset;
     75       1.3     yamt 	int i, count, ch, policy;
     76       1.1    rmind 	pid_t pid;
     77       1.1    rmind 	lwpid_t lid;
     78       1.1    rmind 	bool set;
     79       1.1    rmind 
     80       1.2    rmind 	ncpu = sysconf(_SC_NPROCESSORS_CONF);
     81       1.2    rmind 
     82       1.1    rmind 	pid = lid = 0;
     83       1.1    rmind 	cpuset = NULL;
     84       1.1    rmind 	set = false;
     85       1.1    rmind 
     86       1.4  xtraeme 	sp = calloc(1, sizeof(struct sched_param));
     87       1.1    rmind 	if (sp == NULL)
     88       1.4  xtraeme 		err(EXIT_FAILURE, "calloc");
     89       1.1    rmind 
     90       1.1    rmind 	sp->sched_priority = PRI_NONE;
     91       1.3     yamt 	policy = SCHED_NONE;
     92       1.1    rmind 
     93       1.1    rmind 	while ((ch = getopt(argc, argv, "A:C:P:p:t:")) != -1) {
     94       1.1    rmind 		switch (ch) {
     95       1.1    rmind 		case 'p':
     96       1.1    rmind 			/* PID */
     97       1.1    rmind 			pid = atoi(optarg);
     98       1.1    rmind 			break;
     99       1.1    rmind 		case 't':
    100       1.1    rmind 			/* Thread (LWP) ID */
    101       1.1    rmind 			lid = atoi(optarg);
    102       1.1    rmind 			break;
    103       1.1    rmind 		case 'A':
    104       1.1    rmind 			/* Affinity */
    105       1.1    rmind 			cpuset = makecpuset(optarg);
    106       1.1    rmind 			if (cpuset == NULL) {
    107       1.1    rmind 				fprintf(stderr, "%s: invalid CPU value\n",
    108       1.1    rmind 				    getprogname());
    109       1.1    rmind 				exit(EXIT_FAILURE);
    110       1.1    rmind 			}
    111       1.1    rmind 			break;
    112       1.1    rmind 		case 'C':
    113       1.1    rmind 			/* Scheduling class */
    114       1.6       ad 			for (policy = 0; class_str[policy] != NULL; policy++) {
    115       1.6       ad 				if (strcasecmp(optarg, class_str[policy]) == 0)
    116       1.6       ad 					break;
    117       1.6       ad 			}
    118       1.6       ad 			if (class_str[policy] == NULL)
    119       1.6       ad 				policy = atoi(optarg);
    120       1.3     yamt 			if (policy < SCHED_OTHER || policy > SCHED_RR) {
    121       1.1    rmind 				fprintf(stderr,
    122       1.1    rmind 				    "%s: invalid scheduling class\n",
    123       1.1    rmind 				    getprogname());
    124       1.1    rmind 				exit(EXIT_FAILURE);
    125       1.1    rmind 			}
    126       1.1    rmind 			set = true;
    127       1.1    rmind 			break;
    128       1.1    rmind 		case 'P':
    129       1.1    rmind 			/* Priority */
    130       1.1    rmind 			sp->sched_priority = atoi(optarg);
    131       1.1    rmind 			if (sp->sched_priority < sysconf(_SC_SCHED_PRI_MIN) ||
    132       1.1    rmind 			    sp->sched_priority > sysconf(_SC_SCHED_PRI_MAX)) {
    133       1.1    rmind 				fprintf(stderr, "%s: invalid priority\n",
    134       1.1    rmind 				    getprogname());
    135       1.1    rmind 				exit(EXIT_FAILURE);
    136       1.1    rmind 			}
    137       1.1    rmind 			set = true;
    138       1.1    rmind 			break;
    139       1.1    rmind 		default:
    140       1.1    rmind 			usage();
    141       1.1    rmind 		}
    142       1.1    rmind 	}
    143       1.1    rmind 
    144       1.1    rmind 	/* At least PID must be specified */
    145       1.7       ad 	if (pid == 0) {
    146       1.9       ad 		if (argv[optind] == NULL || lid != 0)
    147       1.7       ad 			usage();
    148       1.7       ad 		pid = getpid();
    149       1.7       ad 	} else {
    150       1.9       ad 		if (argv[optind] != NULL)
    151       1.7       ad 			usage();
    152       1.7       ad 	}
    153       1.1    rmind 
    154       1.1    rmind 	/* Set the scheduling information for thread/process */
    155       1.3     yamt 	sched_set(pid, lid, policy, set ? sp : NULL, cpuset);
    156       1.1    rmind 
    157       1.1    rmind 	/* Show information about each thread */
    158       1.7       ad 	if (pid != getpid()) {
    159       1.7       ad 		kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
    160       1.7       ad 		if (kd == NULL)
    161       1.7       ad 			err(EXIT_FAILURE, "kvm_open");
    162       1.7       ad 		lwp_list = kvm_getlwps(kd, pid, 0, sizeof(struct kinfo_lwp), &count);
    163       1.7       ad 		if (lwp_list == NULL)
    164       1.7       ad 			err(EXIT_FAILURE, "kvm_getlwps");
    165       1.7       ad 		for (lwp = lwp_list, i = 0; i < count; lwp++, i++) {
    166       1.7       ad 			if (lid && lid != lwp->l_lid)
    167       1.7       ad 				continue;
    168  1.12.4.1   bouyer 			if (lwp->l_stat == LSIDL || lwp->l_stat == LSZOMB)
    169  1.12.4.1   bouyer 				continue;
    170       1.7       ad 			thread_info(pid, lwp->l_lid);
    171       1.7       ad 		}
    172       1.7       ad 		kvm_close(kd);
    173       1.7       ad 		free(sp);
    174      1.11    rmind 		cpuset_destroy(cpuset);
    175       1.7       ad 		return 0;
    176       1.1    rmind 	}
    177       1.1    rmind 
    178       1.7       ad 	(void)execvp(argv[optind], argv + optind);
    179       1.7       ad 	err(EXIT_FAILURE, "execvp");
    180       1.1    rmind }
    181       1.1    rmind 
    182       1.1    rmind static void
    183       1.3     yamt sched_set(pid_t pid, lwpid_t lid, int policy,
    184       1.3     yamt     struct sched_param *sp, cpuset_t *cpuset)
    185       1.1    rmind {
    186       1.1    rmind 	int error;
    187       1.1    rmind 
    188       1.1    rmind 	if (sp) {
    189       1.1    rmind 		/* Set the scheduling parameters for the thread */
    190       1.3     yamt 		error = _sched_setparam(pid, lid, policy, sp);
    191       1.1    rmind 		if (error < 0)
    192       1.1    rmind 			err(EXIT_FAILURE, "_sched_setparam");
    193       1.1    rmind 	}
    194       1.1    rmind 	if (cpuset) {
    195       1.1    rmind 		/* Set the CPU-set for affinity */
    196       1.1    rmind 		error = _sched_setaffinity(pid, lid,
    197      1.11    rmind 		    cpuset_size(cpuset), cpuset);
    198       1.1    rmind 		if (error < 0)
    199       1.1    rmind 			err(EXIT_FAILURE, "_sched_setaffinity");
    200       1.1    rmind 	}
    201       1.1    rmind }
    202       1.1    rmind 
    203       1.1    rmind static void
    204       1.1    rmind thread_info(pid_t pid, lwpid_t lid)
    205       1.1    rmind {
    206       1.1    rmind 	struct sched_param sp;
    207       1.1    rmind 	cpuset_t *cpuset;
    208       1.1    rmind 	char *cpus;
    209       1.3     yamt 	int error, policy;
    210       1.1    rmind 
    211      1.11    rmind 	cpuset = cpuset_create();
    212       1.1    rmind 	if (cpuset == NULL)
    213      1.11    rmind 		err(EXIT_FAILURE, "cpuset_create");
    214       1.1    rmind 
    215       1.3     yamt 	error = _sched_getparam(pid, lid, &policy, &sp);
    216       1.1    rmind 	if (error < 0)
    217       1.1    rmind 		err(EXIT_FAILURE, "_sched_getparam");
    218       1.1    rmind 
    219      1.11    rmind 	error = _sched_getaffinity(pid, lid, cpuset_size(cpuset), cpuset);
    220       1.1    rmind 	if (error < 0)
    221       1.1    rmind 		err(EXIT_FAILURE, "_sched_getaffinity");
    222       1.1    rmind 
    223       1.1    rmind 	printf("  LID:              %d\n", lid);
    224       1.1    rmind 	printf("  Priority:         %d\n", sp.sched_priority);
    225       1.3     yamt 	printf("  Class:            %s\n", class_str[policy]);
    226       1.1    rmind 
    227       1.1    rmind 	cpus = showcpuset(cpuset);
    228       1.1    rmind 	printf("  Affinity (CPUs):  %s\n", cpus);
    229       1.1    rmind 	free(cpus);
    230       1.1    rmind 
    231      1.11    rmind 	cpuset_destroy(cpuset);
    232       1.1    rmind }
    233       1.1    rmind 
    234       1.1    rmind static cpuset_t *
    235       1.1    rmind makecpuset(char *str)
    236       1.1    rmind {
    237       1.1    rmind 	cpuset_t *cpuset;
    238       1.1    rmind 	char *cpustr, *s;
    239       1.1    rmind 
    240       1.1    rmind 	if (str == NULL)
    241       1.1    rmind 		return NULL;
    242       1.1    rmind 
    243      1.11    rmind 	cpuset = cpuset_create();
    244       1.1    rmind 	if (cpuset == NULL)
    245      1.11    rmind 		err(EXIT_FAILURE, "cpuset_create");
    246      1.11    rmind 	cpuset_zero(cpuset);
    247       1.1    rmind 
    248       1.1    rmind 	cpustr = strdup(str);
    249       1.1    rmind 	if (cpustr == NULL)
    250       1.1    rmind 		err(EXIT_FAILURE, "strdup");
    251       1.1    rmind 	s = cpustr;
    252       1.1    rmind 
    253       1.1    rmind 	while (s != NULL) {
    254       1.1    rmind 		char *p;
    255       1.1    rmind 		int i;
    256       1.1    rmind 
    257       1.1    rmind 		/* Get the CPU number and validate the range */
    258       1.1    rmind 		p = strsep(&s, ",");
    259       1.1    rmind 		if (p == NULL) {
    260      1.11    rmind 			cpuset_destroy(cpuset);
    261       1.1    rmind 			cpuset = NULL;
    262       1.1    rmind 			break;
    263       1.1    rmind 		}
    264       1.1    rmind 		i = atoi(p);
    265       1.1    rmind 		if (i == -1) {
    266      1.11    rmind 			cpuset_zero(cpuset);
    267       1.1    rmind 			break;
    268       1.1    rmind 		}
    269       1.2    rmind 		if ((unsigned int)i >= ncpu) {
    270      1.11    rmind 			cpuset_destroy(cpuset);
    271       1.1    rmind 			cpuset = NULL;
    272       1.1    rmind 			break;
    273       1.1    rmind 		}
    274       1.1    rmind 
    275       1.1    rmind 		/* Set the bit */
    276      1.11    rmind 		cpuset_set(i, cpuset);
    277       1.1    rmind 	}
    278       1.1    rmind 
    279       1.1    rmind 	free(cpustr);
    280       1.1    rmind 	return cpuset;
    281       1.1    rmind }
    282       1.1    rmind 
    283       1.1    rmind static char *
    284       1.1    rmind showcpuset(cpuset_t *cpuset)
    285       1.1    rmind {
    286       1.1    rmind 	char *buf;
    287       1.1    rmind 	size_t size;
    288       1.1    rmind 	int i;
    289       1.1    rmind 
    290       1.2    rmind 	size = 3 * ncpu;	/* XXX */
    291       1.1    rmind 	buf = malloc(size + 1);
    292       1.5  xtraeme 	if (buf == NULL)
    293       1.1    rmind 		err(EXIT_FAILURE, "malloc");
    294       1.1    rmind 	memset(buf, '\0', size + 1);
    295       1.1    rmind 
    296       1.2    rmind 	for (i = 0; i < ncpu; i++)
    297      1.11    rmind 		if (cpuset_isset(i, cpuset))
    298       1.1    rmind 			snprintf(buf, size, "%s%d,", buf, i);
    299       1.1    rmind 
    300       1.1    rmind 	i = strlen(buf);
    301       1.1    rmind 	if (i != 0) {
    302       1.1    rmind 		buf[i - 1] = '\0';
    303       1.1    rmind 	} else {
    304       1.1    rmind 		strncpy(buf, "<none>", size);
    305       1.1    rmind 	}
    306       1.1    rmind 
    307       1.1    rmind 	return buf;
    308       1.1    rmind }
    309       1.1    rmind 
    310       1.1    rmind static void
    311       1.1    rmind usage(void)
    312       1.1    rmind {
    313       1.7       ad 
    314       1.8      wiz 	fprintf(stderr, "usage: %s [-A processor] [-C class] "
    315       1.8      wiz 	    "[-P priority] [-t lid] {-p pid|command}\n", getprogname());
    316       1.1    rmind 	exit(EXIT_FAILURE);
    317       1.1    rmind }
    318