Skip to content

Commit

Permalink
addition/correction to last commit: usage of concurrent-classes
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4626 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Mar 30, 2008
1 parent b215005 commit 2c34038
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions source/de/anomic/server/serverProfiling.java
Expand Up @@ -28,18 +28,18 @@

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class serverProfiling extends Thread {

private static Map<String, TreeMap<Long, Event>> historyMaps; // key=name of history, value=TreeMap of Long/Event
private static Map<String, ConcurrentLinkedQueue<Event>> historyMaps; // key=name of history, value=TreeMap of Long/Event
private static Map<String, Integer> eventCounter; // key=name of history, value=Integer of event counter
private static serverProfiling systemProfiler;

static {
// initialize profiling
historyMaps = new ConcurrentHashMap<String, TreeMap<Long, Event>>();
historyMaps = new ConcurrentHashMap<String, ConcurrentLinkedQueue<Event>>();
eventCounter = new ConcurrentHashMap<String, Integer>();
//lastCompleteCleanup = System.currentTimeMillis();
systemProfiler = null;
Expand Down Expand Up @@ -76,33 +76,37 @@ public void run() {
public static void update(String eventName, Object eventPayload) {
// get event history container
int counter = eventCounter.containsKey(eventName) ? ((Integer) eventCounter.get(eventName)).intValue() : 0;
TreeMap<Long, Event> history = historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new TreeMap<Long, Event>();
if (historyMaps.containsKey(eventName)) {
ConcurrentLinkedQueue<Event> history = historyMaps.get(eventName);

// update entry
Long time = new Long(System.currentTimeMillis());
history.put(time, new Event(counter, eventPayload));
counter++;
eventCounter.put(eventName, new Integer(counter));

// clean up too old entries
cleanup(history);

// store map
historyMaps.put(eventName, history);
}

private static void cleanup(TreeMap<Long, Event> history) {
// clean up too old entries
while (history.size() > 0) {
Long time = history.firstKey();
if (System.currentTimeMillis() - time.longValue() < 600000) break;
history.remove(time);
}

// update entry
history.add(new Event(counter, eventPayload));
counter++;
eventCounter.put(eventName, new Integer(counter));

// clean up too old entries
Event e;
long now = System.currentTimeMillis();
while (history.size() > 0) {
e = history.peek();
if (now - e.time < 600000) break;
history.poll();
}
} else {
ConcurrentLinkedQueue<Event> history = new ConcurrentLinkedQueue<Event>();

// update entry
history.add(new Event(counter, eventPayload));
counter++;
eventCounter.put(eventName, new Integer(counter));

// store map
historyMaps.put(eventName, history);
}
}

public static Iterator<Event> history(String eventName) {
return (historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new TreeMap<Long, Event>()).values().iterator();
return (historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new ConcurrentLinkedQueue<Event>()).iterator();
}

public static class Event {
Expand Down

0 comments on commit 2c34038

Please sign in to comment.