]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/sys/linux/test_scheduler.c
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / sys / linux / test_scheduler.c
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #include <stdio.h>
30 #include <sys/time.h>
31 #include <sched.h>
32 #include <errno.h>
33
34 /*
35 ================
36 Sys_Milliseconds
37 ================
38 */
39 /* base time in seconds, that's our origin
40    timeval:tv_sec is an int: 
41    assuming this wraps every 0x7fffffff - ~68 years since the Epoch (1970) - we're safe till 2038
42    using unsigned long data type to work right with Sys_XTimeToSysTime */
43 unsigned long sys_timeBase = 0;
44 /* current time in ms, using sys_timeBase as origin
45    NOTE: sys_timeBase*1000 + curtime -> ms since the Epoch
46      0x7fffffff ms - ~24 days
47                  or is it 48 days? the specs say int, but maybe it's casted from unsigned int?
48 */
49 int Sys_Milliseconds(void)
50 {
51         int curtime;
52         struct timeval tp;
53
54         gettimeofday(&tp, NULL);
55
56         if (!sys_timeBase) {
57                 sys_timeBase = tp.tv_sec;
58                 return tp.tv_usec / 1000;
59         }
60
61         curtime = (tp.tv_sec - sys_timeBase) * 1000 + tp.tv_usec / 1000;
62
63         return curtime;
64 }
65
66 #define STAT_BUF 100
67
68 int main(int argc, void *argv[]) {
69         int start = 30; // start waiting with 30 ms 
70         int dec = 2; // decrement by 2 ms
71         int min = 4; // min wait test
72         int i, j, now, next;
73         int stats[STAT_BUF];
74
75         struct sched_param parm;
76         
77         Sys_Milliseconds(); // init
78
79         // set schedule policy to see if that affects usleep
80         // (root rights required for that)
81         parm.sched_priority = 99;
82         if ( sched_setscheduler(0, SCHED_RR, &parm) != 0 ) {
83                 printf("sched_setscheduler SCHED_RR failed: %s\n", strerror(errno) );
84         } else {
85                 printf("sched_setscheduler SCHED_RR ok\n");
86         }
87         
88         // now run the test
89         for( i = start ; i >= min ; i -= dec ) {
90                 printf( "sleep %d ms", i );
91                 for( j = 0 ; j < STAT_BUF ; j++ ) {
92                         now = Sys_Milliseconds();
93                         usleep(i*1000);                 
94                         stats[j] = Sys_Milliseconds() - now;
95                 }
96                 for( j = 0; j < STAT_BUF; j++) {
97                         if ( ! (j & 0xf) ) {
98                                 printf("\n");
99                         }
100                         printf( "%d ", stats[j] );
101                 }
102                 printf("\n");
103         }
104         return 0;
105 }