[SORTA OT] Getting CPU usage of a process

J

Joe Van Dyk

Hi,

I'd like to be able to get the CPU usage of a particular process.=20
What are my options for doing so from Ruby?

I've found the libgtop ruby binding, and I could use that, but I would
rather not bring in another dependency. This will run on Linux
systems only.

And would I want to use the elapsed real cpu time? Or the sum of the
elapsed user and system cpu time? (and then I believe I divide that
by the elapsed wall clock time)

Thanks,
Joe
 
K

Kero

I'd like to be able to get the CPU usage of a particular process.
What are my options for doing so from Ruby?

I've found the libgtop ruby binding, and I could use that, but I would
rather not bring in another dependency. This will run on Linux
systems only.

And would I want to use the elapsed real cpu time? Or the sum of the
elapsed user and system cpu time? (and then I believe I divide that
by the elapsed wall clock time)

man proc, look for "/proc/[number]/stat"
It contains a set of times spent by the process and its children.
it also contains the starttime, so this should allow you to compute "Cpu
usage of the process".

Hth,
Kero.

+--- Kero ---------------------------------- kero@chello@nl ---+
| The last good thing written in C++ was the Pachelbel Canon |
| Jerry Olson |
+--- M38c ------------ http://members.chello.nl/k.vangelder ---+
 
J

Joe Van Dyk

I'd like to be able to get the CPU usage of a particular process.
What are my options for doing so from Ruby?

I've found the libgtop ruby binding, and I could use that, but I would
rather not bring in another dependency. This will run on Linux
systems only.

And would I want to use the elapsed real cpu time? Or the sum of the
elapsed user and system cpu time? (and then I believe I divide that
by the elapsed wall clock time)

man proc, look for "/proc/[number]/stat"
It contains a set of times spent by the process and its children.
it also contains the starttime, so this should allow you to compute "Cpu
usage of the process".

Thanks, I'm aware of /procproc.

I want to be able to constantly keep the user updated on how much of
the cpu the application is using.

Would I take the sum of the elapsed user and system cpu time and
divide that by the elapsed wall clock time to get the current cpu
percentage?
 
J

Joe Van Dyk

I'd like to be able to get the CPU usage of a particular process.
What are my options for doing so from Ruby?

I've found the libgtop ruby binding, and I could use that, but I woul= d
rather not bring in another dependency. This will run on Linux
systems only.

And would I want to use the elapsed real cpu time? Or the sum of the
elapsed user and system cpu time? (and then I believe I divide that
by the elapsed wall clock time)

man proc, look for "/proc/[number]/stat"
It contains a set of times spent by the process and its children.
it also contains the starttime, so this should allow you to compute "Cp= u
usage of the process".

Thanks, I'm aware of /procproc.

I want to be able to constantly keep the user updated on how much of
the cpu the application is using.

Would I take the sum of the elapsed user and system cpu time and
divide that by the elapsed wall clock time to get the current cpu
percentage?

And one other question, does anyone know how I might do this on
Windows via Ruby?
 
X

x1

Seems to be lots of win32ole stuff that's usefull here:
http://msdn.microsoft.com/library/default.asp?url=3D/library/en-us/wmisdk/w=
mi/wmi_tasks__performance_monitoring.asp

this seems tto work ok on a process name but.. not sure how to do it on an =
id..
wscript.exe test.vbs
-------test.vbs-------
strComputer =3D "."
Set objWMIService =3D GetObject("winmgmts:" _
& "{impersonationLevel=3Dimpersonate}!\\" _
& strComputer & "\root\cimv2")
set PerfProcess =3D objWMIService.Get(_
"Win32_PerfFormattedData_PerfProc_Process.Name=3D'Idle'")

While (True)
PerfProcess.Refresh_
Wscript.Echo PerfProcess.PercentProcessorTime
Wscript.Sleep 1000
Wend
--------/test.vbs--------



I'd like to be able to get the CPU usage of a particular process.
What are my options for doing so from Ruby?

I've found the libgtop ruby binding, and I could use that, but I wo= uld
rather not bring in another dependency. This will run on Linux
systems only.

And would I want to use the elapsed real cpu time? Or the sum of t= he
elapsed user and system cpu time? (and then I believe I divide tha= t
by the elapsed wall clock time)

man proc, look for "/proc/[number]/stat"
It contains a set of times spent by the process and its children.
it also contains the starttime, so this should allow you to compute "= Cpu
usage of the process".

Thanks, I'm aware of /procproc.

I want to be able to constantly keep the user updated on how much of
the cpu the application is using.

Would I take the sum of the elapsed user and system cpu time and
divide that by the elapsed wall clock time to get the current cpu
percentage?

And one other question, does anyone know how I might do this on
Windows via Ruby?
 
K

Kero

I'd like to be able to get the CPU usage of a particular process.
What are my options for doing so from Ruby?

And would I want to use the elapsed real cpu time? Or the sum of the
elapsed user and system cpu time? (and then I believe I divide that
by the elapsed wall clock time)

man proc, look for "/proc/[number]/stat"
It contains a set of times spent by the process and its children.
it also contains the starttime, so this should allow you to compute "Cpu
usage of the process".

Would I take the sum of the elapsed user and system cpu time and
divide that by the elapsed wall clock time to get the current cpu
percentage?

That's what I would start with.

Then you'll want to remember values of 'some time ago' so you can do the
computation 'over the last some-time', as well as 'from the start of the
process'.

+--- Kero ---------------------------------- kero@chello@nl ---+
| The last good thing written in C++ was the Pachelbel Canon |
| Jerry Olson |
+--- M38c ------------ http://members.chello.nl/k.vangelder ---+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top