test_scheduler.c 3.4 KB

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