views:

199

answers:

2

I need to get the cpu usage of the current process using jsp.

My scenario is like when user navigate from one page to other I need to check the cpu utilization, memory usage of the process.

A: 

If you want to do this on every request, also in production (although I highly question the need for this, it would only make it more CPU-expensive), then grab JMX. If you want to do this for testing/profiling purposes (which would make more sense), then grab a Java profiler.

As to the JSP-targeted question, JSP is just a Java based view technology. Raw Java code belongs in a real Java class. If you want to go for JMX, then you rather want to do this in a Filter which is mapped on the url-pattern of interest, e.g. *.jsp.

BalusC
A: 

Getting the CPU usage (e.g. percentage or load figures) means you're going to have to have some native call.

On Windows you can typically do this using WMI (Windows Management Interface). Unfortunately the simplest way to do this (shelling out to a CScript.exe process and running a script) will probably hit your CPU a lot harder than the serving of the JSP page.

On Unix the CPU figures are easier to obtain (via the /proc filesystem, if memory serves).

Depending on your OS, check SO for answers particular to your scenario. But my concern is that you don't taint your gathered stats by an inefficient means of gathering them.

Brian Agnew
CPU usage for the current thread is available in Java through the ThreadMXBean class. There is no need to use native code for this.
jarnbjo
That's CPU *time*. I meant (and have edited to indicate) CPU usage, as in percentage/load figures etc.
Brian Agnew
You *can* get the CPU load-average figure from `OperatingSystemMXBean`, though (see duplicate Q)
skaffman
Ah. Added in 1.6. Interesting. Didn't know that.
Brian Agnew