jack1 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.

98 lines
2.6KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. This is the GNU/Linux version.
  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. $Id$
  16. */
  17. #ifndef __jack_time_h__
  18. #define __jack_time_h__
  19. #include <stdio.h>
  20. #include <jack/internal.h>
  21. #include <sysdeps/cycles.h>
  22. /* This is a kludge. We need one global instantiation of this
  23. * variable in each address space. So, libjack/client.c declares the
  24. * actual storage. Other source files will see it as an extern. */
  25. #define JACK_TIME_GLOBAL_DECL jack_time_t __jack_cpu_mhz
  26. extern JACK_TIME_GLOBAL_DECL;
  27. static inline jack_time_t
  28. jack_get_microseconds (void) {
  29. return get_cycles() / __jack_cpu_mhz;
  30. }
  31. /*
  32. * This is another kludge. It looks CPU-dependent, but actually it
  33. * reflects the lack of standards for the Linux kernel formatting of
  34. * /proc/cpuinfo.
  35. */
  36. static inline jack_time_t
  37. jack_get_mhz (void)
  38. {
  39. FILE *f = fopen("/proc/cpuinfo", "r");
  40. if (f == 0)
  41. {
  42. perror("can't open /proc/cpuinfo\n");
  43. exit(1);
  44. }
  45. for ( ; ; )
  46. {
  47. jack_time_t mhz;
  48. int ret;
  49. char buf[1000];
  50. if (fgets(buf, sizeof(buf), f) == NULL) {
  51. jack_error ("FATAL: cannot locate cpu MHz in "
  52. "/proc/cpuinfo\n");
  53. exit(1);
  54. }
  55. #if defined(__powerpc__)
  56. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  57. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  58. defined(__x86_64__)
  59. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  60. #elif defined( __sparc__ )
  61. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  62. #elif defined( __mc68000__ )
  63. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  64. #elif defined( __s390__ )
  65. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  66. #else /* MIPS, ARM, alpha */
  67. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  68. #endif
  69. if (ret == 1)
  70. {
  71. fclose(f);
  72. return (jack_time_t)mhz;
  73. }
  74. }
  75. }
  76. /* This should only be called ONCE per process. */
  77. static inline void
  78. jack_init_time ()
  79. {
  80. __jack_cpu_mhz = jack_get_mhz ();
  81. }
  82. #endif /* __jack_time_h__ */