Skip to content

Commit

Permalink
Use table for specific slow ticks
Browse files Browse the repository at this point in the history
Signed-off-by: Ross Allan <rallanpcl@gmail.com>
  • Loading branch information
LunNova committed Feb 7, 2013
1 parent 748a10f commit 4c56aeb
Showing 1 changed file with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
public class EntityTickProfiler {
private int ticks;
private final AtomicLong totalTime = new AtomicLong();
private final StringBuffer slowTicks = new StringBuffer(); // buffer for threadsafety

public void record(Object o, long time) {
if (time < 0) {
time = 0;
} else if (time > 25000000 && slowTicks.length() <= 500) {
slowTicks.append(o + " took too long: " + time / 1000000 + "ms\n"); // No chained append for threadsafety
} else if (time > 500000) {
AtomicLong currentTime = getSingleTime(o);
synchronized (currentTime) {
if (currentTime.get() < time) {
currentTime.set(time);
}
}
//slowTicks.append(o + " took too long: " + time / 1000000 + "ms\n"); // No chained append for threadsafety
}
Class<?> clazz = o.getClass();
getTime(clazz).addAndGet(time);
Expand All @@ -42,11 +47,25 @@ public void tick() {
}

public TableFormatter writeData(TableFormatter tf) {
tf.sb.append(slowTicks);
Map<Class<?>, Long> time = new HashMap<Class<?>, Long>();
for (Map.Entry<Class<?>, AtomicLong> entry : this.time.entrySet()) {
time.put(entry.getKey(), entry.getValue().get());
}
Map<Object, Long> singleTime = new HashMap<Object, Long>();
for (Map.Entry<Object, AtomicLong> entry : this.singleTime.entrySet()) {
singleTime.put(entry.getKey(), entry.getValue().get());
}
final List<Object> sortedSingleKeysByTime = Ordering.natural().reverse().onResultOf(Functions.forMap(singleTime)).immutableSortedCopy(singleTime.keySet());
tf
.heading("Obj")
.heading("Max Time(ms)");
for (int i = 0; i < 5 && i < sortedSingleKeysByTime.size(); i++) {
tf
.row(sortedSingleKeysByTime.get(i).toString().substring(0, 48))
.row(singleTime.get(sortedSingleKeysByTime.get(i)) / 1000000d);
}
tf.finishTable();
tf.sb.append('\n');
final List<Class<?>> sortedKeysByTime = Ordering.natural().reverse().onResultOf(Functions.forMap(time)).immutableSortedCopy(time.keySet());
tf
.heading("Class")
Expand Down Expand Up @@ -94,6 +113,7 @@ private static String niceName(Class<?> clazz) {

private final Map<Class<?>, AtomicInteger> invocationCount = new NonBlockingHashMap<Class<?>, AtomicInteger>();
private final Map<Class<?>, AtomicLong> time = new NonBlockingHashMap<Class<?>, AtomicLong>();
private final Map<Object, AtomicLong> singleTime = new NonBlockingHashMap<Object, AtomicLong>();

// We synchronize on the class name as it is always the same object
// We do not synchronize on the class object as that would also
Expand All @@ -112,6 +132,20 @@ private AtomicInteger getInvocationCount(Class<?> clazz) {
return i;
}

private AtomicLong getSingleTime(Object o) {
AtomicLong t = singleTime.get(o);
if (t == null) {
synchronized (o) {
t = singleTime.get(o);
if (t == null) {
t = new AtomicLong();
singleTime.put(o, t);
}
}
}
return t;
}

private AtomicLong getTime(Class<?> clazz) {
AtomicLong t = time.get(clazz);
if (t == null) {
Expand Down

0 comments on commit 4c56aeb

Please sign in to comment.