JackEngineControl.cpp 3.25 KB
Newer Older
sletz's avatar
sletz committed
1
/*
2
Copyright (C) 2003 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5

This program is free software; you can redistribute it and/or modify
sletz's avatar
sletz committed
6
7
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
sletz's avatar
sletz committed
8
9
10
11
12
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
sletz's avatar
sletz committed
13
GNU Lesser General Public License for more details.
sletz's avatar
sletz committed
14

sletz's avatar
sletz committed
15
16
17
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
sletz's avatar
sletz committed
18
19
20

*/

sletz's avatar
sletz committed
21
#include "JackClientInterface.h"
sletz's avatar
sletz committed
22
#include "JackEngineControl.h"
23
#include "JackGraphManager.h"
sletz's avatar
sletz committed
24
#include "JackClientControl.h"
sletz's avatar
sletz committed
25
#include <algorithm>
26
#include <math.h>
sletz's avatar
sletz committed
27
28
29
30

namespace Jack
{

sletz's avatar
sletz committed
31
static inline jack_time_t JACK_MAX(jack_time_t a, jack_time_t b)
32
33
34
35
{
    return (a < b) ? b : a;
}

sletz's avatar
sletz committed
36
37
38
39
void JackEngineControl::CalcCPULoad(JackClientInterface** table, 
                                    JackGraphManager* manager, 
                                    jack_time_t cur_cycle_begin, 
                                    jack_time_t prev_cycle_end)
sletz's avatar
sletz committed
40
{
sletz's avatar
sletz committed
41
42
43
44
45
46
    fPrevCycleTime = fCurCycleTime;
    fCurCycleTime = cur_cycle_begin;
    jack_time_t last_cycle_end = prev_cycle_end;
    
    // In Asynchronous mode, last cycle end is the max of client end dates
    if (!fSyncMode) {
47
        for (int i = fDriverNum; i < CLIENT_NUM; i++) {
sletz's avatar
sletz committed
48
49
50
            JackClientInterface* client = table[i];
            JackClientTiming* timing = manager->GetClientTiming(i);
            if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) 
51
                last_cycle_end = JACK_MAX(last_cycle_end, timing->fFinishedAt);
sletz's avatar
sletz committed
52
53
54
        }
    }

sletz's avatar
sletz committed
55
56
57
    // Store the execution time for later averaging 
    fRollingClientUsecs[fRollingClientUsecsIndex++] = last_cycle_end - fPrevCycleTime;
    if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) 
sletz's avatar
sletz committed
58
        fRollingClientUsecsIndex = 0;
sletz's avatar
sletz committed
59
60
61
62
  
    // Every so often, recompute the current maximum use over the
    // last JACK_ENGINE_ROLLING_COUNT client iterations.
 
sletz's avatar
sletz committed
63
64
    if (++fRollingClientUsecsCnt % fRollingInterval == 0) {

sletz's avatar
sletz committed
65
66
        jack_time_t max_usecs = 0;
        for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) 
67
            max_usecs = JACK_MAX(fRollingClientUsecs[i], max_usecs);
sletz's avatar
sletz committed
68
    
69
        fMaxUsecs = JACK_MAX(fMaxUsecs, max_usecs);
sletz's avatar
sletz committed
70
        fSpareUsecs = jack_time_t((max_usecs < fPeriodUsecs) ? fPeriodUsecs - max_usecs : 0);
71
        fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
sletz's avatar
sletz committed
72
73
74
    }
}

75
void JackEngineControl::ResetRollingUsecs()
sletz's avatar
sletz committed
76
77
78
79
80
{
    memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
    fRollingClientUsecsIndex = 0;
    fRollingClientUsecsCnt = 0;
    fSpareUsecs = 0;
sletz's avatar
sletz committed
81
    fRollingInterval = int(floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs));
sletz's avatar
sletz committed
82
}
83
    
84
void JackEngineControl::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
85
{
86
    ResetFrameTime(callback_usecs);
87
88
89
90
91
    fXrunDelayedUsecs = delayed_usecs;
    if (delayed_usecs > fMaxDelayedUsecs)
        fMaxDelayedUsecs = delayed_usecs;
}
    
sletz's avatar
sletz committed
92
} // end of namespace