Home | History | Annotate | Line # | Download | only in schedctl
schedctl.c revision 1.1
      1  1.1  rmind /*	$NetBSD: schedctl.c,v 1.1 2008/01/15 03:37:15 rmind 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.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  1.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  rmind  * POSSIBILITY OF 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.1  rmind __RCSID("$NetBSD: schedctl.c,v 1.1 2008/01/15 03:37:15 rmind 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/param.h>
     49  1.1  rmind #include <sys/pset.h>
     50  1.1  rmind #include <sys/sched.h>
     51  1.1  rmind #include <sys/sysctl.h>
     52  1.1  rmind #include <sys/types.h>
     53  1.1  rmind 
     54  1.1  rmind static const char *class_str[] = {
     55  1.1  rmind 	"SCHED_OTHER",
     56  1.1  rmind 	"SCHED_FIFO",
     57  1.1  rmind 	"SCHED_RR"
     58  1.1  rmind };
     59  1.1  rmind 
     60  1.1  rmind static void	sched_set(pid_t, lwpid_t, 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.1  rmind int
     67  1.1  rmind main(int argc, char **argv)
     68  1.1  rmind {
     69  1.1  rmind 	kvm_t *kd;
     70  1.1  rmind 	struct kinfo_lwp *lwp_list, *lwp;
     71  1.1  rmind 	struct sched_param *sp;
     72  1.1  rmind 	cpuset_t *cpuset;
     73  1.1  rmind 	int i, count, ch;
     74  1.1  rmind 	pid_t pid;
     75  1.1  rmind 	lwpid_t lid;
     76  1.1  rmind 	bool set;
     77  1.1  rmind 
     78  1.1  rmind 	pid = lid = 0;
     79  1.1  rmind 	cpuset = NULL;
     80  1.1  rmind 	set = false;
     81  1.1  rmind 
     82  1.1  rmind 	sp = malloc(sizeof(struct sched_param));
     83  1.1  rmind 	if (sp == NULL)
     84  1.1  rmind 		err(EXIT_FAILURE, "malloc");
     85  1.1  rmind 
     86  1.1  rmind 	memset(sp, 0, sizeof(struct sched_param));
     87  1.1  rmind 	sp->sched_class = SCHED_NONE;
     88  1.1  rmind 	sp->sched_priority = PRI_NONE;
     89  1.1  rmind 
     90  1.1  rmind 	while ((ch = getopt(argc, argv, "A:C:P:p:t:")) != -1) {
     91  1.1  rmind 		switch (ch) {
     92  1.1  rmind 		case 'p':
     93  1.1  rmind 			/* PID */
     94  1.1  rmind 			pid = atoi(optarg);
     95  1.1  rmind 			break;
     96  1.1  rmind 		case 't':
     97  1.1  rmind 			/* Thread (LWP) ID */
     98  1.1  rmind 			lid = atoi(optarg);
     99  1.1  rmind 			break;
    100  1.1  rmind 		case 'A':
    101  1.1  rmind 			/* Affinity */
    102  1.1  rmind 			cpuset = makecpuset(optarg);
    103  1.1  rmind 			if (cpuset == NULL) {
    104  1.1  rmind 				fprintf(stderr, "%s: invalid CPU value\n",
    105  1.1  rmind 				    getprogname());
    106  1.1  rmind 				exit(EXIT_FAILURE);
    107  1.1  rmind 			}
    108  1.1  rmind 			break;
    109  1.1  rmind 		case 'C':
    110  1.1  rmind 			/* Scheduling class */
    111  1.1  rmind 			sp->sched_class = atoi(optarg);
    112  1.1  rmind 			if (sp->sched_class < SCHED_OTHER ||
    113  1.1  rmind 			    sp->sched_class > SCHED_RR) {
    114  1.1  rmind 				fprintf(stderr,
    115  1.1  rmind 				    "%s: invalid scheduling class\n",
    116  1.1  rmind 				    getprogname());
    117  1.1  rmind 				exit(EXIT_FAILURE);
    118  1.1  rmind 			}
    119  1.1  rmind 			set = true;
    120  1.1  rmind 			break;
    121  1.1  rmind 		case 'P':
    122  1.1  rmind 			/* Priority */
    123  1.1  rmind 			sp->sched_priority = atoi(optarg);
    124  1.1  rmind 			if (sp->sched_priority < sysconf(_SC_SCHED_PRI_MIN) ||
    125  1.1  rmind 			    sp->sched_priority > sysconf(_SC_SCHED_PRI_MAX)) {
    126  1.1  rmind 				fprintf(stderr, "%s: invalid priority\n",
    127  1.1  rmind 				    getprogname());
    128  1.1  rmind 				exit(EXIT_FAILURE);
    129  1.1  rmind 			}
    130  1.1  rmind 			set = true;
    131  1.1  rmind 			break;
    132  1.1  rmind 		default:
    133  1.1  rmind 			usage();
    134  1.1  rmind 		}
    135  1.1  rmind 	}
    136  1.1  rmind 
    137  1.1  rmind 	/* At least PID must be specified */
    138  1.1  rmind 	if (pid == 0)
    139  1.1  rmind 		usage();
    140  1.1  rmind 
    141  1.1  rmind 	/* Set the scheduling information for thread/process */
    142  1.1  rmind 	sched_set(pid, lid, set ? sp : NULL, cpuset);
    143  1.1  rmind 
    144  1.1  rmind 	/* Show information about each thread */
    145  1.1  rmind 	kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
    146  1.1  rmind 	if (kd == NULL)
    147  1.1  rmind 		err(EXIT_FAILURE, "kvm_open");
    148  1.1  rmind 	lwp_list = kvm_getlwps(kd, pid, 0, sizeof(struct kinfo_lwp), &count);
    149  1.1  rmind 	if (lwp_list == NULL)
    150  1.1  rmind 		err(EXIT_FAILURE, "kvm_getlwps");
    151  1.1  rmind 	for (lwp = lwp_list, i = 0; i < count; lwp++, i++) {
    152  1.1  rmind 		if (lid && lid != lwp->l_lid)
    153  1.1  rmind 			continue;
    154  1.1  rmind 		thread_info(pid, lwp->l_lid);
    155  1.1  rmind 	}
    156  1.1  rmind 	kvm_close(kd);
    157  1.1  rmind 
    158  1.1  rmind 	free(sp);
    159  1.1  rmind 	free(cpuset);
    160  1.1  rmind 	return 0;
    161  1.1  rmind }
    162  1.1  rmind 
    163  1.1  rmind static void
    164  1.1  rmind sched_set(pid_t pid, lwpid_t lid, struct sched_param *sp, cpuset_t *cpuset)
    165  1.1  rmind {
    166  1.1  rmind 	int error;
    167  1.1  rmind 
    168  1.1  rmind 	if (sp) {
    169  1.1  rmind 		/* Set the scheduling parameters for the thread */
    170  1.1  rmind 		error = _sched_setparam(pid, lid, sp);
    171  1.1  rmind 		if (error < 0)
    172  1.1  rmind 			err(EXIT_FAILURE, "_sched_setparam");
    173  1.1  rmind 	}
    174  1.1  rmind 	if (cpuset) {
    175  1.1  rmind 		/* Set the CPU-set for affinity */
    176  1.1  rmind 		error = _sched_setaffinity(pid, lid,
    177  1.1  rmind 		    sizeof(cpuset_t), cpuset);
    178  1.1  rmind 		if (error < 0)
    179  1.1  rmind 			err(EXIT_FAILURE, "_sched_setaffinity");
    180  1.1  rmind 	}
    181  1.1  rmind }
    182  1.1  rmind 
    183  1.1  rmind static void
    184  1.1  rmind thread_info(pid_t pid, lwpid_t lid)
    185  1.1  rmind {
    186  1.1  rmind 	struct sched_param sp;
    187  1.1  rmind 	cpuset_t *cpuset;
    188  1.1  rmind 	char *cpus;
    189  1.1  rmind 	int error;
    190  1.1  rmind 
    191  1.1  rmind 	cpuset = malloc(sizeof(cpuset_t));
    192  1.1  rmind 	if (cpuset == NULL)
    193  1.1  rmind 		err(EXIT_FAILURE, "malloc");
    194  1.1  rmind 
    195  1.1  rmind 	error = _sched_getparam(pid, lid, &sp);
    196  1.1  rmind 	if (error < 0)
    197  1.1  rmind 		err(EXIT_FAILURE, "_sched_getparam");
    198  1.1  rmind 
    199  1.1  rmind 	error = _sched_getaffinity(pid, lid, sizeof(cpuset_t), cpuset);
    200  1.1  rmind 	if (error < 0)
    201  1.1  rmind 		err(EXIT_FAILURE, "_sched_getaffinity");
    202  1.1  rmind 
    203  1.1  rmind 	printf("  LID:              %d\n", lid);
    204  1.1  rmind 	printf("  Priority:         %d\n", sp.sched_priority);
    205  1.1  rmind 	printf("  Class:            %s\n", class_str[sp.sched_class]);
    206  1.1  rmind 
    207  1.1  rmind 	cpus = showcpuset(cpuset);
    208  1.1  rmind 	printf("  Affinity (CPUs):  %s\n", cpus);
    209  1.1  rmind 	free(cpus);
    210  1.1  rmind 
    211  1.1  rmind 	free(cpuset);
    212  1.1  rmind }
    213  1.1  rmind 
    214  1.1  rmind static cpuset_t *
    215  1.1  rmind makecpuset(char *str)
    216  1.1  rmind {
    217  1.1  rmind 	cpuset_t *cpuset;
    218  1.1  rmind 	char *cpustr, *s;
    219  1.1  rmind 
    220  1.1  rmind 	if (str == NULL)
    221  1.1  rmind 		return NULL;
    222  1.1  rmind 
    223  1.1  rmind 	cpuset = malloc(sizeof(cpuset_t));
    224  1.1  rmind 	if (cpuset == NULL)
    225  1.1  rmind 		err(EXIT_FAILURE, "malloc");
    226  1.1  rmind 	memset(cpuset, 0, sizeof(cpuset_t));
    227  1.1  rmind 
    228  1.1  rmind 	cpustr = strdup(str);
    229  1.1  rmind 	if (cpustr == NULL)
    230  1.1  rmind 		err(EXIT_FAILURE, "strdup");
    231  1.1  rmind 	s = cpustr;
    232  1.1  rmind 
    233  1.1  rmind 	while (s != NULL) {
    234  1.1  rmind 		char *p;
    235  1.1  rmind 		int i;
    236  1.1  rmind 
    237  1.1  rmind 		/* Get the CPU number and validate the range */
    238  1.1  rmind 		p = strsep(&s, ",");
    239  1.1  rmind 		if (p == NULL) {
    240  1.1  rmind 			free(cpuset);
    241  1.1  rmind 			cpuset = NULL;
    242  1.1  rmind 			break;
    243  1.1  rmind 		}
    244  1.1  rmind 		i = atoi(p);
    245  1.1  rmind 		if (i == -1) {
    246  1.1  rmind 			memset(cpuset, 0, sizeof(cpuset_t));
    247  1.1  rmind 			break;
    248  1.1  rmind 		}
    249  1.1  rmind 		if ((unsigned int)i > MAXCPUS) {
    250  1.1  rmind 			free(cpuset);
    251  1.1  rmind 			cpuset = NULL;
    252  1.1  rmind 			break;
    253  1.1  rmind 		}
    254  1.1  rmind 
    255  1.1  rmind 		/* Set the bit */
    256  1.1  rmind 		CPU_SET(i, cpuset);
    257  1.1  rmind 	}
    258  1.1  rmind 
    259  1.1  rmind 	free(cpustr);
    260  1.1  rmind 	return cpuset;
    261  1.1  rmind }
    262  1.1  rmind 
    263  1.1  rmind static char *
    264  1.1  rmind showcpuset(cpuset_t *cpuset)
    265  1.1  rmind {
    266  1.1  rmind 	char *buf;
    267  1.1  rmind 	size_t size;
    268  1.1  rmind 	int i;
    269  1.1  rmind 
    270  1.1  rmind 	size = 3 * MAXCPUS;	/* XXX */
    271  1.1  rmind 	buf = malloc(size + 1);
    272  1.1  rmind 	if (cpuset == NULL)
    273  1.1  rmind 		err(EXIT_FAILURE, "malloc");
    274  1.1  rmind 	memset(buf, '\0', size + 1);
    275  1.1  rmind 
    276  1.1  rmind 	for (i = 0; i < MAXCPUS; i++)
    277  1.1  rmind 		if (CPU_ISSET(i, cpuset))
    278  1.1  rmind 			snprintf(buf, size, "%s%d,", buf, i);
    279  1.1  rmind 
    280  1.1  rmind 	i = strlen(buf);
    281  1.1  rmind 	if (i != 0) {
    282  1.1  rmind 		buf[i - 1] = '\0';
    283  1.1  rmind 	} else {
    284  1.1  rmind 		strncpy(buf, "<none>", size);
    285  1.1  rmind 	}
    286  1.1  rmind 
    287  1.1  rmind 	return buf;
    288  1.1  rmind }
    289  1.1  rmind 
    290  1.1  rmind static void
    291  1.1  rmind usage(void)
    292  1.1  rmind {
    293  1.1  rmind 	const char *progname = getprogname();
    294  1.1  rmind 
    295  1.1  rmind 	fprintf(stderr, "usage: %s -p pid [ -t lid ] [ -A processor ]\n"
    296  1.1  rmind 	    "\t [ -C class ] [ -P priority ]\n", progname);
    297  1.1  rmind 	exit(EXIT_FAILURE);
    298  1.1  rmind }
    299