<commit_before>package com.yammer.metrics.core;

import java.lang.ref.SoftReference;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * A meter metric which measures mean throughput and one-, five-, and
 * fifteen-minute exponentially-weighted moving average throughputs.
 *
 * @author coda
 * @see <a href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">EMA</a>
 */
public class MeterMetric implements Metric {
	private static final ScheduledExecutorService TICK_THREAD =
			Executors.newScheduledThreadPool(2, new NamedThreadFactory("metrics-meter-tick"));
	private static final long INTERVAL = 5; // seconds
	private static final double INTERVAL_IN_NS = TimeUnit.SECONDS.toNanos(INTERVAL);
	private static final double ONE_MINUTE_FACTOR = 1 / Math.exp(TimeUnit.SECONDS.toMinutes(INTERVAL));
	private static final double FIVE_MINUTE_FACTOR = ONE_MINUTE_FACTOR / 5;
	private static final double FIFTEEN_MINUTE_FACTOR = ONE_MINUTE_FACTOR / 15;

	/**
	 * Creates a new {@link MeterMetric}.
	 *
	 * @param eventType the plural name of the event the meter is measuring
	 *                  (e.g., {@code "requests"})
	 * @param scaleUnit the scale unit of the new meter
	 * @return a new {@link MeterMetric}
	 */
	public static MeterMetric newMeter(String eventType, TimeUnit scaleUnit) {
		return newMeter(INTERVAL, TimeUnit.SECONDS, eventType, scaleUnit);
	}

	/**
	 * Creates a new {@link MeterMetric} with a given tick interval.
	 *
	 *
	 * @param interval the duration of a meter tick
	 * @param intervalUnit the unit of {@code interval}
	 * @param eventType the plural name of the event the meter is measuring
	 *                  (e.g., {@code "requests"})
	 *@param scaleUnit the scale unit of the new meter  @return a new {@link MeterMetric}
	 */
	public static MeterMetric newMeter(long interval, TimeUnit intervalUnit, String eventType, TimeUnit scaleUnit) {
		final MeterMetric meter = new MeterMetric(eventType, scaleUnit);
		final SoftReference<MeterMetric> reference = new SoftReference<MeterMetric>(meter);
		final Runnable job = new Runnable() {
			@Override
			public void run() {
				// This will throw a NullPointerException if the meter has been
				// collected. This is actually the desired behavior, as the
				// NPE will cause the ScheduledExecutorService to deschedule
				// this job.
				reference.get().tick();
			}
		};
		TICK_THREAD.scheduleAtFixedRate(job, interval, interval, intervalUnit);
		return meter;
	}

	private final AtomicLong uncounted = new AtomicLong();
	private final AtomicLong count = new AtomicLong();
	private final long startTime = System.nanoTime();
	private final TimeUnit scaleUnit;
	private final String eventType;
	private volatile boolean initialized;
	private volatile double _oneMinuteRate;
	private volatile double _fiveMinuteRate;
	private volatile double _fifteenMinuteRate;

	private MeterMetric(String eventType, TimeUnit scaleUnit) {
		initialized = false;
		_oneMinuteRate = _fiveMinuteRate = _fifteenMinuteRate = 0.0;
		this.scaleUnit = scaleUnit;<commit_msg>Stop tracking soft references for meters.<commit_after>package com.yammer.metrics.core;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * A meter metric which measures mean throughput and one-, five-, and
 * fifteen-minute exponentially-weighted moving average throughputs.
 *
 * @author coda
 * @see <a href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">EMA</a>
 */
public class MeterMetric implements Metric {
	private static final ScheduledExecutorService TICK_THREAD =
			Executors.newScheduledThreadPool(2, new NamedThreadFactory("metrics-meter-tick"));
	private static final long INTERVAL = 5; // seconds
	private static final double INTERVAL_IN_NS = TimeUnit.SECONDS.toNanos(INTERVAL);
	private static final double ONE_MINUTE_FACTOR = 1 / Math.exp(TimeUnit.SECONDS.toMinutes(INTERVAL));
	private static final double FIVE_MINUTE_FACTOR = ONE_MINUTE_FACTOR / 5;
	private static final double FIFTEEN_MINUTE_FACTOR = ONE_MINUTE_FACTOR / 15;

	/**
	 * Creates a new {@link MeterMetric}.
	 *
	 * @param eventType the plural name of the event the meter is measuring
	 *                  (e.g., {@code "requests"})
	 * @param scaleUnit the scale unit of the new meter
	 * @return a new {@link MeterMetric}
	 */
	public static MeterMetric newMeter(String eventType, TimeUnit scaleUnit) {
		return newMeter(INTERVAL, TimeUnit.SECONDS, eventType, scaleUnit);
	}

	/**
	 * Creates a new {@link MeterMetric} with a given tick interval.
	 *
	 *
	 * @param interval the duration of a meter tick
	 * @param intervalUnit the unit of {@code interval}
	 * @param eventType the plural name of the event the meter is measuring
	 *                  (e.g., {@code "requests"})
	 *@param scaleUnit the scale unit of the new meter  @return a new {@link MeterMetric}
	 */
	public static MeterMetric newMeter(long interval, TimeUnit intervalUnit, String eventType, TimeUnit scaleUnit) {
		final MeterMetric meter = new MeterMetric(eventType, scaleUnit);
		final Runnable job = new Runnable() {
			@Override
			public void run() {
				meter.tick();
			}
		};
		TICK_THREAD.scheduleAtFixedRate(job, interval, interval, intervalUnit);
		return meter;
	}

	private final AtomicLong uncounted = new AtomicLong();
	private final AtomicLong count = new AtomicLong();
	private final long startTime = System.nanoTime();
	private final TimeUnit scaleUnit;
	private final String eventType;
	private volatile boolean initialized;
	private volatile double _oneMinuteRate;
	private volatile double _fiveMinuteRate;
	private volatile double _fifteenMinuteRate;

	private MeterMetric(String eventType, TimeUnit scaleUnit) {
		initialized = false;
		_oneMinuteRate = _fiveMinuteRate = _fifteenMinuteRate = 0.0;
		this.scaleUnit = scaleUnit;
{'fraction_non_alphanumeric': 0.08608724388631857, 'fraction_numerical': 0.0026437541308658294, 'mean_word_length': 4.487760652765186, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6435846', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1706, 'n_words': 569}