pa_cpuload.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * $Id: pa_cpuload.c 1577 2011-02-01 13:03:45Z rossb $
  3. * Portable Audio I/O Library CPU Load measurement functions
  4. * Portable CPU load measurement facility.
  5. *
  6. * Based on the Open Source API proposed by Ross Bencina
  7. * Copyright (c) 2002 Ross Bencina
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files
  11. * (the "Software"), to deal in the Software without restriction,
  12. * including without limitation the rights to use, copy, modify, merge,
  13. * publish, distribute, sublicense, and/or sell copies of the Software,
  14. * and to permit persons to whom the Software is furnished to do so,
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  24. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  25. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * The text above constitutes the entire PortAudio license; however,
  30. * the PortAudio community also makes the following non-binding requests:
  31. *
  32. * Any person wishing to distribute modifications to the Software is
  33. * requested to send the modifications to the original developer so that
  34. * they can be incorporated into the canonical version. It is also
  35. * requested that these non-binding requests be included along with the
  36. * license above.
  37. */
  38. /** @file
  39. @ingroup common_src
  40. @brief Functions to assist in measuring the CPU utilization of a callback
  41. stream. Used to implement the Pa_GetStreamCpuLoad() function.
  42. @todo Dynamically calculate the coefficients used to smooth the CPU Load
  43. Measurements over time to provide a uniform characterisation of CPU Load
  44. independent of rate at which PaUtil_BeginCpuLoadMeasurement /
  45. PaUtil_EndCpuLoadMeasurement are called. see http://www.portaudio.com/trac/ticket/113
  46. */
  47. #include "pa_cpuload.h"
  48. #include <assert.h>
  49. #include "pa_util.h" /* for PaUtil_GetTime() */
  50. void PaUtil_InitializeCpuLoadMeasurer( PaUtilCpuLoadMeasurer* measurer, double sampleRate )
  51. {
  52. assert( sampleRate > 0 );
  53. measurer->samplingPeriod = 1. / sampleRate;
  54. measurer->averageLoad = 0.;
  55. }
  56. void PaUtil_ResetCpuLoadMeasurer( PaUtilCpuLoadMeasurer* measurer )
  57. {
  58. measurer->averageLoad = 0.;
  59. }
  60. void PaUtil_BeginCpuLoadMeasurement( PaUtilCpuLoadMeasurer* measurer )
  61. {
  62. measurer->measurementStartTime = PaUtil_GetTime();
  63. }
  64. void PaUtil_EndCpuLoadMeasurement( PaUtilCpuLoadMeasurer* measurer, unsigned long framesProcessed )
  65. {
  66. double measurementEndTime, secondsFor100Percent, measuredLoad;
  67. if( framesProcessed > 0 ){
  68. measurementEndTime = PaUtil_GetTime();
  69. assert( framesProcessed > 0 );
  70. secondsFor100Percent = framesProcessed * measurer->samplingPeriod;
  71. measuredLoad = (measurementEndTime - measurer->measurementStartTime) / secondsFor100Percent;
  72. /* Low pass filter the calculated CPU load to reduce jitter using a simple IIR low pass filter. */
  73. /** FIXME @todo these coefficients shouldn't be hardwired see: http://www.portaudio.com/trac/ticket/113 */
  74. #define LOWPASS_COEFFICIENT_0 (0.9)
  75. #define LOWPASS_COEFFICIENT_1 (0.99999 - LOWPASS_COEFFICIENT_0)
  76. measurer->averageLoad = (LOWPASS_COEFFICIENT_0 * measurer->averageLoad) +
  77. (LOWPASS_COEFFICIENT_1 * measuredLoad);
  78. }
  79. }
  80. double PaUtil_GetCpuLoad( PaUtilCpuLoadMeasurer* measurer )
  81. {
  82. return measurer->averageLoad;
  83. }