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.

155 lines
4.7KB

  1. /*
  2. Copyright (C) 2003 Paul Davis
  3. Copyright (C) 2004-2006 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 <math.h>
  20. namespace Jack
  21. {
  22. void JackEngineControl::CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  23. {
  24. // Transport
  25. fTransport.CycleBegin(fSampleRate, callback_usecs);
  26. // Timer
  27. fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
  28. // Timing
  29. GetTimeMeasure(table, manager, callback_usecs);
  30. CalcCPULoad(table, manager);
  31. }
  32. void JackEngineControl::CycleEnd(JackClientInterface** table)
  33. {
  34. fTransport.CycleEnd(table, fSampleRate, fBufferSize);
  35. }
  36. void JackEngineControl::InitFrameTime()
  37. {
  38. fFrameTimer.InitFrameTime();
  39. }
  40. void JackEngineControl::ResetFrameTime(jack_time_t callback_usecs)
  41. {
  42. fFrameTimer.ResetFrameTime(fSampleRate, callback_usecs, fPeriodUsecs);
  43. }
  44. void JackEngineControl::ReadFrameTime(JackTimer* timer)
  45. {
  46. fFrameTimer.ReadFrameTime(timer);
  47. }
  48. // Private
  49. inline jack_time_t MAX(jack_time_t a, jack_time_t b)
  50. {
  51. return (a < b) ? b : a;
  52. }
  53. void JackEngineControl::CalcCPULoad(JackClientInterface** table, JackGraphManager* manager)
  54. {
  55. jack_time_t lastCycleEnd = fLastProcessTime;
  56. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  57. JackClientInterface* client = table[i];
  58. JackClientTiming* timing = manager->GetClientTiming(i);
  59. if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) {
  60. lastCycleEnd = MAX(lastCycleEnd, timing->fFinishedAt);
  61. }
  62. }
  63. /* store the execution time for later averaging */
  64. fRollingClientUsecs[fRollingClientUsecsIndex++] = lastCycleEnd - fLastTime;
  65. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) {
  66. fRollingClientUsecsIndex = 0;
  67. }
  68. /* every so often, recompute the current maximum use over the
  69. last JACK_ENGINE_ROLLING_COUNT client iterations.
  70. */
  71. if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
  72. jack_time_t maxUsecs = 0;
  73. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  74. maxUsecs = MAX(fRollingClientUsecs[i], maxUsecs);
  75. }
  76. fMaxUsecs = MAX(fMaxUsecs, maxUsecs);
  77. fSpareUsecs = jack_time_t((maxUsecs < fPeriodUsecs) ? fPeriodUsecs - maxUsecs : 0);
  78. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  79. }
  80. }
  81. void JackEngineControl::ResetRollingUsecs()
  82. {
  83. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  84. fRollingClientUsecsIndex = 0;
  85. fRollingClientUsecsCnt = 0;
  86. fSpareUsecs = 0;
  87. fRollingInterval = (int)floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs);
  88. }
  89. void JackEngineControl::GetTimeMeasure(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  90. {
  91. int pos = (++fAudioCycle) % TIME_POINTS;
  92. fLastTime = fCurTime;
  93. fCurTime = callback_usecs;
  94. fLastProcessTime = fProcessTime;
  95. fProcessTime = GetMicroSeconds();
  96. if (fLastTime > 0) {
  97. fMeasure[pos].fEngineTime = fLastTime;
  98. fMeasure[pos].fAudioCycle = fAudioCycle;
  99. for (int i = 0; i < CLIENT_NUM; i++) {
  100. JackClientInterface* client = table[i];
  101. JackClientTiming* timing = manager->GetClientTiming(i);
  102. if (client && client->GetClientControl()->fActive) {
  103. fMeasure[pos].fClientTable[i].fRefNum = i;
  104. fMeasure[pos].fClientTable[i].fSignaledAt = timing->fSignaledAt;
  105. fMeasure[pos].fClientTable[i].fAwakeAt = timing->fAwakeAt;
  106. fMeasure[pos].fClientTable[i].fFinishedAt = timing->fFinishedAt;
  107. fMeasure[pos].fClientTable[i].fStatus = timing->fStatus;
  108. }
  109. }
  110. }
  111. }
  112. void JackEngineControl::ClearTimeMeasures()
  113. {
  114. for (int i = 0; i < TIME_POINTS; i++) {
  115. for (int j = 0; j < CLIENT_NUM; j++) {
  116. fMeasure[i].fClientTable[j].fRefNum = 0;
  117. fMeasure[i].fClientTable[j].fSignaledAt = 0;
  118. fMeasure[i].fClientTable[j].fAwakeAt = 0;
  119. fMeasure[i].fClientTable[j].fFinishedAt = 0;
  120. }
  121. }
  122. fLastTime = fCurTime = 0;
  123. }
  124. } // end of namespace