Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

siimilar tool for intel macs? #70

Open
akohlsmith opened this issue Jan 30, 2024 · 1 comment
Open

siimilar tool for intel macs? #70

akohlsmith opened this issue Jan 30, 2024 · 1 comment

Comments

@akohlsmith
Copy link

I understand that asitop is for apple silicon only. I'm curious if there is a similar tool for intel macs. I've not yet been able to find the magic search terms (if the exist)

@emmagamma
Copy link

emmagamma commented Jan 31, 2024

@akohlsmith
Unfortunately, since most intel macs have AMD GPU's (if any) and because AMD doesn't write the drivers for macOS (gosh I wish they did, then it would probably be open source) we're left with a situation where the only way to actually talk directly to the card is through closed source libraries created by Apple (afaik, anyway), and if you ask them really nicely they will probably still not tell you lol but maybe worth a shot? So AMD support on macOS is next to none... however I've figured out a few things and gotten to something that basically works well enough for my needs for now... but it's far from ideal.

There's: sudo powermetrics --samplers gpu_power -i1000 -n1 to get one sample (-n2 would give you two samples, and so on) over the course of 1000 milliseconds (-i1000) using the relevant sampler (gpu_power) to give us the "GPU Busy Percent" among other details, but unfortunately this is ONLY the busy percent for the integrated graphics, often listed as an 'Intel' GPU... there are also other samplers available that give you a little bit more info but nothing really on par with what you get from this repo or tools like nvtop. Use powermetrics -h to read about the other samplers and flags you can use.

You can also try system_profiler SPDisplaysDataType just to get some basic info about your AMD GPU (what's nice is this doesn't require the use of sudo, so you can easily use it in a script/daemon without needing to give it root access or require a password). But unfortunately again, this also doesn't show anything along the lines of current usage/percentage/business/frequency or anything like that but it does show stuff like VRAM, Driver version, Displays it's connected to, and some other basic info.

If you don't mind being a bit imprecise though, one workable solution (which thankfully also doesn't need sudo/root access) is looking at the output from top which can show you per process what the CPU usage % and Power usage is for your system. Then just subtract the CPU% from the Power and you have an approximate value for the GPU busy percent.

Side note: Power is supposed to be based on the total number of platform idle wakeups, but Apple is not entirely clear about how it calculates this, all we know is that at least according to Apple, the values for CPU % and Power are apparently using the same units or are similar enough that we can do this math without worrying about the units.

Here's how I'm doing it in bash at the moment (on macOS, but should probably* work on most linux/unix OS's too):

# in just one line:
top -l 2 -stats "cpu,power" | awk -v N=2 '/%CPU[[:blank:]]*POWER/{++n} n>=N && $0 !~ /0.0[[:space:]]*$/' | tail -n +2 | awk '{cpu_total += $1}; {power_total += $2} END {print power_total - cpu_total "%"}'

# Explanation:
# 1. so we start by listing the CPU and Power usage for each running process
top -l 2 -stats "cpu,power"

# here, `-l 2` means take two samples and print both the results
# the only keys we need to pass in to `-stats` to get an approximate GPU busy percentage are cpu and power
# but we can also pass in other keys like: command and pid
# which would show the process name and id respectively
# (use `man top` to learn more about other keys you can pass in)

# for debugging purposes, just to make sure things looked correct, I started with:
top -l 2 -stats "pid,cpu,power,command"

# 2. we pipe that into awk and look for the 2nd occurrence of "%CPU POWER"
## which we expect from top's output, since we're printing two samples
# `-v N=2` passes in a hard-coded variable that we can use in our awk program
## to grab all the lines *after* the 2nd occurrence of the relevant header string
# the `[[:blank:]]` means match whitespace, and `*` means any number of them in a row
## since the output from top could contain more than one whitespace char between the headers
# everything between the /'s is a regex, and then we increment n with each line that we process
## and check if the current line is greater than or equal to our Nth match (second match)
## this will include only the matching line and all lines after it, so we ignore the first sample
## because the first sample is (almost) always all 0.0's for every value, both cpu and power
# next, we use `&&` to run a second filter, which removes any lines where Power has a value of 0.0
## `$0` is the whole line, and `!~` means don't include what matches the following regex
## `[[:space:]]` is similar to `[[:blank:]]` but it includes newline characters as well.
## `*` is any number of them just to be safe
## and `$` checks that the match is at the end of the line, where we expect the power value to be
awk -v N=2 '/%CPU[[:blank:]]*POWER/{++n} n>=N && $0 !~ /0.0[[:space:]]*$/'

# 3. after that, it's handy to remove the Header line...
## we could have done this in awk in the previous step
## but it makes things a bit more complicated imo, so let's pipe the output from step 2, above, into:
tail -n +2 

# which removes the header AND a preceding blank line
# whereas if we just use `-n +1` we only remove the preceding blank line and not the header

# 4. now let's pipe that into awk one more time to do some simple math
## add up the values from column $1 into cpu_total
## add up the values from column $2 into power_total
## then subtract the cpu from the power, and print it with a percent on the end as well
awk '{cpu_total += $1}; {power_total += $2} END {print power_total - cpu_total "%"}'

# 5. you should see output similar to:
11.5%

# although it could be any number from 0% to 100%, assuming top is working correctly, and assuming we're filtering the list and calculating the totals all correctly.

and obviously, instead of adding up the values from each column, you could modify this to just calculate the GPU busy percentage for a specific process, or for each process individually, if you wanted.

I notice when comparing the output to what I see from Activity Monitor (press Cmd + 4 to bring up the GPU History) the values seem roughly correlated, but this method doesn't tend to show you spikes and dips in performance as much as you'd see from the GPU History view in activity monitor, unless you were running this command more frequently, but I would recommend keeping it somewhere around every 1-5 seconds at the smallest. I have this feeding into my tmux, but you could display this output however you like really ^.^ good luck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants