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.

93 lines
3.2KB

  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. // Store the execution time for later averaging
  46. fRollingClientUsecs[fRollingClientUsecsIndex++] = last_cycle_end - fPrevCycleTime;
  47. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT)
  48. fRollingClientUsecsIndex = 0;
  49. // Every so often, recompute the current maximum use over the
  50. // last JACK_ENGINE_ROLLING_COUNT client iterations.
  51. if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
  52. jack_time_t max_usecs = 0;
  53. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++)
  54. max_usecs = JACK_MAX(fRollingClientUsecs[i], max_usecs);
  55. fMaxUsecs = JACK_MAX(fMaxUsecs, max_usecs);
  56. fSpareUsecs = jack_time_t((max_usecs < fPeriodUsecs) ? fPeriodUsecs - max_usecs : 0);
  57. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  58. }
  59. }
  60. void JackEngineControl::ResetRollingUsecs()
  61. {
  62. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  63. fRollingClientUsecsIndex = 0;
  64. fRollingClientUsecsCnt = 0;
  65. fSpareUsecs = 0;
  66. fRollingInterval = int(floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs));
  67. }
  68. void JackEngineControl::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
  69. {
  70. ResetFrameTime(callback_usecs);
  71. fXrunDelayedUsecs = delayed_usecs;
  72. if (delayed_usecs > fMaxDelayedUsecs)
  73. fMaxDelayedUsecs = delayed_usecs;
  74. }
  75. } // end of namespace