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.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
00147 if (this.bstats != null) {
00148 return this.bstats;
00149 }
00150 this.service = new Service(ProActive.getBodyOnThis());
00151
00152
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
00165 if (this.timer == null) {
00166 hts = new HierarchicalTimerStatistics();
00167 } else {
00168 hts = this.timer.getStats();
00169 }
00170
00171
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
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
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
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
00261 for (i = 0; i < firstBag.size(); i++) {
00262
00263 data = firstBag.getEventData(i);
00264
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 }