Skip to content
Permalink
Browse files

display memory usage in user interface

  • Loading branch information...
mmckegg committed Dec 31, 2017
1 parent 5502b96 commit 0ef4cc4234c5ab88334d4488f0c4dac5e6c88cc9
Showing with 51 additions and 1 deletion.
  1. +9 −0 index.js
  2. +1 −0 main.js
  3. +20 −1 nodes/project/tabbed-editor.js
  4. +21 −0 styles/memory-usage.mcss
@@ -78,9 +78,18 @@ var audioContext = new global.AudioContext()
// monkey patch until we upgrade to chrome > 60
audioContext.baseLatency = 1 / audioContext.sampleRate * 256

var heapSize = Observ(0)
setInterval(() => {
var newValue = Math.round(window.performance.memory.usedJSHeapSize / 1024)
if (heapSize() !== newValue) {
heapSize.set(newValue)
}
}, 1000)

var nodes = require('./nodes')
var rootContext = window.rootContext = {
fs: fs,
heapSize,
audio: audioContext,
periodicWaves: PeriodicWaves(audioContext),
midiClockOffset: MidiClockOffset(audioContext),
@@ -12,6 +12,7 @@ var storage = require('electron-json-storage')

// expose manual gc()
electron.app.commandLine.appendSwitch('js-flags', '--expose_gc')
electron.app.commandLine.appendSwitch('enable-precise-memory-info')

var menu = defaultMenu(electron.app, electron.shell)
menu.splice(1, 0, {
@@ -2,7 +2,6 @@ var h = require('lib/h')
var send = require('mutant/send')
var getBaseName = require('path').basename
var getDirectory = require('path').dirname
var join = require('path').join
var Map = require('mutant/map')
var computed = require('mutant/computed')
var when = require('mutant/when')
@@ -13,6 +12,7 @@ var ToggleButton = require('lib/params/toggle-button')
var renderNode = require('lib/render-node')
var rawEditor = require('./editor/raw.js')
var renderWelcome = require('./welcome')
var mags = ' KMGTPEZY';

module.exports = TabbedEditor

@@ -40,7 +40,18 @@ function TabbedEditor (project) {
})
})

var memoryClass = computed(project.context.heapSize, (kb) => {
if (kb > 150 * 1024) {
return '-critical'
} else if (kb > 100 * 1024) {
return '-warn'
}
})

var controls = h('span.controls', [
h('MemoryUsage', {classList: memoryClass, tabIndex: 1, 'ev-click': send(actions.purge), title: 'Click to purge'}, [
computed(project.context.heapSize, (v) => humanSize(v * 1000))
]),
ToggleButton(project.rawMode, {
title: 'raw'
}),
@@ -101,3 +112,11 @@ function getName (path) {
function eq (a, b) {
return a === b
}


function humanSize(bytes, precision) {
var magnitude = Math.min(Math.log(bytes) / Math.log(1024) | 0, mags.length - 1);
var result = bytes / Math.pow(1024, magnitude);
var suffix = mags[magnitude].trim() + 'B';
return result.toFixed(precision) + ' ' + suffix;
}
@@ -0,0 +1,21 @@
MemoryUsage {
padding: 3px 6px;
font-size: 10px;
border: 1px solid #6e7b6e;
background: #000;
color: #ACA;
margin-right: 3px
cursor: pointer

-critical {
border: 1px solid #ff6b6b;
background: #711313;
color: #ffd6d6;
}

-warn {
border: 1px solid #b5ad53;
background: #655907;
color: #fffb07;
}
}

0 comments on commit 0ef4cc4

Please sign in to comment.
You can’t perform that action at this time.