Is there any way to calculate CPU load on current android versions?

152 Views Asked by At

My actual goal is to the calculate CPU load of the phone. But this seems not possible on Android 8+ devices. Because the access to /proc/stat is restricted on API 26.

So I am trying to calculate my app's CPU load in my app. This can also work on my case. I can access and read /proc/self/stat without any problem. But every post I saw on the internet for calculations needed proc/uptime. Unfortunately, it is also restricted. So the question is the CPU load calculations possible anymore?

1

There are 1 best solutions below

0
On

This is how neofetch does it, at least in the version I'm using 7.1.0:

cpu_usage="$(ps aux | awk 'BEGIN {sum=0} {sum+=$3}; END {print sum}')"
cpu_usage="$((${cpu_usage/\.*} / ${cores:-1}))"