Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

CPU Monitor

by siema24 (Initiate)
on Feb 18, 2015 at 15:12 UTC ( [id://1117101]=perlquestion: print w/replies, xml ) Need Help??

siema24 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm dealing with a problem of monitoring CPU usage. Basicly I'd like to know what was the average CPU usage % for any piece of my perl code (for example while loop).

I've tried to use AnyEvent::Monitor::CPU with all of it's goodies to put into my code but unfortunately I've found this problem too difficult to solve.

Could you please help me by giving some simple examples of possible solutions?

Best regards, Peter :)

Replies are listed 'Best First'.
Re: CPU Monitor
by BrowserUk (Patriarch) on Feb 18, 2015 at 15:36 UTC
    Basicly I'd like to know what was the average CPU usage % for any piece of my perl code (for example while loop).

    Your question makes no sense. Either your program has the cpu and is using 100% of it; or it doesn't and it is using 0%.

    And it switches between those two circumstances many times a second depending upon a variety of factors almost none of which have anything to do with your program.

    For example, if there are (say) five other processes running on your (single cpu) system -- and all six are totally cpu-bound (ie.do no I/O) -- and all six processes are running with equal priority, then the most cpu time your process can expect to get is 1/6th or 16.67%.

    Now assume that two of those other processes do occasional I/O: then for the short period whilst those two processes are waiting for their IO requests to complete, your process has the potential to get 25% of the CPU.

    Now assume that there is a 7th process running that only wakes up and does something every 10 seconds; but when it does, it runs with a higher priority than all the others; then for the period whilst that 7th process is running, your process will likely get 0% cpu.

    If you add in multiple cpus as is the norm these days; and the fact that most OSs have dozens of system processes, running at various priorities, running all the time, and things change again.

    And they change from millisecond to millisecond, for the entire runtime of your program. Or even your while loop.

    The bottom line is that even if you could formulate and implement some mechanism for measuring the "average CPU usage % for any piece of my perl code", it would change every time you ran the program; and would therefore be a completely meaningless statistic that would tell you nothing useful about your code.

    So, having got that out of the way; what were you hoping to achieve with the measurement? Ie. What is the real problem you are trying to solve?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: CPU Monitor
by afoken (Chancellor) on Feb 18, 2015 at 15:26 UTC

    Averaging CPU usage on a multi-user, multi-task system? Maybe you are looking for a profiler. Try / read the docs of Devel::NYTProf.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: CPU Monitor
by CoVAX (Beadle) on Feb 19, 2015 at 04:05 UTC

    Have a look at Time::HiRes.

    Its Examples section includes an example for timing portions of code.

    E.g.:

    # measure elapsed time # (could also do by subtracting 2 gettimeofday return values) $t0 = [gettimeofday]; # do bunch of stuff here $t1 = [gettimeofday]; # do more stuff here $t0_t1 = tv_interval $t0, $t1;
    Searched for donut and crumpit. Found donate and stumbit instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1117101]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2026-01-14 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (118 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.