org.eclipse.linuxtools.oprofile.core.Oprofile.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.linuxtools.oprofile.core.Oprofile.java

Source

/*******************************************************************************
 * Copyright (c) 2004, 2008, 2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Keith Seitz <keiths@redhat.com> - initial API and implementation
 *    Kent Sebastian <ksebasti@redhat.com> - 
 *******************************************************************************/

package org.eclipse.linuxtools.oprofile.core;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.linuxtools.oprofile.core.daemon.OpEvent;
import org.eclipse.linuxtools.oprofile.core.daemon.OpInfo;
import org.eclipse.linuxtools.oprofile.core.model.OpModelEvent;
import org.eclipse.linuxtools.oprofile.core.model.OpModelImage;
import org.eclipse.linuxtools.oprofile.core.opxml.checkevent.CheckEventsProcessor;

/**
 * Common class wrapper for all things Oprofile.
 */
public class Oprofile {
    // Ugh. Need to know whether the module is loaded without running oprofile commands...
    private static final String[] _OPROFILE_CPU_TYPE_FILES = { "/dev/oprofile/cpu_type", //$NON-NLS-1$
            "/proc/sys/dev/oprofile/cpu_type" //$NON-NLS-1$
    };

    // Oprofile information
    private static OpInfo _info;

    // Make sure that oprofile is ready to go
    static {
        initializeOprofileModule();
    }

    /**
     * Initialize the oprofile module
     * 
     * This function will check if the kernel module is
     * loaded. If it is not, it will attempt to load it
     * (which will cause the system to prompt the user for
     * root access).
     */
    static private void initializeOprofileModule() {
        // Check if kernel module is loaded, if not, try to load it
        if (!isKernelModuleLoaded())
            _initializeOprofile();

        //it still may not have loaded, if not, critical error
        if (!isKernelModuleLoaded()) {
            OprofileCorePlugin.showErrorDialog("oprofileInit", null); //$NON-NLS-1$
            throw new ExceptionInInitializerError(OprofileProperties.getString("fatal.kernelModuleNotLoaded")); //$NON-NLS-1$
        } else {
            _initializeOprofileCore();
        }
    }

    // This requires more inside knowledge about Oprofile than one would like,
    // but it is the only way of knowing whether the module is loaded (and we can
    // succesfully call into the oprofile wrapper library without causing it to print out
    // a lot of warnings).
    private static boolean isKernelModuleLoaded() {
        for (int i = 0; i < _OPROFILE_CPU_TYPE_FILES.length; ++i) {
            File f = new File(_OPROFILE_CPU_TYPE_FILES[i]);
            if (f.exists())
                return true;
        }

        return false;
    }

    // initialize oprofile module by calling `opcontrol --init`
    private static void _initializeOprofile() {
        try {
            OprofileCorePlugin.getDefault().getOpcontrolProvider().initModule();
        } catch (OpcontrolException e) {
            OprofileCorePlugin.showErrorDialog("opcontrolProvider", e); //$NON-NLS-1$
        }
    }

    // Initializes static data for oprofile.   
    private static void _initializeOprofileCore() {
        _info = OpInfo.getInfo();

        if (_info == null) {
            throw new ExceptionInInitializerError(OprofileProperties.getString("fatal.opinfoNotParsed")); //$NON-NLS-1$
        }
    }

    /**
     * Queries oprofile for the number of counters on the current CPU.
     * Used only in launch config tabs.
     * @return the number of counters
     */
    public static int getNumberOfCounters() {
        return _info.getNrCounters();
    }

    /**
     * Returns the CPU speed of the current configuration.
     * @return the cpu speed in MHz
     */
    public static double getCpuFrequency() {
        return _info.getCPUSpeed();
    }

    /**
     * Finds the event with the given name
     * @param name the event's name (i.e., CPU_CLK_UNHALTED)
     * @return the event or <code>null</code> if not found
     */
    public static OpEvent findEvent(String name) {
        return _info.findEvent(name);
    }

    /**
     * Get all the events that may be collected on the given counter.
     * @param num the counter number
     * @return an array of all valid events -- NEVER RETURNS NULL!
     */
    public static OpEvent[] getEvents(int num) {
        return _info.getEvents(num);
    }

    /**
     * Returns the default location of the oprofile samples directory.
     * @return the default samples directory
     */
    public static String getDefaultSamplesDirectory() {
        return _info.getDefault(OpInfo.DEFAULT_SAMPLE_DIR);
    }

    /**
     * Returns the oprofile daemon log file.
     * @return the log file (absolute pathname)
     */
    public static String getLogFile() {
        return _info.getDefault(OpInfo.DEFAULT_LOG_FILE);
    }

    /**
     * Returns whether or not oprofile is in timer mode.
     * @return true if oprofile is in timer mode, false otherwise
     */
    public static boolean getTimerMode() {
        return _info.getTimerMode();
    }

    /**
     * Checks the requested counter, event, and unit mask for vailidity.
     * @param ctr   the counter
     * @param event   the event number
     * @param um   the unit mask
     * @return whether the requested event is valid
     */
    public static Boolean checkEvent(int ctr, int event, int um) {
        int[] validResult = new int[1];
        try {
            IRunnableWithProgress opxml = OprofileCorePlugin.getDefault().getOpxmlProvider().checkEvents(ctr, event,
                    um, validResult);
            opxml.run(null);
        } catch (InvocationTargetException e) {
        } catch (InterruptedException e) {
        } catch (OpxmlException e) {
            OprofileCorePlugin.showErrorDialog("opxmlProvider", e); //$NON-NLS-1$
            return null;
        }

        return (validResult[0] == CheckEventsProcessor.EVENT_OK);
    }

    /**
     * Returns a list of all the events collected on the system, as well as
     * the sessions under each of them.
     * @returns a list of all collected events
     */
    public static OpModelEvent[] getEvents() {
        OpModelEvent[] events = null;

        ArrayList<OpModelEvent> sessionList = new ArrayList<OpModelEvent>();
        try {
            IRunnableWithProgress opxml = OprofileCorePlugin.getDefault().getOpxmlProvider().sessions(sessionList);
            opxml.run(null);
            events = new OpModelEvent[sessionList.size()];
            sessionList.toArray(events);
        } catch (InvocationTargetException e) {
        } catch (InterruptedException e) {
        } catch (OpxmlException e) {
            OprofileCorePlugin.showErrorDialog("opxmlProvider", e); //$NON-NLS-1$
        }
        return events;
    }

    /**
     * Return a list of all the Samples in the given session.
     * @param session the session for which to get samples
     * @param shell the composite shell to use for the progress dialog
     */
    public static OpModelImage getModelData(String eventName, String sessionName) {
        OpModelImage image = new OpModelImage();

        final IRunnableWithProgress opxml;
        try {
            opxml = OprofileCorePlugin.getDefault().getOpxmlProvider().modelData(eventName, sessionName, image);
            opxml.run(null);
        } catch (InvocationTargetException e) {
        } catch (InterruptedException e) {
        } catch (OpxmlException e) {
            OprofileCorePlugin.showErrorDialog("opxmlProvider", e); //$NON-NLS-1$
            return null;
        }

        return image;
    }
}