Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psutil.cpu_percent does not work as advertised #168

Closed
giampaolo opened this issue May 23, 2014 · 6 comments
Closed

psutil.cpu_percent does not work as advertised #168

giampaolo opened this issue May 23, 2014 · 6 comments

Comments

@giampaolo
Copy link
Owner

From philip.r...@gmail.com on June 03, 2011 04:23:02

What steps will reproduce the problem?  
1. Call psutil.cpu_percent(interval=0) multiple times. 

What is the expected output?  


What do you see instead?  
Expected: each call should give cpu usage since previous call.
Actual: gives cpu usage since module import. 

Please provide any additional information below.  
I think this fixes the problem:

Index: psutil/__init__.py
===================================================================
--- psutil/__init__.py  ( revision 980 )
+++ psutil/__init__.py  (working copy)
@@ -601,7 +601,7 @@
     t2_all = sum(t2)
     t2_busy = t2_all - t2.idle

-    _last_cpu_times = t1
+    _last_cpu_times = t2
     # this usually indicates a float precision issue
     if t2_busy <= t1_busy:
         return 0.0

Original issue: http://code.google.com/p/psutil/issues/detail?id=168

@giampaolo
Copy link
Owner Author

From g.rodola on June 03, 2011 00:34:42

> Expected: each call should give cpu usage since previous call.
> Actual: gives cpu usage since module import.

I think you're misinterpreting the code.
This is true only *the first time* the function is called.
Since then times are compared against previous function call.

@giampaolo
Copy link
Owner Author

From jlo...@gmail.com on June 03, 2011 09:28:11

The current code looks like this:

   if blocking:
        t1 = cpu_times()
        time.sleep(interval)
    else:
        t1 = _last_cpu_times

    t1_all = sum(t1)
    t1_busy = t1_all - t1.idle

    t2 = cpu_times()
    t2_all = sum(t2)
    t2_busy = t2_all - t2.idle

    _last_cpu_times = t1

So if you are using the non-blocking code, it looks like we set t1 = 
_last_cpu_times and then later we are setting _last_cpu_times = t1 without 
modifying it, so it's effectively a no-op. I guess this got introduced when 
adding the blocking code path.

@giampaolo
Copy link
Owner Author

From g.rodola on June 03, 2011 10:16:03

You guys are right, sorry.
This is now fixed in r981 .

Status: FixedInSVN
Labels: Milestone-0.2.2

@giampaolo
Copy link
Owner Author

From g.rodola on June 27, 2011 10:52:41

Labels: Milestone-0.3.0

@giampaolo
Copy link
Owner Author

From g.rodola on July 08, 2011 12:07:35

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 04:00:43

Updated csets after the SVN -> Mercurial migration: r981 == revision 
a415d2627242

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant