forked from lutris/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.c
166 lines (143 loc) · 4.75 KB
/
scheduler.c
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
/*
* Scheduler priority management
*
* Copyright (C) 2015 Joakim Hernberg
* Copyright (C) 2015 Sebastian Lackner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#define _GNU_SOURCE /* for SCHED_BATCH, SCHED_IDLE */
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef HAVE_SCHED_H
# include <sched.h>
#endif
#ifndef SCHED_RESET_ON_FORK
# define SCHED_RESET_ON_FORK 0x40000000
#endif
#ifndef SCHED_IDLE
#define SCHED_IDLE 5
#endif
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "winternl.h"
#include "thread.h"
#if defined(__linux__) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_SCHED_H)
static int thread_base_priority = -1;
/* gets the priority value from an environment variable */
static int get_priority( const char *variable, int min, int max )
{
const char *env;
int val;
env = getenv( variable );
if (!env) return -1;
val = atoi( env );
if (val >= min && val <= max) return val;
fprintf( stderr, "wineserver: %s should be between %d and %d\n", variable, min, max );
return -1;
}
/* initializes the scheduler */
void init_scheduler( void )
{
int min, max, priority;
min = sched_get_priority_min( SCHED_FIFO );
max = sched_get_priority_max( SCHED_FIFO );
if (min == -1 || max == -1)
return;
/* change the wineserver priority */
if ((priority = get_priority( "STAGING_RT_PRIORITY_SERVER", min, max )) != -1)
{
struct sched_param param;
memset( ¶m, 0, sizeof(param) );
param.sched_priority = priority;
if (sched_setscheduler( 0, SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m ) == -1 &&
sched_setscheduler( 0, SCHED_FIFO, ¶m ) == -1)
{
fprintf( stderr, "wineserver: failed to change priority to SCHED_FIFO/%d\n",
param.sched_priority );
/* do not bother to check the rest */
return;
}
if (debug_level) fprintf( stderr, "wineserver: changed priority to SCHED_FIFO/%d\n",
param.sched_priority );
}
/* determine base priority which will be used for all threads */
if ((priority = get_priority( "STAGING_RT_PRIORITY_BASE", min, max - 4 )) != -1)
{
thread_base_priority = priority;
if (debug_level) fprintf( stderr, "wineserver: initialized thread base priority to %d\n",
thread_base_priority );
}
}
/* sets the scheduler priority of a windows thread */
void set_scheduler_priority( struct thread *thread )
{
struct sched_param param;
int policy;
if (thread_base_priority == -1) return;
if (thread->unix_tid == -1) return;
memset( ¶m, 0, sizeof(param) );
if (thread->priority >= THREAD_PRIORITY_TIME_CRITICAL)
{
policy = SCHED_FIFO;
param.sched_priority = thread_base_priority + 4;
}
else if (thread->priority >= THREAD_PRIORITY_HIGHEST)
{
policy = SCHED_FIFO;
param.sched_priority = thread_base_priority + 2;
}
else if (thread->priority >= THREAD_PRIORITY_ABOVE_NORMAL)
{
policy = SCHED_FIFO;
param.sched_priority = thread_base_priority;
}
else if (thread->priority >= THREAD_PRIORITY_NORMAL)
{
policy = SCHED_OTHER;
}
else if (thread->priority >= THREAD_PRIORITY_LOWEST)
{
policy = SCHED_BATCH;
}
else
{
policy = SCHED_IDLE;
}
if (sched_setscheduler(thread->unix_tid, policy | SCHED_RESET_ON_FORK, ¶m) == -1 &&
sched_setscheduler(thread->unix_tid, policy, ¶m) == -1)
{
static int once;
if (debug_level || !once++)
fprintf( stderr, "%04x: failed to change priority to %d/%d\n",
thread->id, policy, param.sched_priority );
return;
}
if (debug_level) fprintf( stderr, "%04x: changed priority to %d/%d\n",
thread->id, policy, param.sched_priority );
}
#else
void init_scheduler( void )
{
}
void set_scheduler_priority( struct thread *thread )
{
}
#endif