org/objectweb/proactive/core/util/profiling/PAProfilerEngine.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.util.profiling;
00032 
00033 import java.util.ArrayList;
00034 import java.util.Iterator;
00035 
00036 import org.objectweb.proactive.core.util.timer.AverageMicroTimer;
00037 
00038 
00044 public class PAProfilerEngine implements Runnable {
00045     ArrayList<Timer> profilerList = new ArrayList<Timer>();
00046     private static PAProfilerEngine engine;
00047 
00048     static {
00049         engine = new PAProfilerEngine();
00050         try {
00051             Runtime.getRuntime().addShutdownHook(new Thread(engine));
00052         } catch (Exception e) {
00053             e.printStackTrace();
00054         }
00055     }
00056 
00061     public static Timer createTimer() {
00062         Timer tmp = new AverageMicroTimer();
00063         registerTimer(tmp);
00064         return tmp;
00065     }
00066 
00071     public static void registerTimer(Timer papr) {
00072         synchronized (engine.profilerList) {
00073             engine.profilerList.add(papr);
00074         }
00075     }
00076 
00082     public static boolean removeTimer(Timer papr) {
00083         synchronized (engine.profilerList) {
00084             return engine.profilerList.remove(papr);
00085         }
00086     }
00087 
00088     private PAProfilerEngine() {
00089     }
00090 
00094     public void run() {
00095         dump();
00096     }
00097 
00101     public void dump() {
00102         Iterator<Timer> it = profilerList.iterator();
00103         while (it.hasNext()) {
00104             it.next().dump();
00105         }
00106     }
00107 
00108     public static void main(String[] args) {
00109         System.out.println("Creating a profiler and registering it");
00110         PAProfilerEngine.createTimer();
00111         System.out.println("Creating an AverageTimeProfiler and registering it");
00112         Timer avg = new AverageMicroTimer("Test ");
00113         PAProfilerEngine.registerTimer(avg);
00114 
00115         for (int i = 0; i < 10; i++) {
00116             avg.start();
00117             try {
00118                 Thread.sleep(500);
00119             } catch (InterruptedException e) {
00120                 e.printStackTrace();
00121             }
00122             avg.stop();
00123         }
00124         System.out.println("Now dying");
00125     }
00126 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1