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.

135 lines
4.3KB

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