Skip to content

Apple Silicon GPU support #1470

Draft
Ruaneri-Portela wants to merge 1 commit intoaristocratos:mainfrom
Ruaneri-Portela:main
Draft

Apple Silicon GPU support #1470
Ruaneri-Portela wants to merge 1 commit intoaristocratos:mainfrom
Ruaneri-Portela:main

Conversation

@Ruaneri-Portela
Copy link
Copy Markdown
Contributor

@Ruaneri-Portela Ruaneri-Portela commented Jan 13, 2026

With help from the folks at Issues #701 , I organized it using only IOKit, which provides the data without root.

It has only been tested on my M4 Pro platform, so anyone willing to test it would be very welcome.

Apparently, we can use the IOReport framework to obtain energy data for the CPU, such as usage in watts and clock speed.

We can also use the same framework to add the ANE (Apple Neural Engine).

image

@deckstose
Copy link
Copy Markdown
Collaborator

deckstose commented Jan 13, 2026

So this adds a powemetrics api, and then removes it?

Copy link
Copy Markdown
Collaborator

@deckstose deckstose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the code is not properly formatted.

There is a huge amount of AI assisted code in there, this is what I was getting from the issue comments (?).

Most of the code needs COMMENTS. Especially the function pointer magic.

Comment thread btop.desktop
@Ruaneri-Portela
Copy link
Copy Markdown
Contributor Author

I had kept the power metrics to read the CPU data. But I think it's outside the scope to just add the GPU. I removed it for now.

With a little work, you can get the data via IOKit.

I took Emanuele Zattin's code, which was allegedly written by him using AI, and reworked some things that I thought were relevant.

I can redo it by scheduling with AI.

@Ruaneri-Portela Ruaneri-Portela force-pushed the main branch 6 times, most recently from 2e6951e to 215cbe0 Compare January 13, 2026 23:58
@Ruaneri-Portela
Copy link
Copy Markdown
Contributor Author

I added some comments, but I think it might not be enough, and I haven't formatted it properly yet.

@deckstose
Copy link
Copy Markdown
Collaborator

I don't care about the boolean operators ! == not etc, it's more about indentation, line length and naming (snake_case) functions.

Also IMO comments should not include the question mark (although it's done in other parts of the code). It's fine either way, but redundant in a way. And I'm not sure if editors with their commenting feature support it.

@vandabbin
Copy link
Copy Markdown
Contributor

Also IMO comments should not include the question mark (although it's done in other parts of the code). It's fine either way, but redundant in a way. And I'm not sure if editors with their commenting feature support it.

whoops that how I was commenting code here because almost all the comments I already saw in the code had it. I will keep that in mind in the future

@deckstose
Copy link
Copy Markdown
Collaborator

No worries, I have merged code with this style

@deckstose deckstose marked this pull request as draft January 14, 2026 17:16
@deckstose
Copy link
Copy Markdown
Collaborator

Please mark this ready for review when this is ready

@Ruaneri-Portela Ruaneri-Portela force-pushed the main branch 2 times, most recently from 0331902 to 0321cd7 Compare January 14, 2026 17:44
@Ruaneri-Portela
Copy link
Copy Markdown
Contributor Author

I made the changes, but I'm not sure if it's any good.

If you could take a look

@aristocratos aristocratos added the AI generated Majority of included code is AI generated label Jan 15, 2026
@deckstose
Copy link
Copy Markdown
Collaborator

I will take a look but it's not going to be anytime soon

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds GPU monitoring support for Apple Silicon Macs by leveraging IOKit framework to collect GPU metrics without requiring root privileges. This addresses issue #701 by providing a sudoless alternative to powermetrics for GPU monitoring on Apple Silicon.

Changes:

  • Added new IOKit utility framework for CoreFoundation object handling and IOService iteration
  • Implemented Apple Silicon GPU monitoring via IOAccelerator and IOReport frameworks
  • Refactored existing code to use new safe helper functions for CF object conversions
  • Integrated GPU support into build systems (Makefile and CMake)

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 17 comments.

Show a summary per file
File Description
src/osx/smc.hpp Removed unused masterPort variable
src/osx/sensors.cpp Refactored to use new safe CF conversion helpers and moved HID declarations
src/osx/iokit.hpp New header defining IOKit utility functions and IOReport framework bindings
src/osx/iokit.cpp Implementation of safe CF converters and IOService iterators
src/osx/gpu.hpp New header defining GPU monitoring classes (GPU, GPUActivities, IOGPU)
src/osx/gpu.cpp Implementation of GPU metrics collection via IOAccelerator and IOReport
src/osx/btop_collect.cpp Integrated GPU collection into main collection loop
src/btop_shared.hpp Added Apple-specific shutdown namespace for GPU support
src/btop_config.cpp Added "apple" to default shown_gpus config on macOS
src/btop.cpp Added IOAccelerator shutdown call for macOS
Makefile Enabled GPU_SUPPORT for macOS ARM64
CMakeLists.txt Added iokit.cpp and gpu.cpp sources, enabled GPU_SUPPORT definition

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/osx/gpu.hpp Outdated
Comment thread src/osx/gpu.hpp Outdated
Comment thread src/osx/gpu.hpp Outdated
Comment thread src/osx/gpu.hpp Outdated
Comment thread src/osx/gpu.cpp Outdated
Comment thread src/osx/btop_collect.cpp Outdated
Comment thread src/osx/btop_collect.cpp
Comment thread src/osx/gpu.cpp Outdated
Comment thread src/osx/iokit.cpp Outdated
Comment thread src/osx/gpu.cpp Outdated
@aristocratos
Copy link
Copy Markdown
Owner

Seems to work as intended on a Mac Mini M1 running Tahoe.

But would be great to have some testing done on other Mac hardware.

What of the code is AI generated? Because that code would need to be checked so it hasn't been "lifted" from some other project (and might have missing attribution or incompatible licensing).

@Ruaneri-Portela
Copy link
Copy Markdown
Contributor Author

Seems to work as intended on a Mac Mini M1 running Tahoe.

But would be great to have some testing done on other Mac hardware.

What of the code is AI generated? Because that code would need to be checked so it hasn't been "lifted" from some other project (and might have missing attribution or incompatible licensing).

My hardware is an M4 Pro, and that's where I tested it.

Regarding the code being marked by AI, and because of Emanuele Zattin's contribution, parts of what the code estimates to be CPU usage were inspired by AI. I say inspired because I didn't request a code per se, but rather the procedure.

Our reference code is https://github.com/dehydratedpotato/socpowerbud
Whose code is MIT.

@aristocratos aristocratos removed the AI generated Majority of included code is AI generated label Jan 19, 2026
Copy link
Copy Markdown
Collaborator

@deckstose deckstose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pointed out a couple of patterns, they should apply everywhere.

I can't say much about the actual implementation, but it looks good over all.

@aristocratos

I wonder if it is necessary to have a struct indicating no GPU support, the undefined GPU_SUPPORT macro should be enough, right?

btop/src/btop_shared.hpp

Lines 192 to 196 in 0c11d73

#else
struct gpu_info {
bool supported = false;
};
#endif

EDIT: Resolved in #1495.

Comment thread src/osx/btop_collect.cpp


namespace Gpu {
#ifdef GPU_SUPPORT
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namespace should be inside ifdef

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends on #1495

Comment thread src/osx/btop_collect.cpp
namespace rng = std::ranges;
using namespace Tools;


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needless newline.

Comment thread src/osx/btop_collect.cpp
#include "sensors.hpp"
#endif
#include "smc.hpp"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needless newline.

Comment thread src/osx/btop_collect.cpp Outdated
Mem::old_uptime = system_uptime();
Mem::collect();

#ifdef GPU_SUPPORT
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put macros at the start of the line.

Comment thread src/osx/btop_collect.cpp Outdated
Comment thread src/osx/gpu.cpp Outdated
Comment thread src/osx/gpu.cpp Outdated
};

if (auto it = map.find(key); it != map.end()) {
usage.*(it->second) = value;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be rewritten in a more readable syntax.

Comment thread src/osx/gpu.cpp Outdated
proc = std::stoul(s.substr(pidPos + 4, commaPos - (pidPos + 4)));
name = s.substr(commaPos + 2);
} catch (...) {
return;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing log?

Comment thread src/osx/gpu.cpp Outdated
Comment thread CMakeLists.txt Outdated
Comment on lines +196 to +198
if(APPLE AND BTOP_GPU)
target_compile_definitions(libbtop PUBLIC GPU_SUPPORT)
endif()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine this branch with the Linux branch with GPU_SUPPORT defined

@vandabbin
Copy link
Copy Markdown
Contributor

vandabbin commented Jan 19, 2026

Seems to work as intended on a Mac Mini M1 running Tahoe.

But would be great to have some testing done on other Mac hardware.

I tested it just now on my M1 Macbook Pro running 15.7.3 and it also seems to be working. I don't have any newer apple hardware to test with though.

However maybe I'm wrong and its just apple silicon proving how efficient it is but I have trouble believing that I was getting 1 watt power usage playing 4k video. It fluctuated between .73 and 1.03

Screenshot 2026-01-19 at 3 43 34 PM

also I'm not so sure that the power meter is displaying very nicely? Those numbers just don't seem to match well. when the usage drops the meter spikes. and then half the meter is .05 watts but the full meter is 1 watt? If it is supposed to scale like that It would be nice if there was some sort of indicator. (ex. the way net has numbers to indicate the scale)

video example
Screen.Recording.2026-01-19.at.3.47.45.PM.mov

@Ruaneri-Portela
Copy link
Copy Markdown
Contributor Author

Seems to work as intended on a Mac Mini M1 running Tahoe.
But would be great to have some testing done on other Mac hardware.

I tested it just now on my M1 Macbook Pro running 15.7.3 and it also seems to be working. I don't have any newer apple hardware to test with though.

However maybe I'm wrong and its just apple silicon proving how efficient it is but I have trouble believing that I was getting 1 watt power usage playing 4k video. It fluctuated between .73 and 1.03

Screenshot 2026-01-19 at 3 43 34 PM also I'm not so sure that the power meter is displaying very nicely? Those numbers just don't seem to match well. when the usage drops the meter spikes. and then half the meter is .05 watts but the full meter is 1 watt? If it is supposed to scale like that It would be nice if there was some sort of indicator. (ex. the way net has numbers to indicate the scale)

Screen.Recording.2026-01-19.at.3.47.45.PM.mov

This metric in W is only for the GPU module, excluding use for CPU and ANE, which also need to be computed.

Considering that your video is actually running on Apple's decode ASICs and not on the GPU shaders, I see it as a correct value.

@vandabbin
Copy link
Copy Markdown
Contributor

vandabbin commented Jan 20, 2026

This metric in W is only for the GPU module, excluding use for CPU and ANE, which also need to be computed.

Considering that your video is actually running on Apple's decode ASICs and not on the GPU shaders, I see it as a correct value.

Yeah you are probably right about that.

I do still think the meter shouldn't scale as the value gets smaller but maybe that's just me. It looks super weird to be jumping from half to full for a value change of 0.05w to 0.09w. I would expect both those values to only produce 1 bar on the meter if even that. I don't usually have the GPU box open though on my other systems (relying mainly on the small GPU meter in the CPU box) so maybe that is how they all do it?

edit: just checked on my framework laptop and the gpu power meter does not scale like that on amd gpu. (picture is with super tux kart running in the background) And it idles around 5w or so

Screenshot_20260120_014925

regarding scaling of the power meter. I think it would make sense for it to be scaled based on the total power usage ever expected from a given gpu model

@deckstose

This comment was marked as outdated.

@Ruaneri-Portela Ruaneri-Portela force-pushed the main branch 5 times, most recently from a19adf5 to 4348f11 Compare January 23, 2026 03:59
@deckstose
Copy link
Copy Markdown
Collaborator

deckstose commented Jan 24, 2026

CI is failing

@Ruaneri-Portela
Copy link
Copy Markdown
Contributor Author

CI is failing

Fixed

@deckstose deckstose added the gpu Issues or pull requests related to GPU functionality label Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu Issues or pull requests related to GPU functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants