Update of /cvsroot/freenet/freenet/src/freenet/thread
In directory sc8-pr-cvs1:/tmp/cvs-serv3970/src/freenet/thread

Added Files:
        ThreadStatusSnapshot.java 
Log Message:
Added ThreadStatusSnapshot, can be used to take a snapshot of current thread status
Made EnvironmentInfolet use the above mentioned class

--- NEW FILE: ThreadStatusSnapshot.java ---
/*
 * Created on Sep 28, 2003
 *
 */
package freenet.thread;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


/**
 * @author Iakin
 * Takes a snapshot of the current thread hiearchy and allow the user to access 
various statuc information
 * as well as dumping some stats to HTML 
 *
 */
public class ThreadStatusSnapshot {
        Hashtable consumers = new Hashtable();
        ThreadCount tc = new ThreadCount();
        group root;
        public ThreadStatusSnapshot(){
                ThreadGroup tg = Thread.currentThread().getThreadGroup().getParent();
                ThreadGroup topMost = null;
                while(tg != null)
                {
                        topMost = tg;
                        tg = tg.getParent();
                }
                root = new group(topMost);
        }
        
        public Hashtable getPoolConsumers(){
                return consumers;
        }
        /* Returns the total number of/the number of available threads in freds 
threadPool*/
        public ThreadCount getPoolThreadCounts(){
                return tc;
        }
        private void countPooledThread(PooledThread t) { //TODO: Wouldn't it be better 
to just ask the threadpool for the information counted here?
                tc.totalPooled++;
                Runnable job = t.job();
                if (job != null)
                        countConsumer(job);
                else
                        tc.pooledAvailable++;
        }
                
        private void countConsumer(Runnable job){
                String type = job.toString();
                type = type.substring(0, type.indexOf("@"));
                Consumer con = (Consumer) consumers.get(type);
                if (con == null) {
                        con = new Consumer();
                        consumers.put(type, con);
                }
                con.number++;
        }
        public static class ThreadCount {
                int totalPooled = 0;
                int pooledAvailable = 0;
        }
        private static class Consumer {
                /**
                 * The number of current consumers of this type.
                 */
                int number = 0;
        }
                
        class group{
                String groupName;
                List lThreads = new LinkedList();
                List lSubGRoups = new LinkedList();
                
                group(ThreadGroup group){
                        groupName = group.getName();
                        Thread[] aThreads = new Thread[group.activeCount()];
                        int threads = group.enumerate(aThreads, false);
                        for(int j=0; j<threads; j++){
                                Thread t = aThreads[j];
                                if(t != null) //Yes.. sometimes this can happen /Iakin
                                        lThreads.add(new ThreadInfo(t));
                        }
                        ThreadGroup[] groups = new 
ThreadGroup[group.activeGroupCount()];
                        int groupcount=group.enumerate(groups, false);
                        for(int i=0; i<groupcount; i++)
                                lSubGRoups.add(new group(groups[i]));
                }
                
                void toHTML(StringBuffer buffer){
                        buffer.append("\n<li><b>" + groupName + "</b><ul>");
                        Iterator it = lThreads.iterator();
                        while(it.hasNext()){
                                ThreadInfo t = (ThreadInfo)it.next();
                                buffer.append("\n<li>" +t.name);
                                if(t.jobString != null)
                                        buffer.append(": " + t.jobString);
                        }
                        it = lSubGRoups.iterator();
                        while(it.hasNext())
                                ((group)it.next()).toHTML(buffer);
                        buffer.append("</ul>");
                }
                
                class ThreadInfo{
                        String name;
                        String jobString;
                        ThreadInfo(Thread t){
                                name = t.getName();
                                if (t instanceof PooledThread) {
                                        Runnable r = ((PooledThread)t).job();
                                        if(r != null)
                                        jobString = r.toString();
                                        countPooledThread((PooledThread)t);
                                }
                        }
                }
        }
        
        public String threadTreeToHTML(){
                StringBuffer buffer = new StringBuffer();
                root.toHTML(buffer);
                return buffer.toString();
        }
        
        public String threadStatusToHTML(){
                StringWriter ssw = new StringWriter(200);
                PrintWriter sw = new PrintWriter(ssw);
                sw.println("<table width=\"100%\">");
                sw.println("<tr><td>Total pooled threads</td><td align=right>" + 
tc.totalPooled + "</td></tr>");
                sw.println("<tr><td>Available pooled threads</td><td align=right>" + 
tc.pooledAvailable + "</td></tr>");
                sw.println("<tr><td>Pooled threads in use</td><td align=right>" + 
(tc.totalPooled - tc.pooledAvailable) + "</td></tr>");
                sw.println("</table>");
                return ssw.toString();
        }
        
        public String poolConsumersToHTML(){
                StringWriter ssw = new StringWriter(200);
                PrintWriter sw = new PrintWriter(ssw);
                sw.println("<table width=\"100%\">");
                sw.println("<tr><th align=\"left\">Class</th><th 
align=\"right\">Threads used</th>");
                Object[] types = getPoolConsumers().keySet().toArray();
                java.util.Arrays.sort(types);
                for (int x = 0; x < types.length; x++)
                        sw.println("<tr><td>" + types[x] + "</td><td align=\"right\">" 
+
                   ((Consumer) getPoolConsumers().get(types[x])).number + 
"</td></tr>");
                sw.println("</table>");
                return ssw.toString();
        }
}

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to