justingardner / mgl

A suite of mex/m files for displaying psychophysics stimuli
http://justingardner.net/mgl
Other
18 stars 22 forks source link

Slow responsiveness #57

Closed justingardner closed 2 years ago

justingardner commented 2 years ago

I've had an issue in which when I am interacting with an open mgl window by typing commands at the buffer, that if I haven't done anything for a while, the commands take a long time to run - e.g. do nothing for a few minutes (or edit a file) and then run mglClearScreen then mglFlush and it will take several seconds for these to run - what is going on? Is the application in a sleep mode? Can we avoid this from happening when experiments are being run, (it's ok during development)...

benjamin-heasly commented 2 years ago

Potentially OS scheduling and backgrounding.

justingardner commented 2 years ago

Screen Shot 2022-04-24 at 12 21 54 PM I think this is happening because mglMetal is going into an "App Nap" status. It starts out in Activity Monitor as App Nap = "No" and then if you leave it for a while with giving any commands it goes into App Nap = "Yes" at which point it behaves like the above (accepts commands with a long lag). If you hit it with a few commands, it comes back to instant responsiveness and App Nap = "No". Solution should be pretty simple, I would think, must be something to check in the build that prevents apps from napping?

benjamin-heasly commented 2 years ago

Oh, nice one. "App nap" rhymes so nicely, but I guess we want to turn it off despite.

It looks like folks have a straightforward way to disable this: https://stackoverflow.com/questions/27653939/disable-app-nap-in-swift

So hopefully this will be straightforward.

benjamin-heasly commented 2 years ago

OK cool, I think this is resolved in d84e42f1e966da8d7aa58696fa3f8e08ea51aec4.

Here's the official recommendation from Apple, similar to the SO post I linked above https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/PrioritizeWorkAtTheAppLevel.html

Testing on my machine, I could reproduce the App Nap by letting mglMetal sit idle for about a minute. Then I added a "user initiated" "activity" to the app and I no longer see the App Nap.

Screen Shot 2022-04-29 at 11 46 19 AM

I also see that the app is preventing sleep, which I think we want, but is a choice we could revisit if necessary. It looks like there's a more granular type of "activity" that would prevent App Nap for mglMetal, but also let the system sleep when everything is idle.