00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 }