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.

279 lines
6.8KB

  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 "cycles.h"
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <sys/mman.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <inttypes.h>
  34. static jack_time_t __jack_cpu_mhz = 0;
  35. jack_time_t (*_jack_get_microseconds)(void) = 0;
  36. #if defined(__gnu_linux__) && (defined(__i386__) || defined(__x86_64__))
  37. #define HPET_SUPPORT
  38. #define HPET_MMAP_SIZE 1024
  39. #define HPET_CAPS 0x000
  40. #define HPET_PERIOD 0x004
  41. #define HPET_COUNTER 0x0f0
  42. #define HPET_CAPS_COUNTER_64BIT (1 << 13)
  43. #if defined(__x86_64__)
  44. typedef uint64_t hpet_counter_t;
  45. #else
  46. typedef uint32_t hpet_counter_t;
  47. #endif
  48. static int hpet_fd;
  49. static unsigned char *hpet_ptr;
  50. static uint32_t hpet_period; /* period length in femto secs */
  51. static uint64_t hpet_offset = 0;
  52. static uint64_t hpet_wrap;
  53. static hpet_counter_t hpet_previous = 0;
  54. #endif /* defined(__gnu_linux__) && (__i386__ || __x86_64__) */
  55. #ifdef HPET_SUPPORT
  56. static int jack_hpet_init ()
  57. {
  58. uint32_t hpet_caps;
  59. hpet_fd = open("/dev/hpet", O_RDONLY);
  60. if (hpet_fd < 0) {
  61. jack_error ("This system has no accessible HPET device (%s)", strerror (errno));
  62. return -1;
  63. }
  64. hpet_ptr = (unsigned char *) mmap(NULL, HPET_MMAP_SIZE,
  65. PROT_READ, MAP_SHARED, hpet_fd, 0);
  66. if (hpet_ptr == MAP_FAILED) {
  67. jack_error ("This system has no mappable HPET device (%s)", strerror (errno));
  68. close (hpet_fd);
  69. return -1;
  70. }
  71. /* this assumes period to be constant. if needed,
  72. it can be moved to the clock access function
  73. */
  74. hpet_period = *((uint32_t *) (hpet_ptr + HPET_PERIOD));
  75. hpet_caps = *((uint32_t *) (hpet_ptr + HPET_CAPS));
  76. hpet_wrap = ((hpet_caps & HPET_CAPS_COUNTER_64BIT) &&
  77. (sizeof(hpet_counter_t) == sizeof(uint64_t))) ?
  78. 0 : ((uint64_t) 1 << 32);
  79. return 0;
  80. }
  81. static jack_time_t jack_get_microseconds_from_hpet (void)
  82. {
  83. hpet_counter_t hpet_counter;
  84. long double hpet_time;
  85. hpet_counter = *((hpet_counter_t *) (hpet_ptr + HPET_COUNTER));
  86. if (hpet_counter < hpet_previous)
  87. hpet_offset += hpet_wrap;
  88. hpet_previous = hpet_counter;
  89. hpet_time = (long double) (hpet_offset + hpet_counter) *
  90. (long double) hpet_period * (long double) 1e-9;
  91. return ((jack_time_t) (hpet_time + 0.5));
  92. }
  93. #else
  94. static int jack_hpet_init ()
  95. {
  96. jack_error ("This version of JACK or this computer does not have HPET support.\n"
  97. "Please choose a different clock source.");
  98. return -1;
  99. }
  100. static jack_time_t jack_get_microseconds_from_hpet (void)
  101. {
  102. /* never called */
  103. return 0;
  104. }
  105. #endif /* HPET_SUPPORT */
  106. static jack_time_t jack_get_microseconds_from_cycles (void) {
  107. return get_cycles() / __jack_cpu_mhz;
  108. }
  109. /*
  110. * This is another kludge. It looks CPU-dependent, but actually it
  111. * reflects the lack of standards for the Linux kernel formatting of
  112. * /proc/cpuinfo.
  113. */
  114. static jack_time_t jack_get_mhz (void)
  115. {
  116. FILE *f = fopen("/proc/cpuinfo", "r");
  117. if (f == 0)
  118. {
  119. perror("can't open /proc/cpuinfo\n");
  120. exit(1); // TODO : should be remplaced by an exception
  121. }
  122. for (;;)
  123. {
  124. jack_time_t mhz;
  125. int ret;
  126. char buf[1000];
  127. if (fgets(buf, sizeof(buf), f) == NULL) {
  128. jack_error ("FATAL: cannot locate cpu MHz in /proc/cpuinfo\n");
  129. exit(1); // TODO : should be remplaced by an exception
  130. }
  131. #if defined(__powerpc__)
  132. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  133. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  134. defined(__x86_64__)
  135. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  136. #elif defined( __sparc__ )
  137. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  138. #elif defined( __mc68000__ )
  139. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  140. #elif defined( __s390__ )
  141. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  142. #elif defined( __sh__ )
  143. ret = sscanf(buf, "bogomips : %" SCNu64, &mhz);
  144. #else /* MIPS, ARM, alpha */
  145. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  146. #endif
  147. if (ret == 1)
  148. {
  149. fclose(f);
  150. return (jack_time_t)mhz;
  151. }
  152. }
  153. }
  154. #define HAVE_CLOCK_GETTIME 1
  155. #ifndef HAVE_CLOCK_GETTIME
  156. static jack_time_t jack_get_microseconds_from_system (void)
  157. {
  158. jack_time_t jackTime;
  159. struct timeval tv;
  160. gettimeofday (&tv, NULL);
  161. jackTime = (jack_time_t) tv.tv_sec * 1000000 + (jack_time_t) tv.tv_usec;
  162. return jackTime;
  163. }
  164. #else
  165. static jack_time_t jack_get_microseconds_from_system (void)
  166. {
  167. jack_time_t jackTime;
  168. struct timespec time;
  169. clock_gettime(CLOCK_MONOTONIC, &time);
  170. jackTime = (jack_time_t) time.tv_sec * 1e6 +
  171. (jack_time_t) time.tv_nsec / 1e3;
  172. return jackTime;
  173. }
  174. #endif /* HAVE_CLOCK_GETTIME */
  175. SERVER_EXPORT void JackSleep(long usec)
  176. {
  177. usleep(usec);
  178. }
  179. SERVER_EXPORT void InitTime()
  180. {
  181. __jack_cpu_mhz = jack_get_mhz ();
  182. }
  183. SERVER_EXPORT void EndTime()
  184. {}
  185. void SetClockSource(jack_timer_type_t source)
  186. {
  187. jack_log("Clock source : %s", ClockSourceName(source));
  188. switch (source)
  189. {
  190. case JACK_TIMER_CYCLE_COUNTER:
  191. _jack_get_microseconds = jack_get_microseconds_from_cycles;
  192. break;
  193. case JACK_TIMER_HPET:
  194. if (jack_hpet_init () == 0) {
  195. _jack_get_microseconds = jack_get_microseconds_from_hpet;
  196. } else {
  197. _jack_get_microseconds = jack_get_microseconds_from_system;
  198. }
  199. break;
  200. case JACK_TIMER_SYSTEM_CLOCK:
  201. default:
  202. _jack_get_microseconds = jack_get_microseconds_from_system;
  203. break;
  204. }
  205. }
  206. const char* ClockSourceName(jack_timer_type_t source)
  207. {
  208. switch (source) {
  209. case JACK_TIMER_CYCLE_COUNTER:
  210. return "cycle counter";
  211. case JACK_TIMER_HPET:
  212. return "hpet";
  213. case JACK_TIMER_SYSTEM_CLOCK:
  214. #ifdef HAVE_CLOCK_GETTIME
  215. return "system clock via clock_gettime";
  216. #else
  217. return "system clock via gettimeofday";
  218. #endif
  219. }
  220. /* what is wrong with gcc ? */
  221. return "unknown";
  222. }
  223. SERVER_EXPORT jack_time_t GetMicroSeconds()
  224. {
  225. return _jack_get_microseconds();
  226. }
  227. SERVER_EXPORT jack_time_t jack_get_microseconds()
  228. {
  229. return _jack_get_microseconds();
  230. }