jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.2KB

  1. /*
  2. Copyright (C) 2003 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackEngineControl.h"
  17. #include "JackGraphManager.h"
  18. #include "JackClientControl.h"
  19. #include <algorithm>
  20. #include <math.h>
  21. namespace Jack
  22. {
  23. void JackEngineControl::CycleIncTime(jack_time_t callback_usecs)
  24. {
  25. // Timer
  26. fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
  27. }
  28. void JackEngineControl::CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  29. {
  30. // Transport
  31. fTransport.CycleBegin(fSampleRate, callback_usecs);
  32. // Timing
  33. GetTimeMeasure(table, manager, callback_usecs);
  34. CalcCPULoad(table, manager);
  35. }
  36. void JackEngineControl::CycleEnd(JackClientInterface** table)
  37. {
  38. fTransport.CycleEnd(table, fSampleRate, fBufferSize);
  39. }
  40. void JackEngineControl::InitFrameTime()
  41. {
  42. fFrameTimer.InitFrameTime();
  43. }
  44. void JackEngineControl::ResetFrameTime(jack_time_t callback_usecs)
  45. {
  46. fFrameTimer.ResetFrameTime(fSampleRate, callback_usecs, fPeriodUsecs);
  47. }
  48. void JackEngineControl::ReadFrameTime(JackTimer* timer)
  49. {
  50. fFrameTimer.ReadFrameTime(timer);
  51. }
  52. // Private
  53. #ifdef WIN32
  54. inline jack_time_t MAX(jack_time_t a, jack_time_t b)
  55. {
  56. return (a < b) ? b : a;
  57. }
  58. #else
  59. #define MAX(a,b) std::max((a),(b))
  60. #endif
  61. void JackEngineControl::CalcCPULoad(JackClientInterface** table, JackGraphManager* manager)
  62. {
  63. jack_time_t lastCycleEnd = fLastProcessTime;
  64. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  65. JackClientInterface* client = table[i];
  66. JackClientTiming* timing = manager->GetClientTiming(i);
  67. if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) {
  68. lastCycleEnd = MAX(lastCycleEnd, timing->fFinishedAt);
  69. }
  70. }
  71. /* store the execution time for later averaging */
  72. fRollingClientUsecs[fRollingClientUsecsIndex++] = lastCycleEnd - fLastTime;
  73. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) {
  74. fRollingClientUsecsIndex = 0;
  75. }
  76. /* every so often, recompute the current maximum use over the
  77. last JACK_ENGINE_ROLLING_COUNT client iterations.
  78. */
  79. if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
  80. jack_time_t maxUsecs = 0;
  81. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  82. maxUsecs = MAX(fRollingClientUsecs[i], maxUsecs);
  83. }
  84. fMaxUsecs = MAX(fMaxUsecs, maxUsecs);
  85. fSpareUsecs = jack_time_t((maxUsecs < fPeriodUsecs) ? fPeriodUsecs - maxUsecs : 0);
  86. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  87. }
  88. }
  89. void JackEngineControl::ResetRollingUsecs()
  90. {
  91. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  92. fRollingClientUsecsIndex = 0;
  93. fRollingClientUsecsCnt = 0;
  94. fSpareUsecs = 0;
  95. fRollingInterval = (int)floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs);
  96. }
  97. void JackEngineControl::GetTimeMeasure(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  98. {
  99. int pos = (++fAudioCycle) % TIME_POINTS;
  100. fLastTime = fCurTime;
  101. fCurTime = callback_usecs;
  102. fLastProcessTime = fProcessTime;
  103. fProcessTime = GetMicroSeconds();
  104. if (fLastTime > 0) {
  105. fMeasure[pos].fEngineTime = fLastTime;
  106. fMeasure[pos].fAudioCycle = fAudioCycle;
  107. for (int i = 0; i < CLIENT_NUM; i++) {
  108. JackClientInterface* client = table[i];
  109. JackClientTiming* timing = manager->GetClientTiming(i);
  110. if (client && client->GetClientControl()->fActive) {
  111. fMeasure[pos].fClientTable[i].fRefNum = i;
  112. fMeasure[pos].fClientTable[i].fSignaledAt = timing->fSignaledAt;
  113. fMeasure[pos].fClientTable[i].fAwakeAt = timing->fAwakeAt;
  114. fMeasure[pos].fClientTable[i].fFinishedAt = timing->fFinishedAt;
  115. fMeasure[pos].fClientTable[i].fStatus = timing->fStatus;
  116. }
  117. }
  118. }
  119. }
  120. void JackEngineControl::ClearTimeMeasures()
  121. {
  122. for (int i = 0; i < TIME_POINTS; i++) {
  123. for (int j = 0; j < CLIENT_NUM; j++) {
  124. fMeasure[i].fClientTable[j].fRefNum = 0;
  125. fMeasure[i].fClientTable[j].fSignaledAt = 0;
  126. fMeasure[i].fClientTable[j].fAwakeAt = 0;
  127. fMeasure[i].fClientTable[j].fFinishedAt = 0;
  128. }
  129. }
  130. fLastTime = fCurTime = 0;
  131. }
  132. void JackEngineControl::NotifyXRun(float delayed_usecs)
  133. {
  134. fXrunDelayedUsecs = delayed_usecs;
  135. if (delayed_usecs > fMaxDelayedUsecs)
  136. fMaxDelayedUsecs = delayed_usecs;
  137. }
  138. void JackEngineControl::ResetXRun()
  139. {
  140. fMaxDelayedUsecs = 0.f;
  141. }
  142. } // end of namespace