Thread: Net cpu usage of pthreads?!

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    Net cpu usage of pthreads?!

    Measuring gross and net cpu usage of pthreads? From one point of the code to another (not average like calling ps)

    i can measure gross cpu usage by reading special Pentium registers like for instance using opencv functions cvGetTickCount() and cvGetTickFrequency()

    i can measure net cpu usage by all threads (one process) by using clock()

    But I don't have currently available functions to measure net cpu usage by one single thread!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    Measuring gross and net cpu usage of pthreads? From one point of the code to another (not average like calling ps)

    i can measure gross cpu usage by reading special Pentium registers like for instance using opencv functions cvGetTickCount() and cvGetTickFrequency()

    i can measure net cpu usage by all threads (one process) by using clock()

    But I don't have currently available functions to measure net cpu usage by one single thread!!
    And part of that is because threads are not accounted separately in the Linux kernel, if I remember correctly. Search the forum for "Thread CPU time" and I'm sure there's another thread where this was discussed, and some sort of hacky solution was found, IIRC.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    i did not find what i need
    it's strange

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm sure I've worked on this before, but one of your threads have a comment:
    http://cboard.cprogramming.com/showt...t=thread+linux

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5

  6. #6
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    excellent
    this seems what i was looking for

  7. #7
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    But unfortunately I found that the required
    #define _POSIX_THREAD_CPUTIME 0
    is 0 in my system so the function clock_gettime () is not supported for a call like
    Code:
    #include <time.h>
    #include <unistd.h> // for sysconf
    int err;
    struct timespec t;
    if (sysconf(_POSIX_THREAD_CPUTIME)){
      err = clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t);
    }

  8. #8
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    but the header posix_opt.h says:
    Code:
    /* Clock support in threads must be also checked at runtime.  */
    #define _POSIX_THREAD_CPUTIME	0
    so how can i check it run time?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Check the return value, it will return some sort of error saying "not supported" if it's not supported, one would assume.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    Check the return value, it will return some sort of error saying "not supported" if it's not supported, one would assume.

    --
    Mats
    yes sorry I misread
    sysconf() checks for availability
    But I don't understand why it should be checked at run time??

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    yes sorry I misread
    sysconf() checks for availability
    But I don't understand why it should be checked at run time??
    Becuase, presumably, it can be configured in or out of any particular OS - I don't KNOW the reason, but it makes sense that the reason that you can't know at compile time is that some OS's support it and others don't.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    Unfortunately I tried clock_gettime(CLOCK_THREAD_CPUTIME_ID,t); and I found out that it gives a gross value (time elapsed include time used by other threads!!)

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I found out that it gives a gross value (time elapsed include time used by other threads!!)
    How did you confirm that?

    You could also compare the value with what's in /proc/<PID>/task/<TID>/stat, for Linux 2.6 and up.

    gg

  14. #14
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    >> I found out that it gives a gross value (time elapsed include time used by other threads!!)
    How did you confirm that?

    You could also compare the value with what's in /proc/<PID>/task/<TID>/stat, for Linux 2.6 and up.

    gg
    I have debian with kernel 2.6.18
    one main thread
    one station thread
    two camera threads
    (several other threads not measured, but basically idle)
    the measure given was always the same for all threads (except some difference due to different termination instant) and the same as the value given by Open CV pentium clock measure
    So despite what said the measure is per process not per thread

  15. #15
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Still Looking!! Please other suggestions!!
    ps with special options gives a net per thread cpu usage so this info should be vailable with some special functions at kernel level??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. time measure (net and gross) with pthreads under linux
    By mynickmynick in forum Linux Programming
    Replies: 12
    Last Post: 12-01-2008, 07:39 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 03:07 AM