jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
5.3KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2005 Jussi Laako
  4. Copyright (C) 2004-2008 Grame
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include "JackConstants.h"
  18. #include "JackTime.h"
  19. #include "JackTypes.h"
  20. #include "JackError.h"
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <sys/mman.h>
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <inttypes.h>
  33. jack_time_t (*_jack_get_microseconds)(void) = 0;
  34. #if defined(__gnu_linux__) && (defined(__i386__) || defined(__x86_64__))
  35. #define HPET_SUPPORT
  36. #define HPET_MMAP_SIZE 1024
  37. #define HPET_CAPS 0x000
  38. #define HPET_PERIOD 0x004
  39. #define HPET_COUNTER 0x0f0
  40. #define HPET_CAPS_COUNTER_64BIT (1 << 13)
  41. #if defined(__x86_64__)
  42. typedef uint64_t hpet_counter_t;
  43. #else
  44. typedef uint32_t hpet_counter_t;
  45. #endif
  46. static int hpet_fd;
  47. static unsigned char *hpet_ptr;
  48. static uint32_t hpet_period; /* period length in femto secs */
  49. static uint64_t hpet_offset = 0;
  50. static uint64_t hpet_wrap;
  51. static hpet_counter_t hpet_previous = 0;
  52. #endif /* defined(__gnu_linux__) && (__i386__ || __x86_64__) */
  53. #ifdef HPET_SUPPORT
  54. static int jack_hpet_init ()
  55. {
  56. uint32_t hpet_caps;
  57. hpet_fd = open("/dev/hpet", O_RDONLY);
  58. if (hpet_fd < 0) {
  59. jack_error ("This system has no accessible HPET device (%s)", strerror (errno));
  60. return -1;
  61. }
  62. hpet_ptr = (unsigned char *) mmap(NULL, HPET_MMAP_SIZE,
  63. PROT_READ, MAP_SHARED, hpet_fd, 0);
  64. if (hpet_ptr == MAP_FAILED) {
  65. jack_error ("This system has no mappable HPET device (%s)", strerror (errno));
  66. close (hpet_fd);
  67. return -1;
  68. }
  69. /* this assumes period to be constant. if needed,
  70. it can be moved to the clock access function
  71. */
  72. hpet_period = *((uint32_t *) (hpet_ptr + HPET_PERIOD));
  73. hpet_caps = *((uint32_t *) (hpet_ptr + HPET_CAPS));
  74. hpet_wrap = ((hpet_caps & HPET_CAPS_COUNTER_64BIT) &&
  75. (sizeof(hpet_counter_t) == sizeof(uint64_t))) ?
  76. 0 : ((uint64_t) 1 << 32);
  77. return 0;
  78. }
  79. static jack_time_t jack_get_microseconds_from_hpet (void)
  80. {
  81. hpet_counter_t hpet_counter;
  82. long double hpet_time;
  83. hpet_counter = *((hpet_counter_t *) (hpet_ptr + HPET_COUNTER));
  84. if (hpet_counter < hpet_previous)
  85. hpet_offset += hpet_wrap;
  86. hpet_previous = hpet_counter;
  87. hpet_time = (long double) (hpet_offset + hpet_counter) *
  88. (long double) hpet_period * (long double) 1e-9;
  89. return ((jack_time_t) (hpet_time + 0.5));
  90. }
  91. #else
  92. static int jack_hpet_init ()
  93. {
  94. jack_error ("This version of JACK or this computer does not have HPET support.\n"
  95. "Please choose a different clock source.");
  96. return -1;
  97. }
  98. static jack_time_t jack_get_microseconds_from_hpet (void)
  99. {
  100. /* never called */
  101. return 0;
  102. }
  103. #endif /* HPET_SUPPORT */
  104. #define HAVE_CLOCK_GETTIME 1
  105. #ifndef HAVE_CLOCK_GETTIME
  106. static jack_time_t jack_get_microseconds_from_system (void)
  107. {
  108. jack_time_t jackTime;
  109. struct timeval tv;
  110. gettimeofday (&tv, NULL);
  111. jackTime = (jack_time_t) tv.tv_sec * 1000000 + (jack_time_t) tv.tv_usec;
  112. return jackTime;
  113. }
  114. #else
  115. static jack_time_t jack_get_microseconds_from_system (void)
  116. {
  117. jack_time_t jackTime;
  118. struct timespec time;
  119. #ifdef CLOCK_MONOTONIC_RAW
  120. clock_gettime(CLOCK_MONOTONIC_RAW, &time);
  121. #else
  122. clock_gettime(CLOCK_MONOTONIC, &time);
  123. #endif
  124. jackTime = (jack_time_t) time.tv_sec * 1e6 +
  125. (jack_time_t) time.tv_nsec / 1e3;
  126. return jackTime;
  127. }
  128. #endif /* HAVE_CLOCK_GETTIME */
  129. SERVER_EXPORT void JackSleep(long usec)
  130. {
  131. usleep(usec);
  132. }
  133. SERVER_EXPORT void InitTime()
  134. {
  135. /* nothing to do on a generic system - we use the system clock */
  136. }
  137. SERVER_EXPORT void EndTime()
  138. {}
  139. void SetClockSource(jack_timer_type_t source)
  140. {
  141. jack_log("Clock source : %s", ClockSourceName(source));
  142. switch (source)
  143. {
  144. case JACK_TIMER_HPET:
  145. if (jack_hpet_init () == 0) {
  146. _jack_get_microseconds = jack_get_microseconds_from_hpet;
  147. } else {
  148. _jack_get_microseconds = jack_get_microseconds_from_system;
  149. }
  150. break;
  151. case JACK_TIMER_SYSTEM_CLOCK:
  152. default:
  153. _jack_get_microseconds = jack_get_microseconds_from_system;
  154. break;
  155. }
  156. }
  157. const char* ClockSourceName(jack_timer_type_t source)
  158. {
  159. switch (source) {
  160. case JACK_TIMER_HPET:
  161. return "hpet";
  162. case JACK_TIMER_SYSTEM_CLOCK:
  163. #ifdef HAVE_CLOCK_GETTIME
  164. return "system clock via clock_gettime";
  165. #else
  166. return "system clock via gettimeofday";
  167. #endif
  168. }
  169. /* what is wrong with gcc ? */
  170. return "unknown";
  171. }
  172. SERVER_EXPORT jack_time_t GetMicroSeconds()
  173. {
  174. return _jack_get_microseconds();
  175. }
  176. SERVER_EXPORT jack_time_t jack_get_microseconds()
  177. {
  178. return _jack_get_microseconds();
  179. }