C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-23-2008, 03:56 AM   #1
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
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!!
mynickmynick is offline   Reply With Quote
Old 09-23-2008, 04:00 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-23-2008, 05:54 AM   #3
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
i did not find what i need
it's strange
mynickmynick is offline   Reply With Quote
Old 09-23-2008, 06:49 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
I'm sure I've worked on this before, but one of your threads have a comment:
time measure (net and gross) with pthreads under 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.
matsp is offline   Reply With Quote
Old 09-23-2008, 07:57 AM   #5
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
More info: http://www.linuxforums.org/forum/lin...cess-time.html

gg
Codeplug is offline   Reply With Quote
Old 09-25-2008, 03:10 AM   #6
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
Quote:
Originally Posted by Codeplug View Post
excellent
this seems what i was looking for
mynickmynick is offline   Reply With Quote
Old 09-25-2008, 03:47 AM   #7
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
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);
}
mynickmynick is offline   Reply With Quote
Old 09-25-2008, 03:51 AM   #8
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
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?
mynickmynick is offline   Reply With Quote
Old 09-25-2008, 03:56 AM   #9
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-25-2008, 03:57 AM   #10
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
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??
mynickmynick is offline   Reply With Quote
Old 09-25-2008, 04:09 AM   #11
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-25-2008, 07:31 AM   #12
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
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!!)
mynickmynick is offline   Reply With Quote
Old 09-25-2008, 10:23 AM   #13
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
>> 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
Codeplug is offline   Reply With Quote
Old 09-26-2008, 01:48 AM   #14
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
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
mynickmynick is offline   Reply With Quote
Old 09-26-2008, 01:49 AM   #15
Alessio Stella
 
Join Date: May 2008
Location: Italy, Bologna
Posts: 235
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??
mynickmynick is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:57 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22