CPU usage monitoring

I’ve done no research, so I’m open to be flamed. :slight_smile:

Is there any way to detect what the cpu is doing while your program is running? My thought is that I could run a ‘stress test’ on the computer the program is running on. Depending on the results, it would set program options differently at runtime. I could turn off music or load lower quality images, etc.

Any info?

Google for profilers. Java actually has one itself (hotspot profiler) - try running your app with -xprof parameter. If you’re using eclipse there’s a really nice profiler plugin for it.

Thanks for the info on the -Xprof. I have started using that just recently.

I’m actually trying to get at the info in the application itself. In pseudo code, it might look like this -


if(CPU.getActivity() >= 0.60f) {
    graphics.antiAliasing(false);
    music.stop();
}

if(CPU.getActivity() <= 0.20f) {
    graphics.antiAliasing(true);
    music.start();
     botAI.setDifficulty(AI_HIGH);
}


In the ‘code’ CPU.getActivity would return the processors current loading (think task manager in Windows, or ps or top for linux) as a float 0 being not loaded 1 being fully loaded.

I’ve used a similar technique before. When network traffic got very high, it would start sucking the CPU. In the graphics thread, I checked the loading and if it was too high, I skipped the draw for that frame. This was in a C# program. (Don’t flog me for not using java!)

Am I asking for the impossible?

Regards,
Aaron R>

You need to write a bit of JNI to do this.

Cas :slight_smile:

Thats easy, its running your program =)

I don’t know why you want to measure CPU load instead of something more relevant to your game, like FPS. FPS can easily be measured in-game, so why not do something like:

if (fps < 60) {
disableExpensiveStuff();
}

  • elias