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.

160 lines
4.9KB

  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 General Public License as published by
  6. the Free Software Foundation; either version 2 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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::CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  24. {
  25. // Transport
  26. fTransport.CycleBegin(fSampleRate, callback_usecs);
  27. // Timer
  28. fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
  29. // Timing
  30. GetTimeMeasure(table, manager, callback_usecs);
  31. CalcCPULoad(table, manager);
  32. }
  33. void JackEngineControl::CycleEnd(JackClientInterface** table)
  34. {
  35. fTransport.CycleEnd(table, fSampleRate, fBufferSize);
  36. }
  37. void JackEngineControl::InitFrameTime()
  38. {
  39. fFrameTimer.InitFrameTime();
  40. }
  41. void JackEngineControl::ResetFrameTime(jack_time_t callback_usecs)
  42. {
  43. fFrameTimer.ResetFrameTime(fSampleRate, callback_usecs, fPeriodUsecs);
  44. }
  45. void JackEngineControl::ReadFrameTime(JackTimer* timer)
  46. {
  47. fFrameTimer.ReadFrameTime(timer);
  48. }
  49. // Private
  50. #ifdef WIN32
  51. inline jack_time_t MAX(jack_time_t a, jack_time_t b)
  52. {
  53. return (a < b) ? b : a;
  54. }
  55. #else
  56. #define MAX(a,b) std::max((a),(b))
  57. #endif
  58. void JackEngineControl::CalcCPULoad(JackClientInterface** table, JackGraphManager* manager)
  59. {
  60. jack_time_t lastCycleEnd = fLastProcessTime;
  61. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  62. JackClientInterface* client = table[i];
  63. JackClientTiming* timing = manager->GetClientTiming(i);
  64. if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) {
  65. lastCycleEnd = MAX(lastCycleEnd, timing->fFinishedAt);
  66. }
  67. }
  68. /* store the execution time for later averaging */
  69. fRollingClientUsecs[fRollingClientUsecsIndex++] = lastCycleEnd - fLastTime;
  70. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) {
  71. fRollingClientUsecsIndex = 0;
  72. }
  73. /* every so often, recompute the current maximum use over the
  74. last JACK_ENGINE_ROLLING_COUNT client iterations.
  75. */
  76. if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
  77. jack_time_t maxUsecs = 0;
  78. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  79. maxUsecs = MAX(fRollingClientUsecs[i], maxUsecs);
  80. }
  81. fMaxUsecs = MAX(fMaxUsecs, maxUsecs);
  82. fSpareUsecs = jack_time_t((maxUsecs < fPeriodUsecs) ? fPeriodUsecs - maxUsecs : 0);
  83. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  84. }
  85. }
  86. void JackEngineControl::ResetRollingUsecs()
  87. {
  88. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  89. fRollingClientUsecsIndex = 0;
  90. fRollingClientUsecsCnt = 0;
  91. fSpareUsecs = 0;
  92. fRollingInterval = (int)floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs);
  93. }
  94. void JackEngineControl::GetTimeMeasure(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  95. {
  96. int pos = (++fAudioCycle) % TIME_POINTS;
  97. fLastTime = fCurTime;
  98. fCurTime = callback_usecs;
  99. fLastProcessTime = fProcessTime;
  100. fProcessTime = GetMicroSeconds();
  101. if (fLastTime > 0) {
  102. fMeasure[pos].fEngineTime = fLastTime;
  103. fMeasure[pos].fAudioCycle = fAudioCycle;
  104. for (int i = 0; i < CLIENT_NUM; i++) {
  105. JackClientInterface* client = table[i];
  106. JackClientTiming* timing = manager->GetClientTiming(i);
  107. if (client && client->GetClientControl()->fActive) {
  108. fMeasure[pos].fClientTable[i].fRefNum = i;
  109. fMeasure[pos].fClientTable[i].fSignaledAt = timing->fSignaledAt;
  110. fMeasure[pos].fClientTable[i].fAwakeAt = timing->fAwakeAt;
  111. fMeasure[pos].fClientTable[i].fFinishedAt = timing->fFinishedAt;
  112. fMeasure[pos].fClientTable[i].fStatus = timing->fStatus;
  113. }
  114. }
  115. }
  116. }
  117. void JackEngineControl::ClearTimeMeasures()
  118. {
  119. for (int i = 0; i < TIME_POINTS; i++) {
  120. for (int j = 0; j < CLIENT_NUM; j++) {
  121. fMeasure[i].fClientTable[j].fRefNum = 0;
  122. fMeasure[i].fClientTable[j].fSignaledAt = 0;
  123. fMeasure[i].fClientTable[j].fAwakeAt = 0;
  124. fMeasure[i].fClientTable[j].fFinishedAt = 0;
  125. }
  126. }
  127. fLastTime = fCurTime = 0;
  128. }
  129. } // end of namespace