cpuload.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-FileCopyrightText: 2012-2026 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. // Per-plugin CPU panel, opened by clicking the CPU bar.
  4. //
  5. // mod-host times each plugin on every cycle and pushes "plugin_cpu_load <instance> <pct>"
  6. // on the websocket whenever a plugin beats its own record. That record starts again from
  7. // zero on every feedback round, so the panel keeps the peak itself. Peaks, not averages: a
  8. // plugin that averages 5% and spikes to 60% is the one that drops audio, and an
  9. // average hides exactly that. Nothing is polled, here or in mod-host -- reporting is
  10. // switched on while the panel is open and off again when it closes.
  11. //
  12. // Two columns, because "expensive" means two things. The percentage is the plugin's
  13. // share of one audio cycle. What it costs the cycle is different: plugins run on
  14. // parallel jack threads, so one on a branch that finishes early can be costly and
  15. // still free in xrun terms. The biggest consumer is regularly not the one to remove.
  16. function CpuLoadPanel(options) {
  17. var self = this
  18. options = $.extend({
  19. window: $('<div>'),
  20. list: $('<div>'),
  21. summary: $('<div>'),
  22. pedalboard: $('<div>'),
  23. }, options)
  24. var CRITICAL_TIP = 'On the longest chain of plugins that wait on each other. ' +
  25. 'Whatever you save here comes straight off the audio cycle.'
  26. // instance -> worst share of a cycle seen since monitoring was last reset
  27. this.peaks = {}
  28. this.order = []
  29. this.open_ = false
  30. this.repaint = null
  31. // The explanation is long and only wanted once, so it hides behind the (i).
  32. var help = options.window.find('.cpu-load-help')
  33. var info = options.window.find('.cpu-load-info')
  34. info.click(function (e) {
  35. e.stopPropagation()
  36. help.toggleClass('mod-hidden')
  37. info.attr('aria-expanded', help.hasClass('mod-hidden') ? 'false' : 'true')
  38. .toggleClass('open', !help.hasClass('mod-hidden'))
  39. })
  40. this.open = function () {
  41. if (self.open_) {
  42. return
  43. }
  44. self.open_ = true
  45. self.peaks = {}
  46. self.order = []
  47. options.list.html('<div class="cpu-load-empty">Waiting for the first cycle...</div>')
  48. options.summary.text('')
  49. help.addClass('mod-hidden')
  50. info.attr('aria-expanded', 'false').removeClass('open')
  51. options.window.removeClass('mod-hidden')
  52. self.subscribe(1)
  53. }
  54. this.close = function () {
  55. if (! self.open_) {
  56. return
  57. }
  58. self.open_ = false
  59. self.subscribe(0)
  60. options.window.addClass('mod-hidden')
  61. }
  62. this.toggle = function () {
  63. if (self.open_) { self.close() } else { self.open() }
  64. }
  65. this.subscribe = function (enable) {
  66. $.ajax({
  67. url: '/cpu_monitor/' + enable,
  68. type: 'POST',
  69. cache: false,
  70. dataType: 'json',
  71. success: function (ok) {
  72. // false is mod-host refusing the command (or not answering in time): a
  73. // mod-host without monitor_cpu_load would otherwise leave the panel waiting forever
  74. if (! ok && enable && self.open_) {
  75. options.list.html('<div class="cpu-load-empty">Per-plugin CPU monitoring is not ' +
  76. 'available: mod-host did not accept the monitor_cpu_load command.</div>')
  77. }
  78. },
  79. error: function () {
  80. if (self.open_) {
  81. self.close()
  82. new Bug("Could not start plugin CPU monitoring")
  83. }
  84. },
  85. })
  86. }
  87. // The panel holds the peaks, so clearing them here is the reset; enabling again
  88. // also starts mod-host's records from zero
  89. this.reset = function () {
  90. self.peaks = {}
  91. self.order = []
  92. options.list.html('<div class="cpu-load-empty">Waiting for the first cycle...</div>')
  93. self.subscribe(1)
  94. }
  95. // One pushed reading. They arrive per plugin as records are beaten, so repaint on a
  96. // timer rather than per message -- a burst would otherwise redraw the list per row.
  97. this.setLoad = function (instance, percent) {
  98. if (! self.open_) {
  99. return
  100. }
  101. // mod-host's record restarts every feedback round, so a lower value is only a
  102. // quieter round, not a new peak
  103. if (self.peaks[instance] !== undefined && self.peaks[instance] >= percent) {
  104. return
  105. }
  106. self.peaks[instance] = percent
  107. if (self.repaint === null) {
  108. self.repaint = setTimeout(function () {
  109. self.repaint = null
  110. if (self.open_) { self.render() }
  111. }, 200)
  112. }
  113. }
  114. // Signal-flow edges between instances, audio only. MIDI connections order the jack
  115. // graph too, but they do not carry the DSP work, and treating them as dependencies
  116. // makes the graph look far more serial than it runs.
  117. //
  118. // Hardware ports keep their own port path as the node name. Reducing them to their
  119. // instance would fold capture and playback into one node and invent a loop through
  120. // the whole pedalboard.
  121. this.edges = function () {
  122. var connMgr = options.pedalboard.data('connectionManager')
  123. var edges = []
  124. if (!connMgr) {
  125. return edges
  126. }
  127. function node(port) {
  128. var instance = port.substring(0, port.lastIndexOf("/"))
  129. return instance === "/graph" ? port : instance
  130. }
  131. connMgr.iterate(function (jack) {
  132. var destination = jack.data('destination')
  133. if (!destination || !destination.hasClass('mod-audio-input')) {
  134. return
  135. }
  136. var from = node(jack.data('origin').attr('mod-port'))
  137. var to = node(destination.attr('mod-port'))
  138. if (from !== to) {
  139. edges.push([from, to])
  140. }
  141. })
  142. return edges
  143. }
  144. // Row order is settled once, while the first readings come in, and then left
  145. // alone. Peaks creep upward forever, so re-sorting on every update makes
  146. // near-equal plugins trade places continuously -- motion that carries no
  147. // information. Values and bars keep updating in place; "Reset peaks" re-ranks.
  148. this.render = function () {
  149. var instances = Object.keys(self.peaks)
  150. if (instances.length === 0) {
  151. options.list.html('<div class="cpu-load-empty">No plugins loaded</div>')
  152. options.summary.text('')
  153. self.order = []
  154. return
  155. }
  156. // a plugin reporting for the first time is still the list filling up
  157. if (instances.length !== self.order.length) {
  158. self.order = instances.sort(function (a, b) { return self.peaks[b] - self.peaks[a] })
  159. self.build()
  160. }
  161. var graph = graphSlack(self.peaks, self.edges())
  162. var total = 0
  163. var i
  164. for (i = 0; i < self.order.length; i++) {
  165. var instance = self.order[i]
  166. var load = self.peaks[instance]
  167. var slack = graph.slack[instance]
  168. var critical = slack !== undefined && slack < 0.01
  169. var row = options.list.find('.cpu-load-row[mod-instance="' + instance + '"]')
  170. total += load
  171. // Scaled against a whole cycle, not against the busiest plugin. Scaling to
  172. // the busiest means its every new record resizes every other bar, so the
  173. // whole list twitches when one plugin moves. This way a bar only changes
  174. // when its own plugin does, and its width means something on its own.
  175. row.find('.cpu-load-meter').css('width', Math.min(100, load).toFixed(1) + '%')
  176. row.find('.cpu-load-value').text(load.toFixed(1) + '%')
  177. row.find('.cpu-load-slack').text(critical ? 'critical path'
  178. : (slack === undefined ? '' : slack.toFixed(1) + '% spare'))
  179. row.toggleClass('critical', critical)
  180. row.attr('title', row.data('name') + ' - ' + load.toFixed(1) + '% of a cycle at worst. ' +
  181. (critical ? CRITICAL_TIP
  182. : 'Runs in parallel with something slower, and finishes early. It could use ' +
  183. (slack === undefined ? '0' : slack.toFixed(1)) +
  184. '% more CPU before it started making the cycle longer, ' +
  185. 'so making it cheaper buys no headroom.'))
  186. }
  187. options.summary.html(
  188. escapeHtml(self.order.length + ' plugins. Worst cycle seen so far for each. ' +
  189. 'Adding up the longest chain gives ' + graph.critical.toFixed(0) + '% of a cycle, ' +
  190. 'a worst case that has to stay under 100%.'))
  191. }
  192. // Rebuild the rows. Only called when the set of plugins changes, so the list does
  193. // not flicker while values update.
  194. this.build = function () {
  195. var html = '<div class="cpu-load-head">' +
  196. '<span class="cpu-load-name">plugin</span>' +
  197. '<span class="cpu-load-value">cpu</span>' +
  198. '<span class="cpu-load-slack">effect on the audio cycle</span>' +
  199. '</div>'
  200. var names = {}
  201. for (var i = 0; i < self.order.length; i++) {
  202. var instance = self.order[i]
  203. var gui = options.pedalboard.pedalboard('getGui', instance)
  204. var name = (gui && gui.effect && (gui.effect.label || gui.effect.name)) ||
  205. instance.replace('/graph/', '')
  206. names[instance] = name
  207. html += '<div class="cpu-load-row" mod-instance="' + escapeHtml(instance) + '">' +
  208. '<div class="cpu-load-meter"></div>' +
  209. '<span class="cpu-load-name">' + escapeHtml(name) + '</span>' +
  210. '<span class="cpu-load-value"></span>' +
  211. '<span class="cpu-load-slack"></span>' +
  212. '</div>'
  213. }
  214. options.list.html(html)
  215. options.list.find('.cpu-load-row').each(function () {
  216. var row = $(this)
  217. row.data('name', names[row.attr('mod-instance')])
  218. })
  219. // clicking a row jumps to that plugin on the board
  220. options.list.find('.cpu-load-row').click(function () {
  221. var instance = $(this).attr('mod-instance')
  222. var pedal = $('.mod-pedal[mod-instance="' + instance + '"]')
  223. if (pedal.length === 0) {
  224. return
  225. }
  226. self.close()
  227. options.pedalboard.pedalboard('focusPlugin', pedal)
  228. })
  229. }
  230. function escapeHtml(text) {
  231. return $('<div>').text(text).html()
  232. }
  233. }