<commit_before>package de.qaware.chronix.server.util;

import com.sun.management.OperatingSystemMXBean;
import de.qaware.chronix.shared.DataModels.Pair;
import de.qaware.chronix.shared.DataModels.Tuple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by mcqueen666 on 19.08.16.
 */
public class DockerStatsUtil {
    private final Logger logger = LoggerFactory.getLogger(DockerStatsUtil.class);
    private final long MEASURE_INTERVAL_MILLISECONDS = 100;
    private final long DOCKER_STATS_REACTION_MILLISECONDS = 1000;
    private final int NUMBER_OF_THREADS = 4;

    private static DockerStatsUtil instance;
    private volatile MeasureRunner[] threads;


    private DockerStatsUtil(){
       // OperatingSystemMXBean oSMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        threads = new MeasureRunner[NUMBER_OF_THREADS];

    }

    public static synchronized DockerStatsUtil getInstance(){
        if(instance == null){
            instance = new DockerStatsUtil();
        }
        return instance;
    }


    /**
     * Starts a threaded docker stats measurement of given container id.
     *
     * @param containerID the containerID of the container to be measured.
     */
    public synchronized void startDockerContainerMeasurement(String containerID){
        if(containerID != null && !containerID.isEmpty()) {
            for(int i = 0; i < threads.length; i++) {
                threads[i] = new MeasureRunner(containerID, true);
                threads[i].start();
                try {
                    Thread.sleep(DOCKER_STATS_REACTION_MILLISECONDS / threads.length);
                } catch (InterruptedException e) {
                    logger.error("Error DockerStatsUtil startDockerContainerMeasurement: " + e.getLocalizedMessage());
                }

            }
        }
    }

    /**
     * Ends the previously started docker stats measurement.
     *
     * @return A list of Pairs containing as Pair.first the cpu usage in % and as Pair.second the memory usage in %
     */
    public synchronized List<DockerStatsRecord> stopDockerContainerMeasurement(){
        List<DockerStatsRecord> completeMeasures = new LinkedList<>();
        for(int i = 0; i < threads.length; i++){
            threads[i].stopRunning();
            try {
                threads[i].join(2 * DOCKER_STATS_REACTION_MILLISECONDS);
                completeMeasures.addAll(threads[i].getMeasures());

            } catch (Exception e) {
               logger.error("Error DockerStatsUtil stopDockerContainerMeasurement: " + e.getLocalizedMessage());
            }
        }

        return completeMeasures;
    }


    /**
     * Estimates the storage size of the given container.
     *
     * @param containerName the name of the docker container
     * @param storageDirectoryPath the path to the storage directory.
     * @return the size of the storage directory mapped from container to host in bytes.
     */
    public Long estimateStorageSize(String containerName, String storageDirectoryPath, boolean external){
        Long resultBytes = -1L;
        String containerID = DockerCommandLineUtil.getRunningContainerId(containerName);
        if(!containerID.isEmpty()){
            String[] command;
            if(external) {
                command = ServerSystemUtil.getOsSpecificCommand(new String[]{"/usr/bin/du -c -b --max=1 "
                                + storageDirectoryPath
                                + " | awk '{print $1}'"});
            } else {
                command = ServerSystemUtil.getOsSpecificCommand(new String[]{DockerCommandLineUtil.getDockerInstallPath()
                        + "docker exec -t "
                        + containerID
                        + " /usr/bin/du -c -b --max=1 "
                        + storageDirectoryPath
                        + " | awk '{print $1}'"});
            }

            List<String> answers = ServerSystemUtil.executeCommand(command);
            if(!answers.isEmpty()){
                try {
                    resultBytes = Long.valueOf(answers.get(answers.size() - 1));
                } catch (Exception e){
                    logger.error("Error estimating storage size. Result is not a number: {}",e.getLocalizedMessage());
                    resultBytes = -1L;
                }
            }
        }

        return resultBytes;
    }


    private class MeasureRunner extends Thread{
        private List<DockerStatsRecord> measures = new LinkedList<>();
        private String containerID;
        private String[] command;
        private volatile boolean running;

        public MeasureRunner(String containerID, boolean running){
            this.containerID = containerID;
            command = ServerSystemUtil.getOsSpecificCommand(new String[]{DockerCommandLineUtil.getDockerInstallPath()
                    + "docker stats "
                    + containerID
                    + " --no-stream | grep "
                    + containerID
                    + " | awk '{print $2 $8 $14$15\"%\" $17$18\"%\" $9$10\"%\" $12$13\"%\"}'"}); // cpu%mem%readblock%writeblock%netDOWN%netUP
            this.running = running;<commit_msg>more logger info<commit_after>package de.qaware.chronix.server.util;

import com.sun.management.OperatingSystemMXBean;
import de.qaware.chronix.shared.DataModels.Pair;
import de.qaware.chronix.shared.DataModels.Tuple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by mcqueen666 on 19.08.16.
 */
public class DockerStatsUtil {
    private final Logger logger = LoggerFactory.getLogger(DockerStatsUtil.class);
    private final long MEASURE_INTERVAL_MILLISECONDS = 100;
    private final long DOCKER_STATS_REACTION_MILLISECONDS = 1000;
    private final int NUMBER_OF_THREADS = 4;

    private static DockerStatsUtil instance;
    private volatile MeasureRunner[] threads;


    private DockerStatsUtil(){
       // OperatingSystemMXBean oSMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        threads = new MeasureRunner[NUMBER_OF_THREADS];

    }

    public static synchronized DockerStatsUtil getInstance(){
        if(instance == null){
            instance = new DockerStatsUtil();
        }
        return instance;
    }


    /**
     * Starts a threaded docker stats measurement of given container id.
     *
     * @param containerID the containerID of the container to be measured.
     */
    public synchronized void startDockerContainerMeasurement(String containerID){
        if(containerID != null && !containerID.isEmpty()) {
            for(int i = 0; i < threads.length; i++) {
                threads[i] = new MeasureRunner(containerID, true);
                threads[i].start();
                try {
                    Thread.sleep(DOCKER_STATS_REACTION_MILLISECONDS / threads.length);
                } catch (InterruptedException e) {
                    logger.error("Error DockerStatsUtil startDockerContainerMeasurement: " + e.getLocalizedMessage());
                }

            }
        }
    }

    /**
     * Ends the previously started docker stats measurement.
     *
     * @return A list of Pairs containing as Pair.first the cpu usage in % and as Pair.second the memory usage in %
     */
    public synchronized List<DockerStatsRecord> stopDockerContainerMeasurement(){
        List<DockerStatsRecord> completeMeasures = new LinkedList<>();
        for(int i = 0; i < threads.length; i++){
            threads[i].stopRunning();
            try {
                threads[i].join(2 * DOCKER_STATS_REACTION_MILLISECONDS);
                completeMeasures.addAll(threads[i].getMeasures());

            } catch (Exception e) {
               logger.error("Error DockerStatsUtil stopDockerContainerMeasurement: " + e.getLocalizedMessage());
            }
        }

        return completeMeasures;
    }


    /**
     * Estimates the storage size of the given container.
     *
     * @param containerName the name of the docker container
     * @param storageDirectoryPath the path to the storage directory.
     * @return the size of the storage directory mapped from container to host in bytes.
     */
    public Long estimateStorageSize(String containerName, String storageDirectoryPath, boolean external){
        Long resultBytes = -1L;
        String containerID = DockerCommandLineUtil.getRunningContainerId(containerName);
        if(!containerID.isEmpty()){
            String[] command;
            if(external) {
                command = ServerSystemUtil.getOsSpecificCommand(new String[]{"/usr/bin/du -c -b --max=1 "
                                + storageDirectoryPath
                                + " | awk '{print $1}'"});
            } else {
                command = ServerSystemUtil.getOsSpecificCommand(new String[]{DockerCommandLineUtil.getDockerInstallPath()
                        + "docker exec -t "
                        + containerID
                        + " /usr/bin/du -c -b --max=1 "
                        + storageDirectoryPath
                        + " | awk '{print $1}'"});
            }

            List<String> answers = ServerSystemUtil.executeCommand(command);
            if(!answers.isEmpty()){
                try {
                    resultBytes = Long.valueOf(answers.get(answers.size() - 1));
                } catch (Exception e){
                    logger.error("Error estimating storage size. Result \"{}\" is not a number: {}",answers.get(answers.size() - 1),e.getLocalizedMessage());
                    resultBytes = -1L;
                }
            }
        }

        return resultBytes;
    }


    private class MeasureRunner extends Thread{
        private List<DockerStatsRecord> measures = new LinkedList<>();
        private String containerID;
        private String[] command;
        private volatile boolean running;

        public MeasureRunner(String containerID, boolean running){
            this.containerID = containerID;
            command = ServerSystemUtil.getOsSpecificCommand(new String[]{DockerCommandLineUtil.getDockerInstallPath()
                    + "docker stats "
                    + containerID
                    + " --no-stream | grep "
                    + containerID
                    + " | awk '{print $2 $8 $14$15\"%\" $17$18\"%\" $9$10\"%\" $12$13\"%\"}'"}); // cpu%mem%readblock%writeblock%netDOWN%netUP
            this.running = running;
{'fraction_non_alphanumeric': 0.09668353007307476, 'fraction_numerical': 0.008712759977515458, 'mean_word_length': 2.1406295969402764, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741932', 'n_tokens_mistral': 2897, 'n_tokens_neox': 2681, 'n_words': 666}