Engine Offload/System Cycle Rate?

Looking for a nice way to keep my games cycle under 6 seconds, and every 45 seconds outprint the following: (If it can be calculated)

Average Cycle Time: 0.0ms. (Under control =D)
Engine Offload: 0.0%. (Possibly out of 100%?)

Right now how my game’s loop looks is like the following:


	public void run() {
		try {
			if (sleepTime > 0) {
				Thread.sleep(getCycle());
				mainProcessHandler();
				cycles++;
				debug();
			}
		} catch (InterruptedException e) {
			System.out.println("A Fatal Exception has occured during Runtime!");
			e.printStackTrace(System.err);
			System.exit(0);
			return;
		}
	}

And at the end it calls the debugging method (where i’d like this to outprint every 45 seconds).


	private void debug() {
		try {
			if ((debugTimer.elapsed() > averageDebugTime)  && (debugging())) {
				cycleTime = cycleTimer.elapsed();
				System.out.println("Average Cycle Time: "+cycleTime+"ms.");
				System.out.println("Total Cycles: "+getCycles());
				cycles = 0;
				sleepTime = getCycle();
				cycleTimer.reset();
				debugTimer.reset();
				System.gc();
				System.runFinalization();
			}
		} catch (Exception e1) {
			System.err.println("A Fatal Exception has occured while processing debug!");
			e1.printStackTrace(System.err);
			System.exit(0);
			return;
		}
	}

I really don’t like what i’ve came up with before this, or the example above.

Right now it just outprints:


[2/5/12 6:05 PM]: Average Cycle Time: 30007ms.
[2/5/12 6:05 PM]: Total Cycles: 1938

So i’m asking if anyone could come up or share some nice way of keeping track of these variables, and adjusting them through out my main game class.

Now what exactly is a cycle? The call to update() then draw()? Just update()?

A ‘Cycles’ variable is added++ everytime “mainProcessHandler();” is called & the initial “Cycle” is completed:
So a cycle in this would be the “mainProcessHandler()” method.


	private void mainProcessHandler() {
		try {
			Player.process();		
			WalkingCheck.process();
			WalkingHandler.process();
			repaint();
			updateFps();
		} catch (Exception e1) {
			System.err.println("A Fatal Exception has occured while processing the mainHandler!");
			e1.printStackTrace(System.err);
			System.exit(0);
			return;
		}
	}

Sorry for posting so much code, it’s just that i’ve never been good with System.currentTimeMillis() and getting results to return accurately.
The Cycles variable i don’t care about (I added that just for looks after all else failed to return accurate results).
I’d just like some fancy debugging/engine offload variables that when printed out give me accurate results of usage out of 100(percent). <(keyboard failure).

(The average time between debugs i’ve already handled via a SimpleTimer class).

This is what I was using before, and since I can’t comprehend on how to change those variables to adjust to a much smaller sleepTime (3 to six) ms. I bring my question here asking for help D=


	/**
	 * Main Server Tick
	 */
	try {
		if (sleepTime > 0)
			Thread.sleep(sleepTime);
		engineTimer.reset();

		if(cycleTime < 575)
			sleepTime = cycleRate - cycleTime;
		else
			sleepTime = 0;
		cycleTime = engineTimer.elapsed();				
		sleepTime = cycleRate - cycleTime;
		totalCycleTime += cycleTime;
		cycles++;
		debug();
	} catch (Exception ex) {
		ex.printStackTrace();
		System.out.println("A fatal exception has been thrown!");
		System.exit(0);
	}

Everytime that got printed out it would just say usage is = 0.0, simply because the hop between sleepTime was to great for anything to even build up/redraw intime for a smaller cycle (Sprite based game).

Here is that fancy methods debug() (math/results portion)


	private static void debug() {
		if (debugTimer.elapsed() > 360 * 1000) {
			long averageCycleTime = totalCycleTime / cycles;
			System.out.println("Average Cycle Time: " + averageCycleTime + "ms");
			double engineLoad = ((double) averageCycleTime / (double) cycleRate);
			System.out.println("Engine load: "+ debugPercentFormat.format(engineLoad));
			totalCycleTime = 0;
			cycles = 0;
			debugTimer.reset();
		}
	}

Ah that sounds like the normal inaccuracy of currentTimeMillis() :slight_smile:

This should help explain things quite a bit and offer a quick hack to fix it :slight_smile: