| 1 | /* |
| 2 | * $Id: pa_cpuload.c,v 1.1 2006/06/10 21:30:55 dmazzoni Exp $ |
| 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 | * Any person wishing to distribute modifications to the Software is |
| 21 | * requested to send the modifications to the original developer so that |
| 22 | * they can be incorporated into the canonical version. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 27 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 28 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 29 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 30 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | /** @file |
| 34 | @brief Functions to assist in measuring the CPU utilization of a callback |
| 35 | stream. Used to implement the Pa_GetStreamCpuLoad() function. |
| 36 | |
| 37 | @todo Dynamically calculate the coefficients used to smooth the CPU Load |
| 38 | Measurements over time to provide a uniform characterisation of CPU Load |
| 39 | independent of rate at which PaUtil_BeginCpuLoadMeasurement / |
| 40 | PaUtil_EndCpuLoadMeasurement are called. |
| 41 | */ |
| 42 | |
| 43 | |
| 44 | #include "pa_cpuload.h" |
| 45 | |
| 46 | #include <assert.h> |
| 47 | |
| 48 | #include "pa_util.h" /* for PaUtil_GetTime() */ |
| 49 | |
| 50 | |
| 51 | void PaUtil_InitializeCpuLoadMeasurer( PaUtilCpuLoadMeasurer* measurer, double sampleRate ) |
| 52 | { |
| 53 | assert( sampleRate > 0 ); |
| 54 | |
| 55 | measurer->samplingPeriod = 1. / sampleRate; |
| 56 | measurer->averageLoad = 0.; |
| 57 | } |
| 58 | |
| 59 | void PaUtil_ResetCpuLoadMeasurer( PaUtilCpuLoadMeasurer* measurer ) |
| 60 | { |
| 61 | measurer->averageLoad = 0.; |
| 62 | } |
| 63 | |
| 64 | void PaUtil_BeginCpuLoadMeasurement( PaUtilCpuLoadMeasurer* measurer ) |
| 65 | { |
| 66 | measurer->measurementStartTime = PaUtil_GetTime(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | void PaUtil_EndCpuLoadMeasurement( PaUtilCpuLoadMeasurer* measurer, unsigned long framesProcessed ) |
| 71 | { |
| 72 | double measurementEndTime, secondsFor100Percent, measuredLoad; |
| 73 | |
| 74 | if( framesProcessed > 0 ){ |
| 75 | measurementEndTime = PaUtil_GetTime(); |
| 76 | |
| 77 | assert( framesProcessed > 0 ); |
| 78 | secondsFor100Percent = framesProcessed * measurer->samplingPeriod; |
| 79 | |
| 80 | measuredLoad = (measurementEndTime - measurer->measurementStartTime) / secondsFor100Percent; |
| 81 | |
| 82 | /* Low pass filter the calculated CPU load to reduce jitter using a simple IIR low pass filter. */ |
| 83 | /** FIXME @todo these coefficients shouldn't be hardwired */ |
| 84 | #define LOWPASS_COEFFICIENT_0 (0.9) |
| 85 | #define LOWPASS_COEFFICIENT_1 (0.99999 - LOWPASS_COEFFICIENT_0) |
| 86 | |
| 87 | measurer->averageLoad = (LOWPASS_COEFFICIENT_0 * measurer->averageLoad) + |
| 88 | (LOWPASS_COEFFICIENT_1 * measuredLoad); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | double PaUtil_GetCpuLoad( PaUtilCpuLoadMeasurer* measurer ) |
| 94 | { |
| 95 | return measurer->averageLoad; |
| 96 | } |