2001-03-21 15:00:29 +01:00
|
|
|
/* sched.cc: scheduler interface for Cygwin
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012,
|
|
|
|
2013 Red Hat, Inc.
|
2001-03-21 15:00:29 +01:00
|
|
|
|
|
|
|
Written by Robert Collins <rbtcollins@hotmail.com>
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2008-04-07 18:15:45 +02:00
|
|
|
#include "miscfuncs.h"
|
2001-03-21 15:00:29 +01:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "pinfo.h"
|
|
|
|
/* for getpid */
|
|
|
|
#include <unistd.h>
|
2003-09-27 05:44:31 +02:00
|
|
|
#include "registry.h"
|
|
|
|
|
2001-03-21 15:00:29 +01:00
|
|
|
/* Win32 priority to UNIX priority Mapping.
|
2013-04-23 11:44:36 +02:00
|
|
|
|
2001-03-21 15:00:29 +01:00
|
|
|
For now, I'm just following the spec: any range of priorities is ok.
|
|
|
|
There are probably many many issues with this...
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
FIXME: We don't support pre-Windows 2000 so we should fix the priority
|
|
|
|
computation. Here's the description for the current code:
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
We don't want process's going realtime. Well, they probably could, but
|
|
|
|
the issues with avoiding the priority values 17-22 and 27-30 (not
|
|
|
|
supported before win2k) make that inefficient.
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
However to complicate things most unixes use lower is better priorities.
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
So we map -14 to 15, and 15 to 1 via (16- ((n+16) >> 1)). We then map 1
|
|
|
|
to 15 to various process class and thread priority combinations. Then we
|
|
|
|
need to look at the threads process priority. As win95, 98 and NT 4
|
|
|
|
don't support opening threads cross-process (unless a thread HANDLE is
|
|
|
|
passed around) for now, we'll just use the priority class.
|
2001-06-26 16:47:48 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
The code and logic are present to calculate the priority for thread, if a
|
|
|
|
thread handle can be obtained. Alternatively, if the symbols wouldn't be
|
|
|
|
resolved until they are used we could support this.
|
|
|
|
|
|
|
|
Lastly, because we can't assume that the pid we're given are Windows pids,
|
|
|
|
we can't alter non-cygwin started programs. */
|
2001-03-21 15:00:29 +01:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
|
|
|
/* max priority for policy */
|
|
|
|
int
|
|
|
|
sched_get_priority_max (int policy)
|
|
|
|
{
|
|
|
|
if (policy < 1 || policy > 3)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return -14;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* min priority for policy */
|
|
|
|
int
|
|
|
|
sched_get_priority_min (int policy)
|
|
|
|
{
|
|
|
|
if (policy < 1 || policy > 3)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
/* Check a scheduler parameter struct for valid settings */
|
|
|
|
int
|
2002-09-23 02:31:31 +02:00
|
|
|
valid_sched_parameters (const struct sched_param *param)
|
2001-04-12 06:04:53 +02:00
|
|
|
{
|
|
|
|
if (param->sched_priority < -14 || param->sched_priority > 15)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-03-21 15:00:29 +01:00
|
|
|
/* get sched params for process
|
|
|
|
|
2001-06-26 16:47:48 +02:00
|
|
|
Note, I'm never returning EPERM,
|
2001-03-21 15:00:29 +01:00
|
|
|
Always ESRCH. This is by design (If cygwin ever looks at paranoid security
|
2001-06-26 16:47:48 +02:00
|
|
|
Walking the pid values is a known hole in some os's)
|
2001-03-21 15:00:29 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sched_getparam (pid_t pid, struct sched_param *param)
|
|
|
|
{
|
|
|
|
pid_t localpid;
|
|
|
|
int winpri;
|
|
|
|
if (!param || pid < 0)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
localpid = pid ? pid : getpid ();
|
|
|
|
|
|
|
|
DWORD Class;
|
|
|
|
int ThreadPriority;
|
|
|
|
HANDLE process;
|
|
|
|
pinfo p (localpid);
|
|
|
|
|
|
|
|
/* get the class */
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
process = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, p->dwProcessId);
|
|
|
|
if (!process)
|
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Class = GetPriorityClass (process);
|
|
|
|
CloseHandle (process);
|
|
|
|
if (!Class)
|
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ThreadPriority = THREAD_PRIORITY_NORMAL;
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
/* calculate the unix priority. */
|
2001-03-21 15:00:29 +01:00
|
|
|
|
|
|
|
switch (Class)
|
|
|
|
{
|
|
|
|
case IDLE_PRIORITY_CLASS:
|
|
|
|
switch (ThreadPriority)
|
|
|
|
{
|
|
|
|
case THREAD_PRIORITY_IDLE:
|
|
|
|
winpri = 1;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_LOWEST:
|
|
|
|
winpri = 2;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_BELOW_NORMAL:
|
|
|
|
winpri = 3;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_NORMAL:
|
|
|
|
winpri = 4;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_ABOVE_NORMAL:
|
|
|
|
winpri = 5;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_HIGHEST:
|
|
|
|
default:
|
|
|
|
winpri = 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HIGH_PRIORITY_CLASS:
|
|
|
|
switch (ThreadPriority)
|
|
|
|
{
|
|
|
|
case THREAD_PRIORITY_IDLE:
|
|
|
|
winpri = 1;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_LOWEST:
|
|
|
|
winpri = 11;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_BELOW_NORMAL:
|
|
|
|
winpri = 12;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_NORMAL:
|
|
|
|
winpri = 13;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_ABOVE_NORMAL:
|
|
|
|
winpri = 14;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_HIGHEST:
|
|
|
|
default:
|
|
|
|
winpri = 15;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NORMAL_PRIORITY_CLASS:
|
|
|
|
default:
|
|
|
|
switch (ThreadPriority)
|
|
|
|
{
|
|
|
|
case THREAD_PRIORITY_IDLE:
|
|
|
|
winpri = 1;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_LOWEST:
|
|
|
|
winpri = 7;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_BELOW_NORMAL:
|
|
|
|
winpri = 8;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_NORMAL:
|
|
|
|
winpri = 9;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_ABOVE_NORMAL:
|
|
|
|
winpri = 10;
|
|
|
|
break;
|
|
|
|
case THREAD_PRIORITY_HIGHEST:
|
|
|
|
default:
|
|
|
|
winpri = 11;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reverse out winpri = (16- ((unixpri+16) >> 1)) */
|
2001-06-26 16:47:48 +02:00
|
|
|
/*
|
2001-03-21 15:00:29 +01:00
|
|
|
winpri-16 = - (unixpri +16 ) >> 1
|
|
|
|
|
|
|
|
-(winpri-16) = unixpri +16 >> 1
|
|
|
|
(-(winpri-16)) << 1 = unixpri+16
|
|
|
|
((-(winpri - 16)) << 1) - 16 = unixpri
|
|
|
|
*/
|
|
|
|
|
|
|
|
param->sched_priority = ((-(winpri - 16)) << 1) - 16;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the scheduler for pid
|
|
|
|
|
|
|
|
All process's on WIN32 run with SCHED_FIFO.
|
2001-06-26 16:47:48 +02:00
|
|
|
So we just give an answer.
|
2001-03-21 15:00:29 +01:00
|
|
|
(WIN32 uses a multi queue FIFO).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sched_getscheduler (pid_t pid)
|
|
|
|
{
|
|
|
|
if (pid < 0)
|
|
|
|
return ESRCH;
|
|
|
|
else
|
|
|
|
return SCHED_FIFO;
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
/* get the time quantum for pid */
|
2001-03-21 15:00:29 +01:00
|
|
|
int
|
|
|
|
sched_rr_get_interval (pid_t pid, struct timespec *interval)
|
|
|
|
{
|
2003-09-27 05:44:31 +02:00
|
|
|
static const char quantable[2][2][3] =
|
|
|
|
{{{12, 24, 36}, { 6, 12, 18}},
|
|
|
|
{{36, 36, 36}, {18, 18, 18}}};
|
|
|
|
/* FIXME: Clocktickinterval can be 15 ms for multi-processor system. */
|
|
|
|
static const int clocktickinterval = 10;
|
|
|
|
static const int quantapertick = 3;
|
|
|
|
|
|
|
|
HWND forwin;
|
|
|
|
DWORD forprocid;
|
2011-04-23 15:15:46 +02:00
|
|
|
DWORD vfindex, slindex, qindex, prisep;
|
2003-09-27 05:44:31 +02:00
|
|
|
long nsec;
|
|
|
|
|
|
|
|
forwin = GetForegroundWindow ();
|
|
|
|
if (!forwin)
|
|
|
|
GetWindowThreadProcessId (forwin, &forprocid);
|
|
|
|
else
|
|
|
|
forprocid = 0;
|
|
|
|
|
2011-04-19 12:02:06 +02:00
|
|
|
reg_key reg (HKEY_LOCAL_MACHINE, KEY_READ, L"SYSTEM", L"CurrentControlSet",
|
|
|
|
L"Control", L"PriorityControl", NULL);
|
2003-09-27 05:44:31 +02:00
|
|
|
if (reg.error ())
|
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-23 15:15:46 +02:00
|
|
|
prisep = reg.get_dword (L"Win32PrioritySeparation", 2);
|
2003-09-27 05:44:31 +02:00
|
|
|
pinfo pi (pid ? pid : myself->pid);
|
|
|
|
if (!pi)
|
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pi->dwProcessId == forprocid)
|
|
|
|
{
|
|
|
|
qindex = prisep & 3;
|
|
|
|
qindex = qindex == 3 ? 2 : qindex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
qindex = 0;
|
|
|
|
vfindex = ((prisep >> 2) & 3) % 3;
|
|
|
|
if (vfindex == 0)
|
2008-09-11 07:43:11 +02:00
|
|
|
vfindex = wincap.is_server () || (prisep & 3) == 0 ? 1 : 0;
|
2003-09-27 05:44:31 +02:00
|
|
|
else
|
|
|
|
vfindex -= 1;
|
|
|
|
slindex = ((prisep >> 4) & 3) % 3;
|
|
|
|
if (slindex == 0)
|
|
|
|
slindex = wincap.is_server () ? 1 : 0;
|
|
|
|
else
|
|
|
|
slindex -= 1;
|
|
|
|
|
|
|
|
nsec = quantable[vfindex][slindex][qindex] / quantapertick
|
|
|
|
* clocktickinterval * 1000000;
|
|
|
|
interval->tv_sec = nsec / 1000000000;
|
|
|
|
interval->tv_nsec = nsec % 1000000000;
|
|
|
|
|
|
|
|
return 0;
|
2001-03-21 15:00:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the scheduling parameters */
|
|
|
|
int
|
|
|
|
sched_setparam (pid_t pid, const struct sched_param *param)
|
|
|
|
{
|
|
|
|
pid_t localpid;
|
|
|
|
int winpri;
|
|
|
|
DWORD Class;
|
|
|
|
HANDLE process;
|
|
|
|
|
|
|
|
if (!param || pid < 0)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-09-23 02:31:31 +02:00
|
|
|
if (!valid_sched_parameters (param))
|
2001-03-21 15:00:29 +01:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* winpri = (16- ((unixpri+16) >> 1)) */
|
|
|
|
winpri = 16 - ((param->sched_priority + 16) >> 1);
|
|
|
|
|
|
|
|
/* calculate our desired priority class and thread priority */
|
|
|
|
|
|
|
|
if (winpri < 7)
|
|
|
|
Class = IDLE_PRIORITY_CLASS;
|
|
|
|
else if (winpri > 10)
|
|
|
|
Class = HIGH_PRIORITY_CLASS;
|
|
|
|
else
|
|
|
|
Class = NORMAL_PRIORITY_CLASS;
|
|
|
|
|
|
|
|
localpid = pid ? pid : getpid ();
|
|
|
|
|
|
|
|
pinfo p (localpid);
|
|
|
|
|
|
|
|
/* set the class */
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
set_errno (ESRCH);
|
2001-03-21 15:00:29 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
process =
|
|
|
|
OpenProcess (PROCESS_SET_INFORMATION, FALSE, (DWORD) p->dwProcessId);
|
|
|
|
if (!process)
|
|
|
|
{
|
|
|
|
set_errno (2); //ESRCH);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!SetPriorityClass (process, Class))
|
|
|
|
{
|
|
|
|
CloseHandle (process);
|
|
|
|
set_errno (EPERM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CloseHandle (process);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
/* we map -14 to 15, and 15 to 1 via (16- ((n+16) >> 1)). This lines up with
|
|
|
|
the allowed values we return elsewhere in the sched* functions. We then
|
|
|
|
map in groups of three to allowed thread priority's. The reason for dropping
|
|
|
|
accuracy while still returning a wide range of values is to allow more
|
|
|
|
flexible code in the future. */
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
2002-09-23 02:31:31 +02:00
|
|
|
sched_set_thread_priority (HANDLE thread, int priority)
|
2001-04-12 06:04:53 +02:00
|
|
|
{
|
|
|
|
int real_pri;
|
|
|
|
real_pri = 16 - ((priority + 16) >> 1);
|
|
|
|
if (real_pri <1 || real_pri > 15)
|
|
|
|
return EINVAL;
|
2001-06-26 16:47:48 +02:00
|
|
|
|
|
|
|
if (real_pri < 4)
|
2001-04-12 06:04:53 +02:00
|
|
|
real_pri = THREAD_PRIORITY_LOWEST;
|
|
|
|
else if (real_pri < 7)
|
|
|
|
real_pri = THREAD_PRIORITY_BELOW_NORMAL;
|
|
|
|
else if (real_pri < 10)
|
|
|
|
real_pri = THREAD_PRIORITY_NORMAL;
|
|
|
|
else if (real_pri < 13)
|
|
|
|
real_pri = THREAD_PRIORITY_ABOVE_NORMAL;
|
|
|
|
else
|
|
|
|
real_pri = THREAD_PRIORITY_HIGHEST;
|
|
|
|
|
2002-09-23 02:31:31 +02:00
|
|
|
if (!SetThreadPriority (thread, real_pri))
|
2001-04-12 06:04:53 +02:00
|
|
|
/* invalid handle, no access are the only expected errors. */
|
|
|
|
return EPERM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-03-21 15:00:29 +01:00
|
|
|
/* set the scheduler */
|
|
|
|
int
|
|
|
|
sched_setscheduler (pid_t pid, int policy,
|
|
|
|
const struct sched_param *param)
|
|
|
|
{
|
|
|
|
/* on win32, you can't change the scheduler. Doh! */
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* yield the cpu */
|
|
|
|
int
|
2005-07-05 05:16:46 +02:00
|
|
|
sched_yield ()
|
2001-03-21 15:00:29 +01:00
|
|
|
{
|
2011-07-06 20:35:44 +02:00
|
|
|
SwitchToThread ();
|
2001-03-21 15:00:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|