1 From 1dceb627db51a239a63ed5276f7e8911be6751bc Mon Sep 17 00:00:00 2001
2 From: Joakim B Hernberg <jhernberg@alchemy.lu>
3 Date: Sun, 7 Nov 2010 19:10:49 +0100
4 Subject: [PATCH] 3:rd wine-rt patch 101107
7 README.WINE-RT | 27 +++++++++++++++++
8 server/main.c | 60 ++++++++++++++++++++++++++++++++++++++
9 server/thread.c | 87 ++++++++++++++++++++++++++++++++++++++++++------------
10 3 files changed, 154 insertions(+), 20 deletions(-)
11 create mode 100644 README.WINE-RT
13 diff --git a/README.WINE-RT b/README.WINE-RT
15 index 0000000..3f3f2c1
20 +The Wine-RT patch allows programs that use windows' concept of thread priority to gain similar functionality under linux. It maps windows priority levels to linux scheduling policies. THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST and THREAD_PRIORITY_TIME_CRITICAL levels which are made to run as linux SCHED_FIFO threads at priority levels that are defined by the WINERT variable. THREAD_PRIORITY_NORMAL threads are run as normal linux threads (as all threads are without the patch), and the priorities below normal (THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_LOWEST) are run as SCHED_BATCH. THREAD_PRIORITY_IDLE threads are run as SCHED_IDLE.
21 +Windows' concept of priority classes is not implemented at all.
23 +Please note that threads running SCHED_FIFO might hang your entire system, so please exercise caution!
26 +When a windows program asks for a thread to be run at a higher priority, Wine will ask the linux system to schedule it as a SCHED_FIFO thread, which means that the tread will keep on executing until it has either finished, voluntarily yields execution or gets preempted by a higher priority SCHED_FIFO thread. This is already done by many linux audio applications, to ensure less xruns on lower buffer sizes. With Wine-RT, the same thing can be done for Wine applications.
29 +The Wine-RT functionality is not enabled by default. Instead it is controlled by 2 environment variables "WINE_RT" and "WINE_SRV_RT".
31 +The "WINE_RT" variable has 2 purposes, it has to be set in order to activate the patch, and it determines the priority of the SCHED_FIFO threads, Its value can be set from 1 to your system's rtprio max value minus 10, as set in limits.conf or limits.d/audio.conf. (In Debian, Ubuntu and KXStudio this value is 99). THREAD_PRIORITY_ABOVE_NORMAL threads will run at this priority level, THREAD_PRIORITY_HIGHEST threads at this level + 5, and THREAD_PRIORITY_TIME_CRITICAL threads at this level + 10.
33 +WINE_SRV_RT makes the wineserver main thread run SCHED_FIFO. Valid values range from 1 to your system's rtprio max value.
35 +We can set these variables in 2 simple ways.
36 +First one is using a terminal with "exports", like this:
41 +or just prefix your application with 'env VARIABLE=value', like this:
42 +env WINE_RT=# WINE_SRV_RT=# wine <app>
44 +A recommended starting point might be "env WINE_RT=15 WINE_SRV_RT=10 wine appname.exe".
46 diff --git a/server/main.c b/server/main.c
47 index 2d841e8..a89d1e0 100644
54 +#include <sys/resource.h>
55 +#include <sys/mman.h>
63 +#define SCHED_NORMAL SCHED_OTHER
69 @@ -44,6 +52,9 @@ int foreground = 0;
70 timeout_t master_socket_timeout = 3 * -TICKS_PER_SEC; /* master socket timeout, default is 3 seconds */
71 const char *server_argv0;
73 +/* global variable used here and in thread.c to determine whether wine runs with rt threads and at what base value */
74 +int base_rt_priority = -1;
78 static void usage(void)
79 @@ -125,6 +136,51 @@ static void sigterm_handler( int signum )
80 exit(1); /* make sure atexit functions get called */
84 +void init_rt_scheduling( void )
86 + struct sched_param param;
87 + struct rlimit limit;
88 + int priority_max, policy, wine_server_rt_priority;
89 + char *enviroment, *endptr;
91 + getrlimit( RLIMIT_RTPRIO, &limit );
92 + priority_max = limit.rlim_max;
94 + /* check for realtime mode and set the base priority level */
96 + if (!(enviroment = getenv( "WINE_RT" )))
98 + base_rt_priority = (int) strtol( enviroment, &endptr, 10 );
99 + if (enviroment == endptr || base_rt_priority == 0 || base_rt_priority > priority_max - 10)
101 + fprintf( stderr, "Unable to run WINE in rt mode, WINE_RT values supported on this system range from 1 to %i\n", priority_max - 10 );
102 + base_rt_priority = -1;
105 + fprintf( stderr, "WINE realtime scheduling hack enabled, realtime base priority has been set to %i\n", base_rt_priority );
107 + /* determine scheduling policy for the main wineserver thread */
109 + if (!(enviroment = getenv( "WINE_SRV_RT" )))
111 + fprintf( stderr, "wineserver running SCHED_NORMAL\n" );
114 + wine_server_rt_priority = (int) strtol( enviroment, &endptr, 10 );
115 + if (enviroment == endptr || wine_server_rt_priority == 0 || wine_server_rt_priority > priority_max)
117 + fprintf( stderr, "Unable to run the wineserver SCHED_FIFO, valid WINE_SRV_RT values range from 1 to %i\n", priority_max );
120 + fprintf( stderr, "wineserver running SCHED_FIFO at priority %i\n", wine_server_rt_priority );
121 + policy = SCHED_FIFO;
122 + param.sched_priority = wine_server_rt_priority;
123 + if (sched_setscheduler ( 0, policy, ¶m) != 0)
124 + fprintf (stderr, "Error scheduling wineserver as SCHED_FIFO\n");
128 int main( int argc, char *argv[] )
130 setvbuf( stderr, NULL, _IOLBF, 0 );
131 @@ -138,6 +194,10 @@ int main( int argc, char *argv[] )
132 signal( SIGTERM, sigterm_handler );
133 signal( SIGABRT, sigterm_handler );
136 + init_rt_scheduling();
138 + mlockall(MCL_FUTURE);
140 open_master_socket();
142 diff --git a/server/thread.c b/server/thread.c
143 index 05e4121..2d103b4 100644
144 --- a/server/thread.c
145 +++ b/server/thread.c
147 #include <sys/types.h>
155 +#ifndef SCHED_NORMAL
156 +#define SCHED_NORMAL SCHED_OTHER
159 +#define SCHED_IDLE 5 /* missing from my glibc, taken from linux/sched.h */
167 #include "ntstatus.h"
168 @@ -164,6 +171,8 @@ static const struct fd_ops thread_fd_ops =
170 static struct list thread_list = LIST_INIT(thread_list);
172 +extern int base_rt_priority;
174 /* initialize the structure for a newly allocated thread */
175 static inline void init_thread_structure( struct thread *thread )
177 @@ -432,29 +441,67 @@ int set_thread_affinity( struct thread *thread, affinity_t affinity )
181 -#define THREAD_PRIORITY_REALTIME_HIGHEST 6
182 -#define THREAD_PRIORITY_REALTIME_LOWEST -7
183 +void set_thread_priority( struct thread *thread, int priority )
186 + struct sched_param param;
189 + if (base_rt_priority == -1 || (thread->unix_tid == -1)) return;
193 + case THREAD_PRIORITY_TIME_CRITICAL:
194 + param.sched_priority = base_rt_priority + 10;
195 + policy = SCHED_FIFO;
196 + fprintf( stderr, "Thread %i at THREAD_PRIORITY_TIME_CRITICAL set to SCHED_FIFO - priority %i\n", thread->unix_tid, param.sched_priority );
198 + case THREAD_PRIORITY_HIGHEST:
199 + param.sched_priority = base_rt_priority + 5;
200 + policy = SCHED_FIFO;
201 + fprintf( stderr, "Thread %i at THREAD_PRIORITY_HIGHEST set to SCHED_FIFO - priority %i\n", thread->unix_tid, param.sched_priority );
203 + case THREAD_PRIORITY_ABOVE_NORMAL:
204 + param.sched_priority = base_rt_priority;
205 + policy = SCHED_FIFO;
206 + fprintf( stderr, "Thread %i at THREAD_PRIORITY_ABOVE_NORMAL set to SCHED_FIFO - priority %i\n", thread->unix_tid, param.sched_priority );
208 + case THREAD_PRIORITY_NORMAL:
209 + param.sched_priority = 0;
210 + policy = SCHED_NORMAL;
211 + fprintf( stderr, "Setting thread %i at level THREAD_PRIORITY_NORMAL to SCHED_NORMAL\n", thread->unix_tid );
213 + case THREAD_PRIORITY_BELOW_NORMAL:
214 + param.sched_priority = 0;
215 + policy = SCHED_BATCH;
216 + fprintf( stderr, "Setting thread %i at level THREAD_PRIORITY_BELOW_NORMAL to SCHED_BATCH\n", thread->unix_tid );
218 + case THREAD_PRIORITY_LOWEST:
219 + param.sched_priority = 0;
220 + policy = SCHED_BATCH;
221 + fprintf( stderr, "Setting thread %i at THREAD_PRIORITY_LOWEST level to SCHED_BATCH\n", thread->unix_tid );
223 + case THREAD_PRIORITY_IDLE:
224 + param.sched_priority = 0;
225 + policy = SCHED_IDLE;
226 + fprintf( stderr, "Setting thread %i with level THREAD_PRIORITY_IDLE to SCHED_IDLE\n", thread->unix_tid );
229 + fprintf( stderr, "Error setting scheduling priority level, unknown should never come here\n" );
232 + if (sched_setscheduler (thread->unix_tid, policy, ¶m) != 0) fprintf (stderr, "Error setting priorities\n");
233 + thread->priority = priority;
238 /* set all information about a thread */
239 static void set_thread_info( struct thread *thread,
240 const struct set_thread_info_request *req )
242 if (req->mask & SET_THREAD_INFO_PRIORITY)
244 - int max = THREAD_PRIORITY_HIGHEST;
245 - int min = THREAD_PRIORITY_LOWEST;
246 - if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
248 - max = THREAD_PRIORITY_REALTIME_HIGHEST;
249 - min = THREAD_PRIORITY_REALTIME_LOWEST;
251 - if ((req->priority >= min && req->priority <= max) ||
252 - req->priority == THREAD_PRIORITY_IDLE ||
253 - req->priority == THREAD_PRIORITY_TIME_CRITICAL)
254 - thread->priority = req->priority;
256 - set_error( STATUS_INVALID_PARAMETER );
258 + set_thread_priority( thread, req->priority );
259 if (req->mask & SET_THREAD_INFO_AFFINITY)
261 if ((req->affinity & thread->process->affinity) != req->affinity)