From 365a8e2444173dfc13a2a21c9eb17a573b2625fc Mon Sep 17 00:00:00 2001 From: philburk Date: Fri, 12 Apr 2002 18:07:20 +0000 Subject: [PATCH] Use getrusage() instead of gettimeofday() for CPU Load calculation. --- pa_mac_core/pa_mac_core.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pa_mac_core/pa_mac_core.c b/pa_mac_core/pa_mac_core.c index 760aa28..e608402 100644 --- a/pa_mac_core/pa_mac_core.c +++ b/pa_mac_core/pa_mac_core.c @@ -50,6 +50,7 @@ Check for getenv("PA_MIN_LATEWNCY_MSEC") to set latency externally. Better error checking for invalid channel counts and invalid devices. 3.29.2002 - Phil Burk - Fixed Pa_GetCPULoad() for small buffers. + 3.31.2002 - Phil Burk - Use getrusage() instead of gettimeofday() for CPU Load calculation. TODO: O- how do mono output? @@ -59,6 +60,7 @@ O- FIFO between input and output callbacks if different devices, like in pa_mac. #include #include #include +#include #include #include "portaudio.h" @@ -98,7 +100,7 @@ typedef struct PaHostSoundControl int pahsc_FramesPerHostBuffer; int pahsc_UserBuffersPerHostBuffer; /* For measuring CPU utilization. */ - struct timeval pahsc_EntryTime; + struct rusage pahsc_EntryRusage; double pahsc_InverseMicrosPerHostBuffer; /* 1/Microseconds of real-time audio per user buffer. */ } PaHostSoundControl; @@ -156,8 +158,8 @@ static void Pa_StartUsageCalculation( internalPortAudioStream *past ) { PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData; if( pahsc == NULL ) return; - /* Query system timer for usage analysis and to prevent overuse of CPU. */ - gettimeofday( &pahsc->pahsc_EntryTime, NULL ); + /* Query user CPU timer for usage analysis and to prevent overuse of CPU. */ + getrusage( RUSAGE_SELF, &pahsc->pahsc_EntryRusage ); } static long SubtractTime_AminusB( struct timeval *timeA, struct timeval *timeB ) @@ -174,7 +176,7 @@ static long SubtractTime_AminusB( struct timeval *timeA, struct timeval *timeB ) */ static void Pa_EndUsageCalculation( internalPortAudioStream *past ) { - struct timeval currentTime; + struct rusage currentRusage; long usecsElapsed; double newUsage; @@ -183,10 +185,11 @@ static void Pa_EndUsageCalculation( internalPortAudioStream *past ) PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData; if( pahsc == NULL ) return; - - if( gettimeofday( ¤tTime, NULL ) == 0 ) + + if( getrusage( RUSAGE_SELF, ¤tRusage ) == 0 ) { - usecsElapsed = SubtractTime_AminusB( ¤tTime, &pahsc->pahsc_EntryTime ); + usecsElapsed = SubtractTime_AminusB( ¤tRusage.ru_utime, &pahsc->pahsc_EntryRusage.ru_utime ); + /* Use inverse because it is faster than the divide. */ newUsage = usecsElapsed * pahsc->pahsc_InverseMicrosPerHostBuffer; -- 2.43.0