<commit_before>/* This file is part of VoltDB.
 * Copyright (C) 2008-2011 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.voltdb;

import java.util.ArrayList;
import java.util.Iterator;

import org.voltdb.VoltTable.ColumnInfo;

/**
 * A class to track and generate statistics regarding task starvation.
 * A worker thread can instantiate one of these and report when starvation begins and ends.
 */
public class StarvationTracker extends SiteStatsSource {

    public StarvationTracker(String name, int siteId) {
        super(name, siteId, false);
        m_lastStartTime = m_startTime = System.nanoTime();
    }

    /*
     * Keep track of the time since the tracker was created or in the interval case, the last time
     * the stat was checked
     */
    private long m_startTime;
    private long m_lastStartTime;

    private long m_count = 0;
    private long m_lastCount = 0;
    private long m_sumOfSquares = 0;
    private long m_lastSumOfSquares = 0;
    private long m_totalTime = 0;
    private long m_lastTotalTime = 0;
    private long m_max = 0;
    private long m_lastMax = 0;
    private long m_min = Long.MAX_VALUE;
    private long m_lastMin = Long.MAX_VALUE;

    private long m_starvationStartTime;

    private boolean m_interval;

    /**
     * Is there currently starvation
     */
    private boolean m_starved = false;
    public void beginStarvation() {
        if (m_starved) {
            return;
        }
        m_starved = true;
        m_starvationStartTime = System.nanoTime();
    }

    public void endStarvation() {
        if (!m_starved) {
            return;
        }
        m_starved = false;
        m_count++;
        long delta = System.nanoTime() - m_starvationStartTime;
        m_totalTime += delta;
        m_sumOfSquares += delta * delta / 1000000;
        m_max = Math.max(m_max, delta);
        m_lastMax = Math.max(m_lastMax, delta);
        m_min = Math.min(m_min, delta);
        m_lastMin = Math.min(m_lastMin, delta);
    }

    @Override
    protected void populateColumnSchema(ArrayList<ColumnInfo> columns) {
        super.populateColumnSchema(columns);
        columns.add(new ColumnInfo("COUNT", VoltType.BIGINT));
        columns.add(new ColumnInfo("PERCENT", VoltType.FLOAT));
        columns.add(new ColumnInfo("AVG", VoltType.BIGINT));
        columns.add(new ColumnInfo("MIN", VoltType.BIGINT));
        columns.add(new ColumnInfo("MAX", VoltType.BIGINT));
        columns.add(new ColumnInfo("STDDEV", VoltType.BIGINT));
    }

    @Override
    protected void updateStatsRow(Object rowKey, Object rowValues[]) {
        if (m_interval) {
            final long now = System.nanoTime();
            final long totalTime = now - m_lastStartTime;
            final long count = m_count - m_lastCount;
            final long totalStarvedTime = m_totalTime - m_lastTotalTime;
            final long sumOfSquares = m_sumOfSquares - m_lastSumOfSquares;
            m_lastStartTime = now;
            m_lastSumOfSquares = m_sumOfSquares;
            m_lastTotalTime = m_totalTime;
            m_lastCount = m_count;
            rowValues[columnNameToIndex.get("COUNT")] = count;
            rowValues[columnNameToIndex.get("PERCENT")] = totalStarvedTime / (totalTime / 100.0);
            long uSecs = totalStarvedTime / 1000;
            rowValues[columnNameToIndex.get("AVG")] = (totalStarvedTime / count) / 1000;
            rowValues[columnNameToIndex.get("MIN")] = m_lastMin;
            rowValues[columnNameToIndex.get("MAX")] = m_lastMax;
            m_lastMax = 0;
            m_lastMin = Long.MAX_VALUE;
            rowValues[columnNameToIndex.get("STDDEV")] = (long)Math.sqrt(sumOfSquares / count - uSecs * uSecs);
        } else {
            final long totalTime = System.nanoTime() - m_startTime;
            rowValues[columnNameToIndex.get("COUNT")] = m_count;
            rowValues[columnNameToIndex.get("PERCENT")] = m_totalTime / (totalTime / 100.0);
            long uSecs = (m_totalTime / m_count) / 1000;
            rowValues[columnNameToIndex.get("AVG")] = (m_totalTime / m_count) / 1000;
            rowValues[columnNameToIndex.get("MIN")] = m_min;
            rowValues[columnNameToIndex.get("MAX")] = m_max;
            rowValues[columnNameToIndex.get("STDDEV")] = (long)Math.sqrt(m_sumOfSquares / m_count - uSecs * uSecs);
        }
        super.updateStatsRow(rowKey, rowValues);
    }

    @Override
    protected Iterator<Object> getStatsRowKeyIterator(final boolean interval) {
        m_interval = interval;
        return new Iterator<Object>() {
            boolean returnRow = true;
            @Override
            public boolean hasNext() {<commit_msg>Fix division by 0 if the starvation stats is queried before there is any starvation.<commit_after>/* This file is part of VoltDB.
 * Copyright (C) 2008-2011 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.voltdb;

import java.util.ArrayList;
import java.util.Iterator;

import org.voltdb.VoltTable.ColumnInfo;

/**
 * A class to track and generate statistics regarding task starvation.
 * A worker thread can instantiate one of these and report when starvation begins and ends.
 */
public class StarvationTracker extends SiteStatsSource {

    public StarvationTracker(String name, int siteId) {
        super(name, siteId, false);
        m_lastStartTime = m_startTime = System.nanoTime();
    }

    /*
     * Keep track of the time since the tracker was created or in the interval case, the last time
     * the stat was checked
     */
    private long m_startTime;
    private long m_lastStartTime;

    private long m_count = 0;
    private long m_lastCount = 0;
    private long m_sumOfSquares = 0;
    private long m_lastSumOfSquares = 0;
    private long m_totalTime = 0;
    private long m_lastTotalTime = 0;
    private long m_max = 0;
    private long m_lastMax = 0;
    private long m_min = Long.MAX_VALUE;
    private long m_lastMin = Long.MAX_VALUE;

    private long m_starvationStartTime;

    private boolean m_interval;

    /**
     * Is there currently starvation
     */
    private boolean m_starved = false;
    public void beginStarvation() {
        if (m_starved) {
            return;
        }
        m_starved = true;
        m_starvationStartTime = System.nanoTime();
    }

    public void endStarvation() {
        if (!m_starved) {
            return;
        }
        m_starved = false;
        m_count++;
        long delta = System.nanoTime() - m_starvationStartTime;
        m_totalTime += delta;
        m_sumOfSquares += delta * delta / 1000000;
        m_max = Math.max(m_max, delta);
        m_lastMax = Math.max(m_lastMax, delta);
        m_min = Math.min(m_min, delta);
        m_lastMin = Math.min(m_lastMin, delta);
    }

    @Override
    protected void populateColumnSchema(ArrayList<ColumnInfo> columns) {
        super.populateColumnSchema(columns);
        columns.add(new ColumnInfo("COUNT", VoltType.BIGINT));
        columns.add(new ColumnInfo("PERCENT", VoltType.FLOAT));
        columns.add(new ColumnInfo("AVG", VoltType.BIGINT));
        columns.add(new ColumnInfo("MIN", VoltType.BIGINT));
        columns.add(new ColumnInfo("MAX", VoltType.BIGINT));
        columns.add(new ColumnInfo("STDDEV", VoltType.BIGINT));
    }

    @Override
    protected void updateStatsRow(Object rowKey, Object rowValues[]) {
        if (m_interval) {
            final long now = System.nanoTime();
            final long totalTime = now - m_lastStartTime;
            final long count = m_count - m_lastCount;
            final long totalStarvedTime = m_totalTime - m_lastTotalTime;
            final long sumOfSquares = m_sumOfSquares - m_lastSumOfSquares;
            m_lastStartTime = now;
            m_lastSumOfSquares = m_sumOfSquares;
            m_lastTotalTime = m_totalTime;
            m_lastCount = m_count;
            rowValues[columnNameToIndex.get("COUNT")] = count;
            rowValues[columnNameToIndex.get("PERCENT")] = totalStarvedTime / (totalTime / 100.0);
            long uSecs = totalStarvedTime / 1000;
            rowValues[columnNameToIndex.get("AVG")] = (totalStarvedTime / count) / 1000;
            rowValues[columnNameToIndex.get("MIN")] = m_lastMin;
            rowValues[columnNameToIndex.get("MAX")] = m_lastMax;
            m_lastMax = 0;
            m_lastMin = Long.MAX_VALUE;
            rowValues[columnNameToIndex.get("STDDEV")] = (long)Math.sqrt(sumOfSquares / count - uSecs * uSecs);
        } else {
            final long totalTime = System.nanoTime() - m_startTime;
            rowValues[columnNameToIndex.get("COUNT")] = m_count;
            rowValues[columnNameToIndex.get("PERCENT")] = m_totalTime / (totalTime / 100.0);
            long uSecs = m_count > 0 ? (m_totalTime / m_count) / 1000 : 0l;
            rowValues[columnNameToIndex.get("AVG")] = uSecs;
            rowValues[columnNameToIndex.get("MIN")] = m_min;
            rowValues[columnNameToIndex.get("MAX")] = m_max;
            rowValues[columnNameToIndex.get("STDDEV")] = (long)Math.sqrt(m_sumOfSquares / m_count - uSecs * uSecs);
        }
        super.updateStatsRow(rowKey, rowValues);
    }

    @Override
    protected Iterator<Object> getStatsRowKeyIterator(final boolean interval) {
        m_interval = interval;
        return new Iterator<Object>() {
            boolean returnRow = true;
            @Override
            public boolean hasNext() {
{'fraction_non_alphanumeric': 0.09302764411736957, 'fraction_numerical': 0.009151806774223984, 'mean_word_length': 2.670360110803324, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2550741', 'n_tokens_mistral': 3281, 'n_tokens_neox': 3105, 'n_words': 859}