<commit_before>package net.anotheria.moskito.core.util;

import net.anotheria.moskito.core.config.MoskitoConfigurationHolder;
import net.anotheria.moskito.core.registry.IProducerRegistry;
import net.anotheria.moskito.core.registry.ProducerRegistryFactory;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * A utility for starting built-in producers upon moskito start / initialization.
 * @author lrosenberg
 *
 */
public class StartBuiltInProducers {
	private static volatile boolean initialized = false;
	
	public static synchronized void startbuiltin(){
		if (initialized)
			return;
		initialized = true;

		startJavaMemoryProducers();
		startJavaThreadingProducers();
		startOsProducers();
		startGcProducers();
		startMBeanProducers();
	}
	
	public static void restartbuiltin(){
		initialized = false;
		startbuiltin();
	}

	private static void startJavaThreadingProducers(){
		if (!MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isJavaThreadingProducers())
			return;
		new BuiltInThreadStatesProducer();
		new BuiltInThreadCountProducer();
	}
	
	private static void startOsProducers(){
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isOsProducer())
			new BuiltInOSProducer();
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isRuntimeProducer())
			new BuiltInRuntimeProducer();
	}

	private static void startGcProducers(){
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isGcProducer())
			new BuiltInGCProducer();
	}
	
	private static void startJavaMemoryProducers(){
		IProducerRegistry registry = ProducerRegistryFactory.getProducerRegistryInstance();
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isJavaMemoryProducers()){
			registry.registerProducer(new BuiltInMemoryProducer(BuiltInMemoryProducer.FREE));
			registry.registerProducer(new BuiltInMemoryProducer(BuiltInMemoryProducer.MAX));
			registry.registerProducer(new BuiltInMemoryProducer(BuiltInMemoryProducer.TOTAL));
		}

		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isJavaMemoryPoolProducers()){
			Map<MemoryType, List<BuiltInMemoryPoolProducer>> producers = new EnumMap<>(MemoryType.class);

			List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
			for (MemoryPoolMXBean pool : pools){
				BuiltInMemoryPoolProducer p = new BuiltInMemoryPoolProducer(pool);
				registry.registerProducer(p);
				List<BuiltInMemoryPoolProducer> pp = producers.get(pool.getType());
				if (pp==null){
					pp = new ArrayList<>(pools.size());
					producers.put(pool.getType(), pp);
				}
				pp.add(p);
			}

			//now finally add virtual producers
			for (Map.Entry<MemoryType,List<BuiltInMemoryPoolProducer>> t : producers.entrySet()){
				BuiltInMemoryPoolVirtualProducer vp = new BuiltInMemoryPoolVirtualProducer(t.getKey(), t.getValue());
				registry.registerProducer(vp);
			}
		}
		<commit_msg>added initialization of builtinerrorproducer on start.<commit_after>package net.anotheria.moskito.core.util;

import net.anotheria.moskito.core.config.MoskitoConfigurationHolder;
import net.anotheria.moskito.core.registry.IProducerRegistry;
import net.anotheria.moskito.core.registry.ProducerRegistryFactory;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * A utility for starting built-in producers upon moskito start / initialization.
 * @author lrosenberg
 *
 */
public class StartBuiltInProducers {
	private static volatile boolean initialized = false;
	
	public static synchronized void startbuiltin(){
		if (initialized)
			return;
		initialized = true;

		startJavaMemoryProducers();
		startJavaThreadingProducers();
		startOsProducers();
		startGcProducers();
		startMBeanProducers();
	}
	
	public static void restartbuiltin(){
		initialized = false;
		startbuiltin();
	}

	private static void startJavaThreadingProducers(){
		if (!MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isJavaThreadingProducers())
			return;
		new BuiltInThreadStatesProducer();
		new BuiltInThreadCountProducer();
	}
	
	private static void startOsProducers(){
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isOsProducer())
			new BuiltInOSProducer();
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isRuntimeProducer())
			new BuiltInRuntimeProducer();
	}

	private static void startGcProducers(){
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isGcProducer())
			new BuiltInGCProducer();
	}
	
	private static void startJavaMemoryProducers(){
		//Ensure builtin error producer is initialized.
		BuiltInErrorProducer.getInstance();

		IProducerRegistry registry = ProducerRegistryFactory.getProducerRegistryInstance();
		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isJavaMemoryProducers()){
			registry.registerProducer(new BuiltInMemoryProducer(BuiltInMemoryProducer.FREE));
			registry.registerProducer(new BuiltInMemoryProducer(BuiltInMemoryProducer.MAX));
			registry.registerProducer(new BuiltInMemoryProducer(BuiltInMemoryProducer.TOTAL));
		}

		if (MoskitoConfigurationHolder.getConfiguration().getBuiltinProducersConfig().isJavaMemoryPoolProducers()){
			Map<MemoryType, List<BuiltInMemoryPoolProducer>> producers = new EnumMap<>(MemoryType.class);

			List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
			for (MemoryPoolMXBean pool : pools){
				BuiltInMemoryPoolProducer p = new BuiltInMemoryPoolProducer(pool);
				registry.registerProducer(p);
				List<BuiltInMemoryPoolProducer> pp = producers.get(pool.getType());
				if (pp==null){
					pp = new ArrayList<>(pools.size());
					producers.put(pool.getType(), pp);
				}
				pp.add(p);
			}

			//now finally add virtual producers
			for (Map.Entry<MemoryType,List<BuiltInMemoryPoolProducer>> t : producers.entrySet()){
				BuiltInMemoryPoolVirtualProducer vp = new BuiltInMemoryPoolVirtualProducer(t.getKey(), t.getValue());
				registry.registerProducer(vp);
			}
		}
		
{'fraction_non_alphanumeric': 0.10219882316506658, 'fraction_numerical': 0.0, 'mean_word_length': 6.331441543700341, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '2408956', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1853, 'n_words': 324}