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.

162 lines
4.8KB

  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. /*
  51. #ifdef WIN32
  52. inline jack_time_t MAX(jack_time_t a, jack_time_t b)
  53. {
  54. return (a < b) ? b : a;
  55. }
  56. #else
  57. #endif
  58. */
  59. void JackEngineControl::CalcCPULoad(JackClientInterface** table, JackGraphManager* manager)
  60. {
  61. jack_time_t lastCycleEnd = fLastProcessTime;
  62. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  63. JackClientInterface* client = table[i];
  64. JackClientTiming* timing = manager->GetClientTiming(i);
  65. if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) {
  66. lastCycleEnd = std::max(lastCycleEnd, timing->fFinishedAt);
  67. }
  68. }
  69. /* store the execution time for later averaging */
  70. fRollingClientUsecs[fRollingClientUsecsIndex++] = lastCycleEnd - fLastTime;
  71. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) {
  72. fRollingClientUsecsIndex = 0;
  73. }
  74. /* every so often, recompute the current maximum use over the
  75. last JACK_ENGINE_ROLLING_COUNT client iterations.
  76. */
  77. if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
  78. jack_time_t maxUsecs = 0;
  79. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  80. maxUsecs = std::max(fRollingClientUsecs[i], maxUsecs);
  81. }
  82. fMaxUsecs = std::max(fMaxUsecs, maxUsecs);
  83. fSpareUsecs = jack_time_t((maxUsecs < fPeriodUsecs) ? fPeriodUsecs - maxUsecs : 0);
  84. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  85. }
  86. }
  87. void JackEngineControl::ResetRollingUsecs()
  88. {
  89. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  90. fRollingClientUsecsIndex = 0;
  91. fRollingClientUsecsCnt = 0;
  92. fSpareUsecs = 0;
  93. fRollingInterval = (int)floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs);
  94. }
  95. void JackEngineControl::GetTimeMeasure(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  96. {
  97. int pos = (++fAudioCycle) % TIME_POINTS;
  98. fLastTime = fCurTime;
  99. fCurTime = callback_usecs;
  100. fLastProcessTime = fProcessTime;
  101. fProcessTime = GetMicroSeconds();
  102. if (fLastTime > 0) {
  103. fMeasure[pos].fEngineTime = fLastTime;
  104. fMeasure[pos].fAudioCycle = fAudioCycle;
  105. for (int i = 0; i < CLIENT_NUM; i++) {
  106. JackClientInterface* client = table[i];
  107. JackClientTiming* timing = manager->GetClientTiming(i);
  108. if (client && client->GetClientControl()->fActive) {
  109. fMeasure[pos].fClientTable[i].fRefNum = i;
  110. fMeasure[pos].fClientTable[i].fSignaledAt = timing->fSignaledAt;
  111. fMeasure[pos].fClientTable[i].fAwakeAt = timing->fAwakeAt;
  112. fMeasure[pos].fClientTable[i].fFinishedAt = timing->fFinishedAt;
  113. fMeasure[pos].fClientTable[i].fStatus = timing->fStatus;
  114. }
  115. }
  116. }
  117. }
  118. void JackEngineControl::ClearTimeMeasures()
  119. {
  120. for (int i = 0; i < TIME_POINTS; i++) {
  121. for (int j = 0; j < CLIENT_NUM; j++) {
  122. fMeasure[i].fClientTable[j].fRefNum = 0;
  123. fMeasure[i].fClientTable[j].fSignaledAt = 0;
  124. fMeasure[i].fClientTable[j].fAwakeAt = 0;
  125. fMeasure[i].fClientTable[j].fFinishedAt = 0;
  126. }
  127. }
  128. fLastTime = fCurTime = 0;
  129. }
  130. } // end of namespace