代码改变世界

how to debug thread cpu 100%

2014-11-17 22:40  achuan_hu  阅读(170)  评论(0编辑  收藏  举报

when we write a program, cpu and memory usages are very important to indicate the stability of the program. Once the cpu usage reached 90%, there are some bugs in your program, and you must find the problem. Here is a simply guide to debug with cpu 100%. For example:

void *first_routine (void * args)
{
    while(1)
    {
        int a = 1;
        usleep(1);
    }

    return NULL;
}

int main()
{
    pthread_t thread_first, thread_second;
    pthread_create(&thread_first, NULL, &first_routine, NULL);

    while(1)
    {
        sleep(4);
    }
    return 0;
}

then complile it : gcc debug.c -o debug -lpthread , and run it .  

iii) use top tool find the bug thread,  top -p $pid -H

iv) use pstack : pstack $tid(bug thread), to look the stack of the bug thread. but pstack can't see the paraments. if you still can't determine the bug line, user gcore to get the program core.  (gcore $pid)

v)  gdb -c  $core.file  ./exe , and the use  command  bt(back trace), then use the paraments to determine the bug code.