<commit_before>package org.trustsoft.slastic.simulation.model.hardware.controller.cpu;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.trustsoft.slastic.simulation.config.Constants;

import desmoj.core.simulator.Model;
import desmoj.core.simulator.Queue;
import desmoj.core.simulator.QueueBased;
import desmoj.core.simulator.SimTime;

public class CPURRScheduler extends CPUScheduler {

	private static Log log = LogFactory.getLog(CPURRScheduler.class);

	private final Queue<CPUSchedulableProcess> activeProcess;

	private long cyclesPerSlice;

	private boolean idle = true;

	public CPURRScheduler(final Model model, final String name) {
		super(model, name, new Queue<CPUSchedulableProcess>(model, name
				+ "JobQueue", Constants.DEBUG, Constants.DEBUG));
		this.activeProcess = new Queue<CPUSchedulableProcess>(model, name,
				QueueBased.FIFO, 1, Constants.DEBUG, Constants.DEBUG);
	}

	@Override
	public synchronized void schedule(final CPUSchedulableProcess process) {
		this.queue.insert(process);
		this.cyclesPerSlice = (Constants.PS_SLICE * Constants.CPU_SCALE * this
				.getOwner().getCapacity()) / 1000;
		CPURRScheduler.log.info("Task arrived: " + process);
		if (this.idle) {
			CPURRScheduler.log.info("Resuming scheduler with tick at "
					+ " with " + this.cyclesPerSlice + " cycles per slice");
			super.getTickEventGenerator().resume();
		}

	}

	@Override
	public synchronized SimTime tick() {
		SimTime ret = null;
		CPURRScheduler.log.info("Got " + this.queue.length() + " processes");
		/**
		 * new algo:<br />
		 * 1. remove active process ap and subtract processing time<br />
		 * 2. if ap.processingtime >0 readd to queue<br />
		 * 3. get next process np from queue<br />
		 * if np!=0 <br />
		 * 4a calc max processing time for np<br />
		 * 4b make np active<br />
		 * 4c schedule event<br />
		 * else<br />
		 * 5a dont schedule event, mark ap finished<br />
		 */
		final CPUSchedulableProcess ap = this.activeProcess.first();
		CPURRScheduler.log.info("Removing active process " + ap);
		if (ap != null) {
			ap.substractFromRemaining(this.cyclesPerSlice);
			CPURRScheduler.log.info(ap + " needs " + ap.getCyclesRemaining()
					+ " cycles to finish");
			this.activeProcess.remove(ap);
			if (ap.getCyclesRemaining() > 0) {
				this.queue.insert(ap);
			}
		}
		final CPUSchedulableProcess np = this.queue.first();
		if (np != null) {
			CPURRScheduler.log.info("Activating process " + np + " with "
					+ np.getCyclesRemaining() + " remaining cycles");
			this.queue.remove(np);
			this.activeProcess.insert(np);
			final long prun = np.getCyclesRemaining();
			if (prun > this.cyclesPerSlice) {
				ret = this.getTickSimTime();
				this.queue.insert(np);
				CPURRScheduler.log.info("Next slice will be started at " + ret);
			} else {
				ret = new SimTime(
						prun
								/ (double) (this.getOwner().getCapacity() * Constants.CPU_SCALE)); // MHz
				// => divide by 1Mega
				CPURRScheduler.log.info("Finishing job in " + ret);
			}
		}
		if (ret == null) {
			this.idle = true;
		} else {
			this.idle = false;
		}
		return ret;
	}

	@Override
	public Queue<CPUSchedulableProcess> getQueue() {
		return this.queue;
	}

	@Override
	public boolean isIdle() {
		return this.queue.isEmpty() && this.activeProcess.isEmpty();
	}

	@Override
	public float getBusiness() {
		return (float) this.activeProcess.averageLength();
	}

	@Override<commit_msg>added probe to RR scheduler<commit_after>package org.trustsoft.slastic.simulation.model.hardware.controller.cpu;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.trustsoft.slastic.simulation.config.Constants;
import org.trustsoft.slastic.simulation.model.hardware.controller.engine.TickEvent;
import org.trustsoft.slastic.simulation.model.hardware.controller.engine.TickEventGenerator;

import desmoj.core.simulator.Model;
import desmoj.core.simulator.Queue;
import desmoj.core.simulator.QueueBased;
import desmoj.core.simulator.SimTime;

public class CPURRScheduler extends CPUScheduler {

	private static Log log = LogFactory.getLog(CPURRScheduler.class);

	private final Queue<CPUSchedulableProcess> activeProcess;
	
	private double utilization;

	private long cyclesPerSlice;

	private boolean idle = true;
	
	private TickEventGenerator utilizationTicker;

	public CPURRScheduler(final Model model, final String name) {
		super(model, name, new Queue<CPUSchedulableProcess>(model, name
				+ "JobQueue", Constants.DEBUG, Constants.DEBUG));
		this.activeProcess = new Queue<CPUSchedulableProcess>(model, name,
				QueueBased.FIFO, 1, Constants.DEBUG, Constants.DEBUG);
		utilizationTicker = new UtilizationProbeEventGenerator(model, name, Constants.DEBUG, this);
	}

	@Override
	public synchronized void schedule(final CPUSchedulableProcess process) {
		this.queue.insert(process);
		this.cyclesPerSlice = (Constants.PS_SLICE * Constants.CPU_SCALE * this
				.getOwner().getCapacity()) / 1000;
		CPURRScheduler.log.info("Task arrived: " + process);
		if (this.idle) {
			CPURRScheduler.log.info("Resuming scheduler with tick at "
					+ " with " + this.cyclesPerSlice + " cycles per slice");
			super.getTickEventGenerator().resume();
		}

	}

	@Override
	public synchronized SimTime tick() {
		SimTime ret = null;
		CPURRScheduler.log.info("Got " + this.queue.length() + " processes");
		/**
		 * new algo:<br />
		 * 1. remove active process ap and subtract processing time<br />
		 * 2. if ap.processingtime >0 readd to queue<br />
		 * 3. get next process np from queue<br />
		 * if np!=0 <br />
		 * 4a calc max processing time for np<br />
		 * 4b make np active<br />
		 * 4c schedule event<br />
		 * else<br />
		 * 5a dont schedule event, mark ap finished<br />
		 */
		final CPUSchedulableProcess ap = this.activeProcess.first();
		CPURRScheduler.log.info("Removing active process " + ap);
		if (ap != null) {
			ap.substractFromRemaining(this.cyclesPerSlice);
			CPURRScheduler.log.info(ap + " needs " + ap.getCyclesRemaining()
					+ " cycles to finish");
			this.activeProcess.remove(ap);
			if (ap.getCyclesRemaining() > 0) {
				this.queue.insert(ap);
			}
		}
		final CPUSchedulableProcess np = this.queue.first();
		if (np != null) {
			CPURRScheduler.log.info("Activating process " + np + " with "
					+ np.getCyclesRemaining() + " remaining cycles");
			this.queue.remove(np);
			this.activeProcess.insert(np);
			final long prun = np.getCyclesRemaining();
			if (prun > this.cyclesPerSlice) {
				ret = this.getTickSimTime();
				this.queue.insert(np);
				CPURRScheduler.log.info("Next slice will be started at " + ret);
			} else {
				ret = new SimTime(
						prun
								/ (double) (this.getOwner().getCapacity() * Constants.CPU_SCALE)); // MHz
				// => divide by 1Mega
				CPURRScheduler.log.info("Finishing job in " + ret);
			}
		}
		if (ret == null) {
			this.idle = true;
		} else {
			this.idle = false;
		}
		return ret;
	}

	@Override
	public Queue<CPUSchedulableProcess> getQueue() {
		return this.queue;
	}

	@Override
	public boolean isIdle() {
		return this.queue.isEmpty() && this.activeProcess.isEmpty();
	}

	@Override
	public double getBusiness() {
		return utilization;
	}
	
	//TODO check API for reset, don't remove processes
	public void recalcUtilization(){
		this.utilization = activeProcess.averageLength();
		activeProcess.reset();
	}

	@Override
{'fraction_non_alphanumeric': 0.12549019607843137, 'fraction_numerical': 0.0043272481406355645, 'mean_word_length': 3.859395532194481, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4503912', 'n_tokens_mistral': 2710, 'n_tokens_neox': 2400, 'n_words': 540}