com.google.common.collect,
org.swtchart
Export-Package: org.eclipse.tracecompass.analysis.os.linux.ui.views.controlflow,
- org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage,
org.eclipse.tracecompass.analysis.os.linux.ui.views.resources,
org.eclipse.tracecompass.internal.analysis.os.linux.ui;x-friends:="org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests",
org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions;x-internal:=true,
org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow;x-friends:="org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests,org.eclipse.tracecompass.analysis.os.linux.ui.tests",
org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.io.diskioactivity;x-internal:=true,
org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.kernelmemoryusage;x-internal:=true,
+ org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage;x-internal:=true,
org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.latency;x-friends:="org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests",
org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.latency.statistics;x-friends:="org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests",
org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.resources;x-friends:="org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests"
<view
allowMultiple="false"
category="org.eclipse.linuxtools.lttng2.ui.views.category"
- class="org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage.CpuUsageView"
+ class="org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage.CpuUsageView"
icon="icons/eview16/cpu-usage.png"
id="org.eclipse.tracecompass.analysis.os.linux.views.cpuusage"
name="%cpuusage.view.name"
+++ /dev/null
-/*******************************************************************************
- * Copyright (c) 2014, 2015 École Polytechnique de Montréal
- *
- * 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:
- * Geneviève Bastien - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage;
-
-import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.TreeSet;
-
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.jface.viewers.ViewerComparator;
-import org.eclipse.osgi.util.NLS;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis;
-import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
-import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
-import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
-import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
-import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
-import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
-import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
-import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
-import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
-import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
-import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
-import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer;
-import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider;
-import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeViewerEntry;
-import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData;
-import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData.ITmfColumnPercentageProvider;
-import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
-
-/**
- * Tree viewer to display CPU usage information in a specified time range. It
- * shows the process's TID, its name, the time spent on the CPU during that
- * range, in % and absolute value.
- *
- * @author Geneviève Bastien
- */
-public class CpuUsageComposite extends AbstractTmfTreeViewer {
-
- // Timeout between to wait for in the updateElements method
- private static final long BUILD_UPDATE_TIMEOUT = 500;
-
- private KernelCpuUsageAnalysis fModule = null;
- private String fSelectedThread = null;
-
- private static final String[] COLUMN_NAMES = new String[] {
- Messages.CpuUsageComposite_ColumnTID,
- Messages.CpuUsageComposite_ColumnProcess,
- Messages.CpuUsageComposite_ColumnPercent,
- Messages.CpuUsageComposite_ColumnTime
- };
-
- /* A map that saves the mapping of a thread ID to its executable name */
- private final Map<String, String> fProcessNameMap = new HashMap<>();
-
- private final @NonNull Set<@NonNull Integer> fCpus = new TreeSet<>();
-
- /** Provides label for the CPU usage tree viewer cells */
- protected static class CpuLabelProvider extends TreeLabelProvider {
-
- @Override
- public String getColumnText(Object element, int columnIndex) {
- CpuUsageEntry obj = (CpuUsageEntry) element;
- if (columnIndex == 0) {
- return obj.getTid();
- } else if (columnIndex == 1) {
- return obj.getProcessName();
- } else if (columnIndex == 2) {
- return String.format(Messages.CpuUsageComposite_TextPercent, obj.getPercent());
- } else if (columnIndex == 3) {
- return NLS.bind(Messages.CpuUsageComposite_TextTime, obj.getTime());
- }
-
- return element.toString();
- }
-
- }
-
- /**
- * Constructor
- *
- * @param parent
- * The parent composite that holds this viewer
- */
- public CpuUsageComposite(Composite parent) {
- super(parent, false);
- setLabelProvider(new CpuLabelProvider());
- }
-
- @Override
- protected ITmfTreeColumnDataProvider getColumnDataProvider() {
- return new ITmfTreeColumnDataProvider() {
-
- @Override
- public List<TmfTreeColumnData> getColumnData() {
- /* All columns are sortable */
- List<TmfTreeColumnData> columns = new ArrayList<>();
- TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]);
- column.setComparator(new ViewerComparator() {
- @Override
- public int compare(Viewer viewer, Object e1, Object e2) {
- CpuUsageEntry n1 = (CpuUsageEntry) e1;
- CpuUsageEntry n2 = (CpuUsageEntry) e2;
-
- return n1.getTid().compareTo(n2.getTid());
-
- }
- });
- columns.add(column);
- column = new TmfTreeColumnData(COLUMN_NAMES[1]);
- column.setComparator(new ViewerComparator() {
- @Override
- public int compare(Viewer viewer, Object e1, Object e2) {
- CpuUsageEntry n1 = (CpuUsageEntry) e1;
- CpuUsageEntry n2 = (CpuUsageEntry) e2;
-
- return n1.getProcessName().compareTo(n2.getProcessName());
-
- }
- });
- columns.add(column);
- column = new TmfTreeColumnData(COLUMN_NAMES[2]);
- column.setComparator(new ViewerComparator() {
- @Override
- public int compare(Viewer viewer, Object e1, Object e2) {
- CpuUsageEntry n1 = (CpuUsageEntry) e1;
- CpuUsageEntry n2 = (CpuUsageEntry) e2;
-
- return n1.getPercent().compareTo(n2.getPercent());
-
- }
- });
- column.setPercentageProvider(new ITmfColumnPercentageProvider() {
-
- @Override
- public double getPercentage(Object data) {
- CpuUsageEntry parent = (CpuUsageEntry) data;
- return parent.getPercent() / 100;
- }
- });
- columns.add(column);
- column = new TmfTreeColumnData(COLUMN_NAMES[3]);
- column.setComparator(new ViewerComparator() {
- @Override
- public int compare(Viewer viewer, Object e1, Object e2) {
- CpuUsageEntry n1 = (CpuUsageEntry) e1;
- CpuUsageEntry n2 = (CpuUsageEntry) e2;
-
- return n1.getTime().compareTo(n2.getTime());
-
- }
- });
- columns.add(column);
-
- return columns;
- }
-
- };
- }
-
- // ------------------------------------------------------------------------
- // Operations
- // ------------------------------------------------------------------------
-
- @Override
- protected void contentChanged(ITmfTreeViewerEntry rootEntry) {
- String selectedThread = fSelectedThread;
- if (selectedThread != null) {
- /* Find the selected thread among the inputs */
- for (ITmfTreeViewerEntry entry : rootEntry.getChildren()) {
- if (entry instanceof CpuUsageEntry) {
- if (selectedThread.equals(((CpuUsageEntry) entry).getTid())) {
- List<ITmfTreeViewerEntry> list = Collections.singletonList(entry);
- super.setSelection(list);
- return;
- }
- }
- }
- }
- }
-
- @Override
- public void initializeDataSource() {
- /* Should not be called while trace is still null */
- ITmfTrace trace = checkNotNull(getTrace());
-
- fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID);
- if (fModule == null) {
- return;
- }
- fModule.schedule();
- fModule.waitForInitialization();
- fProcessNameMap.clear();
- }
-
- @Override
- protected ITmfTreeViewerEntry updateElements(long start, long end, boolean isSelection) {
- if (isSelection || (start == end)) {
- return null;
- }
- if (getTrace() == null || fModule == null) {
- return null;
- }
- fModule.waitForInitialization();
- ITmfStateSystem ss = fModule.getStateSystem();
- if (ss == null) {
- return null;
- }
-
- boolean complete = false;
- long currentEnd = start;
-
- while (!complete && currentEnd < end) {
- complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
- currentEnd = ss.getCurrentEndTime();
- }
-
- /* Initialize the data */
- Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(fCpus, Math.max(start, getStartTime()), Math.min(end, getEndTime()));
-
- TmfTreeViewerEntry root = new TmfTreeViewerEntry(""); //$NON-NLS-1$
- List<ITmfTreeViewerEntry> entryList = root.getChildren();
-
- for (Entry<String, Long> entry : cpuUsageMap.entrySet()) {
- /*
- * Process only entries representing the total of all CPUs and that
- * have time on CPU
- */
- if (entry.getValue() == 0) {
- continue;
- }
- if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) {
- continue;
- }
- String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2);
-
- if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) {
- CpuUsageEntry obj = new CpuUsageEntry(strings[1], getProcessName(strings[1]), (double) entry.getValue() / (double) (end - start) * 100, entry.getValue());
- entryList.add(obj);
- }
- }
-
- return root;
- }
-
- /*
- * Get the process name from its TID by using the LTTng kernel analysis
- * module
- */
- private String getProcessName(String tid) {
- String execName = fProcessNameMap.get(tid);
- if (execName != null) {
- return execName;
- }
- ITmfTrace trace = getTrace();
- if (trace == null) {
- return tid;
- }
- ITmfStateSystem kernelSs = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
- if (kernelSs == null) {
- return tid;
- }
-
- try {
- int cpusNode = kernelSs.getQuarkAbsolute(Attributes.THREADS);
-
- /* Get the quarks for each cpu */
- List<Integer> cpuNodes = kernelSs.getSubAttributes(cpusNode, false);
-
- for (Integer tidQuark : cpuNodes) {
- if (kernelSs.getAttributeName(tidQuark).equals(tid)) {
- int execNameQuark;
- List<ITmfStateInterval> execNameIntervals;
- try {
- execNameQuark = kernelSs.getQuarkRelative(tidQuark, Attributes.EXEC_NAME);
- execNameIntervals = StateSystemUtils.queryHistoryRange(kernelSs, execNameQuark, getStartTime(), getEndTime());
- } catch (AttributeNotFoundException e) {
- /*
- * No information on this thread (yet?), skip it for now
- */
- continue;
- } catch (StateSystemDisposedException e) {
- /* State system is closing down, no point continuing */
- break;
- }
-
- for (ITmfStateInterval execNameInterval : execNameIntervals) {
- if (!execNameInterval.getStateValue().isNull() &&
- execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
- execName = execNameInterval.getStateValue().unboxStr();
- fProcessNameMap.put(tid, execName);
- return execName;
- }
- }
- }
- }
-
- } catch (AttributeNotFoundException e) {
- /* can't find the process name, just return the tid instead */
- }
- return tid;
- }
-
- /**
- * Set the currently selected thread ID
- *
- * @param tid
- * The selected thread ID
- */
- public void setSelectedThread(String tid) {
- fSelectedThread = tid;
- }
-
- /**
- * Add a core
- *
- * @param core
- * the core to add
- * @since 2.0
- */
- public void addCpu(int core) {
- fCpus.add(core);
- updateContent(getWindowStartTime(), getWindowEndTime(), false);
- }
-
- /**
- * Remove a core
- *
- * @param core
- * the core to remove
- * @since 2.0
- */
- public void removeCpu(int core) {
- fCpus.remove(core);
- updateContent(getWindowStartTime(), getWindowEndTime(), false);
- }
-
- /**
- * Clears the cores
- *
- * @since 2.0
- *
- */
- public void clearCpu() {
- fCpus.clear();
- updateContent(getWindowStartTime(), getWindowEndTime(), false);
- }
-
-}
+++ /dev/null
-/*******************************************************************************
- * Copyright (c) 2014, 2015 École Polytechnique de Montréal
- *
- * 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:
- * Geneviève Bastien - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage;
-
-import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
-
-/**
- * Represents an entry in the tree viewer of the CPU usage view. An entry is a
- * thread that occupied part of the CPU in the selected time range.
- *
- * @author Geneviève Bastien
- */
-public class CpuUsageEntry extends TmfTreeViewerEntry {
- private final String fTid;
- private final String fProcessName;
- private final Double fPercent;
- private final Long fTime;
-
- /**
- * Constructor
- *
- * @param tid
- * The TID of the process
- * @param name
- * The thread's name
- * @param percent
- * The percentage CPU usage
- * @param time
- * The total amount of time spent on CPU
- */
- public CpuUsageEntry(String tid, String name, double percent, long time) {
- super(tid);
- fTid = tid;
- fProcessName = name;
- fPercent = percent;
- fTime = time;
- }
-
- /**
- * Get the TID of the thread represented by this entry
- *
- * @return The thread's TID
- */
- public String getTid() {
- return fTid;
- }
-
- /**
- * Get the process name
- *
- * @return The process name
- */
- public String getProcessName() {
- return fProcessName;
- }
-
- /**
- * Get the percentage of time spent on CPU in the time interval represented
- * by this entry.
- *
- * @return The percentage of time spent on CPU
- */
- public Double getPercent() {
- return fPercent;
- }
-
- /**
- * Get the total time spent on CPU in the time interval represented by this
- * entry.
- *
- * @return The total time spent on CPU
- */
- public Long getTime() {
- return fTime;
- }
-}
+++ /dev/null
-/*******************************************************************************
- * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others
- *
- * 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:
- * Geneviève Bastien - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage;
-
-import org.eclipse.jdt.annotation.Nullable;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
-import org.eclipse.swt.events.ControlAdapter;
-import org.eclipse.swt.events.ControlEvent;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal;
-import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
-import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
-import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
-import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
-import org.eclipse.tracecompass.tmf.ui.viewers.TmfViewer;
-import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer;
-import org.eclipse.tracecompass.tmf.ui.views.TmfChartView;
-
-/**
- * CPU usage view. It contains 2 viewers: one tree viewer showing all the
- * threads who were on the CPU in the time range, and one XY chart viewer
- * plotting the total time spent on CPU and the time of the threads selected in
- * the tree viewer.
- *
- * @author Geneviève Bastien
- */
-public class CpuUsageView extends TmfChartView {
-
- /** ID string */
- public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.cpuusage"; //$NON-NLS-1$
-
- private @Nullable CpuUsageComposite fTreeViewer = null;
- private @Nullable CpuUsageXYViewer fXYViewer = null;
-
- /**
- * Constructor
- */
- public CpuUsageView() {
- super(Messages.CpuUsageView_Title);
- }
-
- @Override
- public void createPartControl(Composite parent) {
- super.createPartControl(parent);
-
- /* Initialize the viewers with the currently selected trace */
- ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
- if (trace != null) {
- TmfTraceSelectedSignal signal = new TmfTraceSelectedSignal(this, trace);
- if (fTreeViewer != null) {
- fTreeViewer.traceSelected(signal);
- }
- if (fXYViewer != null) {
- fXYViewer.traceSelected(signal);
- }
- }
- }
-
- @Override
- protected TmfXYChartViewer createChartViewer(Composite parent) {
- CpuUsageXYViewer viewer = new CpuUsageXYViewer(parent);
- viewer.setSendTimeAlignSignals(true);
- fXYViewer = viewer;
- return viewer;
- }
-
- @Override
- public TmfViewer createLeftChildViewer(Composite parent) {
- final CpuUsageComposite viewer = new CpuUsageComposite(parent);
-
- /* Add selection listener to tree viewer */
- viewer.addSelectionChangeListener(new ISelectionChangedListener() {
- @Override
- public void selectionChanged(SelectionChangedEvent event) {
- ISelection selection = event.getSelection();
- if (selection instanceof IStructuredSelection) {
- Object structSelection = ((IStructuredSelection) selection).getFirstElement();
- if (structSelection instanceof CpuUsageEntry) {
- CpuUsageEntry entry = (CpuUsageEntry) structSelection;
- if (fTreeViewer != null) {
- fTreeViewer.setSelectedThread(entry.getTid());
- }
- if (fXYViewer != null) {
- fXYViewer.setSelectedThread(Long.valueOf(entry.getTid()));
- }
- }
- }
- }
- });
-
- viewer.getControl().addControlListener(new ControlAdapter() {
- @Override
- public void controlResized(ControlEvent e) {
- super.controlResized(e);
- }
- });
-
- fTreeViewer = viewer;
- return fTreeViewer;
- }
-
- @Override
- public void setFocus() {
- if (fXYViewer != null) {
- fXYViewer.getControl().setFocus();
- }
- }
-
- /**
- * Signal handler for when a cpu is selected
- *
- * @param signal
- * the cpu being selected
- * @since 2.0
- */
- @TmfSignalHandler
- public void cpuSelect(TmfCpuSelectedSignal signal) {
- final @Nullable CpuUsageXYViewer xyViewer = fXYViewer;
- final @Nullable CpuUsageComposite treeViewer = fTreeViewer;
- if (xyViewer != null && treeViewer != null) {
- int core = signal.getCore();
- if (core >= 0) {
- xyViewer.addCpu(core);
- treeViewer.addCpu(core);
- } else {
- xyViewer.clearCpu();
- treeViewer.clearCpu();
- }
- }
- }
-
-}
+++ /dev/null
-/*******************************************************************************
- * Copyright (c) 2014, 2015 École Polytechnique de Montréal
- *
- * 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:
- * Geneviève Bastien - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage;
-
-import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
-
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.TreeSet;
-
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis;
-import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
-import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
-import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
-import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
-import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
-import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
-
-import com.google.common.base.Joiner;
-
-/**
- * CPU usage viewer with XY line chart. It displays the total CPU usage and that
- * of the threads selected in the CPU usage tree viewer.
- *
- * @author Geneviève Bastien
- */
-public class CpuUsageXYViewer extends TmfCommonXLineChartViewer {
-
- private KernelCpuUsageAnalysis fModule = null;
-
- /* Maps a thread ID to a list of y values */
- private final Map<String, double[]> fYValues = new LinkedHashMap<>();
- /*
- * To avoid up and downs CPU usage when process is in and out of CPU
- * frequently, use a smaller resolution to get better averages.
- */
- private static final double RESOLUTION = 0.4;
-
- // Timeout between updates in the updateData thread
- private static final long BUILD_UPDATE_TIMEOUT = 500;
-
- private long fSelectedThread = -1;
-
- private final @NonNull Set<@NonNull Integer> fCpus = new TreeSet<>();
-
- /**
- * Constructor
- *
- * @param parent
- * parent composite
- */
- public CpuUsageXYViewer(Composite parent) {
- super(parent, Messages.CpuUsageXYViewer_Title, Messages.CpuUsageXYViewer_TimeXAxis, Messages.CpuUsageXYViewer_CpuYAxis);
- setResolution(RESOLUTION);
- }
-
- @Override
- protected void initializeDataSource() {
- ITmfTrace trace = getTrace();
- if (trace != null) {
- fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID);
- if (fModule == null) {
- return;
- }
- fModule.schedule();
- }
- }
-
- private static double[] zeroFill(int nb) {
- double[] arr = new double[nb];
- Arrays.fill(arr, 0.0);
- return arr;
- }
-
- @Override
- protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
- try {
- if (getTrace() == null || fModule == null) {
- return;
- }
- fModule.waitForInitialization();
- ITmfStateSystem ss = fModule.getStateSystem();
- if (ss == null) {
- return;
- }
- double[] xvalues = getXAxis(start, end, nb);
- if (xvalues.length == 0) {
- return;
- }
- setXAxis(xvalues);
-
- boolean complete = false;
- long currentEnd = start;
-
- while (!complete && currentEnd < end) {
-
- if (monitor.isCanceled()) {
- return;
- }
-
- long traceStart = getStartTime();
- long traceEnd = getEndTime();
- long offset = getTimeOffset();
- long selectedThread = fSelectedThread;
-
- complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
- currentEnd = ss.getCurrentEndTime();
-
- /* Initialize the data */
- Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(fCpus, Math.max(start, traceStart), Math.min(end, traceEnd));
- Map<String, String> totalEntries = new HashMap<>();
- fYValues.clear();
- fYValues.put(Messages.CpuUsageXYViewer_Total, zeroFill(xvalues.length));
- String stringSelectedThread = Long.toString(selectedThread);
- if (selectedThread != -1) {
- fYValues.put(stringSelectedThread, zeroFill(xvalues.length));
- }
-
- for (Entry<String, Long> entry : cpuUsageMap.entrySet()) {
- /*
- * Process only entries representing the total of all CPUs
- * and that have time on CPU
- */
- if (entry.getValue() == 0) {
- continue;
- }
- if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) {
- continue;
- }
- String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2);
-
- if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) {
- /* This is the total cpu usage for a thread */
- totalEntries.put(strings[1], entry.getKey());
- }
- }
-
- double prevX = xvalues[0] - 1;
- long prevTime = (long) prevX + offset;
- /*
- * make sure that time is in the trace range after double to
- * long conversion
- */
- prevTime = Math.max(traceStart, prevTime);
- prevTime = Math.min(traceEnd, prevTime);
- /* Get CPU usage statistics for each x value */
- for (int i = 0; i < xvalues.length; i++) {
- if (monitor.isCanceled()) {
- return;
- }
- long totalCpu = 0;
- double x = xvalues[i];
- long time = (long) x + offset;
- time = Math.max(traceStart, time);
- time = Math.min(traceEnd, time);
- if (time == prevTime) {
- /*
- * we need at least 1 time unit to be able to get cpu
- * usage when zoomed in
- */
- prevTime = time - 1;
- }
-
- cpuUsageMap = fModule.getCpuUsageInRange(fCpus, prevTime, time);
-
- /*
- * Calculate the sum of all total entries, and add a data
- * point to the selected one
- */
- for (Entry<String, String> entry : totalEntries.entrySet()) {
- Long cpuEntry = cpuUsageMap.get(entry.getValue());
- cpuEntry = cpuEntry != null ? cpuEntry : 0L;
-
- totalCpu += cpuEntry;
-
- if (entry.getKey().equals(stringSelectedThread)) {
- /* This is the total cpu usage for a thread */
- double[] key = checkNotNull(fYValues.get(entry.getKey()));
- key[i] = (double) cpuEntry / (double) (time - prevTime) * 100;
- }
-
- }
- double[] key = checkNotNull(fYValues.get(Messages.CpuUsageXYViewer_Total));
- key[i] = (double) totalCpu / (double) (time - prevTime) * 100;
- prevTime = time;
- }
- for (Entry<String, double[]> entry : fYValues.entrySet()) {
- setSeries(entry.getKey(), entry.getValue());
- }
- if (monitor.isCanceled()) {
- return;
- }
- updateDisplay();
- }
- } catch (StateValueTypeException e) {
- Activator.getDefault().logError("Error updating the data of the CPU usage view", e); //$NON-NLS-1$
- }
-
- }
-
- /**
- * Set the selected thread ID, which will be graphed in this viewer
- *
- * @param tid
- * The selected thread ID
- */
- public void setSelectedThread(long tid) {
- cancelUpdate();
- deleteSeries(Long.toString(fSelectedThread));
- fSelectedThread = tid;
- updateContent();
- }
-
- /**
- * Gets the analysis module
- *
- * @return the {@link KernelCpuUsageAnalysis}
- *
- * @since 2.0
- */
- public KernelCpuUsageAnalysis getModule() {
- return fModule;
- }
-
- /**
- * Add a core
- *
- * @param core
- * the core to add
- * @since 2.0
- */
- public void addCpu(int core) {
- fCpus.add(core);
- cancelUpdate();
- updateContent();
- getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList());
- }
-
- /**
- * Remove a core
- *
- * @param core
- * the core to remove
- * @since 2.0
- */
- public void removeCpu(int core) {
- fCpus.remove(core);
- cancelUpdate();
- updateContent();
- getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList());
- }
-
- private String getCpuList() {
- return Joiner.on(", ").join(fCpus); //$NON-NLS-1$
- }
-
- /**
- * Clears the cores
- *
- * @since 2.0
- */
- public void clearCpu() {
- fCpus.clear();
- cancelUpdate();
- updateContent();
- getSwtChart().getTitle().setText(Messages.CpuUsageView_Title);
- }
-
-}
+++ /dev/null
-/*******************************************************************************
- * Copyright (c) 2014, 2015 École Polytechnique de Montréal
- *
- * 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:
- * Geneviève Bastien - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage;
-
-import org.eclipse.osgi.util.NLS;
-
-/**
- * Messages used in the LTTng kernel CPU usage view and viewers.
- *
- * @author Geneviève Bastien
- */
-@SuppressWarnings("javadoc")
-public class Messages extends NLS {
-
- private static final String BUNDLE_NAME = "org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage.messages"; //$NON-NLS-1$
-
- public static String CpuUsageComposite_ColumnPercent;
- public static String CpuUsageComposite_ColumnProcess;
- public static String CpuUsageComposite_ColumnTID;
- public static String CpuUsageComposite_ColumnTime;
- public static String CpuUsageComposite_TextPercent;
- public static String CpuUsageComposite_TextTime;
- public static String CpuUsageView_Title;
- public static String CpuUsageXYViewer_CpuYAxis;
- public static String CpuUsageXYViewer_TimeXAxis;
- public static String CpuUsageXYViewer_Title;
- public static String CpuUsageXYViewer_Total;
-
- static {
- // initialize resource bundle
- NLS.initializeMessages(BUNDLE_NAME, Messages.class);
- }
-
- private Messages() {
- }
-}
+++ /dev/null
-###############################################################################
-# Copyright (c) 2014, 2015 École Polytechnique de Montréal
-#
-# 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:
-# Geneviève Bastien - Initial API and implementation
-###############################################################################
-
-CpuUsageComposite_ColumnPercent=%
-CpuUsageComposite_ColumnProcess=Process
-CpuUsageComposite_ColumnTID=TID
-CpuUsageComposite_ColumnTime=Time
-CpuUsageComposite_TextPercent=%1$.3f %%
-CpuUsageComposite_TextTime={0} ns
-CpuUsageView_Title=CPU Usage
-CpuUsageXYViewer_CpuYAxis=% CPU
-CpuUsageXYViewer_TimeXAxis=Time
-CpuUsageXYViewer_Title=CPU usage
-CpuUsageXYViewer_Total=Total
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014, 2015 École Polytechnique de Montréal
+ *
+ * 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:
+ * Geneviève Bastien - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage;
+
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerComparator;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis;
+import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
+import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
+import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
+import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
+import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
+import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
+import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
+import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
+import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer;
+import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider;
+import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeViewerEntry;
+import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData;
+import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData.ITmfColumnPercentageProvider;
+import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
+
+/**
+ * Tree viewer to display CPU usage information in a specified time range. It
+ * shows the process's TID, its name, the time spent on the CPU during that
+ * range, in % and absolute value.
+ *
+ * @author Geneviève Bastien
+ */
+public class CpuUsageComposite extends AbstractTmfTreeViewer {
+
+ // Timeout between to wait for in the updateElements method
+ private static final long BUILD_UPDATE_TIMEOUT = 500;
+
+ private KernelCpuUsageAnalysis fModule = null;
+ private String fSelectedThread = null;
+
+ private static final String[] COLUMN_NAMES = new String[] {
+ Messages.CpuUsageComposite_ColumnTID,
+ Messages.CpuUsageComposite_ColumnProcess,
+ Messages.CpuUsageComposite_ColumnPercent,
+ Messages.CpuUsageComposite_ColumnTime
+ };
+
+ /* A map that saves the mapping of a thread ID to its executable name */
+ private final Map<String, String> fProcessNameMap = new HashMap<>();
+
+ private final @NonNull Set<@NonNull Integer> fCpus = new TreeSet<>();
+
+ /** Provides label for the CPU usage tree viewer cells */
+ protected static class CpuLabelProvider extends TreeLabelProvider {
+
+ @Override
+ public String getColumnText(Object element, int columnIndex) {
+ CpuUsageEntry obj = (CpuUsageEntry) element;
+ if (columnIndex == 0) {
+ return obj.getTid();
+ } else if (columnIndex == 1) {
+ return obj.getProcessName();
+ } else if (columnIndex == 2) {
+ return String.format(Messages.CpuUsageComposite_TextPercent, obj.getPercent());
+ } else if (columnIndex == 3) {
+ return NLS.bind(Messages.CpuUsageComposite_TextTime, obj.getTime());
+ }
+
+ return element.toString();
+ }
+
+ }
+
+ /**
+ * Constructor
+ *
+ * @param parent
+ * The parent composite that holds this viewer
+ */
+ public CpuUsageComposite(Composite parent) {
+ super(parent, false);
+ setLabelProvider(new CpuLabelProvider());
+ }
+
+ @Override
+ protected ITmfTreeColumnDataProvider getColumnDataProvider() {
+ return new ITmfTreeColumnDataProvider() {
+
+ @Override
+ public List<TmfTreeColumnData> getColumnData() {
+ /* All columns are sortable */
+ List<TmfTreeColumnData> columns = new ArrayList<>();
+ TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]);
+ column.setComparator(new ViewerComparator() {
+ @Override
+ public int compare(Viewer viewer, Object e1, Object e2) {
+ CpuUsageEntry n1 = (CpuUsageEntry) e1;
+ CpuUsageEntry n2 = (CpuUsageEntry) e2;
+
+ return n1.getTid().compareTo(n2.getTid());
+
+ }
+ });
+ columns.add(column);
+ column = new TmfTreeColumnData(COLUMN_NAMES[1]);
+ column.setComparator(new ViewerComparator() {
+ @Override
+ public int compare(Viewer viewer, Object e1, Object e2) {
+ CpuUsageEntry n1 = (CpuUsageEntry) e1;
+ CpuUsageEntry n2 = (CpuUsageEntry) e2;
+
+ return n1.getProcessName().compareTo(n2.getProcessName());
+
+ }
+ });
+ columns.add(column);
+ column = new TmfTreeColumnData(COLUMN_NAMES[2]);
+ column.setComparator(new ViewerComparator() {
+ @Override
+ public int compare(Viewer viewer, Object e1, Object e2) {
+ CpuUsageEntry n1 = (CpuUsageEntry) e1;
+ CpuUsageEntry n2 = (CpuUsageEntry) e2;
+
+ return n1.getPercent().compareTo(n2.getPercent());
+
+ }
+ });
+ column.setPercentageProvider(new ITmfColumnPercentageProvider() {
+
+ @Override
+ public double getPercentage(Object data) {
+ CpuUsageEntry parent = (CpuUsageEntry) data;
+ return parent.getPercent() / 100;
+ }
+ });
+ columns.add(column);
+ column = new TmfTreeColumnData(COLUMN_NAMES[3]);
+ column.setComparator(new ViewerComparator() {
+ @Override
+ public int compare(Viewer viewer, Object e1, Object e2) {
+ CpuUsageEntry n1 = (CpuUsageEntry) e1;
+ CpuUsageEntry n2 = (CpuUsageEntry) e2;
+
+ return n1.getTime().compareTo(n2.getTime());
+
+ }
+ });
+ columns.add(column);
+
+ return columns;
+ }
+
+ };
+ }
+
+ // ------------------------------------------------------------------------
+ // Operations
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected void contentChanged(ITmfTreeViewerEntry rootEntry) {
+ String selectedThread = fSelectedThread;
+ if (selectedThread != null) {
+ /* Find the selected thread among the inputs */
+ for (ITmfTreeViewerEntry entry : rootEntry.getChildren()) {
+ if (entry instanceof CpuUsageEntry) {
+ if (selectedThread.equals(((CpuUsageEntry) entry).getTid())) {
+ List<ITmfTreeViewerEntry> list = Collections.singletonList(entry);
+ super.setSelection(list);
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void initializeDataSource() {
+ /* Should not be called while trace is still null */
+ ITmfTrace trace = checkNotNull(getTrace());
+
+ fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID);
+ if (fModule == null) {
+ return;
+ }
+ fModule.schedule();
+ fModule.waitForInitialization();
+ fProcessNameMap.clear();
+ }
+
+ @Override
+ protected ITmfTreeViewerEntry updateElements(long start, long end, boolean isSelection) {
+ if (isSelection || (start == end)) {
+ return null;
+ }
+ if (getTrace() == null || fModule == null) {
+ return null;
+ }
+ fModule.waitForInitialization();
+ ITmfStateSystem ss = fModule.getStateSystem();
+ if (ss == null) {
+ return null;
+ }
+
+ boolean complete = false;
+ long currentEnd = start;
+
+ while (!complete && currentEnd < end) {
+ complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
+ currentEnd = ss.getCurrentEndTime();
+ }
+
+ /* Initialize the data */
+ Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(fCpus, Math.max(start, getStartTime()), Math.min(end, getEndTime()));
+
+ TmfTreeViewerEntry root = new TmfTreeViewerEntry(""); //$NON-NLS-1$
+ List<ITmfTreeViewerEntry> entryList = root.getChildren();
+
+ for (Entry<String, Long> entry : cpuUsageMap.entrySet()) {
+ /*
+ * Process only entries representing the total of all CPUs and that
+ * have time on CPU
+ */
+ if (entry.getValue() == 0) {
+ continue;
+ }
+ if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) {
+ continue;
+ }
+ String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2);
+
+ if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) {
+ CpuUsageEntry obj = new CpuUsageEntry(strings[1], getProcessName(strings[1]), (double) entry.getValue() / (double) (end - start) * 100, entry.getValue());
+ entryList.add(obj);
+ }
+ }
+
+ return root;
+ }
+
+ /*
+ * Get the process name from its TID by using the LTTng kernel analysis
+ * module
+ */
+ private String getProcessName(String tid) {
+ String execName = fProcessNameMap.get(tid);
+ if (execName != null) {
+ return execName;
+ }
+ ITmfTrace trace = getTrace();
+ if (trace == null) {
+ return tid;
+ }
+ ITmfStateSystem kernelSs = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
+ if (kernelSs == null) {
+ return tid;
+ }
+
+ try {
+ int cpusNode = kernelSs.getQuarkAbsolute(Attributes.THREADS);
+
+ /* Get the quarks for each cpu */
+ List<Integer> cpuNodes = kernelSs.getSubAttributes(cpusNode, false);
+
+ for (Integer tidQuark : cpuNodes) {
+ if (kernelSs.getAttributeName(tidQuark).equals(tid)) {
+ int execNameQuark;
+ List<ITmfStateInterval> execNameIntervals;
+ try {
+ execNameQuark = kernelSs.getQuarkRelative(tidQuark, Attributes.EXEC_NAME);
+ execNameIntervals = StateSystemUtils.queryHistoryRange(kernelSs, execNameQuark, getStartTime(), getEndTime());
+ } catch (AttributeNotFoundException e) {
+ /*
+ * No information on this thread (yet?), skip it for now
+ */
+ continue;
+ } catch (StateSystemDisposedException e) {
+ /* State system is closing down, no point continuing */
+ break;
+ }
+
+ for (ITmfStateInterval execNameInterval : execNameIntervals) {
+ if (!execNameInterval.getStateValue().isNull() &&
+ execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
+ execName = execNameInterval.getStateValue().unboxStr();
+ fProcessNameMap.put(tid, execName);
+ return execName;
+ }
+ }
+ }
+ }
+
+ } catch (AttributeNotFoundException e) {
+ /* can't find the process name, just return the tid instead */
+ }
+ return tid;
+ }
+
+ /**
+ * Set the currently selected thread ID
+ *
+ * @param tid
+ * The selected thread ID
+ */
+ public void setSelectedThread(String tid) {
+ fSelectedThread = tid;
+ }
+
+ /**
+ * Add a core
+ *
+ * @param core
+ * the core to add
+ * @since 2.0
+ */
+ public void addCpu(int core) {
+ fCpus.add(core);
+ updateContent(getWindowStartTime(), getWindowEndTime(), false);
+ }
+
+ /**
+ * Remove a core
+ *
+ * @param core
+ * the core to remove
+ * @since 2.0
+ */
+ public void removeCpu(int core) {
+ fCpus.remove(core);
+ updateContent(getWindowStartTime(), getWindowEndTime(), false);
+ }
+
+ /**
+ * Clears the cores
+ *
+ * @since 2.0
+ *
+ */
+ public void clearCpu() {
+ fCpus.clear();
+ updateContent(getWindowStartTime(), getWindowEndTime(), false);
+ }
+
+}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014, 2015 École Polytechnique de Montréal
+ *
+ * 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:
+ * Geneviève Bastien - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage;
+
+import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
+
+/**
+ * Represents an entry in the tree viewer of the CPU usage view. An entry is a
+ * thread that occupied part of the CPU in the selected time range.
+ *
+ * @author Geneviève Bastien
+ */
+public class CpuUsageEntry extends TmfTreeViewerEntry {
+ private final String fTid;
+ private final String fProcessName;
+ private final Double fPercent;
+ private final Long fTime;
+
+ /**
+ * Constructor
+ *
+ * @param tid
+ * The TID of the process
+ * @param name
+ * The thread's name
+ * @param percent
+ * The percentage CPU usage
+ * @param time
+ * The total amount of time spent on CPU
+ */
+ public CpuUsageEntry(String tid, String name, double percent, long time) {
+ super(tid);
+ fTid = tid;
+ fProcessName = name;
+ fPercent = percent;
+ fTime = time;
+ }
+
+ /**
+ * Get the TID of the thread represented by this entry
+ *
+ * @return The thread's TID
+ */
+ public String getTid() {
+ return fTid;
+ }
+
+ /**
+ * Get the process name
+ *
+ * @return The process name
+ */
+ public String getProcessName() {
+ return fProcessName;
+ }
+
+ /**
+ * Get the percentage of time spent on CPU in the time interval represented
+ * by this entry.
+ *
+ * @return The percentage of time spent on CPU
+ */
+ public Double getPercent() {
+ return fPercent;
+ }
+
+ /**
+ * Get the total time spent on CPU in the time interval represented by this
+ * entry.
+ *
+ * @return The total time spent on CPU
+ */
+ public Long getTime() {
+ return fTime;
+ }
+}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others
+ *
+ * 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:
+ * Geneviève Bastien - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage;
+
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.swt.events.ControlAdapter;
+import org.eclipse.swt.events.ControlEvent;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal;
+import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
+import org.eclipse.tracecompass.tmf.ui.viewers.TmfViewer;
+import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer;
+import org.eclipse.tracecompass.tmf.ui.views.TmfChartView;
+
+/**
+ * CPU usage view. It contains 2 viewers: one tree viewer showing all the
+ * threads who were on the CPU in the time range, and one XY chart viewer
+ * plotting the total time spent on CPU and the time of the threads selected in
+ * the tree viewer.
+ *
+ * @author Geneviève Bastien
+ */
+public class CpuUsageView extends TmfChartView {
+
+ /** ID string */
+ public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.cpuusage"; //$NON-NLS-1$
+
+ private @Nullable CpuUsageComposite fTreeViewer = null;
+ private @Nullable CpuUsageXYViewer fXYViewer = null;
+
+ /**
+ * Constructor
+ */
+ public CpuUsageView() {
+ super(Messages.CpuUsageView_Title);
+ }
+
+ @Override
+ public void createPartControl(Composite parent) {
+ super.createPartControl(parent);
+
+ /* Initialize the viewers with the currently selected trace */
+ ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
+ if (trace != null) {
+ TmfTraceSelectedSignal signal = new TmfTraceSelectedSignal(this, trace);
+ if (fTreeViewer != null) {
+ fTreeViewer.traceSelected(signal);
+ }
+ if (fXYViewer != null) {
+ fXYViewer.traceSelected(signal);
+ }
+ }
+ }
+
+ @Override
+ protected TmfXYChartViewer createChartViewer(Composite parent) {
+ CpuUsageXYViewer viewer = new CpuUsageXYViewer(parent);
+ viewer.setSendTimeAlignSignals(true);
+ fXYViewer = viewer;
+ return viewer;
+ }
+
+ @Override
+ public TmfViewer createLeftChildViewer(Composite parent) {
+ final CpuUsageComposite viewer = new CpuUsageComposite(parent);
+
+ /* Add selection listener to tree viewer */
+ viewer.addSelectionChangeListener(new ISelectionChangedListener() {
+ @Override
+ public void selectionChanged(SelectionChangedEvent event) {
+ ISelection selection = event.getSelection();
+ if (selection instanceof IStructuredSelection) {
+ Object structSelection = ((IStructuredSelection) selection).getFirstElement();
+ if (structSelection instanceof CpuUsageEntry) {
+ CpuUsageEntry entry = (CpuUsageEntry) structSelection;
+ if (fTreeViewer != null) {
+ fTreeViewer.setSelectedThread(entry.getTid());
+ }
+ if (fXYViewer != null) {
+ fXYViewer.setSelectedThread(Long.valueOf(entry.getTid()));
+ }
+ }
+ }
+ }
+ });
+
+ viewer.getControl().addControlListener(new ControlAdapter() {
+ @Override
+ public void controlResized(ControlEvent e) {
+ super.controlResized(e);
+ }
+ });
+
+ fTreeViewer = viewer;
+ return fTreeViewer;
+ }
+
+ @Override
+ public void setFocus() {
+ if (fXYViewer != null) {
+ fXYViewer.getControl().setFocus();
+ }
+ }
+
+ /**
+ * Signal handler for when a cpu is selected
+ *
+ * @param signal
+ * the cpu being selected
+ * @since 2.0
+ */
+ @TmfSignalHandler
+ public void cpuSelect(TmfCpuSelectedSignal signal) {
+ final @Nullable CpuUsageXYViewer xyViewer = fXYViewer;
+ final @Nullable CpuUsageComposite treeViewer = fTreeViewer;
+ if (xyViewer != null && treeViewer != null) {
+ int core = signal.getCore();
+ if (core >= 0) {
+ xyViewer.addCpu(core);
+ treeViewer.addCpu(core);
+ } else {
+ xyViewer.clearCpu();
+ treeViewer.clearCpu();
+ }
+ }
+ }
+
+}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014, 2015 École Polytechnique de Montréal
+ *
+ * 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:
+ * Geneviève Bastien - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage;
+
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis;
+import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
+import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
+import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
+import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
+
+import com.google.common.base.Joiner;
+
+/**
+ * CPU usage viewer with XY line chart. It displays the total CPU usage and that
+ * of the threads selected in the CPU usage tree viewer.
+ *
+ * @author Geneviève Bastien
+ */
+public class CpuUsageXYViewer extends TmfCommonXLineChartViewer {
+
+ private KernelCpuUsageAnalysis fModule = null;
+
+ /* Maps a thread ID to a list of y values */
+ private final Map<String, double[]> fYValues = new LinkedHashMap<>();
+ /*
+ * To avoid up and downs CPU usage when process is in and out of CPU
+ * frequently, use a smaller resolution to get better averages.
+ */
+ private static final double RESOLUTION = 0.4;
+
+ // Timeout between updates in the updateData thread
+ private static final long BUILD_UPDATE_TIMEOUT = 500;
+
+ private long fSelectedThread = -1;
+
+ private final @NonNull Set<@NonNull Integer> fCpus = new TreeSet<>();
+
+ /**
+ * Constructor
+ *
+ * @param parent
+ * parent composite
+ */
+ public CpuUsageXYViewer(Composite parent) {
+ super(parent, Messages.CpuUsageXYViewer_Title, Messages.CpuUsageXYViewer_TimeXAxis, Messages.CpuUsageXYViewer_CpuYAxis);
+ setResolution(RESOLUTION);
+ }
+
+ @Override
+ protected void initializeDataSource() {
+ ITmfTrace trace = getTrace();
+ if (trace != null) {
+ fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID);
+ if (fModule == null) {
+ return;
+ }
+ fModule.schedule();
+ }
+ }
+
+ private static double[] zeroFill(int nb) {
+ double[] arr = new double[nb];
+ Arrays.fill(arr, 0.0);
+ return arr;
+ }
+
+ @Override
+ protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
+ try {
+ if (getTrace() == null || fModule == null) {
+ return;
+ }
+ fModule.waitForInitialization();
+ ITmfStateSystem ss = fModule.getStateSystem();
+ if (ss == null) {
+ return;
+ }
+ double[] xvalues = getXAxis(start, end, nb);
+ if (xvalues.length == 0) {
+ return;
+ }
+ setXAxis(xvalues);
+
+ boolean complete = false;
+ long currentEnd = start;
+
+ while (!complete && currentEnd < end) {
+
+ if (monitor.isCanceled()) {
+ return;
+ }
+
+ long traceStart = getStartTime();
+ long traceEnd = getEndTime();
+ long offset = getTimeOffset();
+ long selectedThread = fSelectedThread;
+
+ complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
+ currentEnd = ss.getCurrentEndTime();
+
+ /* Initialize the data */
+ Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(fCpus, Math.max(start, traceStart), Math.min(end, traceEnd));
+ Map<String, String> totalEntries = new HashMap<>();
+ fYValues.clear();
+ fYValues.put(Messages.CpuUsageXYViewer_Total, zeroFill(xvalues.length));
+ String stringSelectedThread = Long.toString(selectedThread);
+ if (selectedThread != -1) {
+ fYValues.put(stringSelectedThread, zeroFill(xvalues.length));
+ }
+
+ for (Entry<String, Long> entry : cpuUsageMap.entrySet()) {
+ /*
+ * Process only entries representing the total of all CPUs
+ * and that have time on CPU
+ */
+ if (entry.getValue() == 0) {
+ continue;
+ }
+ if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) {
+ continue;
+ }
+ String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2);
+
+ if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) {
+ /* This is the total cpu usage for a thread */
+ totalEntries.put(strings[1], entry.getKey());
+ }
+ }
+
+ double prevX = xvalues[0] - 1;
+ long prevTime = (long) prevX + offset;
+ /*
+ * make sure that time is in the trace range after double to
+ * long conversion
+ */
+ prevTime = Math.max(traceStart, prevTime);
+ prevTime = Math.min(traceEnd, prevTime);
+ /* Get CPU usage statistics for each x value */
+ for (int i = 0; i < xvalues.length; i++) {
+ if (monitor.isCanceled()) {
+ return;
+ }
+ long totalCpu = 0;
+ double x = xvalues[i];
+ long time = (long) x + offset;
+ time = Math.max(traceStart, time);
+ time = Math.min(traceEnd, time);
+ if (time == prevTime) {
+ /*
+ * we need at least 1 time unit to be able to get cpu
+ * usage when zoomed in
+ */
+ prevTime = time - 1;
+ }
+
+ cpuUsageMap = fModule.getCpuUsageInRange(fCpus, prevTime, time);
+
+ /*
+ * Calculate the sum of all total entries, and add a data
+ * point to the selected one
+ */
+ for (Entry<String, String> entry : totalEntries.entrySet()) {
+ Long cpuEntry = cpuUsageMap.get(entry.getValue());
+ cpuEntry = cpuEntry != null ? cpuEntry : 0L;
+
+ totalCpu += cpuEntry;
+
+ if (entry.getKey().equals(stringSelectedThread)) {
+ /* This is the total cpu usage for a thread */
+ double[] key = checkNotNull(fYValues.get(entry.getKey()));
+ key[i] = (double) cpuEntry / (double) (time - prevTime) * 100;
+ }
+
+ }
+ double[] key = checkNotNull(fYValues.get(Messages.CpuUsageXYViewer_Total));
+ key[i] = (double) totalCpu / (double) (time - prevTime) * 100;
+ prevTime = time;
+ }
+ for (Entry<String, double[]> entry : fYValues.entrySet()) {
+ setSeries(entry.getKey(), entry.getValue());
+ }
+ if (monitor.isCanceled()) {
+ return;
+ }
+ updateDisplay();
+ }
+ } catch (StateValueTypeException e) {
+ Activator.getDefault().logError("Error updating the data of the CPU usage view", e); //$NON-NLS-1$
+ }
+
+ }
+
+ /**
+ * Set the selected thread ID, which will be graphed in this viewer
+ *
+ * @param tid
+ * The selected thread ID
+ */
+ public void setSelectedThread(long tid) {
+ cancelUpdate();
+ deleteSeries(Long.toString(fSelectedThread));
+ fSelectedThread = tid;
+ updateContent();
+ }
+
+ /**
+ * Gets the analysis module
+ *
+ * @return the {@link KernelCpuUsageAnalysis}
+ *
+ * @since 2.0
+ */
+ public KernelCpuUsageAnalysis getModule() {
+ return fModule;
+ }
+
+ /**
+ * Add a core
+ *
+ * @param core
+ * the core to add
+ * @since 2.0
+ */
+ public void addCpu(int core) {
+ fCpus.add(core);
+ cancelUpdate();
+ updateContent();
+ getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList());
+ }
+
+ /**
+ * Remove a core
+ *
+ * @param core
+ * the core to remove
+ * @since 2.0
+ */
+ public void removeCpu(int core) {
+ fCpus.remove(core);
+ cancelUpdate();
+ updateContent();
+ getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList());
+ }
+
+ private String getCpuList() {
+ return Joiner.on(", ").join(fCpus); //$NON-NLS-1$
+ }
+
+ /**
+ * Clears the cores
+ *
+ * @since 2.0
+ */
+ public void clearCpu() {
+ fCpus.clear();
+ cancelUpdate();
+ updateContent();
+ getSwtChart().getTitle().setText(Messages.CpuUsageView_Title);
+ }
+
+}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014, 2015 École Polytechnique de Montréal
+ *
+ * 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:
+ * Geneviève Bastien - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage;
+
+import org.eclipse.osgi.util.NLS;
+
+/**
+ * Messages used in the LTTng kernel CPU usage view and viewers.
+ *
+ * @author Geneviève Bastien
+ */
+@SuppressWarnings("javadoc")
+public class Messages extends NLS {
+
+ private static final String BUNDLE_NAME = "org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage.messages"; //$NON-NLS-1$
+
+ public static String CpuUsageComposite_ColumnPercent;
+ public static String CpuUsageComposite_ColumnProcess;
+ public static String CpuUsageComposite_ColumnTID;
+ public static String CpuUsageComposite_ColumnTime;
+ public static String CpuUsageComposite_TextPercent;
+ public static String CpuUsageComposite_TextTime;
+ public static String CpuUsageView_Title;
+ public static String CpuUsageXYViewer_CpuYAxis;
+ public static String CpuUsageXYViewer_TimeXAxis;
+ public static String CpuUsageXYViewer_Title;
+ public static String CpuUsageXYViewer_Total;
+
+ static {
+ // initialize resource bundle
+ NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+ }
+
+ private Messages() {
+ }
+}
--- /dev/null
+###############################################################################
+# Copyright (c) 2014, 2015 École Polytechnique de Montréal
+#
+# 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:
+# Geneviève Bastien - Initial API and implementation
+###############################################################################
+
+CpuUsageComposite_ColumnPercent=%
+CpuUsageComposite_ColumnProcess=Process
+CpuUsageComposite_ColumnTID=TID
+CpuUsageComposite_ColumnTime=Time
+CpuUsageComposite_TextPercent=%1$.3f %%
+CpuUsageComposite_TextTime={0} ns
+CpuUsageView_Title=CPU Usage
+CpuUsageXYViewer_CpuYAxis=% CPU
+CpuUsageXYViewer_TimeXAxis=Time
+CpuUsageXYViewer_Title=CPU usage
+CpuUsageXYViewer_Total=Total