1
/*
2
 * $Id$
3
 *
4
 * Copyright (C) 2003-2009 JNode.org
5
 *
6
 * This library is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful, but 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this library; If not, write to the Free Software Foundation, Inc., 
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
 */
20
21
package org.jnode.vm.memmgr.generational;
22
23
import org.jnode.vm.memmgr.GCStatistics;
24
25
/**
26
 * 
27
 * @author loic
28
 *
29
 */
30
final class GenGCStatsCounter extends GCStatistics {
31
32
	private String title;
33
	private long lastDuration;
34
	private long lastPauseDuration;
35
	private long avgDuration;
36
	private long avgPauseDuration;
37
	private long lastExecutionDate;
38
	private long firstExecutionDate = -1L;
39
	private long nbExecutions = 0L;
40
41
	public GenGCStatsCounter(String title) {
42
		this.title = title;
43
	}
44
	
45
	public void addExecutionDuration(long duration) {
46
		long currentDate = currentMillis();
47
		lastDuration = duration;
48
		lastPauseDuration = lastExecutionDate - currentDate;
49
		avgDuration = computeAvg(lastDuration, avgDuration);
50
		avgPauseDuration = computeAvg(lastPauseDuration, avgPauseDuration);
51
		lastExecutionDate = currentDate;
52
		if (firstExecutionDate < 0L) {
53
			firstExecutionDate = currentDate;
54
		}
55
		nbExecutions++;
56
	}
57
	
58
	@Override
59
	public String toString() {
60
		return "--------------------\n\t" +
61
		 title + "--------------------\n" +
62
		 addString("last duration", lastDuration, "ms") +
63
		 addString("last pause duration", lastPauseDuration, "ms") +
64
		 addString("average duration", avgDuration, "ms") +
65
		 addString("average pause duration", avgPauseDuration, "ms") +
66
		 addString("last execution", currentMillis() - lastExecutionDate, "ms ago") +
67
		 addString("overhead", getOverhead()) +
68
		 addString("time executed", nbExecutions) + 
69
		 "--------------------\n";
70
	}
71
72
	private String addString(String property, long value) {
73
		return addString(property, value, "");
74
	}
75
76
	private String addString(String property, long value, String unit) {
77
        return property + ": " + value + " " + unit + "\n";
78
	}
79
	
80
	private long getOverhead() {
81
		return (100 * avgDuration) / (lastExecutionDate - firstExecutionDate);
82
	}
83
84
	private long currentMillis() {
85
        return GenGCStatistics.currentMillis();
86
	}
87
	
88
	private long computeAvg(long newValue, long previousAvg) {
89
		return (previousAvg * nbExecutions + newValue) / (nbExecutions + 1);
90
	}
91
}