<commit_before>package com.martensigwart.fakeload;

import com.sun.management.OperatingSystemMXBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.util.Collections;
import java.util.List;

/**
 * This class acts as the controlling entity of the simulation infrastructure.
 *
 * When the load simulation infrastructure is currently active this class runs as a separate thread to monitor the
 * actual load generated by the simulator threads. Whenever a significant deviation between target and actual load is
 * detected, the class adjusts the load slightly in the direction of target. This way the load generated by the
 * simulation actually reaches the desired level.
 *
 * The {@code SimulationControl} retrieves all load changes via a shared instance of type
 * {@link SystemLoad}. These changes are then propagated to the respective simulator threads.
 *
 * @author <NAME>
 * @since 1.8
 */
public final class SimulationControl implements Runnable {

    private static final Logger log = LoggerFactory.getLogger(SimulationControl.class);
    private static final OperatingSystemMXBean operatingSystem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    private static final int SLEEP_PERIOD = 2000;


    private final SystemLoad systemLoad;
    private final List<CpuSimulator> cpuSimulators;
    private final MemorySimulator memorySimulator;
    private final Object lock;

    SimulationControl(List<CpuSimulator> cpuSimulators, MemorySimulator memorySimulator) {
        this.systemLoad = new SystemLoad();
        this.cpuSimulators = Collections.unmodifiableList(cpuSimulators);
        this.memorySimulator = memorySimulator;
        this.lock = new Object();
    }


    @Override
    public void run() {
        log.trace("Started");

        boolean running = true;
        long lastCpu = 0L;
        while(running) {
            try {
                synchronized (lock) {
                    while (systemLoad.getCpu() == 0) {
                        log.trace("SimulationControl - Waiting...");
                        lock.wait();
                        log.trace("SimulationControl - Woke Up");
                    }
                }

                long actualCpu = (long)(operatingSystem.getProcessCpuLoad() * 100);
                if (Math.abs(lastCpu-actualCpu) > 5) {
                    lastCpu = actualCpu;
                } else {
                    //TODO increase or decrease CPU accordingly
                }
                Thread.sleep(SLEEP_PERIOD);
            } catch (InterruptedException e) {
                log.warn("SimulationControl was interrupted");
                running = false;
            }
        }
    }


    public void increaseSystemLoadBy(FakeLoad load) throws MaximumLoadExceededException {
        systemLoad.increaseBy(load);

        for (CpuSimulator cpuSim: cpuSimulators) {
            cpuSim.setLoad(systemLoad.getCpu());
        }

        synchronized (lock) {
            lock.notify();
        }

        memorySimulator.setLoad(systemLoad.getMemory());
        //TODO propagate changes to simulators
    }<commit_msg>changed log messages<commit_after>package com.martensigwart.fakeload;

import com.sun.management.OperatingSystemMXBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.util.Collections;
import java.util.List;

/**
 * This class acts as the controlling entity of the simulation infrastructure.
 *
 * When the load simulation infrastructure is currently active this class runs as a separate thread to monitor the
 * actual load generated by the simulator threads. Whenever a significant deviation between target and actual load is
 * detected, the class adjusts the load slightly in the direction of target. This way the load generated by the
 * simulation actually reaches the desired level.
 *
 * The {@code SimulationControl} retrieves all load changes via a shared instance of type
 * {@link SystemLoad}. These changes are then propagated to the respective simulator threads.
 *
 * @author <NAME>
 * @since 1.8
 */
public final class SimulationControl implements Runnable {

    private static final Logger log = LoggerFactory.getLogger(SimulationControl.class);
    private static final OperatingSystemMXBean operatingSystem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    private static final int SLEEP_PERIOD = 2000;


    private final SystemLoad systemLoad;
    private final List<CpuSimulator> cpuSimulators;
    private final MemorySimulator memorySimulator;
    private final Object lock;

    SimulationControl(List<CpuSimulator> cpuSimulators, MemorySimulator memorySimulator) {
        this.systemLoad = new SystemLoad();
        this.cpuSimulators = Collections.unmodifiableList(cpuSimulators);
        this.memorySimulator = memorySimulator;
        this.lock = new Object();
    }


    @Override
    public void run() {
        log.trace("SimulationControl - Started");

        boolean running = true;
        long lastCpu = 0L;
        while(running) {
            try {
                synchronized (lock) {
                    while (systemLoad.getCpu() == 0) {
                        log.trace("SimulationControl - Waiting...");
                        lock.wait();
                        log.trace("SimulationControl - Woke Up");
                    }
                }

                long actualCpu = (long)(operatingSystem.getProcessCpuLoad() * 100);
                if (Math.abs(lastCpu-actualCpu) > 5) {
                    lastCpu = actualCpu;
                } else {
                    //TODO increase or decrease CPU accordingly
                }
                Thread.sleep(SLEEP_PERIOD);
            } catch (InterruptedException e) {
                log.warn("SimulationControl - Interrupted");
                running = false;
            }
        }
    }


    public void increaseSystemLoadBy(FakeLoad load) throws MaximumLoadExceededException {
        systemLoad.increaseBy(load);

        for (CpuSimulator cpuSim: cpuSimulators) {
            cpuSim.setLoad(systemLoad.getCpu());
        }

        synchronized (lock) {
            lock.notify();
        }

        memorySimulator.setLoad(systemLoad.getMemory());
        //TODO propagate changes to simulators
    }
{'fraction_non_alphanumeric': 0.07592563661927824, 'fraction_numerical': 0.004374316513044837, 'mean_word_length': 2.462412114656571, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1563976', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1566, 'n_words': 480}