/*
 *  Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
 *  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *  This code is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 only, as
 *  published by the Free Software Foundation.  Oracle designates this
 *  particular file as subject to the "Classpath" exception as provided
 *  by Oracle in the LICENSE file that accompanied this code.
 *
 *  This code 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
 *  version 2 for more details (a copy is included in the LICENSE file that
 *  accompanied this code).
 *
 *  You should have received a copy of the GNU General Public License version
 *  2 along with this work; if not, write to the Free Software Foundation,
 *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 *  or visit www.oracle.com if you need additional information or have any
 *  questions.
 */

package org.graalvm.visualvm.modules.tracer.dtrace;

import org.graalvm.visualvm.application.Application;
import org.graalvm.visualvm.modules.tracer.ItemValueFormatter;
import org.graalvm.visualvm.modules.tracer.ProbeItemDescriptor;
import org.graalvm.visualvm.modules.tracer.TracerProbe;
import org.graalvm.visualvm.modules.tracer.TracerProbeDescriptor;
import org.graalvm.visualvm.modules.tracer.TracerProgressObject;
import java.util.List;
import javax.swing.Icon;
import org.openide.util.Exceptions;
import org.opensolaris.os.dtrace.Aggregate;
import org.opensolaris.os.dtrace.Aggregation;
import org.opensolaris.os.dtrace.AggregationRecord;
import org.opensolaris.os.dtrace.Consumer;
import org.opensolaris.os.dtrace.DTraceException;

/**
 *
 * @author Tomas Hurka
 */
class CpusMonitorProbe extends TracerProbe.SessionAware<Application> {

    private static final String NAME = "CPUs usage";
    private static final String DESCR = "Monitors CPUs usage.";
    private static final int POSITION = 10;
    private static final String AGGREG_VALUE = "cpu";
    private final int processorsCount;
    private long lastValues[];
    private DTracePackage dtrace;
    private long lastTime;

    CpusMonitorProbe(DTracePackage dt) {
        super(createItemDescriptors(dt.getProcessors()));
        processorsCount = getItemsCount();
        dtrace = dt;
    }

    static final TracerProbeDescriptor createDescriptor(Icon icon, boolean available) {
        return new TracerProbeDescriptor(NAME, DESCR, icon, POSITION, available);
    }

    private static final ProbeItemDescriptor[] createItemDescriptors(int procs) {
        ProbeItemDescriptor[] descs = new ProbeItemDescriptor[procs];

        for (int i = 0; i < procs; i++) {
            descs[i] = ProbeItemDescriptor.continuousLineItem("CPU" + i + " usage",
                    "Monitors relative CPU" + i + " usage (%)",
                    ItemValueFormatter.DEFAULT_PERCENT, 1d, 0, 1000);
        }
        return descs;
    }

    @Override
    protected TracerProgressObject sessionInitializing(Application dataSource, int refresh) {
        lastTime = 0;
        lastValues = null;
        return super.sessionInitializing(dataSource, refresh);
    }


    @Override
    public long[] getItemValues(long timestamp) {
        long time;
        long vals[];
        long diffs[] = new long[processorsCount];
        try {
            time = System.currentTimeMillis();
            vals = getCurrentValues();
            if (lastValues != null) {
                long delta = time - lastTime;
                for (int i = 0; i < vals.length; i++) {
                    diffs[i] = (vals[i] - lastValues[i]) / (1000 * delta);
                }
            }
        } catch (DTraceException ex) {
            Exceptions.printStackTrace(ex);
            return diffs;
        }
        lastTime = time;
        lastValues = vals;
        return diffs;
    }

    private long[] getCurrentValues() throws DTraceException {
        long vals[] = new long[processorsCount];
        Consumer c = dtrace.getConsumer();
        Aggregate result = c.getAggregate();
        Aggregation agg = result.getAggregation(AGGREG_VALUE);
        if (agg != null) {
            List<AggregationRecord> records = agg.getRecords();

            for (AggregationRecord record : records) {
                Number cpu = (Number) record.getTuple().get(0).getValue();
                Number val = record.getValue().getValue();
                vals[cpu.intValue()] = val.longValue();
            }
        }
        return vals;
    }

}
