Bug: macOS SystemStats::getCpuSpeedInMegahertz() returns 0

On my mac running 12.0.1 and Apple Silicon, the function mentioned in the title returns 0.

I think this might be a deliberate move by Apple. On both Intel and Arm processors, we call sysctl and request the HW_CPU_FREQ. This works on Intel, but returns 0 on Arm.

Perhaps there are other ways of finding this information, but it looks like Apple doesn’t want to make it easy…

Quick googling only suggested this ugly call. Didn’t check if there’s equivalent syscall.

https://developer.apple.com/forums/thread/671792

Seems like the powermetrics command suggested in the link that @ttg posted actually is a command that executes some measurements when invoked. I just tried it and indeed it takes a few seconds to return, printing out detailed statistics. It needs root rights, so I doubt that this is a real solution.

I’m not completely sure about that, but as far as I know, the M1 ARM SoC uses a hybrid approach with some low power and some performance CPU cores on one chip. I would not be too surprised if those two kinds of CPU cores would run at different clock speeds or if the whole CPU has a dynamically controlled clock speed making a single frequency a quite meaningless number, which might be an explanation why the system call doesn’t report any meaningful number at all. But that are all just guesses.

But anyway, may I ask what’s the actual use-case of querying the CPU speed in an application – besides displaying some machine information?

That’s what I found last night about powermetrics when toying around.

As for the guesses - my light reading hasn’t turned up anything definitive about how Apple manages their cores and clock speeds. That being said, I wouldn’t be surprised if what you said is true: differing clock speeds is a fairly normal operation for an underlying system so as to get lower power use (see dynamic frequency scaling).

But anyway, may I ask what’s the actual use-case of querying the CPU speed in an application – besides displaying some machine information?

Seeing that I don’t work on anything like CPU-Z I haven’t much use for the API myself. The point is to flag to the JUCE devs that the API isn’t doing what it should.