News for hackers. Stuff that matters.
Re:How macOS manages M1 CPU cores by nyanpasu64 on Monday April 25, 2022 @06:33AM (#31152656)
I ran into issues with M1 core scheduling recently in one of my applications: https://github.com/corrscope/corrscope/#m1-slowdown=

corrscope is a PyQt app with a background thread which streams video frames to a ffplay process, which shows these frames in a video player window along with synchronized audio. During playback, ffplay is focused and corrscope lies in the background generating video frames.

> On M1 processors, if you open preview, after 40 or so seconds, the preview may suddenly slow down and audio will stop playing. This is because macOS thinks ffplay is the active app, and Corrscope is a background job burning CPU, so it moves Corrscope to an Efficiency core, slowing it down.

> There is no fix for this issue at the moment. As a workaround, you can click on Corrscope's window to avoid the slowdown, and drag it aside so it doesn't obstruct the preview.

Setting the background worker thread to QOS_CLASS_USER_INTERACTIVE does not prevent it from being moved to E-cores. This is a rather severe issue which damages the user experience, and I haven't found a workaround yet (outside of moving video playback in-process). How do Chrome and Firefox (and Safari I suppose) give their worker processes P-cores while users are interacting with the browser window process?

Re:How macOS manages M1 CPU cores by ninkendo on Monday April 25, 2022 @06:50AM (#31152786)
> How do Chrome and Firefox (and Safari I suppose) give their worker processes P-cores while users are interacting with the browser window process?

Generally if a UI process makes IPC calls out to a daemon or other process, the Mach kernel is supposed to keep track of prioritization and an on-behalf-of relationship for the daemon. That is, if the kernel already thinks the foreground app is P-core eligible, any work it initiates in other processes via IPC should be P-core eligible as well, through a “voucher” object that is passed between processes: https://opensource.apple.com/source/xnu/xnu-3789.41.3/osfmk/...

I’m not sure the mechanism you’re using to put video playback out-of-process, but if there’s any IPC calls involved, make sure they’re originating from a UI task, and not the other way around? Hopefully this gives you something to go on.

Re:How macOS manages M1 CPU cores by nyanpasu64 on Monday April 25, 2022 @07:06AM (#31152883)
Frames are passed through a Unix pipeline (to stdin rather than a named pipe). What's a UI task?
Re:How macOS manages M1 CPU cores by conradev on Monday April 25, 2022 @10:22AM (#31155224)
Is the python process a direct child of the UI process?
Re:How macOS manages M1 CPU cores by nyanpasu64 on Monday April 25, 2022 @08:03PM (#31162460)
The Python thread shares a process with the GUI thread. It actually pipes data to a ffmpeg process in between, to mix video frames and audio into a NUT stream, which gets piped to the ffplay GUI process. IIRC both processes are created by Python, but we only pipe data directly into ffmpeg.
Re:How macOS manages M1 CPU cores by saagarjha on Monday April 25, 2022 @06:49AM (#31152780)
Perhaps you are running into App Nap?
Re:How macOS manages M1 CPU cores by nyanpasu64 on Monday April 25, 2022 @07:58AM (#31153274)
Not sure why you're downvoted; you seem to be right! In Activity Monitor's Energy tab, I have to expand Terminal > login > zsh > python, but my python process is shown as App Nap: Yes.

How would I opt a program out of App Nap? Should I trust https://pypi.org/project/appnope/?

Re:How macOS manages M1 CPU cores by jamesfmilne on Monday April 25, 2022 @04:13PM (#31159947)
This is how you do it from Objective-C:

  // Call NSProcessInfo to disable AppNap et al.
  NSActivityOptions activityOptions =
      NSActivityIdleDisplaySleepDisabled |
      NSActivityIdleSystemSleepDisabled |
      NSActivityLatencyCritical;

  NSProcessInfo* procInfo = [NSProcessInfo processInfo];
  m_NoSleepActivity = [procInfo beginActivityWithOptions:activityOptions reason:@"Running some stuff"];
Re:How macOS manages M1 CPU cores by dave78 on Monday April 25, 2022 @08:14AM (#31153397)
I've used AppNope with success, though I've never tested on an M1.