Java tutorial
/* * TURNUS, the co-exploration framework * * Copyright (C) 2014 EPFL SCI STI MM * * This file is part of TURNUS. * * TURNUS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TURNUS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TURNUS. If not, see <http://www.gnu.org/licenses/>. * * Additional permission under GNU GPL version 3 section 7 * * If you modify this Program, or any covered work, by linking or combining it * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or * an Eclipse library), containing parts covered by the terms of the * Eclipse Public License (EPL), the licensors of this Program grant you * additional permission to convey the resulting work. Corresponding Source * for a non-source form of such a combination shall include the source code * for the parts of Eclipse libraries used as well as that of the covered work. * */ package co.turnus.profiling.util; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.math3.util.FastMath; import org.eclipse.emf.common.util.EMap; import co.turnus.common.Operator; import co.turnus.common.StatisticalData; import co.turnus.dataflow.Action; import co.turnus.dataflow.Actor; import co.turnus.profiling.ActionProfilingData; import co.turnus.profiling.ActionProfilingWeights; import co.turnus.profiling.ActorProfilingWeights; import co.turnus.profiling.ProfilingData; import co.turnus.profiling.ProfilingFactory; import co.turnus.profiling.ProfilingWeights; import com.google.common.collect.Table; public class ProfilingWeightsEstimator { public static final double DEFAULT_OPERATOR_COST = 1.0; private Map<Operator, Double> operatorsWeightMap; public ProfilingWeightsEstimator() { operatorsWeightMap = new HashMap<Operator, Double>(); for (Operator op : Operator.values()) { operatorsWeightMap.put(op, DEFAULT_OPERATOR_COST); } } public ProfilingWeights estimate(ProfilingData data) { ProfilingFactory f = ProfilingFactory.eINSTANCE; ProfilingWeights netWeights = f.createProfilingWeights(); netWeights.setNetwork(data.getNetwork()); Table<Actor, Action, ActionProfilingData> table = data.asTable(); double networkCost = estimateNetworkCost(data); for (Actor actor : table.rowKeySet()) { ActorProfilingWeights actorWeights = f.createActorProfilingWeights(); actorWeights.setActor(actor); double actorCost = 0; for (Entry<Action, ActionProfilingData> actionEntry : table.row(actor).entrySet()) { ActionProfilingData aData = actionEntry.getValue(); ActionProfilingWeights actionWeights = f.createActionProfilingWeights(); actionWeights.setAction(actionEntry.getKey()); EMap<Operator, StatisticalData> opsMap = aData.getOperatorsCall(); int samples = opsMap.size(); double mean = 0; double min = 0; double max = 0; double sum = 0; if (samples > 0) { min = Double.MAX_VALUE; max = Double.MIN_VALUE; for (Entry<Operator, StatisticalData> entry : opsMap.entrySet()) { double c = operatorsWeightMap.get(entry.getKey()); sum += c * entry.getValue().getMean(); min = FastMath.min(min, c * entry.getValue().getMin()); max = FastMath.max(max, c * entry.getValue().getMax()); } mean = sum / samples; } // set clock cycles actionWeights.setClockcycles(mean); actionWeights.setClockcyclesMin(min); actionWeights.setClockcyclesMax(max); // set workload actionWeights.setWorkload(sum / networkCost * 100); actorCost += sum; // finally, add it to the list actorWeights.getActionsWeights().add(actionWeights); } // set workload actorWeights.setWorkload(actorCost / networkCost * 100); // finally, add it to the list netWeights.getActorsWeights().add(actorWeights); } return netWeights; } private double estimateNetworkCost(ProfilingData data) { EMap<Operator, StatisticalData> opsMap = new ProfilingDataAnalyser(data).getSumExecData() .getOperatorsCall(); int samples = opsMap.size(); double sum = 0; if (samples > 0) { for (Entry<Operator, StatisticalData> entry : opsMap.entrySet()) { double c = operatorsWeightMap.get(entry.getKey()); sum += c * entry.getValue().getMean(); } } return sum; } public void setOperatorCost(Operator op, double cost) { cost = cost > 0 ? cost : 0; operatorsWeightMap.put(op, cost); } public void setOperatorCosts(Map<Operator, Double> costsMap) { for (Entry<Operator, Double> entry : costsMap.entrySet()) { setOperatorCost(entry.getKey(), entry.getValue()); } } }