I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
StatSummarizer.java 17.6 KiB
Newer Older
jrandom's avatar
jrandom committed
package net.i2p.router.web;

zzz's avatar
zzz committed
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
zzz's avatar
zzz committed
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Semaphore;
jrandom's avatar
jrandom committed

import net.i2p.I2PAppContext;
import net.i2p.app.ClientApp;
import net.i2p.app.ClientAppState;
import net.i2p.data.DataHelper;
import net.i2p.router.RouterContext;
import static net.i2p.router.web.GraphConstants.*;
import net.i2p.stat.Rate;
import net.i2p.stat.RateStat;
zzz's avatar
zzz committed
import net.i2p.util.FileUtil;
jrandom's avatar
jrandom committed
import net.i2p.util.Log;
import net.i2p.util.SystemVersion;
jrandom's avatar
jrandom committed

jrandom's avatar
jrandom committed
/**
zzz's avatar
zzz committed
 *  A thread started by RouterConsoleRunner that
 *  checks the configuration for stats to be tracked via jrobin,
 *  and adds or deletes RRDs as necessary.
jrandom's avatar
jrandom committed
 *
zzz's avatar
zzz committed
 *  This also contains methods to generate xml or png image output.
 *  The rendering for graphs is in SummaryRenderer.
zzz's avatar
zzz committed
 *
 *  To control memory, the number of simultaneous renderings is limited.
 *
 *  @since 0.6.1.13
jrandom's avatar
jrandom committed
 */
public class StatSummarizer implements Runnable, ClientApp {
    private final RouterContext _context;
    private final Log _log;
jrandom's avatar
jrandom committed
    /** list of SummaryListener instances */
    private final List<SummaryListener> _listeners;
    private static final int MAX_CONCURRENT_PNG = SystemVersion.isARM() ? 2 : 3;
    private final Semaphore _sem;
    private volatile boolean _isRunning;
    private volatile Thread _thread;
    private static final String NAME = "StatSummarizer";
jrandom's avatar
jrandom committed
    
    public StatSummarizer(RouterContext ctx) {
        _context = ctx;
jrandom's avatar
jrandom committed
        _log = _context.logManager().getLog(getClass());
        _listeners = new CopyOnWriteArrayList<SummaryListener>();
        _sem = new Semaphore(MAX_CONCURRENT_PNG, true);
zzz's avatar
zzz committed
        _context.addShutdownTask(new Shutdown());
jrandom's avatar
jrandom committed
    }
    
    /**
     * @return null if disabled
     */
    public static StatSummarizer instance() {
        return instance(I2PAppContext.getGlobalContext());
    }

    /**
     * @return null if disabled
zzz's avatar
zzz committed
     * @since 0.9.38
     */
    public static StatSummarizer instance(I2PAppContext ctx) {
        ClientApp app = ctx.clientAppManager().getRegisteredApp(NAME);
        return (app != null) ? (StatSummarizer) app : null;
    }
jrandom's avatar
jrandom committed
    
    public void run() {
zzz's avatar
zzz committed
        // JRobin 1.5.9 crashes these JVMs
        if (SystemVersion.isApache() ||            // Harmony
            SystemVersion.isGNU()) {               // JamVM or gij
zzz's avatar
zzz committed
            _log.logAlways(Log.WARN, "Graphing not supported with this JVM: " +
                                     System.getProperty("java.vendor") + ' ' +
zzz's avatar
zzz committed
                                     System.getProperty("java.version") + " (" +
                                     System.getProperty("java.runtime.name") + ' ' +
                                     System.getProperty("java.runtime.version") + ')');
            return;
        }
zzz's avatar
zzz committed
        boolean isPersistent = _context.getBooleanPropertyDefaultTrue(SummaryListener.PROP_PERSISTENT);
        if (!isPersistent)
            deleteOldRRDs();
zzz's avatar
zzz committed
        _thread = Thread.currentThread();
        _context.clientAppManager().register(this);
jrandom's avatar
jrandom committed
        String specs = "";
        try {
            while (_isRunning && _context.router().isAlive()) {
                specs = adjustDatabases(specs);
                try { Thread.sleep(60*1000); } catch (InterruptedException ie) {}
            }
        } finally {
            _isRunning = false;
            _context.clientAppManager().unregister(this);
jrandom's avatar
jrandom committed
        }
    }
    
zzz's avatar
zzz committed
    /** @since 0.9.38 */
    public static boolean isDisabled(I2PAppContext ctx) {
        return ctx.clientAppManager().getRegisteredApp(NAME) == null;
zzz's avatar
zzz committed
    }
    
    /**
     * Disable graph generation until restart
     * See SummaryRenderer.render()
     * @since 0.9.6
     */
    static void setDisabled(I2PAppContext ctx) {
        StatSummarizer ss = instance(ctx);
        if (ss != null)
            ss.setDisabled();
    }

    /**
     * Disable graph generation until restart
     * See SummaryRenderer.render()
     * @since 0.9.38
     */
    synchronized void setDisabled() {
        if (_isRunning) {
            _isRunning = false;
            Thread t = _thread;
            if (t != null)
                t.interrupt();
zzz's avatar
zzz committed

    /////// ClientApp methods

    /**
     * Does nothing, we aren't tracked
     * @since 0.9.38
     */
    public void startup() {}

    /**
     * Does nothing, we aren't tracked
     * @since 0.9.38
     */
    public void shutdown(String[] args) {}

    /** @since 0.9.38 */
    public ClientAppState getState() {
        return ClientAppState.RUNNING;
    }

    /** @since 0.9.38 */
    public String getName() {
        return NAME;
    }

    /** @since 0.9.38 */
    public String getDisplayName() {
        return "Console stats summarizer";
    }

    /////// End ClientApp methods

    /**
     *  List of SummaryListener instances
     *  @since public since 0.9.33, was package private
     */
    public List<SummaryListener> getListeners() { return _listeners; }
jrandom's avatar
jrandom committed
    
    /**  @since public since 0.9.33, was package private */
    public static final String DEFAULT_DATABASES = "bw.sendRate.60000" +
jrandom's avatar
jrandom committed
                                                    ",bw.recvRate.60000" +
zzz's avatar
zzz committed
//                                                  ",tunnel.testSuccessTime.60000" +
//                                                  ",udp.outboundActiveCount.60000" +
//                                                  ",udp.receivePacketSize.60000" +
//                                                  ",udp.receivePacketSkew.60000" +
//                                                  ",udp.sendConfirmTime.60000" +
//                                                  ",udp.sendPacketSize.60000" +
zzz's avatar
zzz committed
                                                    ",router.memoryUsed.60000" +
zzz's avatar
zzz committed
                                                    ",router.activePeers.60000";
//                                                  ",router.activeSendPeers.60000" +
//                                                  ",tunnel.acceptLoad.60000" +
//                                                  ",tunnel.dropLoadProactive.60000" +
//                                                  ",tunnel.buildExploratorySuccess.60000" +
//                                                  ",tunnel.buildExploratoryReject.60000" +
//                                                  ",tunnel.buildExploratoryExpire.60000" +
//                                                  ",client.sendAckTime.60000" +
//                                                  ",client.dispatchNoACK.60000" +
//                                                  ",ntcp.sendTime.60000" +
//                                                  ",ntcp.transmitTime.60000" +
//                                                  ",ntcp.sendBacklogTime.60000" +
//                                                  ",ntcp.receiveTime.60000" +
//                                                  ",transport.sendMessageFailureLifetime.60000" +
//                                                  ",transport.sendProcessingTime.60000";
jrandom's avatar
jrandom committed
    
    private String adjustDatabases(String oldSpecs) {
        String spec = _context.getProperty("stat.summaries", DEFAULT_DATABASES);
        if ( ( (spec == null) && (oldSpecs == null) ) ||
             ( (spec != null) && (oldSpecs != null) && (oldSpecs.equals(spec))) )
            return oldSpecs;
        
        Set<Rate> old = parseSpecs(oldSpecs);
        Set<Rate> newSpecs = parseSpecs(spec);
jrandom's avatar
jrandom committed
        
        // remove old ones
        for (Rate r : old) {
jrandom's avatar
jrandom committed
            if (!newSpecs.contains(r))
                removeDb(r);
        }
        // add new ones
        StringBuilder buf = new StringBuilder();
        boolean comma = false;
        for (Rate r : newSpecs) {
jrandom's avatar
jrandom committed
            if (!old.contains(r))
                addDb(r);
jrandom's avatar
jrandom committed
                buf.append(',');
            else
                comma = true;
            buf.append(r.getRateStat().getName()).append(".").append(r.getPeriod());
jrandom's avatar
jrandom committed
        }
        return buf.toString();
    }
    
    private void removeDb(Rate r) {
zzz's avatar
zzz committed
        for (SummaryListener lsnr : _listeners) {
jrandom's avatar
jrandom committed
            if (lsnr.getRate().equals(r)) {
zzz's avatar
zzz committed
                // no iter.remove() in COWAL
                _listeners.remove(lsnr);
jrandom's avatar
jrandom committed
                lsnr.stopListening();
                return;
            }
        }
    }
    private void addDb(Rate r) {
        SummaryListener lsnr = new SummaryListener(r);
zzz's avatar
zzz committed
        boolean success = lsnr.startListening();
        if (success)
            _listeners.add(lsnr);
        else
            _log.error("Failed to add RRD for rate " + r.getRateStat().getName() + '.' + r.getPeriod());
jrandom's avatar
jrandom committed
        //System.out.println("Start listening for " + r.getRateStat().getName() + ": " + r.getPeriod());
    }
jrandom's avatar
jrandom committed
    public boolean renderPng(Rate rate, OutputStream out) throws IOException { 
        return renderPng(rate, out, DEFAULT_X, DEFAULT_Y,
zzz's avatar
zzz committed
                         false, false, false, false, -1, 0, true); 
jrandom's avatar
jrandom committed
    }

    /**
     *  This does the single data graphs.
     *  For the two-data bandwidth graph see renderRatePng().
     *  Synchronized to conserve memory.
zzz's avatar
zzz committed
     *
     *  @param end number of periods before now
     *  @return success
     */
    public boolean renderPng(Rate rate, OutputStream out, int width, int height, boolean hideLegend,
                                          boolean hideGrid, boolean hideTitle, boolean showEvents, int periodCount,
zzz's avatar
zzz committed
                                          int end, boolean showCredit) throws IOException {
        try {
            try {
                _sem.acquire();
            } catch (InterruptedException ie) {}
            try {
                return locked_renderPng(rate, out, width, height, hideLegend, hideGrid, hideTitle, showEvents,
zzz's avatar
zzz committed
                                    periodCount, end, showCredit);
            } catch (NoClassDefFoundError ncdfe) {
                //  java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager
                //  at java.lang.Class.forName0(Native Method)
                //  at java.lang.Class.forName(Class.java:270)
                //  at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:82)
                String s = "Error rendering - disabling graph generation. Install fonts-dejavu font package?";
                _log.logAlways(Log.WARN, s);
                IOException ioe = new IOException(s);
                ioe.initCause(ncdfe);
                throw ioe;
            }
zzz's avatar
zzz committed
            _sem.release();
zzz's avatar
zzz committed
    /**
     *  @param end number of periods before now
     */
    private boolean locked_renderPng(Rate rate, OutputStream out, int width, int height, boolean hideLegend,
                                          boolean hideGrid, boolean hideTitle, boolean showEvents, int periodCount,
zzz's avatar
zzz committed
                                          int end, boolean showCredit) throws IOException {
zzz's avatar
zzz committed
        else if (width <= 0)
            width = DEFAULT_X;
        if (height > MAX_Y)
            height = MAX_Y;
zzz's avatar
zzz committed
        else if (height <= 0)
zzz's avatar
zzz committed
        if (end < 0)
            end = 0;
zzz's avatar
zzz committed
        for (SummaryListener lsnr : _listeners) {
jrandom's avatar
jrandom committed
            if (lsnr.getRate().equals(rate)) {
zzz's avatar
zzz committed
                lsnr.renderPng(out, width, height, hideLegend, hideGrid, hideTitle, showEvents, periodCount, end, showCredit);
jrandom's avatar
jrandom committed
                return true;
            }
        }
        return false;
    }
zzz's avatar
zzz committed
    /** @deprecated unused */
jrandom's avatar
jrandom committed
    public boolean renderPng(OutputStream out, String templateFilename) throws IOException {
        SummaryRenderer.render(_context, out, templateFilename);
        return true;
    }
jrandom's avatar
jrandom committed
    public boolean getXML(Rate rate, OutputStream out) throws IOException {
zzz's avatar
zzz committed
        try {
            try {
                _sem.acquire();
            } catch (InterruptedException ie) {}
            return locked_getXML(rate, out);
        } finally {
            _sem.release();
        }
    }

    private boolean locked_getXML(Rate rate, OutputStream out) throws IOException {
zzz's avatar
zzz committed
        for (SummaryListener lsnr : _listeners) {
jrandom's avatar
jrandom committed
            if (lsnr.getRate().equals(rate)) {
                lsnr.getData().exportXml(out);
                out.write(DataHelper.getUTF8("<!-- Rate: " + lsnr.getRate().getRateStat().getName() + " for period " + lsnr.getRate().getPeriod() + " -->\n"));
                out.write(DataHelper.getUTF8("<!-- Average data source name: " + lsnr.getName() + " event count data source name: " + lsnr.getEventName() + " -->\n"));
jrandom's avatar
jrandom committed
                return true;
            }
        }
        return false;
    }
    
zzz's avatar
zzz committed
    /**
     *  This does the two-data bandwidth graph only.
     *  For all other graphs see renderPng() above.
     *  Synchronized to conserve memory.
     *
     *  @param end number of periods before now
     *  @return success
zzz's avatar
zzz committed
     */
    public boolean renderRatePng(OutputStream out, int width, int height, boolean hideLegend,
                                              boolean hideGrid, boolean hideTitle, boolean showEvents,
                                              int periodCount, int end, boolean showCredit) throws IOException {
        try {
            try {
                _sem.acquire();
            } catch (InterruptedException ie) {}
            try {
                return locked_renderRatePng(out, width, height, hideLegend, hideGrid, hideTitle, showEvents,
                                        periodCount, end, showCredit);
            } catch (NoClassDefFoundError ncdfe) {
                //  java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager
                //  at java.lang.Class.forName0(Native Method)
                //  at java.lang.Class.forName(Class.java:270)
                //  at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:82)
                String s = "Error rendering - disabling graph generation. Install fonts-dejavu font package?";
                _log.logAlways(Log.WARN, s);
                IOException ioe = new IOException(s);
                ioe.initCause(ncdfe);
                throw ioe;
            }
zzz's avatar
zzz committed
            _sem.release();
        }
    }

    private boolean locked_renderRatePng(OutputStream out, int width, int height, boolean hideLegend,
                                              boolean hideGrid, boolean hideTitle, boolean showEvents,
                                              int periodCount, int end, boolean showCredit) throws IOException {
zzz's avatar
zzz committed

        // go to some trouble to see if we have the data for the combined bw graph
        SummaryListener txLsnr = null;
        SummaryListener rxLsnr = null;
        for (SummaryListener lsnr : getListeners()) {
zzz's avatar
zzz committed
            String title = lsnr.getRate().getRateStat().getName();
            if (title.equals("bw.sendRate"))
                txLsnr = lsnr;
            else if (title.equals("bw.recvRate"))
                rxLsnr = lsnr;
        }
        if (txLsnr == null || rxLsnr == null)
            throw new IOException("no rates for combined graph");

zzz's avatar
zzz committed
        else if (width <= 0)
            width = DEFAULT_X;
        if (height > MAX_Y)
            height = MAX_Y;
zzz's avatar
zzz committed
        else if (height <= 0)
        txLsnr.renderPng(out, width, height, hideLegend, hideGrid, hideTitle, showEvents, periodCount,
                         end, showCredit, rxLsnr, _t("Bandwidth usage"));
jrandom's avatar
jrandom committed
    }
    
jrandom's avatar
jrandom committed
    /**
     * @param specs statName.period,statName.period,statName.period
     * @return list of Rate objects
     * @since public since 0.9.33, was package private
jrandom's avatar
jrandom committed
     */
        if (specs == null)
            return Collections.emptySet();
jrandom's avatar
jrandom committed
        StringTokenizer tok = new StringTokenizer(specs, ",");
        Set<Rate> rv = new HashSet<Rate>();
jrandom's avatar
jrandom committed
        while (tok.hasMoreTokens()) {
            String spec = tok.nextToken();
            int split = spec.lastIndexOf('.');
            if ( (split <= 0) || (split + 1 >= spec.length()) )
                continue;
            String name = spec.substring(0, split);
            String per = spec.substring(split+1);
            long period = -1;
            try { 
                period = Long.parseLong(per); 
                RateStat rs = _context.statManager().getRate(name);
                if (rs != null) {
                    Rate r = rs.getRate(period);
                    if (r != null)
                        rv.add(r);
                }
            } catch (NumberFormatException nfe) {}
        }
        return rv;
    }
zzz's avatar
zzz committed

zzz's avatar
zzz committed
    /**
     *  Delete the old rrd dir if we are no longer persistent
zzz's avatar
zzz committed
     *  @since 0.8.7
zzz's avatar
zzz committed
     */
    private void deleteOldRRDs() {
        File rrdDir = new File(_context.getRouterDir(), SummaryListener.RRD_DIR);
        FileUtil.rmdir(rrdDir, false);
    }

    private static final boolean IS_WIN = SystemVersion.isWindows();
zzz's avatar
zzz committed

zzz's avatar
zzz committed
    /** translate a string */
zzz's avatar
zzz committed
        // the RRD font doesn't have zh chars, at least on my system
zzz's avatar
zzz committed
        // Works on 1.5.9 except on windows
        if (IS_WIN && "zh".equals(Messages.getLanguage(_context)))
            return s;
zzz's avatar
zzz committed
        return Messages.getString(s, _context);
    }
zzz's avatar
zzz committed

    /**
     *  Make sure any persistent RRDs are closed
zzz's avatar
zzz committed
     *  @since 0.8.7
zzz's avatar
zzz committed
     */
    private class Shutdown implements Runnable {
        public void run() {
zzz's avatar
zzz committed
            for (SummaryListener lsnr : _listeners) {
zzz's avatar
zzz committed
                // FIXME could cause exceptions if rendering?
zzz's avatar
zzz committed
                lsnr.stopListening();
            }
            _listeners.clear();
        }
    }
jrandom's avatar
jrandom committed
}