co.turnus.analysis.profiler.SerialDynamicProfiler.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.profiler.SerialDynamicProfiler.java

Source

/* 
 * 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.analysis.profiler;

import static co.turnus.analysis.profiler.DynamicProfilerOptions.CREATE_TRACE_PROJECT;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.EXPORT_GANTT;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.EXPORT_PROFILING;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.FIFO_DEFAULT;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.OUTPUT_PATH;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.TRACE_COMPRESS;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import co.turnus.TurnusExtension;
import co.turnus.analysis.profiler.dynamic.ActorDataBox;
import co.turnus.analysis.profiler.dynamic.FifoDataBox;
import co.turnus.analysis.profiler.dynamic.GarbageDataBox;
import co.turnus.analysis.profiler.dynamic.ProfiledStep;
import co.turnus.analysis.profiler.dynamic.StepDataBox;
import co.turnus.analysis.profiler.dynamic.TraceWriter;
import co.turnus.common.Operator;
import co.turnus.dataflow.Action;
import co.turnus.dataflow.Actor;
import co.turnus.dataflow.Fifo;
import co.turnus.dataflow.Network;
import co.turnus.dataflow.Procedure;
import co.turnus.dataflow.StateVariable;
import co.turnus.gantt.GanttChartBuilder;
import co.turnus.generic.AbstractConfigurable;
import co.turnus.profiling.ProfilingData;
import co.turnus.profiling.ProfilingFactory;
import co.turnus.trace.Step;
import co.turnus.trace.TraceProject;
import co.turnus.util.EcoreHelper;

public class SerialDynamicProfiler extends AbstractConfigurable {

    private Map<Actor, ActorDataBox> actorDataMap;

    private Map<Fifo, FifoDataBox> fifoDataMap;

    private long firings;
    private GanttChartBuilder ganttBuilder;
    private Network network;

    private StepDataBox stepData;
    private TraceWriter traceWriter;

    private boolean exportTrace;
    private boolean exportProfiling;
    private boolean exportGantt;

    public void setConfiguration(Configuration configuration) {
        super.setConfiguration(configuration);

        actorDataMap = new HashMap<Actor, ActorDataBox>();
        for (Actor actor : network.getActors()) {
            actorDataMap.put(actor, new ActorDataBox(actor));
        }

        fifoDataMap = new HashMap<Fifo, FifoDataBox>();
        int size = getOption(FIFO_DEFAULT);// FIXME add mapping file
        for (Fifo fifo : network.getFifos()) {
            fifoDataMap.put(fifo, new FifoDataBox(fifo, size));
        }

        exportTrace = getOption(CREATE_TRACE_PROJECT, false);
        exportProfiling = exportTrace || getOption(EXPORT_PROFILING, false);
        exportGantt = getOption(EXPORT_GANTT, false);

        if (exportProfiling || exportGantt) {

            File outDir = new File(getOption(OUTPUT_PATH, ""));

            try {
                FileUtils.forceMkdir(outDir);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (exportTrace) {
                // create the trace project
                String pojo = getOption(TRACE_PROJECT_NAME);
                new TraceProject(pojo, network.getName(), outDir).save();

                // create the file name according to the options and launch the
                // trace file writer
                boolean compress = getOption(TRACE_COMPRESS);
                String ext = compress ? TurnusExtension.TRACE_COMPRESSED : TurnusExtension.TRACE;
                File traceFile = new File(outDir, network.getName() + "." + ext);
                traceWriter = new TraceWriter(traceFile);
                traceWriter.start();
            }

            if (exportGantt) {
                ganttBuilder = new GanttChartBuilder(network, getOption(SIMULATOR_NAME, ""));
                ganttBuilder.setConfiguration(configuration);
            }
        }
    }

    public SerialDynamicProfiler(Network network) {
        this.network = network;
        firings = 0;
        stepData = GarbageDataBox.getGarbage();

    }

    private ProfilingData buildProfilingData() {
        ProfilingFactory factory = ProfilingFactory.eINSTANCE;
        ProfilingData data = factory.createProfilingData();
        data.setNetwork(network);

        for (ActorDataBox actorData : actorDataMap.values()) {
            data.getActorsData().add(actorData.buildProfilingData());
        }

        for (FifoDataBox fifoData : fifoDataMap.values()) {
            data.getFifosData().add(fifoData.buildProfilingData());
        }

        return data;
    }

    public void endFiring() {
        ProfiledStep step = stepData.getStep();
        ActorDataBox actorData = actorDataMap.get(step.getActor(Actor.class));

        if (exportTrace) {
            actorData.buildDependencies(stepData);
            traceWriter.write(step);
        }

        if (exportProfiling) {
            // collect actor/action profiling data
            actorData.collectData(stepData);

            // collect FIFOs profiling data
            for (Fifo fifo : stepData.getUsedFifos()) {
                FifoDataBox fData = fifoDataMap.get(fifo);
                fData.collectData(stepData);
            }
        }

        if (exportGantt) {
            logFifoStatus();
            // use the firing number as an abstract clock-cycle
            ganttBuilder.startNewCycle(firings + 1);
        }

        firings++;
        stepData = GarbageDataBox.getGarbage();
    }

    public void execute(Operator operator) {
        stepData.execute(operator);
    }

    public void execute(Procedure procedure) {
        stepData.execute(procedure);
    }

    public long getFirings() {
        return firings;
    }

    private void logFifoStatus() {
        // log the fifo status
        for (Fifo fifo : network.getFifos()) {
            FifoDataBox res = fifoDataMap.get(fifo);
            String status = res.isFull() ? "FULL" : res.isEmpty() ? "empty" : "nonempty";
            ganttBuilder.log(fifo, status, res.getAvailableTokens());
        }

    }

    public void peek(Fifo fifo, Action action) {
        fifoDataMap.get(fifo).peek(action);
    }

    public void read(Fifo fifo, Object value) {
        Step producer = fifoDataMap.get(fifo).removeTokenProducer();
        stepData.read(fifo, producer);
    }

    public void read(StateVariable variable, Object value) {
        stepData.read(variable);
    }

    public void readMiss(Fifo fifo, Action action) {
        fifoDataMap.get(fifo).readMiss(action);
    }

    public long startFiring(Actor actor, Action action, boolean scheduledByFsm) {
        ProfiledStep step = new ProfiledStep(firings, actor, action);
        stepData = new StepDataBox(step, scheduledByFsm);

        if (exportGantt) {
            ganttBuilder.log(actor, "FIRING");
            ganttBuilder.log(actor, action, "FIRING", firings);
        }

        return firings;
    }

    public void stop() {

        if (exportTrace) {
            traceWriter.stop();
        }

        if (exportProfiling) {
            ProfilingData data = buildProfilingData();

            ResourceSet resources = new ResourceSetImpl();
            File outDir = new File(getOption(OUTPUT_PATH, ""));
            File nFile = new File(outDir, network.getName() + "." + TurnusExtension.NETWORK);
            File dFile = new File(outDir, network.getName() + "." + TurnusExtension.PROFILING);

            EcoreHelper.storeEObject(network, resources, nFile);
            EcoreHelper.storeEObject(data, resources, dFile);
        }

        if (exportGantt) {
            logFifoStatus();
            ganttBuilder.end(firings);
        }
    }

    public void write(Fifo fifo, Object value) {
        fifoDataMap.get(fifo).addTokenProducer(stepData.getStep());
        stepData.write(fifo);
    }

    public void write(StateVariable variable, Object value) {
        stepData.write(variable);
    }

    public void writeMiss(Fifo fifo, Action action) {
        fifoDataMap.get(fifo).writeMiss(action);
    }

}