Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I have written a multithreaded program which seems to take lots of CPU time. When I do top , I see that it is taking 97% of CPU time. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    10

    Question Tracing Threads


    Hi,

    I have written a multithreaded program which seems to take lots of CPU time. When I do top, I see that it is taking 97% of CPU time.

    How can I trace which thread is taking most of the time?



    The code is:
    Code:
    int main(int argc, char **argv)
    {
    
    int i; // Create Threads for(i=0; threads_table[i].fct != NULL; ++i) {
    if(pthread_create (&threads_table[i].thread_id, NULL, (void*) threads_table[i].fct, (void*) &(threads_table[i].args))!=0){ perror("pthread_create"); exit(1);
    } } // Make sure the threads finished. for(i=0; threads_table[i].fct != NULL; ++i){
    if(pthread_join (threads_table[i].thread_id, NULL)!=0){ perror("pthread_join"); exit(1); }
    } //Should never get here return 0;
    }


    Each thread does the same thing:

    Code:
    void thread1(void *param)
    {
    
    THREAD_ARGUMENTS_T* p = (THREAD_ARGUMENTS_T*) param; // Loop Forever while (1) {
    Thread_Sleep(p); // .. do something
    }//while(1)
    }
    I created the Thread_Sleep() function: int Thread_Sleep(THREAD_ARGUMENTS_T* p) {
    //Time start timer_elapsed_t t_start; // Time Now timer_elapsed_t t_now; // Time difference long time_diff_ms = 0, sleep_time_remaining = 0; // Grab Time Start Timer_Read_Elapsed_Time(&t_start); do {
    long time_diff_sec = 0; // Grab Time Now Timer_Read_Elapsed_Time(&t_now); // Calculate time difference time_diff_ms = Timer_Difference_ms (&t_start, &t_now); // Make sure not to sleep forever // Time remaining sleep_time_remaining = ((p->sleep_time_ms < time_diff_ms) ? 0 : (p->sleep_time_ms - time_diff_ms)) ; // Sleep usleep((sleep_time_remaining*1000)); // check
    } while (sleep_time_remaining > 0); return 1;
    }


    Thanks,
    S
    Last edited by MikeTbob; 09-17-2012 at 10:44 PM. Reason: Added code tags

  2. #2
    Administrator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,864
    Just a wild guess but wouldn't a debugger work here?
    GDB should be available in your repos as well as other debuggers also.
    GDB: The GNU Project Debugger
    I do not respond to private messages asking for Linux help, Please keep it on the forums only.
    All new users please read this.** Forum FAQS. ** Adopt an unanswered post.

    I'd rather be lost at the lake than found at home.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •