Search in sources :

Example 11 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class TimeSeriesDatabase method generateCharts.

/**
 * Quantized recorders will have min, max avg charts and distribution charts generated. Counting recorders will have
 * simple charts generated.
 *
 * @param startTimeMillis
 * @param endTimeMillis
 * @param width
 * @param height
 * @return
 * @throws IOException
 */
@JmxExport(value = "generateChartsInterval", description = "generate charts for all measurements in specified interval")
public List<String> generateCharts(final long startTimeMillis, final long endTimeMillis, final int width, final int height) throws IOException {
    try {
        this.flush();
        Collection<TSTable> columnsInfo = this.getTSTables();
        List<String> result = new ArrayList<>(16);
        for (TSTable info : columnsInfo) {
            TimeSeries data = this.read(info.getTableName(), startTimeMillis, endTimeMillis);
            if (data.getTimeStamps().length > 0) {
                if (canGenerateMinMaxAvgCount(info)) {
                    result.add(generateMinMaxAvgCountChart(info, data, width, height));
                }
                if (canGenerateHeatChart(info)) {
                    result.add(generateHeatChart(info, data, width, height));
                }
            }
        }
        Multimap<String, TSTable> counters = getCounters(columnsInfo);
        Map<String, Collection<TSTable>> asMap = counters.asMap();
        for (Map.Entry<String, Collection<TSTable>> entry : asMap.entrySet()) {
            Collection<TSTable> ltables = entry.getValue();
            int l = ltables.size();
            long[][] timestamps = new long[l][];
            double[][] cdata = new double[l][];
            double[][] cdata2 = new double[l][];
            int i = 0;
            String[] measurementNames = new String[cdata.length];
            String[] measurementNames2 = new String[cdata2.length];
            String uom1 = "count";
            String uom2 = "";
            for (TSTable colInfo : ltables) {
                TimeSeries data = this.read(colInfo.getTableName(), startTimeMillis, endTimeMillis);
                timestamps[i] = data.getTimeStamps();
                final long[][] values = data.getValues();
                cdata[i] = Arrays.getColumnAsDoubles(values, colInfo.getColumnIndex("count"));
                cdata2[i] = Arrays.getColumnAsDoubles(values, colInfo.getColumnIndex("total"));
                measurementNames[i] = colInfo.getTableName() + ".count";
                measurementNames2[i] = colInfo.getTableName() + ".total";
                uom2 = new String(colInfo.getTableMetaData(), Charsets.UTF_8);
                i++;
            }
            result.add(generateCountChart(entry.getKey(), timestamps, measurementNames, measurementNames2, uom1, uom2, cdata, cdata2, width, height));
        }
        return result;
    } catch (IOException | RuntimeException ex) {
        throw ex;
    }
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) IOException(java.io.IOException) Collection(java.util.Collection) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JmxExport(org.spf4j.jmx.JmxExport)

Example 12 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class ScalableMeasurementRecorder method getMeasurementsAsString.

@JmxExport(description = "measurements as csv")
public String getMeasurementsAsString() {
    StringWriter sw = new StringWriter(128);
    MeasurementsInfo info = getInfo();
    try {
        Csv.writeCsvRow(sw, (Object[]) info.getMeasurementNames());
        Csv.writeCsvRow(sw, (Object[]) info.getMeasurementUnits());
        final long[] values = get();
        if (values != null) {
            Csv.writeCsvRow(sw, values);
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MeasurementsInfo(org.spf4j.perf.MeasurementsInfo) JmxExport(org.spf4j.jmx.JmxExport)

Example 13 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class Sampler method start.

@JmxExport(description = "start stack sampling")
public synchronized void start() {
    if (stopped) {
        stopped = false;
        final int stNanos = sampleTimeNanos;
        samplerFuture = DefaultExecutor.INSTANCE.submit(new AbstractRunnable("SPF4J-Sampling-Thread") {

            @SuppressWarnings("SleepWhileInLoop")
            @SuppressFBWarnings({ "MDM_THREAD_YIELD", "PREDICTABLE_RANDOM" })
            @Override
            public void doRun() throws IOException, InterruptedException {
                synchronized (sync) {
                    stackCollector = stackCollectorSupp.get(Thread.currentThread());
                }
                final ThreadLocalRandom random = ThreadLocalRandom.current();
                int dumpCounterNanos = 0;
                int coarseCounter = 0;
                int coarseCount = STOP_FLAG_READ_MILLIS / stNanos;
                boolean lstopped = stopped;
                int sleepTimeNanos = 0;
                int halfStNanos = stNanos / 2;
                if (halfStNanos == 0) {
                    halfStNanos = 1;
                }
                int maxSleeepNanos = stNanos + halfStNanos;
                while (!lstopped) {
                    synchronized (sync) {
                        stackCollector.sample();
                    }
                    dumpCounterNanos += sleepTimeNanos;
                    coarseCounter++;
                    if (coarseCounter >= coarseCount) {
                        coarseCounter = 0;
                        lstopped = stopped;
                    }
                    if (dumpCounterNanos >= dumpTimeNanos) {
                        long nanosSinceLastDump = TimeSource.nanoTime() - lastDumpTimeNanos;
                        if (nanosSinceLastDump >= dumpTimeNanos) {
                            dumpCounterNanos = 0;
                            dumpToFile();
                        } else {
                            dumpCounterNanos -= dumpTimeNanos - nanosSinceLastDump;
                        }
                    }
                    sleepTimeNanos = random.nextInt(halfStNanos, maxSleeepNanos);
                    TimeUnit.NANOSECONDS.sleep(sleepTimeNanos);
                }
            }
        });
    } else {
        throw new IllegalStateException("Sampling can only be started once for " + this);
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) JmxExport(org.spf4j.jmx.JmxExport)

Example 14 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class ThreadUsageSampler method start.

@JmxExport
public static synchronized void start(@JmxExport("sampleTimeMillis") final int sampleTime, @JmxExport("withStackTraces") final boolean withStackTraces) {
    if (samplingFuture == null) {
        final MeasurementRecorder cpuUsage = RecorderFactory.createDirectRecorder("peak-thread-count", "count", sampleTime);
        samplingFuture = DefaultScheduler.INSTANCE.scheduleWithFixedDelay(new AbstractRunnable() {

            private int maxThreadsNr = 0;

            @Override
            public void doRun() {
                final int peakThreadCount = TH_BEAN.getPeakThreadCount();
                cpuUsage.record(peakThreadCount);
                if (peakThreadCount > maxThreadsNr) {
                    Thread[] ths = Threads.getThreads();
                    if (ths.length > PEAK_THREAD_NAMES.size()) {
                        if (withStackTraces) {
                            StackTraceElement[][] stackTraces = Threads.getStackTraces(ths);
                            PEAK_THREAD_TRACES.clear();
                            PEAK_THREAD_TRACES.addAll(Arrays.asList(stackTraces));
                        }
                        PEAK_THREAD_NAMES.clear();
                        int i = 0;
                        for (Thread th : ths) {
                            PEAK_THREAD_NAMES.add(th.getName());
                            if (th.isDaemon()) {
                                PEAK_THREAD_DAEMON.set(i);
                            } else {
                                PEAK_THREAD_DAEMON.clear(i);
                            }
                            i++;
                        }
                    }
                    maxThreadsNr = peakThreadCount;
                }
                TH_BEAN.resetPeakThreadCount();
            }
        }, sampleTime, sampleTime, TimeUnit.MILLISECONDS);
    } else {
        throw new IllegalStateException("Thread sampling already started " + samplingFuture);
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) MeasurementRecorder(org.spf4j.perf.MeasurementRecorder) JmxExport(org.spf4j.jmx.JmxExport)

Aggregations

JmxExport (org.spf4j.jmx.JmxExport)14 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)4 AbstractRunnable (org.spf4j.base.AbstractRunnable)4 IOException (java.io.IOException)3 MeasurementRecorder (org.spf4j.perf.MeasurementRecorder)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 TObjectLongMap (gnu.trove.map.TObjectLongMap)2 TObjectLongHashMap (gnu.trove.map.hash.TObjectLongHashMap)2 StringWriter (java.io.StringWriter)2 UncheckedIOException (java.io.UncheckedIOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MeasurementsInfo (org.spf4j.perf.MeasurementsInfo)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1