fix for memory tracker

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6526 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 15 years ago
parent bf08e59334
commit 959b38b61b

@ -26,19 +26,12 @@
package net.yacy.kelondro.util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import de.anomic.yacy.graphics.ProfilingGraph;
import net.yacy.kelondro.logging.Log;
public class MemoryTracker extends Thread {
private static final Map<String, ArrayList<Event>> historyMaps = new ConcurrentHashMap<String, ArrayList<Event>>();
private static final Map<String, Long> eventAccess = new ConcurrentHashMap<String, Long>(); // value: last time when this was accessed
private static MemoryTracker systemProfiler = null;
public static void startSystemProfiling() {
@ -61,7 +54,7 @@ public class MemoryTracker extends Thread {
public void run() {
try {
while (running) {
update("memory", Long.valueOf(MemoryControl.used()), true);
EventTracker.update("memory", Long.valueOf(MemoryControl.used()), true, 30000, ProfilingGraph.maxTime);
try {
Thread.sleep(this.delaytime);
} catch (final InterruptedException e) {
@ -72,75 +65,5 @@ public class MemoryTracker extends Thread {
Log.logException(e);
}
}
public static void update(final String eventName, final Object eventPayload, boolean useProtection) {
// get event history container
Long lastAcc = eventAccess.get(eventName);
if (lastAcc == null) {
eventAccess.put(eventName, Long.valueOf(System.currentTimeMillis()));
} else {
long time = System.currentTimeMillis();
if (!useProtection || time - lastAcc.longValue() > 1000) {
eventAccess.put(eventName, Long.valueOf(time));
} else {
return; // protect against too heavy load
}
}
ArrayList<Event> history = historyMaps.get(eventName);
if (history != null) synchronized (history) {
// update entry
history.add(new Event(eventPayload));
// clean up too old entries
int tp = history.size() - 30000;
while (tp-- > 0) history.remove(0);
if (history.size() % 10 == 0) { // reduce number of System.currentTimeMillis() calls
Event e;
final long now = System.currentTimeMillis();
while (!history.isEmpty()) {
e = history.get(0);
if (now - e.time < 600000) break;
history.remove(0);
}
}
} else {
history = new ArrayList<Event>(100);
// update entry
history.add(new Event(eventPayload));
// store map
historyMaps.put(eventName, history);
}
}
public static ArrayList<Event> history(final String eventName) {
return historyMaps.get(eventName);
}
public static int countEvents(final String eventName, long time) {
ArrayList<Event> event = history(eventName);
if (event == null) return 0;
long now = System.currentTimeMillis();
int count = 0;
synchronized (event) {
Iterator<Event> i = event.iterator();
while (i.hasNext()) {
if (now - i.next().time < time) count++;
}
}
return count;
}
public static class Event {
public Object payload;
public long time;
public Event(final Object payload) {
this.payload = payload;
this.time = System.currentTimeMillis();
}
}
}

Loading…
Cancel
Save