001: // serverProfiling.java
002: // (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
003: // first published 17.11.2007 on http://yacy.net
004: //
005: // This is a part of YaCy, a peer-to-peer based web search engine
006: //
007: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
008: // $LastChangedRevision: 1986 $
009: // $LastChangedBy: orbiter $
010: //
011: // LICENSE
012: //
013: // This program is free software; you can redistribute it and/or modify
014: // it under the terms of the GNU General Public License as published by
015: // the Free Software Foundation; either version 2 of the License, or
016: // (at your option) any later version.
017: //
018: // This program is distributed in the hope that it will be useful,
019: // but WITHOUT ANY WARRANTY; without even the implied warranty of
020: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: // GNU General Public License for more details.
022: //
023: // You should have received a copy of the GNU General Public License
024: // along with this program; if not, write to the Free Software
025: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026:
027: package de.anomic.server;
028:
029: import java.util.Collections;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.TreeMap;
034:
035: public class serverProfiling extends Thread {
036:
037: private static Map<String, TreeMap<Long, Event>> historyMaps; // key=name of history, value=TreeMap of Long/Event
038: private static Map<String, Integer> eventCounter; // key=name of history, value=Integer of event counter
039: private static long lastCompleteCleanup;
040: private static serverProfiling systemProfiler;
041:
042: static {
043: // initialize profiling
044: historyMaps = Collections
045: .synchronizedMap(new HashMap<String, TreeMap<Long, Event>>());
046: eventCounter = Collections
047: .synchronizedMap(new HashMap<String, Integer>());
048: lastCompleteCleanup = System.currentTimeMillis();
049: systemProfiler = null;
050: }
051:
052: public static void startSystemProfiling() {
053: systemProfiler = new serverProfiling(1000);
054: systemProfiler.start();
055: }
056:
057: public static void stopSystemProfiling() {
058: systemProfiler.running = false;
059: }
060:
061: private long delaytime;
062: private boolean running;
063:
064: public serverProfiling(long time) {
065: this .delaytime = time;
066: running = true;
067: }
068:
069: public void run() {
070: while (running) {
071: update("memory", new Long(serverMemory.used()));
072: try {
073: Thread.sleep(this .delaytime);
074: } catch (InterruptedException e) {
075: this .running = false;
076: }
077: }
078: }
079:
080: public static void update(String eventName, Object eventPayload) {
081: // get event history container
082: int counter = eventCounter.containsKey(eventName) ? ((Integer) eventCounter
083: .get(eventName)).intValue()
084: : 0;
085: TreeMap<Long, Event> history = historyMaps
086: .containsKey(eventName) ? (historyMaps.get(eventName))
087: : new TreeMap<Long, Event>();
088:
089: // update entry
090: Long time = new Long(System.currentTimeMillis());
091: history.put(time, new Event(counter, eventPayload));
092: counter++;
093: eventCounter.put(eventName, new Integer(counter));
094:
095: // clean up too old entries
096: cleanup(history);
097: cleanup();
098:
099: // store map
100: historyMaps.put(eventName, history);
101: }
102:
103: private static void cleanup() {
104: if (System.currentTimeMillis() - lastCompleteCleanup < 600000)
105: return;
106: Object[] historyNames = historyMaps.keySet().toArray();
107: for (int i = 0; i < historyNames.length; i++) {
108: cleanup((String) historyNames[i]);
109: }
110: lastCompleteCleanup = System.currentTimeMillis();
111: }
112:
113: private static void cleanup(String eventName) {
114: if (historyMaps.containsKey(eventName)) {
115: TreeMap<Long, Event> history = historyMaps.get(eventName);
116: cleanup(history);
117: if (history.size() > 0) {
118: historyMaps.put(eventName, history);
119: } else {
120: historyMaps.remove(eventName);
121: }
122: }
123: }
124:
125: private static void cleanup(TreeMap<Long, Event> history) {
126: // clean up too old entries
127: while (history.size() > 0) {
128: Long time = history.firstKey();
129: if (System.currentTimeMillis() - time.longValue() < 600000)
130: break;
131: history.remove(time);
132: }
133:
134: }
135:
136: public static Iterator<Event> history(String eventName) {
137: return (historyMaps.containsKey(eventName) ? (historyMaps
138: .get(eventName)) : new TreeMap<Long, Event>()).values()
139: .iterator();
140: }
141:
142: public static class Event {
143: public int count;
144: public Object payload;
145: public long time;
146:
147: public Event(int count, Object payload) {
148: this.count = count;
149: this.payload = payload;
150: this.time = System.currentTimeMillis();
151: }
152: }
153:
154: }
|