December 4, 2022

Robotic Notes

All technology News

Simulate and troubleshoot CPU spikes in Kotlin – Java Code Geeks

2 min read


In this series of simulating and troubleshooting performance issues in Kotlin articles, let’s discuss how to simulate CPU usage to jump to 100% on a host (or container). CPU consumption will increase when a thread goes into an infinite loop.

Kotlin CPU Spike program

Here is a sample kotlin program that generates a CPU spike.

package com.buggyapp
class CPUSpikeApp
      fun start() 
          CPUSpikerThread().start()
          CPUSpikerThread().start()
          CPUSpikerThread().start()
          CPUSpikerThread().start()
          CPUSpikerThread().start()
          CPUSpikerThread().start()
          println("6 threads launched!")
      


class CPUSpikerThread : Thread() 
      override fun run() 
          while (true)  
          // Just looping infinitely
          
      

When a thread goes into an infinite loop, the CPU will start to increase. In the above program, we put 6 threads to go into an infinite loop. In this program, you will notice the “CPUSpikeApp” class. In this class, 6 threads named “CPUSpikerThread” are started. If you notice the code of the “CPUSpikerThread” class, there is a “while (true)” loop without any code in it. This condition will cause the thread to go into an infinite loop. Since the 6 threads are executing this code, all these 6 threads will go into an infinite loop. When this program is executed, the machine’s CPU consumption will skyrocket.

How to troubleshoot Kotlin CPU Spike?

You can diagnose CPU peaking using a manual or automated approach.

Manual approach:

As highlighted in this article, you can use a manual troubleshooting approach:

  1. Capture a thread dump from the application
  2. Capture the output “top -H -p PID”.
  3. Combine these #a and #b and identify the root cause of the CPU spike problem

Automated approach:

On the other hand, you can also use an open source script yCrash which would capture 360 ​​degree data (GC log, 3 thread dump snapshots, heap dump, netstat, iostat, vmstat, top, top -H,… ) from your application stack within a minute and generate a batch zip file. You can then manually analyze these artifacts or upload them to a yCrash server for automated analysis.

We used the automated approach. Once the captured artifacts were uploaded to the yCrash server, it immediately generated the root cause analysis report below highlighting the source of the problem.

Figure 1 is the yCrash report that states that the 6 threads are causing the CPU to spike: Thread-5, Thread-4, Thread-0, Thread-2, Thread-1, Thread-3.These are the 6 threads created in the above kotlin program. When you click on the thread names in the report, you will see the stack traces of those threads (as shown in Fig. 2).

In Figure 2, you can notice that the tool points to exact lines of code, ie. com.restartApp.CPUSpikerThread.run(CPUSpikeApp.kt:18); where the threads spin endlessly and cause the CPU to increase. Armed with this information, one can easily go ahead and fix the problematic code.



Source link