timer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @file libavutil/timer.h
  3. * high precision timer, useful to profile code
  4. *
  5. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #ifndef AVUTIL_TIMER_H
  24. #define AVUTIL_TIMER_H
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include "config.h"
  28. #if ARCH_X86 || ARCH_PPC || ARCH_BFIN
  29. #define AV_READ_TIME read_time
  30. #if ARCH_X86
  31. static inline uint64_t read_time(void)
  32. {
  33. uint32_t a, d;
  34. __asm__ volatile("rdtsc\n\t"
  35. : "=a" (a), "=d" (d));
  36. return ((uint64_t)d << 32) + a;
  37. }
  38. #elif ARCH_BFIN
  39. static inline uint64_t read_time(void)
  40. {
  41. union {
  42. struct {
  43. unsigned lo;
  44. unsigned hi;
  45. } p;
  46. unsigned long long c;
  47. } t;
  48. __asm__ volatile ("%0=cycles; %1=cycles2;" : "=d" (t.p.lo), "=d" (t.p.hi));
  49. return t.c;
  50. }
  51. #else //FIXME check ppc64
  52. static inline uint64_t read_time(void)
  53. {
  54. uint32_t tbu, tbl, temp;
  55. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  56. __asm__ volatile(
  57. "1:\n"
  58. "mftbu %2\n"
  59. "mftb %0\n"
  60. "mftbu %1\n"
  61. "cmpw %2,%1\n"
  62. "bne 1b\n"
  63. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  64. :
  65. : "cc");
  66. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  67. }
  68. #endif
  69. #elif HAVE_GETHRTIME
  70. #define AV_READ_TIME gethrtime
  71. #endif
  72. #ifdef AV_READ_TIME
  73. #define START_TIMER \
  74. uint64_t tend;\
  75. uint64_t tstart= AV_READ_TIME();\
  76. #define STOP_TIMER(id) \
  77. tend= AV_READ_TIME();\
  78. {\
  79. static uint64_t tsum=0;\
  80. static int tcount=0;\
  81. static int tskip_count=0;\
  82. if(tcount<2 || tend - tstart < 8*tsum/tcount || tend - tstart < 2000){\
  83. tsum+= tend - tstart;\
  84. tcount++;\
  85. }else\
  86. tskip_count++;\
  87. if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
  88. av_log(NULL, AV_LOG_ERROR, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n",\
  89. tsum*10/tcount, id, tcount, tskip_count);\
  90. }\
  91. }
  92. #else
  93. #define START_TIMER
  94. #define STOP_TIMER(id) {}
  95. #endif
  96. #endif /* AVUTIL_TIMER_H */