time.c 2.2 KB

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