Re: getting cpu usage on commandline

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



----- "Atte André Jensen" <atte.jensen@xxxxxxxxx> wrote:
> Julien Claassen wrote:> > Hello atte!> >   Two things I can think of: Have you tried the normal top? - I> don't > > know about its accuracy.> > I played around a bit more, and it seems I misjudged "ps aux", since> it > seems to report correct values. Will takt that route and get back in > case of further trouble.> > Thanks for the input, anyhow!
Short solution: top -b -n 1(with procps 3.2.7, just in case itfails for you...)
Long explaination (you may skip it):Note that ps results are not accurate.The CPU usage is reported as % of time spentrunning during the entire lifetime of a process.So if you have a process sleeping for one minuteand then running like hell for one second, you mightthink it won't eat much CPU if you ps it duringthis second even if in reality it eats up allthe CPU.
The /proc/[processID]/stat gives you statisticson the process. You are interested in the fieldutime I guess, which gives you the number ofjiffies the process smoked in user mode at thetime of reading /proc/[processID]/stat. (Theoutput format of /proc/[processID]/stat isdescribed in Documentation/fs/proc.txt found inthe linux sources tarball.)
So to know how much CPU the process smokes youfirst read /proc/.../stat, wait a bit (1s or so),read /proc/.../stat again and do somethinglike (value2 - value1) / nb_jiffies_of_1s(nb_jiffies_of_1s might be defined on yoursystem in /usr/include/asm/param.h, it's theHZ value. It might also exist in your shell'senvironment (try: echo $HZ). Or you can goread the procps sources (in procps-3.2.7this stuff is in proc/sysinfo.c, and it is a bittricky to get this value it seems...)to see how they get it.You can also do:cat /proc/stat; sleep 1; cat /proc/statand see how bigger the 4th number onthe line "cpu: XX" is.(Or /proc/self/stat and the 22nd number.)On my host HZ=100, but on yours itmight well be 250, 300 or 1000 (thisare the values I see when doinga "make config" of the kernel).I guess 1000 is the most probable.
Anyway, there is no way to know how much CPU a givenprocess is eating at a given moment. You have to"wait a bit" and see what changed.
But maybe you are more interested in theglobal times? in which case just do"time command" and you get some statsat the end, when the process exits.
For the "accurate" way to measureit's not too hard to code. The program wouldopen /proc/[processID]/stat, get the number,wait, open again, output the result and exit.A few lines of C code.
Or you can do "top -b -n 1" with other optionsif you like. Just read the manpage.
(Note that it may not be very accurate eitherif the process switches before the end ofa jiffy for example; I had such a problem in thepast with a 2.0 something kernel; the processwas reported as eating 0% CPU which wasfalse. But maybe things are differentnow.)
Hope it helps.Cédric._______________________________________________Linux-audio-user mailing listLinux-audio-user@xxxxxxxxxxxxxxxxxxxxxxxx://lists.linuxaudio.org/mailman/listinfo/linux-audio-user

[Index of Archives]     [Linux Sound]     [ALSA Users]     [Pulse Audio]     [ALSA Devel]     [Sox Users]     [Linux Media]     [Kernel]     [Photo Sharing]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux