From f6d52dfb44140a30155ee8b0a05b9ad4385e522c Mon Sep 17 00:00:00 2001 From: Thomas Nikolajsen Date: Sun, 21 Oct 2012 20:51:37 +0200 Subject: [PATCH] usched(8): Add cpumask --- sbin/usched/usched.8 | 81 +++++++++++++++++++++++++++++++++++----- sbin/usched/usched.c | 88 +++++++++++++++++++++++++++++++------------- 2 files changed, 133 insertions(+), 36 deletions(-) diff --git a/sbin/usched/usched.8 b/sbin/usched/usched.8 index 15f3a9b6ce..47493cffba 100644 --- a/sbin/usched/usched.8 +++ b/sbin/usched/usched.8 @@ -1,6 +1,9 @@ .\" .\" Copyright (c) 2012 .\" The DragonFly Project. All rights reserved. +.\" This code is derived from software contributed to The DragonFly Project +.\" by Matthew Dillon and Thomas Nikolajsen +.\" .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -29,29 +32,87 @@ .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd October 16, 2012 +.Dd October 21, 2012 .Dt USCHED 8 .Os .Sh NAME .Nm usched -.Nd run a program with a specified userland scheduler +.Nd run a program with a specified userland scheduler and cpumask .Sh SYNOPSIS .Nm -.\".Op Fl v -.Ar scheduler +.Op Fl d +.Brq Ar scheduler Ns Oo Cm \&: Ns Ar cpumask Oc | Cm \&: Ns Ar cpumask .Ar program -.Op Ar argument... +.Op Ar argument ... .Sh DESCRIPTION -The -.Nm -utility runs +Run .Ar program using the specified userland -.Ar scheduler . +.Ar scheduler +on +.Tn CPUs +given in +.Ar cpumask . +The userland +.Ar scheduler +can have value +.Sq bsd4 +or +.Sq dfly . +If +.Ar scheduler +is not specified, +userland scheduler is inherited from parent process +.Pq Nm . +.Pp +The following options are available: +.Bl -tag -width indent +.It Fl d +Add debug output. +.El +.Pp +.Nm +is only usable for super user as other users cannot change +userland scheduler or cpumask. +.Pp +System default userland scheduler can be changed in +.Xr loader.conf 5 , +see +.Xr loader 8 . +.Sh EXAMPLES +Run +.Xr sh 1 +using only first 3 +.Tn CPUs . +.Pp +.Dl usched :7 sh +.Pp +Run +.Xr sh 1 +using userland scheduler +.Sq bsd4 +using only first 4 +.Tn CPUs . +.Pp +.Dl usched bsd4:0xf sh +.Sh EXIT STATUS +.Ex -std usched .Sh SEE ALSO -.Xr usched_set 2 +.Xr usched_set 2 , +.Xr loader.conf 5 , +.Xr loader 8 .Sh HISTORY The .Nm utility first appeared in .Dx 3.1 . +.Sh AUTHORS +.An Matthew Dillon Aq dillon@backplane.com +and +.An Thomas Nikolajsen Aq thomas.nikolajsen@mail.dk +.Sh BUGS +Using system default userland scheduler by specifying +.Ar scheduler +as +.Sq default +is not implemented. diff --git a/sbin/usched/usched.c b/sbin/usched/usched.c index b8215c4a4c..a2ca74ee30 100644 --- a/sbin/usched/usched.c +++ b/sbin/usched/usched.c @@ -2,7 +2,8 @@ * Copyright (c) 2012 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project - * by Matthew Dillon + * by Matthew Dillon and Thomas Nikolajsen + * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -41,7 +42,7 @@ static void usage(void); -int VerboseOpt; +int DebugOpt; int main(int ac, char **av) @@ -49,39 +50,78 @@ main(int ac, char **av) int ch; int res; char *sched = NULL; -#if 0 /* XXX cpumask */ char *cpustr = NULL; + char *sched_cpustr = NULL; cpumask_t cpumask = 0; -#endif + int cpuid = -1; - while ((ch = getopt(ac, av, "v")) != -1) { - switch(ch) { - case 'v': - VerboseOpt = 1; + while ((ch = getopt(ac, av, "d")) != -1) { + switch (ch) { + case 'd': + DebugOpt = 1; break; default: usage(); - break; /* NOT REACHED */ + /* NOTREACHED */ } } ac -= optind; av += optind; - if (ac < 2) + if (ac < 2) { usage(); - sched = strtok(strdup(av[0]), ":"); -#if 0 /* XXX cpumask */ - cpustr = strtok(NULL, ""); - if (cpustr == NULL) - cpumask = -1; - else + /* NOTREACHED */ + } + sched_cpustr = strdup(av[0]); + sched = strsep(&sched_cpustr, ":"); + if (strcmp(sched, "default") == 0) + fprintf(stderr, "Ignoring scheduler == \"default\": not implemented\n"); + cpustr = strsep(&sched_cpustr, ""); + if (strlen(sched) == 0 && cpustr == NULL) { + usage(); + /* NOTREACHED */ + } + if (cpustr != NULL) cpumask = strtoul(cpustr, NULL, 0); -#endif - res = usched_set(getpid(), USCHED_SET_SCHEDULER, sched, strlen(sched)); - if (res != 0) { - perror("usched_set"); - exit(1); + if (strlen(sched) != 0) { + if (DebugOpt) + fprintf(stderr, "DEBUG: USCHED_SET_SCHEDULER: scheduler: %s\n", sched); + res = usched_set(getpid(), USCHED_SET_SCHEDULER, sched, strlen(sched)); + if (res != 0) { + perror("usched_set(,USCHED_SET_SCHEDULER,,)"); + exit(1); + } + } + if (cpumask != 0) { + while ((cpumask & 1) == 0) { + cpuid++; + cpumask >>= 1; + } + cpuid++; + cpumask >>= 1; + if (DebugOpt) + fprintf(stderr, "DEBUG: USCHED_SET_CPU: cpuid: %d\n", cpuid); + res = usched_set(getpid(), USCHED_SET_CPU, &cpuid, sizeof(int)); + if (res != 0) { + perror("usched_set(,USCHED_SET_CPU,,)"); + exit(1); + } + while (cpumask != 0) { + while ((cpumask & 1) == 0) { + cpuid++; + cpumask >>= 1; + } + cpuid++; + cpumask >>= 1; + if (DebugOpt) + fprintf(stderr, "DEBUG: USCHED_ADD_CPU: cpuid: %d\n", cpuid); + res = usched_set(getpid(), USCHED_ADD_CPU, &cpuid, sizeof(int)); + if (res != 0) { + perror("usched_set(,USCHED_ADD_CPU,,)"); + exit(1); + } + } } execvp(av[1], av + 1); exit(1); @@ -91,10 +131,6 @@ static void usage(void) { -#if 0 - fprintf(stderr, "usched scheduler[:cpumask] program args...\n"); -#else - fprintf(stderr, "usched scheduler program args...\n"); -#endif + fprintf(stderr, "usage: usched [-d] {scheduler[:cpumask] | :cpumask} program [argument ...]\n"); exit(1); } -- 2.41.0