JackEngineTiming.cpp

00001 /*
00002 Copyright (C) 2004-2006 Grame  
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #include "JackEngineTiming.h"
00021 #include "JackClientInterface.h"
00022 #include "JackEngineControl.h"
00023 #include "JackClientControl.h"
00024 #include <math.h>
00025 #include <algorithm>
00026 #include <iostream> 
00027 #include <assert.h>
00028 
00029 namespace Jack
00030 {
00031 
00032 inline jack_time_t MAX(jack_time_t a, jack_time_t b)
00033 {
00034     return (a < b) ? b : a;
00035 }
00036 
00037 JackEngineTiming::JackEngineTiming(JackClientInterface** table, JackGraphManager* manager, JackEngineControl* control)
00038 {
00039     fClientTable = table;
00040     fGraphManager = manager;
00041     fEngineControl = control;
00042     fLastTime = 0;
00043     fCurTime = 0;
00044     fProcessTime = 0;
00045     fLastProcessTime = 0;
00046     fSpareUsecs = 0;
00047     fMaxUsecs = 0;
00048     fAudioCycle = 0;
00049 }
00050 
00051 void JackEngineTiming::UpdateTiming(jack_time_t callback_usecs)
00052 {
00053     GetTimeMeasure(callback_usecs);
00054     CalcCPULoad();
00055 }
00056 
00057 void JackEngineTiming::CalcCPULoad()
00058 {
00059     jack_time_t lastCycleEnd = fLastProcessTime;
00060 
00061     for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
00062         JackClientInterface* client = fClientTable[i];
00063                 JackClientTiming* timing = fGraphManager->GetClientTiming(i);
00064                 if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) {
00065             lastCycleEnd = MAX(lastCycleEnd, timing->fFinishedAt);
00066         }
00067     }
00068 
00069     /* store the execution time for later averaging */
00070     fRollingClientUsecs[fRollingClientUsecsIndex++] = lastCycleEnd - fLastTime;
00071 
00072     if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) {
00073         fRollingClientUsecsIndex = 0;
00074     }
00075 
00076     /* every so often, recompute the current maximum use over the
00077        last JACK_ENGINE_ROLLING_COUNT client iterations.
00078     */
00079 
00080     if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
00081 
00082         jack_time_t maxUsecs = 0;
00083         for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
00084             maxUsecs = MAX(fRollingClientUsecs[i], maxUsecs);
00085         }
00086 
00087         fMaxUsecs = MAX(fMaxUsecs, maxUsecs);
00088         fSpareUsecs = jack_time_t((maxUsecs < fEngineControl->fPeriodUsecs) ? fEngineControl->fPeriodUsecs - maxUsecs : 0);
00089         fEngineControl->fCPULoad
00090         = ((1.0f - (float(fSpareUsecs) / float(fEngineControl->fPeriodUsecs))) * 50.0f + (fEngineControl->fCPULoad * 0.5f));
00091     }
00092 }
00093 
00094 void JackEngineTiming::ResetRollingUsecs()
00095 {
00096     memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
00097     fRollingClientUsecsIndex = 0;
00098     fRollingClientUsecsCnt = 0;
00099     fSpareUsecs = 0;
00100     fRollingInterval = (int)floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.0f) / fEngineControl->fPeriodUsecs);
00101 }
00102 
00103 void JackEngineTiming::GetTimeMeasure(jack_time_t callbackUsecs)
00104 {
00105     int pos = (++fAudioCycle) % TIME_POINTS;
00106 
00107     fLastTime = fCurTime;
00108     fCurTime = callbackUsecs;
00109 
00110     fLastProcessTime = fProcessTime;
00111     fProcessTime = GetMicroSeconds();
00112 
00113     if (fLastTime > 0) {
00114         fMeasure[pos].fEngineTime = fLastTime;
00115         fMeasure[pos].fAudioCycle = fAudioCycle;
00116 
00117         for (int i = 0; i < CLIENT_NUM; i++) {
00118             JackClientInterface* client = fClientTable[i];
00119                         JackClientTiming* timing = fGraphManager->GetClientTiming(i);
00120             if (client && client->GetClientControl()->fActive) {
00121                             fMeasure[pos].fClientTable[i].fRefNum = i;
00122                 fMeasure[pos].fClientTable[i].fSignaledAt = timing->fSignaledAt;
00123                 fMeasure[pos].fClientTable[i].fAwakeAt = timing->fAwakeAt;
00124                 fMeasure[pos].fClientTable[i].fFinishedAt = timing->fFinishedAt;
00125                 fMeasure[pos].fClientTable[i].fStatus = timing->fStatus;
00126             }
00127         }
00128     }
00129 }
00130 
00131 void JackEngineTiming::ClearTimeMeasures()
00132 {
00133     for (int i = 0; i < TIME_POINTS; i++) {
00134         for (int j = 0; j < CLIENT_NUM; j++) {
00135             fMeasure[i].fClientTable[j].fRefNum = 0;
00136             fMeasure[i].fClientTable[j].fSignaledAt = 0;
00137             fMeasure[i].fClientTable[j].fAwakeAt = 0;
00138             fMeasure[i].fClientTable[j].fFinishedAt = 0;
00139         }
00140     }
00141     fLastTime = fCurTime = 0;
00142 }
00143 
00144 
00145 } // end of namespace
00146 

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1