co.turnus.analysis.profiler.orcc.dynamic.OrccDynamicProfilerOptions.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.profiler.orcc.dynamic.OrccDynamicProfilerOptions.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.orcc.dynamic;

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.SIMULATOR_NAME;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.TRACE_COMPRESS;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.TRACE_PROJECT_NAME;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.VERBOSE;
import static co.turnus.analysis.profiler.DynamicProfilerOptions.VERSIONER;
import static co.turnus.analysis.profiler.orcc.dynamic.OrccDynamicProfiler.NAME;
import static co.turnus.gantt.GanttChartOptions.VCD_FILE;

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

import org.apache.commons.cli.CommandLine;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.eclipse.core.resources.IProject;
import org.eclipse.debug.core.ILaunchConfiguration;

import co.turnus.TurnusExtension;
import co.turnus.TurnusRuntimeException;
import co.turnus.analysis.profiler.orcc.dynamic.scheduler.OrccScheduler;
import co.turnus.analysis.profiler.orcc.dynamic.scheduler.RoundRobinScheduler;
import co.turnus.analysis.profiler.orcc.util.OrccFifoMapping;
import co.turnus.util.EcoreHelper;
import co.turnus.util.TurnusUtils;
import co.turnus.versioning.impl.GitVersioner;

public class OrccDynamicProfilerOptions {

    /**
     * This is a String containing the name of {@link OrccScheduler} used for
     * the simulation
     */
    public static final String ORCC_SCHEDULER = "co.turnus.analysis.profiler.orcc.dynamic.scheduler";

    /** This is a String containing the {@link IProject} name of the network */
    public static final String ORCC_PROJECT = "co.turnus.analysis.profiler.orcc.dynamic.project";

    /** This is a String containing the XDF file name of the simulated network */
    public static final String ORCC_XDF = "co.turnus.analysis.profiler.orcc.dynamic.xdf";

    /** This is a Boolean defining if a Orcc Source actor is used */
    public static final String ORCC_SOURCE_USE = "co.turnus.analysis.profiler.orcc.dynamic.source.use";

    /**
     * This is a String containing the file path of the input stimulus used by
     * the Orcc source actor
     */
    public static final String ORCC_SOURCE_FILE = "co.turnus.analysis.profiler.orcc.dynamic.source.file";

    /** This is a Boolean defining if native ports resizing is enabled */
    public static final String TYPE_RESIZE_NATIVEPORTS = "co.turnus.analysis.profiler.orcc.dynamic.resize.ports";

    /** This is a Boolean defining if type resize 32 bit is enabled */
    public static final String TYPE_RESIZE_TO32BITS = "co.turnus.analysis.profiler.orcc.dynamic.resize.32bits";

    /** This is a Boolean defining if type resize to 2n bit is enabled */
    public static final String TYPE_RESIZE_TONBITS = "co.turnus.analysis.profiler.orcc.dynamic.resize.2nbits";

    /** This is a Boolean defining if the IR constant folding transfo is enabled */
    public static final String TRANSFO_CONSTANTFOLDING = "co.turnus.analysis.profiler.orcc.dynamic.transfo.cfolding";

    /**
     * This is a Map<String,String> that contains the fifo size mapping
     * generated by the GUI. The FIFO key are generated using the
     * {@link OrccFifoMapping#getKey(net.sf.orcc.df.Connection)} method.
     */
    public static final String ORCC_FIFO_MAPPING = "co.turnus.analysis.profiler.orcc.dynamic.fifo.mapping";

    /** This is a Boolean defining if the stack-protection is enabled */
    public static final String STACK_PROTECTION = "co.turnus.analysis.profiler.orcc.dynamic.interpreter.stackProtection";

    /** This is a Boolean defining if the IR constant prop. transfo is enabled */
    public static final String TRANSFO_CONSTANTPROPAGATION = "co.turnus.analysis.profiler.orcc.dynamic.transfo.cpropagation";

    /** This is a Boolean defining if the IR dead actions transfo is enabled */
    public static final String TRANSFO_DEADACTIONS = "co.turnus.analysis.profiler.orcc.dynamic.transfo.deadactions";

    /** This is a Boolean defining if the IR deadcode transfo is enabled */
    public static final String TRANSFO_DEADCODE = "co.turnus.analysis.profiler.orcc.dynamic.transfo.deadcode";

    /** This is a Boolean defining if the IR expr. evaluation transfo is enabled */
    public static final String TRANSFO_EXPREVAL = "co.turnus.analysis.profiler.orcc.dynamic.transfo.expressioneval";

    /** This is a Boolean defining if the IR var initializer transfo is enabled */
    public static final String TRANSFO_VARINIT = "co.turnus.analysis.profiler.orcc.dynamic.transfo.varinitializer";

    /** This is a String containing the user custom output path */
    public static final String OUTPUT_PATH_CUSTOM_VALUE = "co.turnus.analysis.profiler.orcc.dynamic.outputPath.custom";

    /** This is a Boolean indicating if the output path is defined by the user */
    public static final String OUTPUT_PATH_CUSTOM_USE = "co.turnus.analysis.profiler.orcc.dynamic.outputPath.custom.use";

    /** This is a String containing some users notes about the simulation */
    public static final String NOTES = "co.turnus.analysis.profiler.orcc.dynamic.notes";

    public static Configuration getConfiguration(CommandLine cli) {
        try {
            Configuration conf = new BaseConfiguration();
            conf.setProperty(VERBOSE, cli.hasOption("v"));

            conf.setProperty(SIMULATOR_NAME, NAME);

            String stmp = cli.getOptionValue("p", "");
            conf.setProperty(ORCC_PROJECT, stmp);

            stmp = cli.getOptionValue("x", "");
            conf.setProperty(ORCC_XDF, stmp);

            Boolean btmp = cli.hasOption("i");
            if (btmp) {
                stmp = cli.getOptionValue("i", "");
                conf.setProperty(ORCC_SOURCE_USE, !stmp.isEmpty());
                conf.setProperty(ORCC_SOURCE_FILE, stmp);
            } else {
                conf.setProperty(ORCC_SOURCE_USE, false);
                conf.setProperty(ORCC_SOURCE_FILE, "");
            }

            btmp = cli.hasOption("t");
            if (btmp) {
                stmp = cli.getOptionValue("t", "");
                conf.setProperty(CREATE_TRACE_PROJECT, !stmp.isEmpty());
                conf.setProperty(TRACE_PROJECT_NAME, stmp);
            } else {
                conf.setProperty(CREATE_TRACE_PROJECT, false);
                conf.setProperty(TRACE_PROJECT_NAME, "");
            }

            btmp = cli.hasOption("stack-protector");
            conf.setProperty(STACK_PROTECTION, btmp);

            btmp = cli.hasOption("z");
            conf.setProperty(TRACE_COMPRESS, btmp);

            btmp = cli.hasOption("prof");
            conf.setProperty(EXPORT_PROFILING, btmp);

            stmp = cli.getOptionValue("versioner", GitVersioner.NAME);
            conf.setProperty(VERSIONER, stmp);

            stmp = cli.getOptionValue("scheduler", RoundRobinScheduler.NAME);
            conf.setProperty(ORCC_SCHEDULER, stmp);

            boolean resize[] = { false, false, false };
            if (cli.hasOption("resizer")) {
                String[] buff = cli.getOptionValues("resizer");
                for (int i = 0; i < resize.length; i++) {
                    resize[i] = Boolean.parseBoolean(buff[i]);
                }
            }

            conf.setProperty(TYPE_RESIZE_NATIVEPORTS, resize[0]);
            conf.setProperty(TYPE_RESIZE_TO32BITS, resize[1]);
            conf.setProperty(TYPE_RESIZE_TONBITS, resize[2]);

            boolean transfo[] = { false, false, false, false, false, false };
            if (cli.hasOption("transfo")) {
                String[] buff = cli.getOptionValues("transfo");
                for (int i = 0; i < transfo.length; i++) {
                    transfo[i] = Boolean.parseBoolean(buff[i]);
                }
            }
            conf.setProperty(TRANSFO_CONSTANTFOLDING, transfo[0]);
            conf.setProperty(TRANSFO_CONSTANTPROPAGATION, transfo[1]);
            conf.setProperty(TRANSFO_DEADACTIONS, transfo[2]);
            conf.setProperty(TRANSFO_EXPREVAL, transfo[3]);
            conf.setProperty(TRANSFO_DEADCODE, transfo[4]);
            conf.setProperty(TRANSFO_VARINIT, transfo[5]);

            int itmp = Integer.parseInt(cli.getOptionValue("b", "512"));
            conf.setProperty(FIFO_DEFAULT, itmp);

            // set the output path
            stmp = cli.getOptionValue("o", "");
            setOutputPath(conf, stmp);

            // set the gantt chart options
            btmp = cli.hasOption("gantt");
            conf.setProperty(EXPORT_GANTT, btmp);
            if (btmp) {
                stmp = conf.getString(OUTPUT_PATH);
                stmp = new File(stmp, "ganttChart." + TurnusExtension.VCD).getAbsolutePath();
                conf.setProperty(VCD_FILE, stmp);
            }

            return conf;

        } catch (Exception e) {
            throw new TurnusRuntimeException("Error parsing the launch configuration options", e);
        }
    }

    public static Configuration getConfiguration(ILaunchConfiguration configuration, String mode) {
        Configuration conf = new BaseConfiguration();

        try {

            conf.setProperty(SIMULATOR_NAME, NAME);

            conf.setProperty(VERBOSE, mode.equals("debug"));

            String stmp = configuration.getAttribute(ORCC_PROJECT, "");
            conf.setProperty(ORCC_PROJECT, stmp);

            stmp = configuration.getAttribute(ORCC_XDF, "");
            conf.setProperty(ORCC_XDF, stmp);

            Boolean btmp = configuration.getAttribute(ORCC_SOURCE_USE, false);
            conf.setProperty(ORCC_SOURCE_USE, btmp);

            stmp = configuration.getAttribute(ORCC_SOURCE_FILE, "");
            conf.setProperty(ORCC_SOURCE_FILE, stmp);

            btmp = configuration.getAttribute(CREATE_TRACE_PROJECT, false);
            conf.setProperty(CREATE_TRACE_PROJECT, btmp);

            stmp = configuration.getAttribute(TRACE_PROJECT_NAME, "");
            conf.setProperty(TRACE_PROJECT_NAME, stmp);

            btmp = configuration.getAttribute(TRACE_COMPRESS, false);
            conf.setProperty(TRACE_COMPRESS, btmp);

            btmp = configuration.getAttribute(EXPORT_PROFILING, false);
            conf.setProperty(EXPORT_PROFILING, btmp);

            stmp = configuration.getAttribute(VERSIONER, "");
            conf.setProperty(VERSIONER, stmp);

            stmp = configuration.getAttribute(ORCC_SCHEDULER, "");
            conf.setProperty(ORCC_SCHEDULER, stmp);

            btmp = configuration.getAttribute(TYPE_RESIZE_NATIVEPORTS, false);
            conf.setProperty(TYPE_RESIZE_NATIVEPORTS, btmp);
            btmp = configuration.getAttribute(TYPE_RESIZE_TO32BITS, false);
            conf.setProperty(TYPE_RESIZE_TO32BITS, btmp);
            btmp = configuration.getAttribute(TYPE_RESIZE_TONBITS, false);
            conf.setProperty(TYPE_RESIZE_TONBITS, btmp);

            btmp = configuration.getAttribute(TRANSFO_CONSTANTFOLDING, false);
            conf.setProperty(TRANSFO_CONSTANTFOLDING, btmp);
            btmp = configuration.getAttribute(TRANSFO_CONSTANTPROPAGATION, false);
            conf.setProperty(TRANSFO_CONSTANTPROPAGATION, btmp);
            btmp = configuration.getAttribute(TRANSFO_DEADACTIONS, false);
            conf.setProperty(TRANSFO_DEADACTIONS, btmp);
            btmp = configuration.getAttribute(TRANSFO_EXPREVAL, false);
            conf.setProperty(TRANSFO_EXPREVAL, btmp);
            btmp = configuration.getAttribute(TRANSFO_DEADCODE, false);
            conf.setProperty(TRANSFO_DEADCODE, btmp);
            btmp = configuration.getAttribute(TRANSFO_VARINIT, false);
            conf.setProperty(TRANSFO_VARINIT, btmp);

            int itmp = Integer.parseInt(configuration.getAttribute(FIFO_DEFAULT, "512"));
            conf.setProperty(FIFO_DEFAULT, itmp);

            btmp = configuration.getAttribute(STACK_PROTECTION, true);
            conf.setProperty(STACK_PROTECTION, btmp);

            // parse the FIFO size mapping
            Map<String, String> tmpMap = configuration.getAttribute(ORCC_FIFO_MAPPING,
                    new HashMap<String, String>());
            Map<String, Integer> fifoMapping = new HashMap<String, Integer>();
            for (Entry<String, String> entry : tmpMap.entrySet()) {
                fifoMapping.put(entry.getKey(), Integer.parseInt(entry.getValue()));
            }
            conf.setProperty(ORCC_FIFO_MAPPING, fifoMapping);

            // store the users notes
            stmp = configuration.getAttribute(NOTES, "");
            conf.setProperty(NOTES, stmp);

            // set the output path
            stmp = configuration.getAttribute(OUTPUT_PATH_CUSTOM_USE, false)
                    ? configuration.getAttribute(OUTPUT_PATH_CUSTOM_VALUE, "")
                    : "";
            setOutputPath(conf, stmp);

            // set the export gantt chart options
            btmp = configuration.getAttribute(EXPORT_GANTT, false);
            conf.setProperty(EXPORT_GANTT, btmp);
            if (btmp) {
                stmp = conf.getString(OUTPUT_PATH);
                stmp = new File(stmp, "ganttChart." + TurnusExtension.VCD).getAbsolutePath();
                conf.setProperty(VCD_FILE, stmp);
            }

            return conf;

        } catch (Exception e) {
            throw new TurnusRuntimeException("Error parsing the launch configuration options", e);
        }
    }

    private static void setOutputPath(Configuration conf, String customPath) {
        // store the root path for the output.
        File outPath = null;
        boolean traceProject = conf.getBoolean(CREATE_TRACE_PROJECT);
        if (customPath != null && !customPath.isEmpty()) {
            outPath = new File(customPath);
        } else {
            String project = conf.getString(ORCC_PROJECT);
            IProject pojo = EcoreHelper.getProject(project);
            outPath = pojo.getRawLocation().makeAbsolute().toFile();
            outPath = new File(outPath, "turnus");

            if (traceProject) {
                outPath = new File(outPath, "traces");
            } else {
                outPath = new File(outPath, "profiling");
            }
        }

        if (traceProject) {
            String trace = conf.getString(TRACE_PROJECT_NAME);

            // make path os friendly...
            trace = trace.replace(" ", "_");

            File tmpPath = new File(outPath, trace);
            if (tmpPath.exists()) {
                tmpPath = TurnusUtils.findNextAvailable(outPath, trace, "_v", 2);
            }
            outPath = tmpPath;
        }

        conf.setProperty(OUTPUT_PATH, outPath.getAbsolutePath());

    }

}