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

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.profiler.orcc.dynamic.OrccDynamicProfilerApplication.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.VERBOSE;
import net.sf.orcc.util.OrccLogger;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.configuration.Configuration;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class OrccDynamicProfilerApplication implements IApplication {

    private Options cliOptions;

    private boolean isAutoBuildActivated;

    private IWorkspace workspace;

    @SuppressWarnings("static-access")
    public OrccDynamicProfilerApplication() {
        workspace = ResourcesPlugin.getWorkspace();

        cliOptions = new Options();

        {// create all the options
         // verbosity
            cliOptions.addOption("v", false, "enable verbose mode");

            // help console message
            cliOptions.addOption("h", false, "print the help message");

            // Orcc CAL Project name
            Option o = OptionBuilder.withArgName("name").hasArg().withType(String.class)
                    .withDescription("Orcc CAL project name").create("p");
            o.isRequired();
            cliOptions.addOption(o);

            // XDF file name
            o = OptionBuilder.withArgName("name").hasArg().withType(String.class)
                    .withDescription("XDF top project network").create("x");
            o.isRequired();
            cliOptions.addOption(o);

            // input stimulus file file
            o = OptionBuilder.withArgName("file").hasArg().withType(String.class)
                    .withDescription("input stimulus file").create("i");
            cliOptions.addOption(o);

            // input stimulus file file
            o = OptionBuilder.withArgName("value").hasArg().withType(Integer.class)
                    .withDescription("default buffer size").create("b");
            cliOptions.addOption(o);

            // trace project name
            o = OptionBuilder.withArgName("name").hasArg().withType(String.class)
                    .withDescription("generate a trace project with the given name").create("t");
            cliOptions.addOption(o);

            // output path
            o = OptionBuilder.withArgName("path").hasArg().withType(String.class)
                    .withDescription("define the output path").create("o");
            o.isRequired();
            cliOptions.addOption(o);

            // compress the execution trace
            cliOptions.addOption("z", false, "compress the execution trace graph file");

            // store profiling data
            cliOptions.addOption("prof", false,
                    "store profiling data. " + "(already enabled when the trace project is required)");

            // store execution gantt chart
            cliOptions.addOption("gantt", false, "store the execution gantt chart");

            // versioner name
            o = OptionBuilder.withArgName("name").hasArg().withType(String.class).withDescription("versioner name")
                    .create("versioner");
            cliOptions.addOption(o);

            // scheduler name
            o = OptionBuilder.withArgName("name").hasArg().withType(String.class).withDescription("scheduler name")
                    .create("scheduler");
            cliOptions.addOption(o);

            // type resize transformations
            o = OptionBuilder.withArgName("a,b,c").hasArgs(3).withType(Boolean.class)
                    .withDescription("select the type resize transformations to use (true/false). \n"
                            + "a: native ports\n" + "b: to 32 bit\n" + "c: to n bit")
                    .create("resizer");
            o.setValueSeparator(',');
            cliOptions.addOption(o);

            // stack protection
            cliOptions.addOption("stack-protector", false, "enable stack-protection for arrays load");

            // code transformations
            o = OptionBuilder.withArgName("a,b,c,d,e,f").hasArgs(6).withType(Boolean.class)
                    .withDescription("select the code transformations to use (true/false). \n"
                            + "a: constant folding\n" + "b: constant propagation\n" + "c: dead actions\n"
                            + "d: expression evaluation\n" + "e: dead code\n" + "f: variable initializer")
                    .create("transfo");
            o.setValueSeparator(',');
            cliOptions.addOption(o);
        }

    }

    private void disableAutoBuild() throws CoreException {
        IWorkspaceDescription desc = workspace.getDescription();
        if (desc.isAutoBuilding()) {
            isAutoBuildActivated = true;
            desc.setAutoBuilding(false);
            workspace.setDescription(desc);
        }
    }

    private void restoreAutoBuild() throws CoreException {
        if (isAutoBuildActivated) {
            IWorkspaceDescription desc = workspace.getDescription();
            desc.setAutoBuilding(true);
            workspace.setDescription(desc);
        }
    }

    @Override
    public Object start(IApplicationContext context) throws Exception {

        CommandLineParser parser = new PosixParser();
        CommandLine commandLine = parser.parse(cliOptions,
                (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS));

        if (commandLine.hasOption('h')) {
            printUsage();
            return IApplication.EXIT_RELAUNCH;
        }

        try {
            Configuration configuration = OrccDynamicProfilerOptions.getConfiguration(commandLine);

            disableAutoBuild();

            // set the log level
            boolean verbose = configuration.getBoolean(VERBOSE, false);
            OrccLogger.setLevel(verbose ? OrccLogger.ALL : OrccLogger.TRACE);

            // build, configure and launch the profiler
            OrccDynamicProfiler profiler = new OrccDynamicProfiler();
            profiler.setConfiguration(configuration);
            profiler.start();

        } catch (CoreException ce) {
            OrccLogger.severeln("Unable to set the workspace properties.");
            restoreAutoBuild();
            return IApplication.EXIT_RELAUNCH;
        } catch (Exception e) {
            OrccLogger.severeln("Something went wrong: " + e.getLocalizedMessage());
            restoreAutoBuild();
            return IApplication.EXIT_RELAUNCH;
        } finally {
            restoreAutoBuild();
        }

        return IApplication.EXIT_OK;
    }

    private void printUsage() {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.setWidth(100);
        helpFormatter.printHelp(OrccDynamicProfilerApplication.class.getSimpleName(), cliOptions);
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub

    }

}