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.

144 lines
4.4KB

  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackEngineControl.h"
  20. #include "JackGraphManager.h"
  21. #include "JackClientControl.h"
  22. #include <algorithm>
  23. #include <math.h>
  24. namespace Jack
  25. {
  26. void JackEngineControl::CycleIncTime(jack_time_t callback_usecs)
  27. {
  28. // Timer
  29. fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
  30. }
  31. void JackEngineControl::CycleBegin(JackClientInterface** table,
  32. JackGraphManager* manager,
  33. jack_time_t cur_cycle_begin,
  34. jack_time_t prev_cycle_end)
  35. {
  36. // Transport
  37. fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
  38. // Timing
  39. //GetTimeMeasure(table, manager, callback_usecs);
  40. CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
  41. }
  42. void JackEngineControl::CycleEnd(JackClientInterface** table)
  43. {
  44. fTransport.CycleEnd(table, fSampleRate, fBufferSize);
  45. }
  46. void JackEngineControl::InitFrameTime()
  47. {
  48. fFrameTimer.InitFrameTime();
  49. }
  50. void JackEngineControl::ResetFrameTime(jack_time_t cur_cycle_begin)
  51. {
  52. fFrameTimer.ResetFrameTime(fSampleRate, cur_cycle_begin, fPeriodUsecs);
  53. }
  54. void JackEngineControl::ReadFrameTime(JackTimer* timer)
  55. {
  56. fFrameTimer.ReadFrameTime(timer);
  57. }
  58. // Private
  59. #ifdef WIN32
  60. inline jack_time_t MAX(jack_time_t a, jack_time_t b)
  61. {
  62. return (a < b) ? b : a;
  63. }
  64. #else
  65. #define MAX(a,b) std::max((a),(b))
  66. #endif
  67. void JackEngineControl::CalcCPULoad(JackClientInterface** table,
  68. JackGraphManager* manager,
  69. jack_time_t cur_cycle_begin,
  70. jack_time_t prev_cycle_end)
  71. {
  72. fPrevCycleTime = fCurCycleTime;
  73. fCurCycleTime = cur_cycle_begin;
  74. jack_time_t last_cycle_end = prev_cycle_end;
  75. // In Asynchronous mode, last cycle end is the max of client end dates
  76. if (!fSyncMode) {
  77. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  78. JackClientInterface* client = table[i];
  79. JackClientTiming* timing = manager->GetClientTiming(i);
  80. if (client && client->GetClientControl()->fActive && timing->fStatus == Finished)
  81. last_cycle_end = MAX(last_cycle_end, timing->fFinishedAt);
  82. }
  83. }
  84. // Store the execution time for later averaging
  85. fRollingClientUsecs[fRollingClientUsecsIndex++] = last_cycle_end - fPrevCycleTime;
  86. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT)
  87. fRollingClientUsecsIndex = 0;
  88. // Every so often, recompute the current maximum use over the
  89. // last JACK_ENGINE_ROLLING_COUNT client iterations.
  90. if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
  91. jack_time_t max_usecs = 0;
  92. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++)
  93. max_usecs = MAX(fRollingClientUsecs[i], max_usecs);
  94. fMaxUsecs = MAX(fMaxUsecs, max_usecs);
  95. fSpareUsecs = jack_time_t((max_usecs < fPeriodUsecs) ? fPeriodUsecs - max_usecs : 0);
  96. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  97. }
  98. }
  99. void JackEngineControl::ResetRollingUsecs()
  100. {
  101. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  102. fRollingClientUsecsIndex = 0;
  103. fRollingClientUsecsCnt = 0;
  104. fSpareUsecs = 0;
  105. fRollingInterval = int(floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs));
  106. }
  107. void JackEngineControl::NotifyXRun(float delayed_usecs)
  108. {
  109. fXrunDelayedUsecs = delayed_usecs;
  110. if (delayed_usecs > fMaxDelayedUsecs)
  111. fMaxDelayedUsecs = delayed_usecs;
  112. }
  113. void JackEngineControl::ResetXRun()
  114. {
  115. fMaxDelayedUsecs = 0.f;
  116. }
  117. } // end of namespace