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.

110 lines
3.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 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::CalcCPULoad(JackClientInterface** table,
  29. JackGraphManager* manager,
  30. jack_time_t cur_cycle_begin,
  31. jack_time_t prev_cycle_end)
  32. {
  33. fPrevCycleTime = fCurCycleTime;
  34. fCurCycleTime = cur_cycle_begin;
  35. jack_time_t last_cycle_end = prev_cycle_end;
  36. // In Asynchronous mode, last cycle end is the max of client end dates
  37. if (!fSyncMode) {
  38. for (int i = fDriverNum; i < CLIENT_NUM; i++) {
  39. JackClientInterface* client = table[i];
  40. JackClientTiming* timing = manager->GetClientTiming(i);
  41. if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) {
  42. last_cycle_end = JACK_MAX(last_cycle_end, timing->fFinishedAt);
  43. }
  44. }
  45. }
  46. // Store the execution time for later averaging
  47. if (last_cycle_end > 0) {
  48. fRollingClientUsecs[fRollingClientUsecsIndex++] = last_cycle_end - fPrevCycleTime;
  49. }
  50. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) {
  51. fRollingClientUsecsIndex = 0;
  52. }
  53. // Each time we have a full set of iterations, recompute the current
  54. // usage from the latest JACK_ENGINE_ROLLING_COUNT client entries.
  55. if (fRollingClientUsecsCnt && (fRollingClientUsecsIndex == 0)) {
  56. jack_time_t avg_usecs = 0;
  57. jack_time_t max_usecs = 0;
  58. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  59. avg_usecs += fRollingClientUsecs[i]; // This is really a running total to be averaged later
  60. max_usecs = JACK_MAX(fRollingClientUsecs[i], max_usecs);
  61. }
  62. fMaxUsecs = JACK_MAX(fMaxUsecs, max_usecs);
  63. if (max_usecs < ((fPeriodUsecs * 95) / 100)) {
  64. // Average the values from our JACK_ENGINE_ROLLING_COUNT array
  65. fSpareUsecs = (jack_time_t)(fPeriodUsecs - (avg_usecs / JACK_ENGINE_ROLLING_COUNT));
  66. } else {
  67. // Use the 'worst case' value (or zero if we exceeded 'fPeriodUsecs')
  68. fSpareUsecs = jack_time_t((max_usecs < fPeriodUsecs) ? fPeriodUsecs - max_usecs : 0);
  69. }
  70. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  71. }
  72. fRollingClientUsecsCnt++;
  73. }
  74. void JackEngineControl::ResetRollingUsecs()
  75. {
  76. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  77. fRollingClientUsecsIndex = 0;
  78. fRollingClientUsecsCnt = 0;
  79. fSpareUsecs = 0;
  80. fRollingInterval = int(floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs));
  81. }
  82. void JackEngineControl::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
  83. {
  84. ResetFrameTime(callback_usecs);
  85. fXrunDelayedUsecs = delayed_usecs;
  86. if (delayed_usecs > fMaxDelayedUsecs) {
  87. fMaxDelayedUsecs = delayed_usecs;
  88. }
  89. }
  90. } // end of namespace