Home Features Docs Blog Support GitHub

CPU Usage profiling


(Alex Itelman - Muvix) #1

Hello,

I am trying to do CPU usage profiling using Instruments, but I don’t see the java code symbols there, only Obj-C symbols.
How can I do profiling on the Java side?
Can I make it show up in Instruments? (I want to know which calls in the java side consume the most CPU)

Thanks,
Alex.


(Gergely Kis - Migeran) #2

Hi Alex,

you can do profiling using the method tracing functionality in ART.

You can add the following code snippet to the beginning of your app, and it will create a sampling based trace of the first 10 seconds of your app’s runtime. Obviously, you can put the start and stop calls where it makes the most sense for you.

        List<NSURL> l = (List<NSURL>) NSFileManager.defaultManager().
                URLsForDirectoryInDomains(
                        NSSearchPathDirectory.DocumentDirectory,
                        NSSearchPathDomainMask.UserDomainMask);
        NSURL docDirURL = l.get(0); // Error handling is for cowards :)
        String fsPath = docDirURL.fileSystemRepresentation();
        System.out.println("Trace log path: " + fsPath);
        Debug.startMethodTracingSampling(fsPath + "/MOE-sampling.trace", 0, 100);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Debug.stopMethodTracing();
            }
        }, 10000);

The resulting trace file can be opened from the Android Device Monitor (included in Android Studio): https://developer.android.com/studio/profile/traceview.html

Best Regards,
Gergely


(Alex Itelman - Muvix) #3

Hi Gergely,

Thank you for your answer.

Unfortunately this method does not show me CPU usage bottlenecks… Is there a way to see it in Instruments? Or maybe Android CPU Profiler? (https://developer.android.com/studio/profile/cpu-profiler.html)

Thank you,
Alex Itelman.


(Eugene) #4

Maybe it is not as fancy as CPU Profiler in android studio, but Trace View been working fine for several years before android studio even existed. You can see % of execution time spent within a given method and drill down. So, that’s not exactly a CPU usage metric, but it does show same code hotspots where most CPU time is spent at.