get_cpu_percent() way off on Windows 2k8, 8 and possibly others

789 views
Skip to first unread message

mar...@rocketblast.com

unread,
Jul 5, 2013, 7:24:14 PM7/5/13
to psu...@googlegroups.com
So, I have tried to get a number which represents a process' cpu utilization (preferably the same value shown in Task Manager) to no avail.

Using psutil I get 100% a process that is reported to use 13% in Task Manager and 40% on another using 5% in Task Manager. First I figured it was because it was counting all my cores as a 100% so that my machine would have 800%; I would just need to divide the number with the available cores/threads, but this would be incorrect since get_cpu_percent() percent is capped to 100%.

From what I can tell others are having similar problems. But I am not sure.

I am also trying to get memory consumption and those figures are also always off. Sometimes it is pretty close to vms, other times rss. I know Task Manager can show Working Set Size and that rss was supposed to be the same there?

I have also tried using WMI but it is reporting over 300% for processes using the following script:


            for p in ProcessMonitor2.c.Win32_PerfRawData_PerfProc_Process (IDProcess=process.ProcessId):
                n1, d1 = long (p.PercentProcessorTime), long (p.Timestamp_Sys100NS)
                n0, d0 = self.process_info.get (id, (0, 0))
                try:
                    percent_processor_time = (float (n1 - n0) / float (d1 - d0)) * 100.0
                except ZeroDivisionError:
                    percent_processor_time = 0.0
                self.process_info[id] = (n1, d1)
                print process.CommandLine
                print id, str(percent_processor_time), process.WorkingSetSize

Any help?

Giampaolo Rodola'

unread,
Jul 6, 2013, 3:54:47 AM7/6/13
to psu...@googlegroups.com
Hi,
please show the code you're using.
> --
> --
> You received this message because you are subscribed to the "Python process
> utilities (psutil)" project group:
> http://code.google.com/p/psutil
> To post to this group, send email to psu...@googlegroups.com
> To unsubscribe from this group, send email to
> psutil-un...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/psutil
> ---
> You received this message because you are subscribed to the Google Groups
> "psutil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to psutil+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Martin Danielson

unread,
Jul 7, 2013, 10:38:35 AM7/7/13
to psu...@googlegroups.com
       for process in psutil.process_iter():
            if process.name in ['notepad.exe']:
                if process.status == 'running':
                    print 'cpu: {}'.format(process.get_cpu_percent(interval=0))

Tried setting interval to other values too, this script gets run every 5 seconds. So cachable objects should be updated if there are any. I have also tried doing it using process_list and creating instances of psutil.Process, same/similar results.

Maybe I have got this value wrong, maybe it is not giving me the % across all cores/threads as in Windows Task Manager? Is it relative to some other value, like Maximum Frequency? Or do I have to compare it to values gathered from cpu_percent?

Giampaolo Rodola'

unread,
Jul 8, 2013, 10:00:42 AM7/8/13
to psu...@googlegroups.com
Task Manager uses an interval of 1 sec so what happens if you use the same?
Are the two values similar?
Please note that you shouldn't expect psutil and task manager CPU %
values to be exactly the same.
The two should just look more or less similar on a time frame of, say, 60 secs.
...And no, I just tried and couldn't reproduce the issue you're
describing on a Win7 box.

Martin Danielson

unread,
Aug 5, 2013, 6:30:31 PM8/5/13
to psu...@googlegroups.com
Hello again.

Ran some more tests. On a process that varies between 2 and 5% cpu time i got the following output using the code underneath:

0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
14.3
14.3
14.8
29.6
44.4
0.0
16.6
0.0
0.0
0.0
16.6
0.0
0.0
14.3
30.7
0.0
0.0
42.8
0.0
0.0
0.0
16.0
16.0
16.6
0.0
15.4
15.4
14.3
0.0
31.9
16.6
0.0
30.7
0.0
0.0
16.6
0.0
0.0
16.7
16.6
0.0
31.9
28.5
46.1
30.7
33.3
33.3
0.0
0.0
30.7
14.3
0.0
>>> 

import psutil

import os

import time

p = psutil.Process(4048)

for i in range(60):
    time.sleep(1)

    print p.get_cpu_percent(interval=0.1)

I have tried using Intervals between 0 and 10 in order to poll at a better frequency. But as you can see it is very strange numbers being printed and they are no way near the one from Task Manager. It is like I explained before, when it is 0 it is 0. But as soon as it goes up the numbers get really high, with a lot of the values being 0.

I have tried this on a MacBook Air with Bootcamp/Windows 7 64-bit, a standard built PC with Windows 7 64-bit, Windows 8 64-bit and Windows 8.1 64-bit. In addition I have tried this on servers running Windows Server 2008. They all show the same result.

The Python installation is 2.7 and 32-bit. With 1.0.1 of psutil, matching Python 3.7 32-bit.

Please advice, have you any idea what it could be that is causing this? Would really like to use psutil  but these numbers just make it so unreliable.

Kind regards,
Martin

Martin Danielson
CEO
Rocket Blast AB
 


You received this message because you are subscribed to a topic in the Google Groups "psutil" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psutil/bEIq0c2ZN5U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psutil+un...@googlegroups.com.

Martin Danielson

unread,
Aug 5, 2013, 6:35:02 PM8/5/13
to psu...@googlegroups.com
0.0
0.0
0.0
0.0
0.0
0.0
46.1
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
16.0
16.7
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
42.8
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
46.1
30.7
14.3
0.0
0.0
0.0
0.0
0.0
0.0
0.0
15.4
30.7
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
14.8
>>> 

These were run on taskmgr.exe that hovered around 1% cpu. Very spread numbers.

I tested against both 64-bit and 32-bit processes, they both seem to produce the same irradic numbers. Running now on a chrome.exe *32 and as soon as the % goes above ~10% in Task Manager the script outputs 100 or 99.8.

Regards,
Martin

Martin Danielson
CEO
Rocket Blast AB
 


Martin Danielson

unread,
Aug 5, 2013, 6:38:07 PM8/5/13
to psu...@googlegroups.com
Could it be 64 vs 32-bit? Since all the libs I use don't properly work under 64-bit I always use 32-bit Python files, while having a 64-bit OS.

Seems weird that it would be the problem but I am just grabbing things from thin air now, since I have been at this for a long time without any result :)

Kind regards,
Martin

Martin Danielson
CEO
Rocket Blast AB
 


Giampaolo Rodola'

unread,
Aug 6, 2013, 7:14:25 AM8/6/13
to psu...@googlegroups.com
On Tue, Aug 6, 2013 at 12:30 AM, Martin Danielson <mar...@rocketblast.com> wrote:
import psutil
import os
import time

p = psutil.Process(4048)
for i in range(60):
    time.sleep(1)
    print p.get_cpu_percent(interval=0.1)

It doesn't make much sense to use time.sleep *and* interval=0.1 (the latter has precedence).
This is how you should conduct your test:

p = psutil.Process(4048)
for i in range(60):
    print p.get_cpu_percent(interval=None) 
    time.sleep(1)

Please try again and report whether the results look similar to taskmgr's.

Martin Danielson

unread,
Aug 6, 2013, 7:19:00 AM8/6/13
to psu...@googlegroups.com
I have the same result using only "interval", "interval=0" and sleep(1) and different numbers inbetween. The reason I put time.sleep there was because I thought maybe the interval parameter had some caching that messed up the values.

Same (equivalent) result with the script you posted.

Kind regards,
Martin

Martin Danielson
CEO
Rocket Blast AB
 


--

Giampaolo Rodola'

unread,
Aug 6, 2013, 8:12:23 AM8/6/13
to psu...@googlegroups.com
On Tue, Aug 6, 2013 at 1:19 PM, Martin Danielson <mar...@rocketblast.com> wrote:
I have the same result using only "interval", "interval=0" and sleep(1) and different numbers inbetween. The reason I put time.sleep there was because I thought maybe the interval parameter had some caching that messed up the values.

Same (equivalent) result with the script you posted.

The only reliable way which comes to mind to test this is to compare process CPU times as in:

>>> import psutil, os, time
>>> cpt = psutil.Process(os.getpid()).get_cpu_times()
>>> cpt
cputimes(user=0.08, system=0.01)
>>> time.strftime("%M:%S", time.localtime(sum(cpt)))
'00:00'
>>>

You should open Windows task manager -> "View" -> "Select columns" and flag "CPU time" field.
That's gonna show the CPU cumulative times for each process.
The next step is to do the same with psutil.
A quick way to do that is this:

>>> import psutil
>>> psutil.test()
USER         PID %CPU %MEM     VSZ     RSS TTY           START    TIME  COMMAND
SYSTEM         0 100.0    ?       ?      24 ?             Aug05   28:22  System Idle Process
SYSTEM         4  1.6    ?     108     852 ?             Aug05   13:18  System
             284  0.0    ?     468     732 ?             Jul12   00:01  smss.exe
             308  0.0  0.1    2364    2704 ?             Jul26   00:00  cmd.exe
             352  0.0  1.1   19436   22956 ?             Jul12   00:09  svchost.exe
             360  0.0  0.2    3520    4436 ?             Jul12   00:11  csrss.exe
             388  0.0  0.2    1564    3368 ?             Jul12   00:00  wininit.exe
             412  0.4  0.3    2288    5816 ?             Jul12   00:21  csrss.exe
             448  0.0  0.2    1980    3780 ?             Jul12   00:00  winlogon.exe
             496  0.4  0.4    5200    7584 ?             Jul12   06:41  services.exe
             504  0.1  0.4    4480    9232 ?             Jul12   00:28  lsass.exe
             512  0.0  0.2    2532    3296 ?             Jul12   00:05  lsm.exe
             564  0.0  0.5    8456   10208 ?             Jul12   00:10  svchost.exe
             604  0.0  0.3    4112    7104 ?             Jul12   00:04  svchost.exe
             664  0.0  0.2    2236    3944 ?             Jul12   00:55  VBoxService.exe
             712  0.1  0.3    3844    6276 ?             Jul12   00:05  svchost.exe
             824  0.0  0.6   11336   11620 ?             Jul12   00:13  svchost.exe
             856  0.1  1.4   19284   28752 ?             Jul12   00:27  svchost.exe
             896  0.1  4.2   87816   87560 ?             Jul12   01:55  svchost.exe
             956  0.0  0.6    8156   13132 ?             Jul12   00:05  svchost.exe
user        1124  0.0  0.3    4820    7040 ?             Jul12   00:01  TSVNCache.exe
user        1140  0.0  0.9   15752   19456 ?             Jul12   00:01  TortoiseHgOverlayServer.exe
            1324  0.0  0.2    6236    3660 ?             Jul12   00:00  cygrunsrv.exe
user        1340  0.0  0.3    3328    5844 ?             Jul12   00:01  taskhost.exe
user        1384  0.0  0.2    1872    3196 ?             Jul12   00:04  VBoxTray.exe
user        1428  0.0  0.2    1528    3440 ?             Jul12   00:00  dwm.exe
user        1468  0.6  2.6   44208   53736 ?             Jul12   04:19  explorer.exe
            1520  0.0  0.4    5616    8456 ?             Jul12   00:03  svchost.exe
            1556  0.0  0.1    1112    2076 ?             Jul12   00:04  conhost.exe
            1600  0.0  0.4    7524    8120 ?             Jul12   00:00  sshd.exe
            1912  0.0  0.2    2008    4484 ?             Jul12   00:01  svchost.exe
user        2064  0.0  0.4    4328    7432 ?             Jul12   00:02  cmd.exe
user        2072  0.2  0.2    1468    3876 ?             Jul12   00:27  conhost.exe
user        2304  0.2  0.5    8416   11280 ?             04:56   00:00  python.exe
            2600  0.0  0.2    1432    4656 ?             Jul26   00:01  notepad.exe
            2916  0.0  0.4    3484    9216 ?             04:58   00:02  taskmgr.exe
user        2924  0.0  1.7   23176   35428 ?             Jul12   00:18  PyScripter.exe


The "TIME" column is what we're looking for and on my Windows 7 64-bit virtualized box that's exactly the same as "CPU Time" column shown by taskmgr.
That means that the CPU percentage shown by psutil *has* to be correct and/or similar to taskmgr's.
I really can't see how they can be different.

Martin Danielson

unread,
Aug 6, 2013, 8:40:19 AM8/6/13
to psu...@googlegroups.com
Attached is my output compared to Task Manager.

The CPU times seems to be correct. The test() method does not show hours but other than that the times seems to match up well.

However, looking at the output that you get and the output I am getting again you can see that the %CPU is off in my output. System Idle Process which is around 90% for me in Task Manager shows up as 0% in the output. But for you it seems to be correct, showing 100%. You also have a some of the processes showing up as 0.4% etc. and that is more accurate than what I am getting.

I really don't understand what is wrong. Having tested this on so many different machines I don't feel that it is me doing something wrong. Any idea what else it could be? I am administrator but maybe there are other aspects to take into account? Any need of configuration, or maybe I should run the 64-bit version of psutil?

Kind regards,
Martin

Inline image 1

Martin Danielson
CEO
Rocket Blast AB
 


--
cpu-times.png

Giampaolo Rodola'

unread,
Aug 6, 2013, 9:39:59 AM8/6/13
to psu...@googlegroups.com
The CPU percent value shown by psutil.test() is basically meaningless, similarly to the one shown by "ps" on UNIX, because a very short timeout is used.
Also, it doesn't make sense to compare the two as you did in your screenshots because:

1) psutil.test() uses an interval of 0.01 secs or something while taskmgr uses 1 second

...and more importantly:

2) they are "photographs" of a CPU consumption which were taken at two different times so you cannot expect them to me the same

What you seem to not understand is that CPU percentage is different than, say, memory consumption. Two different tools cannot show the exact same values because there's an extra constant: the *time*.
The values you get can only be *similar*, assuming that you're using the same interval in both tools, and that you collect a certain number of values (say 60 values / aka 1 minute and then you calculate the average).
My best guess is that you're not satisfying these 2 prerequisites ending up comparing apples with oranges.

Martin Danielson

unread,
Aug 6, 2013, 10:53:22 AM8/6/13
to psu...@googlegroups.com
I understand that getting the CPU percent is different from say memory.

The problem I am having is understanding the values I am getting and how to make them as close to what Task Manager is showing. I mean, it is not a matter of 1% difference, we are talking anything between twice to 20 times the value.

I also know that Task Manager is building up, but what I don't understand is how even the snapshots can be so different when polling using psutil.

>>> 
4.7% (4.7 / 1)
7.85% (15.7 / 2)
7.86666666667% (23.6 / 3)
8.625% (34.5 / 4)
9.72% (48.6 / 5)
9.66666666667% (58.0 / 6)
9.84285714286% (68.9 / 7)
10.775% (86.2 / 8)
11.3111111111% (101.8 / 9)
11.59% (115.9 / 10)
11.3909090909% (125.3 / 11)
11.225% (134.7 / 12)
11.0846153846% (144.1 / 13)
10.7428571429% (150.4 / 14)
10.86% (162.9 / 15)
10.76875% (172.3 / 16)
10.7764705882% (183.2 / 17)
10.7% (192.6 / 18)
10.4684210526% (198.9 / 19)
10.415% (208.3 / 20)
10.2904761905% (216.1 / 21)
10.5318181818% (231.7 / 22)
10.8217391304% (248.9 / 23)
11.0875% (266.1 / 24)
11.392% (284.8 / 25)
11.6115384615% (301.9 / 26)
11.762962963% (317.6 / 27)
11.7892857143% (330.1 / 28)
11.924137931% (345.8 / 29)
11.8866666667% (356.6 / 30)
12.2064516129% (378.4 / 31)
12.415625% (397.3 / 32)
12.4606060606% (411.2 / 33)
12.4617647059% (423.7 / 34)
12.5542857143% (439.4 / 35)
12.4222222222% (447.2 / 36)
12.4243243243% (459.7 / 37)
12.2605263158% (465.9 / 38)
12.1076923077% (472.2 / 39)
11.8425% (473.7 / 40)
11.8170731707% (484.5 / 41)
11.7595238095% (493.9 / 42)
11.6674418605% (501.7 / 43)
11.5090909091% (506.4 / 44)
11.3933333333% (512.7 / 45)
11.35% (522.1 / 46)
11.3085106383% (531.5 / 47)
11.26875% (540.9 / 48)
11.3612244898% (556.7 / 49)
11.446% (572.3 / 50)
11.6196078431% (592.6 / 51)
11.575% (601.9 / 52)
11.5924528302% (614.4 / 53)
11.5518518519% (623.8 / 54)
11.4545454545% (630.0 / 55)
11.3892857143% (637.8 / 56)
11.3789473684% (648.6 / 57)
11.3448275862% (658.0 / 58)
11.3118644068% (667.4 / 59)
>>> 

p = psutil.Process(4048)

accumulated = 0
for i in range(1, 60):
    accumulated += p.get_cpu_percent(interval=1)
    print '{}% ({} / {})'.format(accumulated / i, accumulated, i)

The process I am monitoring is never showing anything above 6% utilization. However, using above script shows that the average is 11%, while according to Task Manager it is more like 2.5% (determined by just watching the process during the script execution). Measuring over 60 seconds this is quite a huge difference don't you think?

I am not trying to prove something or in any other way cause you extra head-ace, all I want is reliable data. We are going to use the basis of these numbers in order to utilize our hardware in an optimal way so it is important that I know CPU and memory details.

I wish I could output the Task Manager data to be more concrete in my findings but it just doesn't add up for me. I have full respect for me not understanding the numbers but the numbers just seems to high.

Kind regards,
Martin

Martin Danielson
CEO
Rocket Blast AB
 


--

Martin Danielson

unread,
Aug 6, 2013, 10:59:23 AM8/6/13
to psu...@googlegroups.com
Running on Steam.exe *32

>>> 
7.8% (7.8 / 1)
7.0% (14.0 / 2)
5.2% (15.6 / 3)
5.85% (23.4 / 4)
7.52% (37.6 / 5)
6.78333333333% (40.7 / 6)
6.7% (46.9 / 7)
6.4375% (51.5 / 8)
6.76666666667% (60.9 / 9)
6.25% (62.5 / 10)
6.1% (67.1 / 11)
5.85% (70.2 / 12)
5.88461538462% (76.5 / 13)
5.46428571429% (76.5 / 14)
5.41333333333% (81.2 / 15)
5.5625% (89.0 / 16)
5.51176470588% (93.7 / 17)
5.37777777778% (96.8 / 18)
5.34210526316% (101.5 / 19)
5.47% (109.4 / 20)
5.35714285714% (112.5 / 21)
5.25454545455% (115.6 / 22)
5.02608695652% (115.6 / 23)
5.0125% (120.3 / 24)
4.936% (123.4 / 25)
4.92692307692% (128.1 / 26)
5.03333333333% (135.9 / 27)
5.07857142857% (142.2 / 28)
5.11724137931% (148.4 / 29)
5.05% (151.5 / 30)
4.93548387097% (153.0 / 31)
4.928125% (157.7 / 32)
5.01515151515% (165.5 / 33)
4.86764705882% (165.5 / 34)
4.86285714286% (170.2 / 35)
4.81388888889% (173.3 / 36)
4.81081081081% (178.0 / 37)
4.93157894737% (187.4 / 38)
4.92564102564% (192.1 / 39)
4.88% (195.2 / 40)
4.83658536585% (198.3 / 41)
4.83333333333% (203.0 / 42)
4.79302325581% (206.1 / 43)
4.79090909091% (210.8 / 44)
4.75333333333% (213.9 / 45)
4.71739130435% (217.0 / 46)
4.65106382979% (218.6 / 47)
4.68541666667% (224.9 / 48)
4.75102040816% (232.8 / 49)
4.718% (235.9 / 50)
4.71764705882% (240.6 / 51)
4.65769230769% (242.2 / 52)
4.6% (243.8 / 53)
4.57222222222% (246.9 / 54)
4.57272727273% (251.5 / 55)
4.63035714286% (259.3 / 56)
4.60350877193% (262.4 / 57)
4.71206896552% (273.3 / 58)
4.79152542373% (282.7 / 59)
>>> 

I watched the number in Task Manager very carefully and noticed 3% twice during the execution and 4% once, the rest was 0, 1 and 2 %. I would assume that would equate to about 1.75% on average.

Script showing 4.79%. That difference is for me too big again, in a real-world application mean to provision software on available hardware.

PS. Thank you for at least looking into this and trying to help me get this. Really appreciate the work you have done and I love 3rd party libraries that are well supported! DS.

Kind regards,
Martin

Martin Danielson
CEO
Rocket Blast AB
 


Giampaolo Rodola'

unread,
Aug 6, 2013, 3:16:47 PM8/6/13
to psu...@googlegroups.com
1.75% average difference compared to taskmgr looks kind of reasonable to me (despite I don't know how exactly you calculated taskmgr's average; did you take 60 screenshots?).
I wouldn't know how to justify this difference because we verified that the 2 CPU timings are the same and honestly I can't imagine why it matters this much to you.
I mean, what one generally wants to do with this kind of value is to trigger an alarm in case the CPU utilization goes over a certain threshold.
Assuming psutil actually shows a slightly higher percentage compared to taskmgr what do you care? Can't you just increase the threshold?

Martin Danielson

unread,
Aug 7, 2013, 9:08:46 PM8/7/13
to psu...@googlegroups.com
Yes, but that is with very small numbers. Just ran the script on a process with about 4% in Task Manager and it reports as 25% from psutil. The difference is relative to the actual values.

I didn't take 60 screenshots but I looked at the Task Manager the entire time. I know it is not very scientific but my observation was pretty easy to read anyway.

My use-case for this is the following:

We host several different types of game servers. Each game behaves differently. On top of that each modification of the game and even different maps being run require different amount of resources from the computer. Instead of reading these numbers by hand in Task Manager (requires the servers to be full, running that specific game, map etc. Virtually impossible) I want to rely on data.

If I can read CPU (it doesn't have to be percent, but it has to be a number I can compare to a maximum) I can determine the usage requirements of a game and provision game server instances over my available hardware using logic, rather than me guessing which server should be able to run the game or not.

So, even if an alarm on every server reporting when the CPU (overall) is too high what I really want is the CPU pattern for that specific game given certain amount of players (the two biggest factors usually).

As it stands now, the process that gave me 4% in Task Manager was a server with 34 players on it. According to psutil it was at 25% which would mean I could only run a total of 136 slots. We have been running well over 256 on the same hardware without even being close to 100% (Task Manager). Actually we stop loading servers when we reach 70% in order to have some head-room and based on spikes in user behavior.

Given this, what would you suggest I do? Use processor time? Maybe use wmic, wmi or some other native tool for Windows? When you say I don't understand the numbers, please give me an idea of what number I actually should look at. I don't know much details about CPU technology but have a fair background and experience in programming.

Kind regards,
Martin

Martin Danielson
CEO
Rocket Blast AB
 


Giampaolo Rodola'

unread,
Aug 8, 2013, 5:04:49 AM8/8/13
to psu...@googlegroups.com
On Thu, Aug 8, 2013 at 3:08 AM, Martin Danielson <mar...@rocketblast.com> wrote:
Yes, but that is with very small numbers. Just ran the script on a process with about 4% in Task Manager and it reports as 25% from psutil. The difference is relative to the actual values.

I didn't take 60 screenshots but I looked at the Task Manager the entire time. I know it is not very scientific but my observation was pretty easy to read anyway.

My use-case for this is the following:

We host several different types of game servers. Each game behaves differently. On top of that each modification of the game and even different maps being run require different amount of resources from the computer. Instead of reading these numbers by hand in Task Manager (requires the servers to be full, running that specific game, map etc. Virtually impossible) I want to rely on data.

If I can read CPU (it doesn't have to be percent, but it has to be a number I can compare to a maximum) I can determine the usage requirements of a game and provision game server instances over my available hardware using logic, rather than me guessing which server should be able to run the game or not.

So, even if an alarm on every server reporting when the CPU (overall) is too high what I really want is the CPU pattern for that specific game given certain amount of players (the two biggest factors usually).

As it stands now, the process that gave me 4% in Task Manager was a server with 34 players on it. According to psutil it was at 25% which would mean I could only run a total of 136 slots. We have been running well over 256 on the same hardware without even being close to 100% (Task Manager). Actually we stop loading servers when we reach 70% in order to have some head-room and based on spikes in user behavior.

Given this, what would you suggest I do? Use processor time? Maybe use wmic, wmi or some other native tool for Windows? When you say I don't understand the numbers, please give me an idea of what number I actually should look at. I don't know much details about CPU technology but have a fair background and experience in programming.

Kind regards,
Martin

Relying on CPU percent looks like the right thing to do to me in this case, and you probably want to do that by using an interval of 60 secs or something instead of 1.
Other than that I'm not sure what else to recommend as I cannot reproduce the issue you're describing, I'm sorry.

bharath...@gmail.com

unread,
Feb 20, 2014, 5:03:19 AM2/20/14
to psu...@googlegroups.com
Hello Martin,

Did you got any solution for this issue. Even I am facing the same issue.I need to display the CPU usage for a particular function call and the psutil is displaying 100% for most of the cases while task manager show around 50-60% data.

sdm.hom...@gmail.com

unread,
Dec 18, 2014, 3:59:16 PM12/18/14
to psu...@googlegroups.com, bharath...@gmail.com
Just finding this thread recently as I have had similar experiences.  Below is a code example for using process and cpu times from psutil to calculate process % CPU utilization rather than  using proc.cpu_percent() directly.

Maybe someone finds this example useful.

#!/usr/bin/python
"""
Example of using process and CPU times from psutil to calculate process CPU %
utilizaton.  The example here calculates the utilization over a user defined
time interval.
"""
#psutil syntax changes between versions 1 and 2 can create deprecation warnings
#the following two lines would enable ignoring the warnings...
#import warnings
#warnings.filterwarnings('ignore',category=DeprecationWarning)

import psutil
import time
import re

#instead of ignorng warnings, check which psutil version and call accordingly
ver=psutil.__version__
verChk1=re.match('1.[0-9].[0-9]',ver) #check if version 1 or not
if verChk1 != None:
    psutilVer=1
else:
    psutilVer=2

#define calculate CPU percentage function
def get_cpu_pct(pid,pr_procTimes,pr_cpuTimes):
    
    #find the given process
    p=psutil.Process(pid)
    
    #compute the change in process utilization time since previous call
    procTimes=p.get_cpu_times() if psutilVer==1 else p.cpu_times()
    procTimesTotal=procTimes.user+procTimes.system
    pr_procTimesTotal=pr_procTimes.user+pr_procTimes.system
    deltaProcTimes=procTimesTotal-pr_procTimesTotal
    
    #compute the change in CPU time since previous call
    cpuTimes=psutil.cpu_times()
    cpuTimesTotal=cpuTimes.user+cpuTimes.system+cpuTimes.idle
    #need to consider idle time as well when computing total CPU time
    pr_cpuTimesTotal=pr_cpuTimes.user+pr_cpuTimes.system+pr_cpuTimes.idle
    deltaCPUTimes=cpuTimesTotal-pr_cpuTimesTotal
    
    #avoid divison by zero
    if deltaCPUTimes > 0:
        #compute the CPU % utilization
        cpupct=float(deltaProcTimes)/float(deltaCPUTimes)*100
    else:
        #set cpupct to zero if denominator is zero for some reason
        cpupct=0

    #return process and cpu times for next iteration along with CPU % utilized
    return procTimes,cpuTimes,cpupct
    
#Begin main program
pid=int(raw_input('Enter PID: ')) #ask user for a process ID number
proc=psutil.Process(pid) #get the process info for this PID

#initialize history
pr_procTimes=proc.get_cpu_times() if psutilVer==1 else proc.cpu_times()
pr_cpuTimes=psutil.cpu_times()

#Loop to show progress
Nloops=10  #number of times to display results
interval=1 #number of seconds in the time interval between CPU% calls

#loop to observe process CPU % utilization over time
for i in range(Nloops):
    strt_tm=time.time()  #start elapsed time counter
    time.sleep(interval)
    pr_procTimes, pr_cpuTimes, cpupct=get_cpu_pct(pid,pr_procTimes,pr_cpuTimes)
    print "CPU utilization for process: %s is %.2f" % (pid,cpupct),"%","  Elapsed time: %.3f" % (time.time()-strt_tm)," seconds"


Reply all
Reply to author
Forward
0 new messages