co.turnus.analysis.profiler.dynamic.FifoDataBox.java Source code

Java tutorial

Introduction

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

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import org.apache.commons.math3.stat.descriptive.SummaryStatistics;

import co.turnus.TurnusRuntimeException;
import co.turnus.common.util.CommonDataUtil;
import co.turnus.dataflow.Action;
import co.turnus.dataflow.Fifo;
import co.turnus.profiling.ExtendComData;
import co.turnus.profiling.FifoProfilingData;
import co.turnus.profiling.ProfilingFactory;
import co.turnus.trace.Step;

import com.google.common.collect.HashMultiset;

public class FifoDataBox {

    private Fifo fifo;
    private int size;

    private ArrayDeque<Step> tokenProducers;

    private SummaryStatistics readTokens;
    private SummaryStatistics writeTokens;
    private SummaryStatistics occupancy;

    private HashMultiset<Action> readMiss;
    private HashMultiset<Action> writeMiss;
    private HashMultiset<Action> peeks;

    private Map<Action, SummaryStatistics> actionReadTokens;
    private Map<Action, SummaryStatistics> actionWriteTokens;

    public FifoDataBox(Fifo fifo, int size) {
        this.fifo = fifo;
        this.size = size;
        tokenProducers = new ArrayDeque<Step>();

        readTokens = new SummaryStatistics();
        writeTokens = new SummaryStatistics();
        occupancy = new SummaryStatistics();

        readMiss = HashMultiset.create();
        writeMiss = HashMultiset.create();
        peeks = HashMultiset.create();

        actionReadTokens = new HashMap<Action, SummaryStatistics>();
        actionWriteTokens = new HashMap<Action, SummaryStatistics>();
    }

    public void addTokenProducer(Step producer) {
        tokenProducers.offer(producer);
    }

    public FifoProfilingData buildProfilingData() {
        ProfilingFactory f = ProfilingFactory.eINSTANCE;

        FifoProfilingData data = f.createFifoProfilingData();
        data.setFifo(fifo);
        data.setSize(size);
        data.setRemainingTokens(tokenProducers.size());

        data.setPeeks(peeks.size());
        data.setReadMiss(readMiss.size());
        data.setWriteMiss(writeMiss.size());

        data.setOccupancy(CommonDataUtil.createFrom(occupancy));
        data.setReadTokens(CommonDataUtil.createFrom(readTokens));
        data.setWriteTokens(CommonDataUtil.createFrom(writeTokens));

        for (Action reader : fifo.getReaders()) {
            ExtendComData actionData = f.createExtendComData();
            data.getReaders().put(reader, actionData);

            actionData.setPeeks(peeks.count(reader));
            actionData.setReadMiss(readMiss.count(reader));

            SummaryStatistics summary = actionReadTokens.get(reader);
            if (summary != null) {
                actionData.setReadTokens(CommonDataUtil.createFrom(summary));
            }
        }

        for (Action writer : fifo.getWriters()) {
            ExtendComData actionData = f.createExtendComData();
            data.getWriters().put(writer, actionData);

            actionData.setWriteMiss(writeMiss.count(writer));

            SummaryStatistics summary = actionWriteTokens.get(writer);
            if (summary != null) {
                actionData.setWriteTokens(CommonDataUtil.createFrom(summary));
            }

        }

        return data;
    }

    public void collectData(StepDataBox stepData) {
        Action action = stepData.getStep().getAction(Action.class);
        int read = stepData.getReadTokens().count(fifo);
        int write = stepData.getWriteTokens().count(fifo);

        if (read > 0) {
            readTokens.addValue(read);

            // lazy creation of the readers map
            SummaryStatistics stat = actionReadTokens.get(action);
            if (stat == null) {
                stat = new SummaryStatistics();
                actionReadTokens.put(action, stat);
            }
            stat.addValue(read);
        }

        if (write > 0) {
            writeTokens.addValue(write);

            // lazy creation of the writers map
            SummaryStatistics stat = actionWriteTokens.get(action);
            if (stat == null) {
                stat = new SummaryStatistics();
                actionWriteTokens.put(action, stat);
            }
            stat.addValue(write);
        }

        if (read != 0 || write != 0) {
            occupancy.addValue(tokenProducers.size());
        }
    }

    public int getAvailableTokens() {
        return tokenProducers.size();
    }

    public boolean isEmpty() {
        return tokenProducers.isEmpty();
    }

    public boolean isFull() {
        return tokenProducers.size() == size;
    }

    public void peek(Action action) {
        peeks.add(action);

    }

    public void readMiss(Action action) {
        readMiss.add(action);

    }

    public Step removeTokenProducer() {
        try {
            return tokenProducers.removeFirst();
        } catch (NoSuchElementException e) {
            throw new TurnusRuntimeException("No token producer for " + fifo);
        }
    }

    public void writeMiss(Action action) {
        writeMiss.add(action);
    }

}