sched.c revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/cred_impl.h>
#include <ucred.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <sched.h>
#include <strings.h>
#include <pthread.h>
#include <time.h>
#include <thread.h>
#include <alloca.h>
#include <unistd.h>
#include <sys/lx_syscall.h>
#include <sys/lx_debug.h>
#include <sys/lx_brand.h>
#include <sys/lx_sched.h>
/* Linux only has three valid policies, SCHED_FIFO, SCHED_RR and SCHED_OTHER */
static int
validate_policy(int policy)
{
switch (policy) {
case LX_SCHED_FIFO:
return (SCHED_FIFO);
case LX_SCHED_RR:
return (SCHED_RR);
case LX_SCHED_OTHER:
return (SCHED_OTHER);
default:
return (-EINVAL);
}
}
/*
* Check to see if we have the permissions to set scheduler parameters and
* policy, based on Linux' demand that such commands fail with errno set to
* EPERM if the current euid is not the euid or ruid of the process in
* question.
*/
static int
{
/*
* If we're the process to be checked, simply check the euid
* against our ruid.
*/
return (-EPERM);
return (0);
}
/*
* We allocate a ucred_t ourselves rather than call ucred_get(3C)
* because ucred_get() calls malloc(3C), which the brand library cannot
* use. Because we allocate the space with SAFE_ALLOCA(), there's
* no need to free it when we're done.
*/
sz = ucred_size();
return (-ENOMEM);
/*
* If we can't access the process' credentials, fail with errno EPERM
* as the call would not have succeeded anyway.
*/
return (-EPERM);
return (0);
}
static int
{
struct lx_sched_param ls;
return (-errno);
/*
* Linux has a fixed priority range, 0 - 99, which we need to convert to
* Solaris's dynamic range. Linux considers lower numbers to be
* higher priority, so we'll invert the priority within Solaris's range.
*
* The formula to convert between ranges is:
*
* L * (smax - smin)
* S = ----------------- + smin
* (lmax - lmin)
*
* where S is the Solaris equivalent of the linux priority L.
*
* To invert the priority, we use:
* S' = smax - S + smin
*
* Together, these two formulas become:
*
* L * (smax - smin)
* S = smax - ----------------- + 2smin
* 99
*/
lx_debug("ltos_sparam: linux prio %d = Solaris prio %d "
return (0);
}
static int
{
struct lx_sched_param ls;
if (policy == SCHED_OTHER) {
/*
* In Linux, the only valid SCHED_OTHER scheduler priority is 0
*/
ls.lx_sched_prio = 0;
} else {
/*
* Convert Solaris's dynamic, inverted priority range to the
* fixed Linux range of 1 - 99.
*
* The formula is (see above):
*
* (smax - s + 2smin) * 99
* l = -----------------------
* smax - smin
*/
}
lx_debug("stol_sparam: Solaris prio %d = linux prio %d "
? -errno : 0);
}
/* ARGSUSED */
int
{
int sz;
int i;
if (sz == -1)
return (-errno);
/*
* If the target LWP hasn't ever had an affinity mask set, the kernel
* will return a mask of all 0's. If that is the case we must build a
* default mask that has all valid bits turned on.
*/
return (-ENOMEM);
return (-EFAULT);
return (sz);
for (i = 0; i < sz * 8; i++) {
}
}
return (-EFAULT);
return (sz);
}
/* ARGSUSED */
int
{
int ret;
int sz;
int i;
int found;
return (-EINVAL);
return (-ESRCH);
/*
* We only support setting affinity masks for threads in
* the calling process.
*/
return (-EPERM);
/*
* First, get the minimum bitmask size from the kernel.
*/
if (sz == -1)
return (-errno);
return (-ENOMEM);
return (-EFAULT);
/*
* Make sure the mask contains at least one processor that is
* physically on the system. Reduce the user's mask to the set of
* physically present CPUs. Keep track of how many valid
* bits are set in the user's mask.
*/
/*
* This CPU doesn't exist, so clear this bit from
* the user's mask.
*/
continue;
}
found++;
cpuid = i;
}
}
if (found == 0) {
lx_debug("\tlx_sched_setaffinity: mask has no present CPUs\n");
return (-EINVAL);
}
/*
* If only one bit is set, bind the thread to that procesor;
* otherwise, clear the binding.
*/
if (found == 1) {
lx_debug("\tlx_sched_setaffinity: binding thread %d to cpu%d\n",
/*
* It could be that the requested processor is offline,
* so we'll just abandon our good-natured attempt to
* bind to it.
*/
} else {
lx_debug("\tlx_sched_setaffinity: clearing thr %d binding\n",
s_tid);
lx_debug("couldn't clear CPU binding for LWP %d: %s\n",
}
}
/*
* Finally, ask the kernel to make a note of our current (though fairly
* meaningless) affinity mask.
*/
}
int
{
struct sched_param sp;
return (-EINVAL);
return (-ESRCH);
/*
* If we're attempting to get information on our own process, we can
* get data on a per-thread basis; if not, punt and use the specified
* pid.
*/
return (-ret);
} else {
return (-errno);
return (-errno);
}
}
int
{
struct lx_sched_param lp;
struct sched_param sp;
return (-EINVAL);
return (-ESRCH);
struct sched_param dummy;
return (-err);
} else
return (-errno);
return (-errno);
/*
* In Linux, the only valid SCHED_OTHER scheduler priority is 0
*/
return (-EINVAL);
&sp)) != 0)
return (err);
/*
* Check if we're allowed to change the scheduler for the process.
*
* If we're operating on a thread, we can't just call
* pthread_setschedparam() because as all threads reside within a
* single Solaris process, Solaris will allow the modification
*
* If we're operating on a process, we can't just call sched_setparam()
* because Solaris will allow the call to succeed if the scheduler
* parameters do not differ from those being installed, but Linux wants
* the call to fail.
*/
return (err);
? -err : 0);
}
int
{
return (-EINVAL);
return (-ESRCH);
sizeof (struct timespec)) != 0)
return (-errno);
}
int
{
return (-EINVAL);
return (-ESRCH);
struct sched_param dummy;
return (-rv);
} else
return (-errno);
/*
* Linux only supports certain policies; avoid confusing apps with
* alien policies.
*/
switch (policy) {
case SCHED_FIFO:
return (LX_SCHED_FIFO);
case SCHED_OTHER:
return (LX_SCHED_OTHER);
case SCHED_RR:
return (LX_SCHED_RR);
default:
break;
}
return (LX_SCHED_OTHER);
}
int
{
int rt_pol;
int rv;
struct lx_sched_param lp;
struct sched_param sp;
return (-EINVAL);
return (rt_pol);
&sp)) != 0)
return (rv);
return (-errno);
/*
* In Linux, the only valid SCHED_OTHER scheduler priority is 0
*/
return (-EINVAL);
return (-ESRCH);
/*
* Check if we're allowed to change the scheduler for the process.
*
* If we're operating on a thread, we can't just call
* pthread_setschedparam() because as all threads reside within a
* single Solaris process, Solaris will allow the modification.
*
* If we're operating on a process, we can't just call
* sched_setscheduler() because Solaris will allow the call to succeed
* if the scheduler and scheduler parameters do not differ from those
* being installed, but Linux wants the call to fail.
*/
return (rv);
struct sched_param param;
int pol;
return (-errno);
/*
* sched_setscheduler() returns the previous scheduling policy
* on success, so call pthread_getschedparam() to get the
* current thread's scheduling policy and return that if the
* call to pthread_setschedparam() succeeds.
*/
return (-rv);
}
}
int
{
/*
* In Linux, the only valid SCHED_OTHER scheduler priority is 0.
* Linux scheduling priorities are not alterable, so there is no
* Solaris translation necessary.
*/
switch (policy) {
case LX_SCHED_FIFO:
case LX_SCHED_RR:
return (LX_SCHED_PRIORITY_MIN_RRFIFO);
case LX_SCHED_OTHER:
return (LX_SCHED_PRIORITY_MIN_OTHER);
default:
break;
}
return (-EINVAL);
}
int
{
/*
* In Linux, the only valid SCHED_OTHER scheduler priority is 0
* Linux scheduling priorities are not alterable, so there is no
* Solaris translation necessary.
*/
switch (policy) {
case LX_SCHED_FIFO:
case LX_SCHED_RR:
return (LX_SCHED_PRIORITY_MAX_RRFIFO);
case LX_SCHED_OTHER:
return (LX_SCHED_PRIORITY_MAX_OTHER);
default:
break;
}
return (-EINVAL);
}