time.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include <signal.h>
  11. #include <errno.h>
  12. #include "kern_util.h"
  13. #include "user.h"
  14. #include "process.h"
  15. #include "kern_constants.h"
  16. #include "os.h"
  17. #include "uml-config.h"
  18. int set_interval(int is_virtual)
  19. {
  20. int usec = 1000000/hz();
  21. int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL;
  22. struct itimerval interval = ((struct itimerval) { { 0, usec },
  23. { 0, usec } });
  24. if(setitimer(timer_type, &interval, NULL) == -1)
  25. return -errno;
  26. return 0;
  27. }
  28. #ifdef UML_CONFIG_MODE_TT
  29. void enable_timer(void)
  30. {
  31. set_interval(1);
  32. }
  33. #endif
  34. void disable_timer(void)
  35. {
  36. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  37. if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
  38. (setitimer(ITIMER_REAL, &disable, NULL) < 0))
  39. printk("disnable_timer - setitimer failed, errno = %d\n",
  40. errno);
  41. /* If there are signals already queued, after unblocking ignore them */
  42. signal(SIGALRM, SIG_IGN);
  43. signal(SIGVTALRM, SIG_IGN);
  44. }
  45. void switch_timers(int to_real)
  46. {
  47. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  48. struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
  49. { 0, 1000000/hz() }});
  50. int old, new;
  51. if(to_real){
  52. old = ITIMER_VIRTUAL;
  53. new = ITIMER_REAL;
  54. }
  55. else {
  56. old = ITIMER_REAL;
  57. new = ITIMER_VIRTUAL;
  58. }
  59. if((setitimer(old, &disable, NULL) < 0) ||
  60. (setitimer(new, &enable, NULL)))
  61. printk("switch_timers - setitimer failed, errno = %d\n",
  62. errno);
  63. }
  64. #ifdef UML_CONFIG_MODE_TT
  65. void uml_idle_timer(void)
  66. {
  67. if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
  68. panic("Couldn't unset SIGVTALRM handler");
  69. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  70. SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  71. set_interval(0);
  72. }
  73. #endif
  74. unsigned long long os_nsecs(void)
  75. {
  76. struct timeval tv;
  77. gettimeofday(&tv, NULL);
  78. return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
  79. }
  80. void idle_sleep(int secs)
  81. {
  82. struct timespec ts;
  83. ts.tv_sec = secs;
  84. ts.tv_nsec = 0;
  85. nanosleep(&ts, NULL);
  86. }