Thread: time measure (net and gross) with pthreads under linux

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

    time measure (net and gross) with pthreads under linux

    I am currently using
    (1) opencv cvGetTickCount() and cvGetTickFrequency() to measure gross (total also due to others) time from one instruction execution to another
    Basically they depend on the machione. On a Pentium they read the Pentium TSC to get a clock count
    (2) clock() to measure process (but guess not of single thread) net time spent

    What I need is
    (3)
    functions to measure CPU net usage by a single thread, from one instruction to another, a net time that is not counting time used by any other thread or process

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think there's a generic API call to do that - you could open /proc/<pid>/stat and read the data from that, which should give you CPU usage per thread.

    --
    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.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    I don't think there's a generic API call to do that - you could open /proc/<pid>/stat and read the data from that, which should give you CPU usage per thread.

    --
    Mats
    That's too cumulative
    I could use also calls to ps
    (ps -auroot -T -o sched,policy,rtprio,pid,cmd,spid,pcpu,ppid,size,pr iority,state,&#37;cpu,%mem,bsdtime,cputime,etime,l start,psr,sgi_p,cutime,cstime,utime,start_time --sort sched)

    I need more detailed info!!
    I need to know microsec elapsed between two points in the program execution by one single thread, without counting time for other tasks

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    have you tried to use profilers? like VTune?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    That's too cumulative
    I could use also calls to ps
    (ps -auroot -T -o sched,policy,rtprio,pid,cmd,spid,pcpu,ppid,size,pr iority,state,%cpu,%mem,bsdtime,cputime,etime,lstar t,psr,sgi_p,cutime,cstime,utime,start_time --sort sched)

    I need more detailed info!!
    I need to know microsec elapsed between two points in the program execution by one single thread, without counting time for other tasks
    I don't believe there is a method to do that - at least using the API, and non-API methods would only give you real (wall-clock) time, not CPU usage time. [I'm thinking perhaps using RDTSC to get a precise time - but it doesn't tell you if the process was "not running" during that time]

    Of course, I could be wrong. I know the Linux kernel FAIRLY well, but I don't know all of it in great detail.

    --
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    have you tried to use profilers? like VTune?
    That's a good suggestion, but under Linux, I'd recommend "oprofile" - which is like VTune, but open source and supports BOTH Intel and AMD processors (along with some non-x86 processors).

    The only problem would be that it's probably NOT showing how much time was spent per thread, but rather all threads that used a particular function.

    --
    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.

  7. #7
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    thanks for help
    i will try

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Hey
    I have the same problem. I want to measure the execution time between two instructions in multiple posix threads. like
    Code:
    void func()
    {
       instruction1:
        for();
        while();
    
       instruction2:
       for();
    }
    i.e. to calculation how much execution time is required for the instruction between instruction1 and instruction2.
    I can access /proc/<pid>/task/<tid>/stat at instruction1 and instruction2, but the problem is that I alway get the execution time at the start of the thread which is alway zero. Is there any way to access the updated execution time at any instruction in the same program.
    any help will be highly appreciated.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This probably should be separated into a new thread, as the one this is attached to is a rather old one.

    If the time is real short, then you can use RDTSC: http://en.wikipedia.org/wiki/RDTSC
    [Caveat, on some architectures with multiple processors, you may get erroneous results if there is a switch of processor between instruction1 and instruction2].

    Alternatively, you can run the code many times to get an overall time that is longer, and calculate the time (say 1000 times).

    --
    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
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Thanks. But the problem with RDSTC is that it does compute the time elapsed for all the threads running. I want to compute the execution for each thread. And it might not be short execution time between two instructions.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, I misunderstood your question.

    The content of /proc/<pid>/task/<tid>/stat should update as the process runs. Maybe you can post the code you are using to read the data with?

    --
    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
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Thanks. here is the link to code : http://pastebin.ca/1272318

    I created three threads X, Y and Z. I use GetThreadTime to get the time at the start of the thread and then execute the thread for an empty file loop. and at the end I again calculate the time.

    "The content of /proc/<pid>/task/<tid>/stat should update as the process runs": Yes it does update as the process starts execution, and keeps updating as it is running. But when the process terminates /proc/<pid>/task/<tid>/stat is no longer available.

    Inform me if you get some idea from the code to help me.

    Thanks

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to Linux programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM