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.

102 lines
2.4KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackTime.h"
  20. #include <unistd.h>
  21. SERVER_EXPORT void JackSleep(long usec)
  22. {
  23. usleep(usec);
  24. }
  25. #ifdef GETCYCLE_TIME
  26. #include <stdio.h>
  27. #include "cycles.h"
  28. static jack_time_t __jack_cpu_mhz;
  29. static inline jack_time_t GetMhz(void)
  30. {
  31. FILE *f = fopen("/proc/cpuinfo", "r");
  32. if (f == 0) {
  33. perror("can't open /proc/cpuinfo\n");
  34. exit(1);
  35. }
  36. for (;;) {
  37. jack_time_t mhz;
  38. int ret;
  39. char buf[1000];
  40. if (fgets(buf, sizeof(buf), f) == NULL) {
  41. jack_error("FATAL: cannot locate cpu MHz in /proc/cpuinfo\n");
  42. exit(1);
  43. }
  44. #if defined(__powerpc__)
  45. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  46. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  47. defined(__x86_64__)
  48. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  49. #elif defined( __sparc__ )
  50. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  51. #elif defined( __mc68000__ )
  52. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  53. #elif defined( __s390__ )
  54. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  55. #else /* MIPS, ARM, alpha */
  56. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  57. #endif
  58. if (ret == 1) {
  59. fclose(f);
  60. return (jack_time_t)mhz;
  61. }
  62. }
  63. }
  64. SERVER_EXPORT void InitTime()
  65. {
  66. __jack_cpu_mhz = GetMhz();
  67. }
  68. SERVER_EXPORT jack_time_t GetMicroSeconds(void)
  69. {
  70. return get_cycles() / __jack_cpu_mhz;
  71. }
  72. #else
  73. #include <time.h>
  74. SERVER_EXPORT void InitTime()
  75. {}
  76. SERVER_EXPORT jack_time_t GetMicroSeconds(void)
  77. {
  78. struct timespec ts;
  79. clock_gettime(CLOCK_MONOTONIC, &ts);
  80. return (jack_time_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
  81. }
  82. #endif