org/objectweb/proactive/benchmarks/timit/util/TimItReductor.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.benchmarks.timit.util;
00032 
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.util.ArrayList;
00036 
00037 import org.objectweb.proactive.Body;
00038 import org.objectweb.proactive.ProActive;
00039 import org.objectweb.proactive.Service;
00040 import org.objectweb.proactive.benchmarks.timit.util.observing.EventData;
00041 import org.objectweb.proactive.benchmarks.timit.util.observing.EventDataBag;
00042 
00050 public class TimItReductor implements Serializable {
00051 
00055     private static final long serialVersionUID = -3249170377806470213L;
00056 
00058     public static final int TIMEOUT = 5000;
00059 
00060     private transient Service service;
00061 
00062     private int nbReceived;
00063 
00064     private HierarchicalTimer timer;
00065 
00066     private String information;
00067 
00068     private int groupSize;
00069 
00070     private EventDataBag eventDataBag;
00071 
00072     private BenchmarkStatistics bstats;
00073 
00078     private ArrayList<EventDataBag> bagList = new ArrayList<EventDataBag>();
00079 
00080     private static boolean error = false;
00081 
00085     public TimItReductor() {
00086         this.bstats = null;
00087     }
00088 
00094     public long getCurrentTimeMillis() {
00095         return System.currentTimeMillis();
00096     }
00097 
00103     public void receiveTimer(HierarchicalTimer receivedTimer) {
00104 
00105         if (this.nbReceived == 0) {
00106             this.timer = receivedTimer;
00107         }
00108         this.timer.addInstance(receivedTimer);
00109     }
00110 
00111     private void handleInformation(String receivedInformation) {
00112         if (this.information == null || this.information.length() == 0) {
00113             this.information = receivedInformation;
00114 
00115         } else {
00116             this.information += "\n" + receivedInformation;
00117         }
00118     }
00119 
00120     public void setGroupSize(int groupSize) {
00121         this.groupSize = groupSize;
00122     }
00123 
00127     public void clean() {
00128         this.bstats = null;
00129         this.groupSize = 0;
00130         this.nbReceived = 0;
00131         this.eventDataBag = null;
00132         this.information = null;
00133         this.timer = null;
00134         this.bagList = new ArrayList<EventDataBag>();
00135     }
00136 
00144     public BenchmarkStatistics getStatistics() {
00145 
00146         // Check if already computed
00147         if (this.bstats != null) {
00148             return this.bstats;
00149         }
00150         this.service = new Service(ProActive.getBodyOnThis());
00151 
00152         // Wait for the groupSize
00153         while (this.groupSize == 0) {
00154             this.service.blockingServeOldest();
00155         }
00156 
00157         HierarchicalTimerStatistics hts;
00158         EventStatistics events;
00159 
00160         while (this.nbReceived < this.groupSize && !TimItReductor.error) {
00161             this.service.blockingServeOldest(TimItReductor.TIMEOUT);
00162         }
00163 
00164         // Timer counters statistics
00165         if (this.timer == null) {
00166             hts = new HierarchicalTimerStatistics();
00167         } else {
00168             hts = this.timer.getStats();
00169         }
00170 
00171         // Event observers statisitics
00172         EventDataBag sdb = this.collapseEventData();
00173         this.eventDataBag = sdb;
00174         if (sdb == null) {
00175             events = new EventStatistics();
00176         } else {
00177             events = sdb.getStats();
00178         }
00179 
00180         this.bstats = new BenchmarkStatistics(hts, events, this.information);
00181         return this.bstats;
00182     }
00183 
00187     public static void stop() {
00188         TimItReductor.error = true;
00189     }
00190 
00194     public static void ready() {
00195         TimItReductor.error = false;
00196     }
00197 
00198     // //////////////////////////////////////////////////////////////////////////
00199     // Specialized Observer/Observable pattern handling
00200     // //////////////////////////////////////////////////////////////////////////
00201 
00207     public void receiveAll(EventDataBag eventDataBag,
00208             HierarchicalTimer receivedTimer, String receivedInformation) {
00209         if (eventDataBag != null) {
00210             this.bagList.add(eventDataBag);
00211         }
00212 
00213         if (receivedTimer != null) {
00214             this.receiveTimer(receivedTimer);
00215         }
00216 
00217         if (receivedInformation != null) {
00218             this.handleInformation(receivedInformation);
00219         }
00220 
00221         this.nbReceived++;
00222     }
00223 
00229     private EventDataBag collapseEventData() {
00230         if (this.bagList.size() == 0) {
00231             return null;
00232         }
00233 
00234         if (this.nbReceived < this.groupSize && !TimItReductor.error) {
00235             this.service.blockingServeOldest(TimItReductor.TIMEOUT);
00236         }
00237         int i, j;
00238 
00239         // This first bag will contain all collapsed StatDatas
00240         EventDataBag firstBag = this.bagList.get(0);
00241 
00242         if (firstBag == null) {
00243             System.out.println("The first bag is null inside method "
00244                     + "TimItReductor.collapseStatData !");
00245             return null;
00246         }
00247 
00248         // The EventData to collapse
00249         EventData data;
00250 
00251         if (this.bagList.size() == 1) {
00252             for (i = 0; i < firstBag.size(); i++) {
00253                 data = firstBag.getEventData(i);
00254                 data.collapseWith(data, firstBag.getSubjectRank());
00255             }
00256             return firstBag;
00257         }
00258 
00259         EventDataBag anotherBag;
00260         // Iterate through elements of the firstBag
00261         for (i = 0; i < firstBag.size(); i++) {
00262             // Get the data to collapse with
00263             data = firstBag.getEventData(i);
00264             // Iterate through other bags
00265             for (j = 1; j < this.bagList.size(); j++) {
00266                 anotherBag = this.bagList.get(j);
00267                 data.collapseWith(anotherBag.getEventData(i), anotherBag
00268                         .getSubjectRank());
00269             }
00270         }
00271         return firstBag;
00272     }
00273 
00278     public EventDataBag getEventDataBag() {
00279         return this.eventDataBag;
00280     }
00281 
00282     public void terminate() {
00283         try {
00284             Body b = ProActive.getBodyOnThis();
00285             b.getFuturePool().disableAC();
00286             ProActive.getBodyOnThis().terminate();
00287         } catch (IOException e) {
00288             e.printStackTrace();
00289         }
00290     }
00291 }

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