Search⌘ K
AI Features

Identify Spiking Threads

Explore how to identify spiking threads that cause sudden CPU spikes by analyzing Linux core dumps with GDB. Learn to recognize infinite loops and troubleshoot multi-threaded applications by examining thread states, disassembling instructions, and understanding backtraces to diagnose performance issues effectively.

What’s a spiking thread?

We refer to a thread that causes a sudden rise in CPU usage as a spiking thread (as it spikes the CPU). This usually happens due to some sort of infinite loop. In this exercise, we’ll learn how to identify spiking threads from the core dump.

Application source code

We have created a multi-threaded application that generates a spiking thread.

C++ 17
// Build:
// gcc main.c -pthread -lm -static -o App3
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void procA()
{
while (1)
{
sleep(1);
}
}
void procB()
{
double d = 1.0/3.0;
while (1)
{
d = sqrt(d);
}
}
#define THREAD_DECLARE(num,func) void bar_##num() \
{ \
func; \
} \
\
void foo_##num() \
{ \
bar_##num(); \
} \
\
void * thread_##num (void *arg) \
{ \
foo_##num(); \
\
return 0; \
}
THREAD_DECLARE(one,sleep(-1))
THREAD_DECLARE(two,sleep(-1))
THREAD_DECLARE(three,procA())
THREAD_DECLARE(four,sleep(-1))
THREAD_DECLARE(five,procB())
#define THREAD_CREATE(num) {pthread_t threadID_##num; pthread_create (&threadID_##num, NULL, thread_##num, NULL);}
int main(int argc, const char * argv[])
{
THREAD_CREATE(one)
THREAD_CREATE(two)
THREAD_CREATE(three)
THREAD_CREATE(four)
THREAD_CREATE(five)
sleep(-1);
return 0;
}

If we were to run this application and observe the CPU usage using the top command, we’d observe that it’s ...