summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/sched.cc
blob: 085909ea4721414d14eadfcb9ce3b0d7a7c65bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* sched.cc: scheduler interface for Cygwin

   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"
#include "miscfuncs.h"
#include "cygerrno.h"
#include "pinfo.h"
/* for getpid */
#include <unistd.h>
#include "registry.h"

/* Win32 priority to UNIX priority Mapping. */

extern "C"
{

/* We support prio values from 1 to 32.  This is marginally in line with Linux
   (1 - 99) and matches the POSIX requirement to support at least 32 priority
   values. */

/* max priority for policy */
int
sched_get_priority_max (int policy)
{
  switch (policy)
    {
    case SCHED_FIFO:
    case SCHED_RR:
    case SCHED_OTHER:
      return 32;
    }
  set_errno (EINVAL);
  return -1;
}

/* min priority for policy */
int
sched_get_priority_min (int policy)
{
  switch (policy)
    {
    case SCHED_FIFO:
    case SCHED_RR:
    case SCHED_OTHER:
      return 1;
    }
  set_errno (EINVAL);
  return -1;
}

/* Check a scheduler parameter struct for valid settings */
bool
valid_sched_parameters (const struct sched_param *param)
{
  return param->sched_priority >= 1 && param->sched_priority <= 32;
}

/* get sched params for process

   Note, we're never returning EPERM, always ESRCH. This is by design.
   Walking the pid values is a known hole in some OSes. */
int
sched_getparam (pid_t pid, struct sched_param *param)
{
  pid_t localpid;
  if (!param || pid < 0)
    {
      set_errno (EINVAL);
      return -1;
    }

  localpid = pid ? pid : getpid ();

  DWORD pclass;
  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;
    }
  pclass = GetPriorityClass (process);
  CloseHandle (process);
  if (!pclass)
    {
      set_errno (ESRCH);
      return -1;
    }
  /* calculate the unix priority. */
  switch (pclass)
    {
    case IDLE_PRIORITY_CLASS:
      param->sched_priority = 3;
      break;
    case BELOW_NORMAL_PRIORITY_CLASS:
      param->sched_priority = 9;
      break;
    case NORMAL_PRIORITY_CLASS:
    default:
      param->sched_priority = 15;
      break;
    case ABOVE_NORMAL_PRIORITY_CLASS:
      param->sched_priority = 21;
      break;
    case HIGH_PRIORITY_CLASS:
      param->sched_priority = 27;
      break;
    case REALTIME_PRIORITY_CLASS:
      param->sched_priority = 32;
      break;
    }

  return 0;
}

/* get the scheduler for pid

   All process's on WIN32 run with SCHED_FIFO.  So we just give an answer.
   (WIN32 uses a multi queue FIFO).
*/
int
sched_getscheduler (pid_t pid)
{
  if (pid < 0)
    return ESRCH;
  else
    return SCHED_FIFO;
}

/* get the time quantum for pid */
int
sched_rr_get_interval (pid_t pid, struct timespec *interval)
{
  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;
  DWORD vfindex, slindex, qindex, prisep;
  long nsec;

  forwin = GetForegroundWindow ();
  if (!forwin)
    GetWindowThreadProcessId (forwin, &forprocid);
  else
    forprocid = 0;

  reg_key reg (HKEY_LOCAL_MACHINE, KEY_READ, L"SYSTEM", L"CurrentControlSet",
	       L"Control", L"PriorityControl", NULL);
  if (reg.error ())
    {
      set_errno (ESRCH);
      return -1;
    }
  prisep = reg.get_dword (L"Win32PrioritySeparation", 2);
  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)
    vfindex = wincap.is_server () || (prisep & 3) == 0 ? 1 : 0;
  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;
}

/* set the scheduling parameters */
int
sched_setparam (pid_t pid, const struct sched_param *param)
{
  pid_t localpid;
  int pri;
  DWORD pclass;
  HANDLE process;

  if (!param || pid < 0)
    {
      set_errno (EINVAL);
      return -1;
    }

  if (!valid_sched_parameters (param))
    {
      set_errno (EINVAL);
      return -1;
    }

  pri = param->sched_priority;

  /* calculate our desired priority class.  We only reserve a small area
     (31/32) for realtime priority. */
  if (pri <= 6)
    pclass = IDLE_PRIORITY_CLASS;
  else if (pri <= 12)
    pclass = BELOW_NORMAL_PRIORITY_CLASS;
  else if (pri <= 18)
    pclass = NORMAL_PRIORITY_CLASS;
  else if (pri <= 24)
    pclass = ABOVE_NORMAL_PRIORITY_CLASS;
  else if (pri <= 30)
    pclass = HIGH_PRIORITY_CLASS;
  else
    pclass = REALTIME_PRIORITY_CLASS;

  localpid = pid ? pid : getpid ();

  pinfo p (localpid);

  /* set the class */

  if (!p)
    {
      set_errno (ESRCH);
      return -1;
    }
  process = OpenProcess (PROCESS_SET_INFORMATION, FALSE, p->dwProcessId);
  if (!process)
    {
      set_errno (ESRCH);
      return -1;
    }
  if (!SetPriorityClass (process, pclass))
    {
      CloseHandle (process);
      set_errno (EPERM);
      return -1;
    }
  CloseHandle (process);

  return 0;
}

/* POSIX thread priorities loosely compare to Windows thread base priorities.

   Base priority is a function of process priority class and thread priority.
   https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100%28v=vs.85%29.aspx

   Note 1:

     We deliberately handle the REALTIME prority class the same as the HIGH
     priority class.  Realtime has it's own range from 16 to 31 so half the
     arena is reserved for REALTIME.  The problem is that this isn't visible
     nor expected in the POSIX scenario.  Therefore we hide this here and
     fold REALTIME into HIGH.

   Note 2:

     sched_get_thread_priority is only called internally and only for threads
     of the current process, with no good reason for the caller to fail.
     Therefore it never returns an error but a valid priority (base value
     equivalent to process priority class + THREAD_PRIORITY_NORMAL...

   Note 3:

     ...multiplied by 2 to stretch the priorities over the entire range 1 - 32.
*/

static int
sched_base_prio_from_win_prio_class (DWORD pclass)
{
  int base;

  switch (pclass)
    {
    case IDLE_PRIORITY_CLASS:
      base = 4;
      break;
    case BELOW_NORMAL_PRIORITY_CLASS:
      base = 6;
      break;
    case NORMAL_PRIORITY_CLASS:
    default:
      base = 8;
      break;
    case ABOVE_NORMAL_PRIORITY_CLASS:
      base = 10;
      break;
    case HIGH_PRIORITY_CLASS:
    case REALTIME_PRIORITY_CLASS: /* See above note 1 */
      base = 13;
      break;
    }
  return base;
}

int
sched_get_thread_priority (HANDLE thread)
{
  int tprio;
  DWORD pclass;
  int priority;

  tprio = GetThreadPriority (thread);
  pclass = GetPriorityClass (GetCurrentProcess ());
  switch (tprio)
    {
    case THREAD_PRIORITY_ERROR_RETURN:
      priority = sched_base_prio_from_win_prio_class (pclass);
      break;
    case THREAD_PRIORITY_IDLE:
      priority = 1;
      break;
    case THREAD_PRIORITY_TIME_CRITICAL:
      priority = 15;
      break;
    default:
      priority = tprio + sched_base_prio_from_win_prio_class (pclass);
      break;
    }
  return priority << 1; /* See above note 3 */
}

int
sched_set_thread_priority (HANDLE thread, int priority)
{
  DWORD pclass;
  int tprio;

  pclass = GetPriorityClass (GetCurrentProcess ());
  if (!pclass)
    return EPERM;
  if (priority < 1 || priority > 32)
    return EINVAL;

  priority >>= 1; /* See above note 3 */
  if (priority < 1)
    priority = 1;
  else if (priority > 15)
    priority = 15;

  if (priority == 1)
    tprio = THREAD_PRIORITY_IDLE;
  else if (priority == 15)
    tprio = THREAD_PRIORITY_TIME_CRITICAL;
  else
    {
      tprio = priority - sched_base_prio_from_win_prio_class (pclass);
      /* Intermediate values only allowed in REALTIME_PRIORITY_CLASS. */
      if (pclass != REALTIME_PRIORITY_CLASS)
	{
	  if (tprio < THREAD_PRIORITY_LOWEST)
	    tprio = THREAD_PRIORITY_LOWEST;
	  else if (tprio > THREAD_PRIORITY_HIGHEST)
	    tprio = THREAD_PRIORITY_HIGHEST;
	}
    }
  if (!SetThreadPriority (thread, tprio))
    /* invalid handle, no access are the only expected errors. */
    return EPERM;
  return 0;
}

/* 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
sched_yield ()
{
  SwitchToThread ();
  return 0;
}
}