/*
* 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 2013, Joyent, Inc. All rights reserved.
*/
#include <sys/sysmacros.h>
#include <sys/priocntl.h>
#include <sys/schedctl.h>
#include <sys/fsspriocntl.h>
#include <vm/seg_kmem.h>
#include <sys/tnf_probe.h>
/*
* The fair share scheduling class ensures that collections of processes
* (zones and projects) each get their configured share of CPU. This is in
* contrast to the TS class which considers individual processes.
*
* The FSS cpu-share is set on zones using the zone.cpu-shares rctl and on
* projects using the project.cpu-shares rctl. By default the value is 1
* and it can range from 0 - 64k. A value of 0 means that processes in the
* collection will only get CPU resources when there are no other processes
* that need CPU. The cpu-share is used as one of the inputs to calculate a
* thread's "user-mode" priority (umdpri) for the scheduler. The umdpri falls
* in the range 0-59. FSS calculates other, internal, priorities which are not
* visible outside of the FSS class.
*
* The FSS class should approximate TS behavior when there are excess CPU
* resources. When there is a backlog of runnable processes, then the share
* is used as input into the runnable process's priority calculation, where
* the final umdpri is used by the scheduler to determine when the process runs.
*
* Projects in a zone compete with each other for CPU time, receiving CPU
* allocation within a zone proportional to the project's share; at a higher
* level zones compete with each other, receiving allocation in a pset
* proportional to the zone's share.
*
* The FSS priority calculation consists of several parts.
*
* 1) Once per second the fss_update function runs. The first thing it does is
* call fss_decay_usage. This function does three things.
*
* a) fss_decay_usage first decays the maxfsspri value for the pset. This
* value is used in the per-process priority calculation described in step
* (2b). The maxfsspri is decayed using the following formula:
*
* maxfsspri * fss_nice_decay[NZERO])
* maxfsspri = ------------------------------------
* FSS_DECAY_BASE
*
*
* - NZERO is the default process priority (i.e. 20)
*
* The fss_nice_decay array is a fixed set of values used to adjust the
* decay rate of processes based on their nice value. Entries in this
* array are initialized in fss_init using the following formula:
*
* (FSS_DECAY_MAX - FSS_DECAY_MIN) * i
* FSS_DECAY_MIN + -------------------------------------
* FSS_NICE_RANGE - 1
*
* - FSS_DECAY_MIN is 82 = approximates 65% (82/128)
* - FSS_DECAY_MAX is 108 = approximates 85% (108/128)
* - FSS_NICE_RANGE is 40 (range is 0 - 39)
*
* b) The second thing fss_decay_usage does is update each project's "usage"
* for the last second and then recalculates the project's "share usage".
*
* The usage value is the recent CPU usage for all of the threads in the
* project. It is decayed and updated this way:
*
* (usage * FSS_DECAY_USG)
* usage = ------------------------- + ticks;
* FSS_DECAY_BASE
*
* - FSS_DECAY_BASE is 128 - used instead of 100 so we can shift vs divide
* - FSS_DECAY_USG is 96 - approximates 75% (96/128)
* - ticks is updated whenever a process in this project is running
* when the scheduler's tick processing fires. This is not a simple
* counter, the values are based on the entries in the fss_nice_tick
* array (see section 3 below). ticks is then reset to 0 so it can track
* the next seconds worth of nice-adjusted time for the project.
*
* c) The third thing fss_decay_usage does is update each project's "share
* usage" (shusage). This is the normalized usage value for the project and
* is calculated this way:
*
* pset_shares^2 zone_int_shares^2
* usage * ------------- * ------------------
* kpj_shares^2 zone_ext_shares^2
*
* - usage - see (1b) for more details
* - pset_shares is the total of all *active* zone shares in the pset (by
* default there is only one pset)
* - kpj_shares is the individual project's share (project.cpu-shares rctl)
* - zone_int_shares is the sum of shares of all active projects within the
* zone (the zone-internal total)
* - zone_ext_shares is the share value for the zone (zone.cpu-shares rctl)
*
* The shusage is used in step (2b) to calculate the thread's new internal
* priority. A larger shusage value leads to a lower priority.
*
* 2) The fss_update function then calls fss_update_list to update the priority
* of all threads. This does two things.
*
* a) First the thread's internal priority is decayed using the following
* formula:
*
* fsspri * fss_nice_decay[nice_value])
* fsspri = ------------------------------------
* FSS_DECAY_BASE
*
* - FSS_DECAY_BASE is 128 as described above
*
* b) Second, if the thread is runnable (TS_RUN or TS_WAIT) calls fss_newpri
* to update the user-mode priority (umdpri) of the runnable thread.
* Threads that are running (TS_ONPROC) or waiting for an event (TS_SLEEP)
* are not updated at this time. The updated user-mode priority can cause
* threads to change their position in the run queue.
*
* The process's new internal fsspri is calculated using the following
* formula. All runnable threads in the project will use the same shusage
* and nrunnable values in their calculation.
*
* fsspri += shusage * nrunnable * ticks
*
* - shusage is the project's share usage, calculated in (1c)
* - nrunnable is the number of runnable threads in the project
* - ticks is the number of ticks this thread ran since the last fss_newpri
* invocation.
*
* Finally the process's new user-mode priority is calculated using the
* following formula:
*
* (fsspri * umdprirange)
* umdpri = maxumdpri - ------------------------
* maxfsspri
*
* - maxumdpri is MINCLSYSPRI - 1 (i.e. 59)
* - umdprirange is maxumdpri - 1 (i.e. 58)
* - maxfsspri is the largest fsspri seen so far, as we're iterating all
* runnable processes
*
* Thus, a higher internal priority (fsspri) leads to a lower user-mode
* priority which means the thread runs less. The fsspri is higher when
* the project's normalized share usage is higher, when the project has
* more runnable threads, or when the thread has accumulated more run-time.
*
* This code has various checks to ensure the resulting umdpri is in the
* range 1-59. See fss_newpri for more details.
*
* To reiterate, the above processing is performed once per second to recompute
* the runnable thread user-mode priorities.
*
* 3) The final major component in the priority calculation is the tick
* processing which occurs on a thread that is running when the clock
* calls fss_tick.
*
* A thread can run continuously in user-land (compute-bound) for the
* fss_quantum (see "dispadmin -c FSS -g" for the configurable properties).
* The fss_quantum defaults to 11 (i.e. 11 ticks).
*
* Once the quantum has been consumed, the thread will call fss_newpri to
* recompute its umdpri priority, as described above in (2b). Threads that
* were T_ONPROC at the one second interval when runnable thread priorities
* were recalculated will have their umdpri priority recalculated when their
* quanta expires.
*
* To ensure that runnable threads within a project see the expected
* round-robin behavior, there is a special case in fss_newpri for a thread
* that has run for its quanta within the one second update interval. See
* the handling for the quanta_up parameter within fss_newpri.
*
* Also of interest, the fss_tick code increments the project's tick value
* using the fss_nice_tick array entry for the thread's nice value. The idea
* behind the fss_nice_tick array is that the cost of a tick is lower at
* positive nice values (so that it doesn't increase the project's usage
* as much as normal) with a 50% drop at the maximum level and a 50%
* increase at the minimum level. See (1b). The fss_nice_tick array is
* initialized in fss_init using the following formula:
*
* FSS_TICK_COST * (((3 * FSS_NICE_RANGE) / 2) - i)
* --------------------------------------------------
* FSS_NICE_RANGE
*
* - FSS_TICK_COST is 1000, the tick cost for threads with nice level 0
*
* FSS Data Structures:
*
* fsszone
* ----- -----
* ----- | | | |
* | |-------->| |<------->| |<---->...
* | | ----- -----
* | | ^ ^ ^
* | |--- | \ \
* ----- | | \ \
* fsspset | | \ \
* | | \ \
* | ----- ----- -----
* -->| |<--->| |<--->| |
* | | | | | |
* ----- ----- -----
* fssproj
*
* That is, fsspsets contain a list of fsszone's that are currently active in
* the pset, and a list of fssproj's, corresponding to projects with runnable
* threads on the pset. fssproj's in turn point to the fsszone which they
* are a member of.
*
* An fssproj_t is removed when there are no threads in it.
*
* An fsszone_t is removed when there are no projects with threads in it.
*/
"FSS",
0
};
extern struct mod_ops mod_schedops;
/*
* Module linkage information for the kernel.
*/
};
};
/*
* The fssproc_t structures are kept in an array of circular doubly linked
* lists. A hash on the thread pointer is used to determine which list each
* thread should be placed in. Each list has a dummy "head" which is never
* removed, so the list is never empty. fss_update traverses these lists to
* update the priorities of threads that have been waiting on the run queue.
*/
{ \
mutex_enter(lockp); \
mutex_exit(lockp); \
}
{ \
mutex_enter(lockp); \
mutex_exit(lockp); \
}
/*
* Decay rate percentages are based on n/128 rather than n/100 so that
* calculations can avoid having to do an integer divide by 100 (divide
* by FSS_DECAY_BASE == 128 optimizes to an arithmetic shift).
*
* FSS_DECAY_MIN = 83/128 ~= 65%
* FSS_DECAY_MAX = 108/128 ~= 85%
* FSS_DECAY_USG = 96/128 ~= 75%
*/
#define FSS_NICE_MIN 0
static void fss_update(void *);
static int fss_update_list(int);
static int fss_getclinfo(void *);
static int fss_parmsin(void *);
static int fss_parmsout(void *, pc_vaparms_t *);
static int fss_vaparmsin(void *, pc_vaparms_t *);
static int fss_vaparmsout(void *, pc_vaparms_t *);
static int fss_getclpri(pcpri_t *);
static int fss_alloc(void **, int);
static void fss_free(void *);
static void fss_exitclass(void *);
static void fss_parmsget(kthread_t *, void *);
static void fss_active(kthread_t *);
static void fss_inactive(kthread_t *);
static void fss_trapret(kthread_t *);
static void fss_preempt(kthread_t *);
static void fss_setrun(kthread_t *);
static void fss_wakeup(kthread_t *);
static void fss_nullsys();
/* class functions */
/* thread functions */
fss_nullsys, /* set_process_group */
};
int
_init()
{
return (mod_install(&modlinkage));
}
int
_fini()
{
return (EBUSY);
}
int
{
}
/*ARGSUSED*/
static int
{
return (0);
}
void *
{
void **fsslist;
int cnt;
int i;
switch (op) {
case FSS_NPSET_BUF:
break;
case FSS_NPROJ_BUF:
break;
case FSS_ONE_BUF:
cnt = 1;
break;
}
switch (type) {
case FSS_ALLOC_PROJ:
break;
case FSS_ALLOC_ZONE:
break;
}
for (i = 0; i < cnt; i++)
return (fssbuf);
}
void
{
void **fsslist;
int i;
switch (type) {
case FSS_ALLOC_PROJ:
break;
case FSS_ALLOC_ZONE:
break;
}
}
}
static fsspset_t *
{
int i;
int found = 0;
/*
* Search for the cpupart pointer in the array of fsspsets.
*/
for (i = 0; i < max_ncpus; i++) {
found = 1;
break;
}
}
if (found == 0) {
/*
* If we didn't find anything, then use the first
* available slot in the fsspsets array.
*/
for (i = 0; i < max_ncpus; i++) {
found = 1;
break;
}
}
}
return (fsspset);
}
static void
{
fsspset->fssps_maxfsspri = 0;
fsspset->fssps_shares = 0;
}
/*
* The following routine returns a pointer to the fsszone structure which
* belongs to zone "zone" and cpu partition fsspset, if such structure exists.
*/
static fsszone_t *
{
/*
* already. Try to find our zone among them.
*/
do {
return (fsszone);
}
}
return (NULL);
}
/*
* The following routine links new fsszone structure into doubly linked list of
* zones active on the specified cpu partition.
*/
static void
{
/*
* This will be the first fsszone for this fsspset
*/
} else {
/*
* Insert this fsszone to the doubly linked list.
*/
}
}
/*
* The following routine removes a single fsszone structure from the doubly
* linked list of zones active on the specified cpu partition. Note that
* global fsspsets_lock must be held in case this fsszone structure is the last
* on the above mentioned list. Also note that the fsszone structure is not
* freed here, it is the responsibility of the caller to call kmem_free for it.
*/
static void
{
/*
* This is not the last zone in the list.
*/
} else {
/*
* This was the last zone active in this cpu partition.
*/
}
}
/*
* The following routine returns a pointer to the fssproj structure
* which belongs to project kpj and cpu partition fsspset, if such structure
* exists.
*/
static fssproj_t *
{
/*
* There are projects running on this cpu partition already.
* Try to find our project among them.
*/
do {
return (fssproj);
}
}
return (NULL);
}
/*
* The following routine links new fssproj structure into doubly linked list
* of projects running on the specified cpu partition.
*/
static void
{
fsspset->fssps_nproj++;
/*
* This will be the first fssproj for this fsspset
*/
} else {
/*
* Insert this fssproj to the doubly linked list.
*/
}
fsszone->fssz_nproj++;
}
/*
* The following routine removes a single fssproj structure from the doubly
* linked list of projects running on the specified cpu partition. Note that
* global fsspsets_lock must be held in case if this fssproj structure is the
* last on the above mentioned list. Also note that the fssproj structure is
* not freed here, it is the responsibility of the caller to call kmem_free
* for it.
*/
static void
{
fsspset->fssps_nproj--;
fsszone->fssz_nproj--;
/*
* This is not the last part in the list.
*/
if (fsszone->fssz_nproj == 0)
} else {
/*
* This was the last project part running
* at this cpu partition.
*/
}
}
static void
{
ASSERT(THREAD_LOCK_HELD(t));
return;
if (--fssproj->fssp_runnable == 0) {
if (--fsszone->fssz_runnable == 0)
}
fssproc->fss_runnable = 0;
}
static void
{
ASSERT(THREAD_LOCK_HELD(t));
return;
}
}
/*
* Fair share scheduler initialization. Called by dispinit() at boot time.
* We can ignore clparmsz argument since we know that the smallest possible
* parameter buffer is big enough for us.
*/
/*ARGSUSED*/
static pri_t
{
int i;
fss_minglobpri = 0;
/*
* Initialize the fssproc hash table.
*/
for (i = 0; i < FSS_LISTS; i++)
&fss_listhead[i];
*clfuncspp = &fss_classfuncs;
/*
* Fill in fss_nice_tick and fss_nice_decay arrays:
* The cost of a tick is lower at positive nice values (so that it
* will not increase its project's usage as much as normal) with 50%
* drop at the maximum level and 50% increase at the minimum level.
* The fsspri decay is slower at positive nice values. fsspri values
* of processes with negative nice levels must decay faster to receive
* time slices more frequently than normal.
*/
for (i = 0; i < FSS_NICE_RANGE; i++) {
- i)) / FSS_NICE_RANGE;
fss_nice_decay[i] = FSS_DECAY_MIN +
((FSS_DECAY_MAX - FSS_DECAY_MIN) * i) /
(FSS_NICE_RANGE - 1);
}
return (fss_maxglobpri);
}
/*
* Calculate the new fss_umdpri based on the usage, the normalized share usage
* and the number of active threads. Reset the tick counter for this thread.
*
* When calculating the new priority using the standard formula we can hit
* a scenario where we don't have good round-robin behavior. This would be
* most commonly seen when there is a zone with lots of runnable threads.
* In the bad scenario we will see the following behavior when using the
* standard formula and these conditions:
*
* - there are multiple runnable threads in the zone (project)
* - the fssps_maxfsspri is a very large value
* - (we also know all of these threads will use the project's
* fssp_shusage)
*
* Under these conditions, a thread with a low fss_fsspri value is chosen
* to run and the thread gets a high fss_umdpri. This thread can run for
* its full quanta (fss_timeleft) at which time fss_newpri is called to
* calculate the thread's new priority.
*
* In this case, because the newly calculated fsspri value is much smaller
* (orders of magnitude) than the fssps_maxfsspri value, if we used the
* standard formula the thread will still get a high fss_umdpri value and
* will run again for another quanta, even though there are other runnable
* threads in the project.
*
* For a thread that is runnable for a long time, the thread can continue
* to run for many quanta (totaling many seconds) before the thread's fsspri
* exceeds the fssps_maxfsspri and the thread's fss_umdpri is reset back
* down to 1. This behavior also keeps the fssps_maxfsspr at a high value,
* so that the next runnable thread might repeat this cycle.
*
* This leads to the case where we don't have round-robin behavior at quanta
* granularity, but instead, runnable threads within the project only run
* at several second intervals.
*
* To prevent this scenario from occuring, when a thread has consumed its
* quanta and there are multiple runnable threads in the project, we
* immediately cause the thread to hit fssps_maxfsspri so that it gets
* reset back to 1 and another runnable thread in the project can run.
*/
static void
{
return;
/*
* No need to change priority of exited threads.
*/
return;
/*
* Special case: threads with no shares.
*/
return;
}
} else {
/*
* fsspri += fssp_shusage * nrunnable * ticks
* If all three values are non-0, this typically calculates to
* a large number (sometimes > 1M, sometimes > 100B) due to
* fssp_shusage which can be > 1T.
*/
}
/*
* fss_maxumdpri is normally 59, since FSS priorities are 0-59.
* If the previous calculation resulted in 0 (e.g. was 0 and added 0
* because ticks == 0), then instead of 0, we use the largest priority,
* which is still small in comparison to the large numbers we typically
* see.
*/
if (fsspri < fss_maxumdpri)
/*
* The general priority formula:
*
* (fsspri * umdprirange)
* pri = maxumdpri - ------------------------
* maxfsspri
*
* If this thread's fsspri is greater than the previous largest
* fsspri, then record it as the new high and priority for this
* thread will be one (the lowest priority assigned to a thread
* that has non-zero shares). Because of this check, maxfsspri can
* change as this function is called via the
* fss_update -> fss_update_list -> fss_newpri code path to update
* all runnable threads. See the code in fss_update for how we
* mitigate this issue.
*
* Note that this formula cannot produce out of bounds priority
* values (0-59); if it is changed, additional checks may need to be
* added.
*/
} else {
}
}
/*
* Decays usages of all running projects, resets their tick counters and
* calcluates the projects normalized share usage. Called once per second from
* fss_update().
*/
static void
{
int psetid;
/*
* Go through all active processor sets and decay usages of projects
* running on them.
*/
continue;
}
/*
* Decay maxfsspri for this cpu partition with the
* fastest possible decay rate.
*/
if (maxfsspri < fss_maxumdpri)
do {
/*
* Reset zone's FSS stats if they are from a
* previous cycle.
*/
zp->zone_run_ticks = 0;
}
/*
* Decay project usage, then add in this cycle's
* nice tick value.
*/
fssproj->fssp_ticks = 0;
fssproj->fssp_tick_cnt = 0;
/*
* Readjust the project's number of shares if it has
* changed since we checked it last time.
*/
if (fssproj->fssp_runnable != 0) {
fsszone->fssz_shares -=
}
}
/*
* Readjust the zone's number of shares if it
* has changed since we checked it last time.
*/
if (fsszone->fssz_runnable != 0) {
fsspset->fssps_shares -=
fsspset->fssps_shares +=
}
}
/*
* If anything is runnable in the project, track the
* overall project share percent for monitoring useage.
*/
if (fssproj->fssp_runnable > 0) {
/*
* Times 1000 to get tenths of a percent
*
* zone_ext_shares
* zone_shr_pct = ---------------
* pset_shares
*
* kpj_shares
* int_shr_pct = ---------------
* zone_int_shares
*/
if (pset_shares == 0 || zone_int_shares == 0) {
fssproj->fssp_shr_pct = 0;
} else {
(zone_ext_shares * 1000) /
(zone_shr_pct * int_shr_pct) /
1000;
}
} else {
fssproj);
}
/*
* Calculate fssp_shusage value to be used
* for fsspri increments for the next second.
*/
if (kpj_shares == 0 || zone_ext_shares == 0) {
fssproj->fssp_shusage = 0;
/*
* Project 0 in the global zone has 50%
* of its zone. See calculation above for
* the zone's share percent.
*/
if (pset_shares == 0)
zone_shr_pct = 1000;
else
(zone_ext_shares * 1000) /
} else {
/*
* Thread's priority is based on its project's
* normalized usage (shusage) value which gets
* calculated this way:
*
* pset_shares^2 zone_int_shares^2
* usage * ------------- * ------------------
* kpj_shares^2 zone_ext_shares^2
*
* Where zone_int_shares is the sum of shares
* of all active projects within the zone (and
* the pset), and zone_ext_shares is the number
* of zone shares (ie, zone.cpu-shares).
*
* If there is only one zone active on the pset
* the above reduces to:
*
* zone_int_shares^2
* shusage = usage * ---------------------
* kpj_shares^2
*
* If there's only one project active in the
* zone this formula reduces to:
*
* pset_shares^2
* shusage = usage * ----------------------
* zone_ext_shares^2
*
* shusage is one input to calculating fss_pri
* in fss_newpri(). Larger values tend toward
* lower priorities for processes in the proj.
*/
fssproj->fssp_shusage /=
fssproj->fssp_shusage *=
fssproj->fssp_shusage /=
}
}
}
static void
{
ASSERT(THREAD_LOCK_HELD(t));
/*
* curthread is always onproc
*/
THREAD_CHANGE_PRI(t, new_pri);
if (t == cp->cpu_dispthread)
if (DISP_MUST_SURRENDER(t)) {
cpu_surrender(t);
} else {
}
} else {
/*
* When the priority of a thread is changed, it may be
* necessary to adjust its position on a sleep queue or
* dispatch queue. The function thread_change_pri accomplishes
* this.
*/
if (thread_change_pri(t, new_pri, 0)) {
/*
* The thread was on a run queue.
*/
} else {
}
}
}
/*
* Update priorities of all fair-sharing threads that are currently runnable
* at a user mode priority based on the number of shares and current usage.
* Called once per second via timeout which we reset here.
*
* There are several lists of fair-sharing threads broken up by a hash on the
* thread pointer. Each list has its own lock. This avoids blocking all
* fss_enterclass, fss_fork, and fss_exitclass operations while fss_update runs.
* fss_update traverses each list in turn.
*
* through all of the lists. By starting with a different list, we mitigate any
* effects we would see updating the fssps_maxfsspri value in fss_newpri.
*/
static void
{
int i;
static int fss_update_marker;
/*
* Decay and update usages for all projects.
*/
/*
* Start with the fss_update_marker list, then do the rest.
*/
i = fss_update_marker;
/*
* Go around all threads, set new priorities and decay
* per-thread CPU usages.
*/
do {
/*
* If this is the first list after the current marker to have
* threads with priority updates, advance the marker to this
* list for the next time fss_update runs.
*/
if (fss_update_list(i) &&
new_marker = i;
} while ((i = FSS_LIST_NEXT(i)) != fss_update_marker);
/*
* Advance marker for the next fss_update call
*/
if (new_marker != -1)
}
/*
* Updates priority for a list of threads. Returns 1 if the priority of one
* of the threads was actually updated, 0 if none were for various reasons
* (thread is no longer in the FSS class, is not runnable, has the preemption
* control no-preempt bit set, etc.)
*/
static int
fss_update_list(int i)
{
kthread_t *t;
int updated = 0;
mutex_enter(&fss_listlock[i]);
/*
* Lock the thread and verify the state.
*/
thread_lock(t);
/*
* Skip the thread if it is no longer in the FSS class or
* is running with kernel mode priority.
*/
goto next;
goto next;
goto next;
if (fssproj->fssp_shares != 0) {
/*
* Decay fsspri value.
*/
}
if (t->t_schedctl && schedctl_get_nopreempt(t))
goto next;
/*
*/
t->t_trapret = 1;
aston(t);
fssproc);
goto next;
}
updated = 1;
/*
* Only dequeue the thread if it needs to be moved; otherwise
* it should just round-robin here.
*/
if (t->t_pri != fss_umdpri)
next:
thread_unlock(t);
}
mutex_exit(&fss_listlock[i]);
return (updated);
}
/*ARGSUSED*/
static int
{
return (EFAULT);
case FSS_SETADMIN:
if (secpolicy_dispadm(reqpcredp) != 0)
return (EPERM);
return (EINVAL);
break;
case FSS_GETADMIN:
return (EFAULT);
break;
default:
return (EINVAL);
}
return (0);
}
static int
{
return (0);
}
static int
{
/*
* Check validity of parameters.
*/
return (EINVAL);
return (EINVAL);
return (0);
}
/*ARGSUSED*/
static int
{
return (0);
}
static int
{
int priflag = 0;
int limflag = 0;
/*
* FSS_NOCHANGE (-32768) is outside of the range of values for
* fss_uprilim and fss_upri. If the structure fssparms_t is changed,
* FSS_NOCHANGE should be replaced by a flag word.
*/
/*
* Get the varargs parameter and check validity of parameters.
*/
return (EINVAL);
case FSS_KY_UPRILIM:
if (limflag++)
return (EINVAL);
return (EINVAL);
break;
case FSS_KY_UPRI:
if (priflag++)
return (EINVAL);
return (EINVAL);
break;
default:
return (EINVAL);
}
}
if (vaparmsp->pc_vaparmscnt == 0) {
/*
* Use default parameters.
*/
}
return (0);
}
/*
* Copy all selected fair-sharing class parameters to the user. The parameters
* are specified by a key.
*/
static int
{
int priflag = 0;
int limflag = 0;
return (EINVAL);
case FSS_KY_UPRILIM:
if (limflag++)
return (EINVAL);
return (EFAULT);
break;
case FSS_KY_UPRI:
if (priflag++)
return (EINVAL);
return (EFAULT);
break;
default:
return (EINVAL);
}
}
return (0);
}
/*
* Return the user mode scheduling priority range.
*/
static int
{
return (0);
}
static int
{
void *bufp;
return (ENOMEM);
} else {
*p = bufp;
return (0);
}
}
static void
{
if (bufp)
}
/*
* Thread functions
*/
static int
void *bufp)
{
int fsszone_allocated = 0;
/*
* Only root can move threads to FSS class.
*/
return (EPERM);
/*
* Initialize the fssproc structure.
*/
/*
* Use default values.
*/
} else {
/*
* Use supplied values.
*/
reqfssuprilim = 0;
} else {
if (fssparmsp->fss_uprilim > 0 &&
secpolicy_setpriority(reqpcredp) != 0)
return (EPERM);
}
} else {
secpolicy_setpriority(reqpcredp) != 0)
return (EPERM);
/*
* Set the user priority to the requested value or
* the upri limit, whichever is lower.
*/
if (reqfssupri > reqfssuprilim)
}
}
/*
* Put a lock on our fsspset structure.
*/
== NULL) {
return (ENOMEM);
} else {
fsszone_allocated = 1;
}
}
== NULL) {
if (fsszone_allocated) {
}
return (ENOMEM);
} else {
}
}
fssproj->fssp_threads++;
/*
* Reset priority. Process goes to a "user mode" priority here
* regardless of whether or not it has slept since entering the kernel.
*/
thread_lock(t);
t->t_schedflag |= TS_RUNQMATCH;
fss_active(t);
thread_unlock(t);
/*
* Link new structure into fssproc list.
*/
/*
* If this is the first fair-sharing thread to occur since boot,
* we set up the initial call to fss_update() here. Use an atomic
* compare-and-swap since that's easier and faster than a mutex
* (but check with an ordinary load first since most of the time
* this will already be done).
*/
return (0);
}
/*
* Remove fssproc_t from the list.
*/
static void
{
/*
* We should be either getting this thread off the deathrow or
* this thread has already moved to another scheduling class and
* we're being called with its old cldata buffer pointer. In both
* cases, the content of this buffer can not be changed while we're
* here.
*/
thread_lock(t);
/*
* We're being called as a result of the priocntl() system
* call -- someone is trying to move our thread to another
* scheduling class. We can't call fss_inactive() here
* because our thread's t_cldata pointer already points
* to another scheduling class specific data.
*/
if (fssproc->fss_runnable) {
if (--fssproj->fssp_runnable == 0) {
if (--fsszone->fssz_runnable == 0)
fsspset->fssps_shares -=
}
}
thread_unlock(t);
if (--fssproj->fssp_threads == 0) {
if (fsszone->fssz_nproj == 0)
}
} else {
/*
* We're being called from thread_free() when our thread
* is removed from the deathrow. There is nothing we need
* do here since everything should've been done earlier
* in fss_exit().
*/
thread_unlock(t);
}
}
/*ARGSUSED*/
static int
{
/*
* A thread is allowed to exit FSS only if we have sufficient
* privileges.
*/
return (EPERM);
else
return (0);
}
/*
* Initialize fair-share class specific proc structure for a child.
*/
static int
{
/*
* Initialize child's fssproc structure.
*/
cfssproc->fss_fsspri = 0;
fssproj->fssp_threads++;
/*
* Link new structure into fssproc hash table.
*/
return (0);
}
/*
* Child is placed at back of dispatcher queue and parent gives up processor
* so that the child runs first after the fork. This allows the child
* immediately execing to break the multiple use of copy on write pages with no
* disk home. The parent will get to steal them back rather than uselessly
* copying them.
*/
static void
{
/*
* Grab the child's p_lock before dropping pidlock to ensure the
* process does not disappear before we set it running.
*/
thread_lock(t);
/*
* We don't want to call fss_setrun(t) here because it may call
* fss_active, which we don't need.
*/
if (t->t_disp_time != ddi_get_lbolt())
setbackdq(t);
else
setfrontdq(t);
thread_unlock(t);
/*
* Safe to drop p_lock now since it is safe to change
* the scheduling class after this point.
*/
swtch();
}
/*
* Get the fair-sharing parameters of the thread pointed to by fssprocp into
* the buffer pointed by fssparmsp.
*/
static void
{
}
/*ARGSUSED*/
static int
{
char nice;
else
else
/*
* Make sure the user priority doesn't exceed the upri limit.
*/
if (reqfssupri > reqfssuprilim)
/*
* Basic permissions enforced by generic kernel code for all classes
* require that a thread attempting to change the scheduling parameters
* of a target thread be privileged or have a real or effective UID
* matching that of the target thread. We are not called unless these
* basic permission checks have already passed. The fair-sharing class
* requires in addition that the calling thread be privileged if it
* is attempting to raise the upri limit above its current value.
* This may have been checked previously but if our caller passed us
* a non-NULL credential pointer we assume it hasn't and we check it
* here.
*/
secpolicy_raisepriority(reqpcredp) != 0)
return (EPERM);
/*
* Set fss_nice to the nice value corresponding to the user priority we
* are setting. Note that setting the nice field of the parameter
* struct won't affect upri or nice.
*/
if (nice > FSS_NICE_MAX)
nice = FSS_NICE_MAX;
thread_lock(t);
thread_unlock(t);
return (0);
}
thread_unlock(t);
return (0);
}
/*
* The thread is being stopped.
*/
/*ARGSUSED*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
fss_inactive(t);
}
/*
* The current thread is exiting, do necessary adjustments to its project
*/
static void
{
int free = 0;
/*
* Thread t here is either a current thread (in which case we hold
* its process' p_lock), or a thread being destroyed by forklwp_fail(),
* in which case we hold pidlock and thread is no longer on the
* thread list.
*/
thread_lock(t);
if (--fssproj->fssp_runnable == 0) {
if (--fsszone->fssz_runnable == 0)
}
fssproc->fss_runnable = 0;
}
if (--fssproj->fssp_threads == 0) {
free = 1;
}
thread_unlock(t);
if (free) {
if (fsszone->fssz_nproj == 0)
}
/*
* A thread could be exiting in between clock ticks, so we need to
* calculate how much CPU time it used since it was charged last time.
*
* CPU caps are not enforced on exiting processes - it is usually
* desirable to exit as soon as possible to free resources.
*/
if (CPUCAPS_ON()) {
thread_lock(t);
thread_unlock(t);
}
}
static void
{
}
/*
* fss_swapin() returns -1 if the thread is loaded or is not eligible to be
* swapped in. Otherwise, it returns the thread's effective priority based
* on swapout time and size of process (0 <= epri <= 0 SHRT_MAX).
*/
/*ARGSUSED*/
static pri_t
{
ASSERT(THREAD_LOCK_HELD(t));
} else {
/*
* Threads which have been out for a long time,
* have high user mode priority and are associated
* with a small address space are more deserving.
*/
}
/*
* Scale epri so that SHRT_MAX / 2 represents zero priority.
*/
if (epri < 0)
epri = 0;
}
}
/*
* fss_swapout() returns -1 if the thread isn't loaded or is not eligible to
* be swapped out. Otherwise, it returns the thread's effective priority
* based on if the swapper is in softswap or hardswap mode.
*/
static pri_t
{
ASSERT(THREAD_LOCK_HELD(t));
if (INHERITED(t) ||
(t->t_proc_flag & TP_LWPEXIT) ||
!(t->t_schedflag & TS_LOAD) ||
!(SWAP_OK(t)))
return (-1);
epri = 0;
} else {
}
} else {
pri = fss_maxumdpri;
epri = swapin_time -
} else {
}
}
/*
* Scale epri so that SHRT_MAX / 2 represents zero priority.
*/
if (epri < 0)
epri = 0;
}
/*
* If thread is currently at a kernel mode priority (has slept) and is
* returning to the userland we assign it the appropriate user mode priority
* and time quantum here. If we're lowering the thread's priority below that
* of other runnable threads then we will set runrun via cpu_surrender() to
* cause preemption.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
t->t_kpri_req = 0;
/*
* If thread has blocked in the kernel
*/
if (DISP_MUST_SURRENDER(t))
cpu_surrender(t);
}
/*
* Swapout lwp if the swapper is waiting for this thread to reach
* a safe point.
*/
if (t->t_schedflag & TS_SWAPENQ) {
thread_unlock(t);
swapout_lwp(ttolwp(t));
thread_lock(t);
}
}
/*
* Arrange for thread to be placed in appropriate location on dispatcher queue.
* This is called with the current thread in TS_ONPROC and locked.
*/
static void
{
/*
* If preempted in the kernel, make sure the thread has a kernel
* priority if needed.
*/
aston(t);
}
/*
* This thread may be placed on wait queue by CPU Caps. In this case we
* do not need to do anything until it is removed from the wait queue.
* Do not enforce CPU caps on threads running at a kernel priority
*/
if (CPUCAPS_ON()) {
return;
}
/*
* If preempted in user-land mark the thread as swappable because it
* cannot be holding any kernel locks.
*/
t->t_schedflag &= ~TS_DONT_SWAP;
/*
* Check to see if we're doing "preemption control" here. If
* we are, and if the user has requested that this thread not
* be preempted, and if preemptions haven't been put off for
* too long, let the preemption happen here but try to make
* sure the thread is rescheduled as soon as possible. We do
* this by putting it on the front of the highest priority run
* queue in the FSS class. If the preemption has been put off
* for too long, clear the "nopreempt" bit and let the thread
* be preempted.
*/
if (t->t_schedctl && schedctl_get_nopreempt(t)) {
/*
* If not already remembered, remember current
* priority for restoration in fss_yield().
*/
}
t->t_schedflag |= TS_DONT_SWAP;
}
schedctl_set_yield(t, 1);
setfrontdq(t);
return;
} else {
}
schedctl_set_nopreempt(t, 0);
/*
* Fall through and be preempted below.
*/
}
}
setbackdq(t);
setbackdq(t);
} else {
setfrontdq(t);
}
}
/*
* Called when a thread is waking up and is to be placed on the run queue.
*/
static void
{
fss_active(t);
/*
* If previously were running at the kernel priority then keep that
* priority and the fss_timeleft doesn't matter.
*/
if (t->t_disp_time != ddi_get_lbolt())
setbackdq(t);
else
setfrontdq(t);
}
/*
* Prepare thread for sleep. We reset the thread priority so it will run at the
* kernel priority level when it wakes up.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
/*
* Account for time spent on CPU before going to sleep.
*/
fss_inactive(t);
/*
* Assign a system priority to the thread and arrange for it to be
* retained when the thread is next placed on the run queue (i.e.,
* when it wakes up) instead of being given a new pri. Also arrange
* for trapret processing as the thread leaves the system call so it
* will drop back to normal priority range.
*/
if (t->t_kpri_req) {
aston(t);
/*
* The thread has done a THREAD_KPRI_REQUEST(), slept, then
* done THREAD_KPRI_RELEASE() (so no t_kpri_req is 0 again),
* then slept again all without finishing the current system
* call so trapret won't have cleared FSSKPRI
*/
if (DISP_MUST_SURRENDER(curthread))
cpu_surrender(t);
}
}
/*
* A tick interrupt has ocurrend on a running thread. Check to see if our
* time slice has expired. We must also clear the TS_DONT_SWAP flag in
* t_schedflag if the thread is eligible to be swapped out.
*/
static void
{
/*
* It's safe to access fsspset and fssproj structures because we're
* holding our p_lock here.
*/
thread_lock(t);
fssproj->fssp_tick_cnt++;
}
/*
* Keep track of thread's project CPU usage. Note that projects
* get charged even when threads are running in the kernel.
* Do not surrender CPU if running in the SYS class.
*/
if (CPUCAPS_ON()) {
}
/*
* A thread's execution time for threads running in the SYS class
* is not tracked.
*/
/*
* If thread is not in kernel mode, decrement its fss_timeleft
*/
if (--fssproc->fss_timeleft <= 0) {
/*
* If we're doing preemption control and trying to
* avoid preempting this thread, just note that the
* thread should yield soon and let it keep running
* (unless it's been a while).
*/
if (t->t_schedctl && schedctl_get_nopreempt(t)) {
kthread_t *, t);
schedctl_set_yield(t, 1);
return;
}
}
/*
* When the priority of a thread is changed, it may
* be necessary to adjust its position on a sleep queue
* or dispatch queue. The function thread_change_pri
* accomplishes this.
*/
if (thread_change_pri(t, new_pri, 0)) {
if ((t->t_schedflag & TS_LOAD) &&
t->t_schedflag &= ~TS_DONT_SWAP;
} else {
}
/*
* If there is a higher-priority thread which is
* waiting for a processor, then thread surrenders
* the processor.
*/
}
}
/*
* The thread used more than half of its quantum, so assume that
* it used the whole quantum.
*
* Update thread's priority just before putting it on the wait
* queue so that it gets charged for the CPU time from its
* quantum even before that quantum expires.
*/
/*
* We need to call cpu_surrender for this thread due to cpucaps
* enforcement, but fss_change_priority may have already done
* so. In this case FSSBACKQ is set and there is no need to call
* cpu-surrender again.
*/
}
if (call_cpu_surrender) {
cpu_surrender(t);
}
thread_unlock_nopreempt(t); /* clock thread can't be preempted */
}
/*
* Processes waking up go to the back of their queue. We don't need to assign
* a time quantum here because thread is still at a kernel mode priority and
* the time slicing is not done for threads running in the kernel after
* sleeping. The proper time quantum will be assigned by fss_trapret before the
* thread returns to user mode.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
fss_active(t);
/*
* If we already have a kernel priority assigned, then we
* just use it.
*/
setbackdq(t);
} else if (t->t_kpri_req) {
/*
* Give thread a priority boost if we were asked.
*/
setbackdq(t);
aston(t);
} else {
/*
* Otherwise, we recalculate the priority.
*/
if (t->t_disp_time == ddi_get_lbolt()) {
setfrontdq(t);
} else {
setbackdq(t);
}
}
}
/*
* fss_donice() is called when a nice(1) command is issued on the thread to
* alter the priority. The nice(1) command exists in Solaris for compatibility.
* Thread priority adjustments should be done via priocntl(1).
*/
static int
{
int newnice;
/*
* If there is no change to priority, just return current setting.
*/
if (incr == 0) {
if (retvalp)
return (0);
}
return (EPERM);
/*
* Specifying a nice increment greater than the upper limit of
* FSS_NICE_MAX (== 2 * NZERO - 1) will result in the thread's nice
* value being set to the upper limit. We check for this before
* computing the new value because otherwise we could get overflow
* if a privileged user specified some ridiculous increment.
*/
if (incr > FSS_NICE_MAX)
incr = FSS_NICE_MAX;
if (newnice > FSS_NICE_MAX)
else if (newnice < FSS_NICE_MIN)
/*
* Reset the uprilim and upri values of the thread.
*/
/*
* Although fss_parmsset already reset fss_nice it may not have been
* set to precisely the value calculated above because fss_parmsset
* determines the nice value from the user priority and we may have
* truncated during the integer conversion from nice value to user
* priority and back. We reset fss_nice to the value we calculated
* above.
*/
if (retvalp)
return (0);
}
/*
* Increment the priority of the specified thread by incr and
* return the new value in *retvalp.
*/
static int
{
int newpri;
/*
* If there is no change to priority, just return current setting.
*/
if (incr == 0) {
return (0);
}
return (EINVAL);
/*
* Reset the uprilim and upri values of the thread.
*/
}
/*
* Return the global scheduling priority that would be assigned to a thread
* entering the fair-sharing class with the fss_upri.
*/
/*ARGSUSED*/
static pri_t
{
return (fss_maxumdpri / 2);
}
/*
* Called from the yield(2) system call when a thread is yielding (surrendering)
* the processor. The kernel thread is placed at the back of a dispatch queue.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
/*
* Collect CPU usage spent before yielding
*/
/*
* Clear the preemption control "yield" bit since the user is
* doing a yield.
*/
if (t->t_schedctl)
schedctl_set_yield(t, 0);
/*
* If fss_preempt() artifically increased the thread's priority
* to avoid preemption, restore the original priority now.
*/
}
if (fssproc->fss_timeleft < 0) {
/*
* Time slice was artificially extended to avoid preemption,
* so pretend we're preempting it now.
*/
}
setbackdq(t);
}
void
{
int free = 0;
int id;
return;
if (fssproj_old == NULL) {
return;
}
return;
}
/*
* If the zone for the new project is not currently active on
* the cpu partition we're on, get one of the pre-allocated
* buffers and link it in our per-pset zone list. Such buffers
* should already exist.
*/
break;
}
}
}
/*
* If our new project is not currently running
* on the cpu partition we're on, get one of the
* pre-allocated buffers and link it in our new cpu
* partition doubly linked list. Such buffers should already
* exist.
*/
break;
}
}
}
thread_lock(t);
fss_inactive(t);
if (--fssproj_old->fssp_threads == 0) {
free = 1;
}
fssproc->fss_fsspri = 0;
fss_active(t);
thread_unlock(t);
if (free) {
if (fsszone_old->fssz_nproj == 0)
}
}
void
{
int id;
return;
if (fssproj_old == NULL) {
return;
}
return;
}
break;
}
}
}
break;
}
}
}
thread_lock(t);
fss_inactive(t);
fssproc->fss_fsspri = 0;
fss_active(t);
thread_unlock(t);
if (--fssproj_old->fssp_threads == 0) {
if (fsszone_old->fssz_nproj == 0)
}
}