1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.flightrecorder.ui.pages;
  34 
  35 
  36 import java.util.ArrayList;
  37 import java.util.LinkedHashMap;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Set;
  41 import java.util.stream.Collectors;
  42 import java.util.stream.Stream;
  43 
  44 import org.eclipse.jface.action.IAction;
  45 import org.eclipse.jface.resource.ImageDescriptor;
  46 import org.eclipse.swt.SWT;
  47 import org.eclipse.swt.custom.CTabFolder;
  48 import org.eclipse.swt.custom.SashForm;
  49 import org.eclipse.swt.layout.GridData;
  50 import org.eclipse.swt.layout.GridLayout;
  51 import org.eclipse.swt.widgets.Composite;
  52 import org.eclipse.swt.widgets.Control;
  53 import org.eclipse.ui.forms.widgets.Form;
  54 import org.eclipse.ui.forms.widgets.FormToolkit;
  55 import org.openjdk.jmc.common.IMCClassLoader;
  56 import org.openjdk.jmc.common.IState;
  57 import org.openjdk.jmc.common.IWritableState;
  58 import org.openjdk.jmc.common.item.Aggregators;
  59 import org.openjdk.jmc.common.item.IAttribute;
  60 import org.openjdk.jmc.common.item.IItemCollection;
  61 import org.openjdk.jmc.common.item.IItemFilter;
  62 import org.openjdk.jmc.common.item.ItemFilters;
  63 import org.openjdk.jmc.common.item.RangeMatchPolicy;
  64 import org.openjdk.jmc.common.unit.IQuantity;
  65 import org.openjdk.jmc.common.unit.IRange;
  66 import org.openjdk.jmc.common.unit.UnitLookup;
  67 import org.openjdk.jmc.flightrecorder.JfrAttributes;
  68 import org.openjdk.jmc.flightrecorder.jdk.JdkAttributes;
  69 import org.openjdk.jmc.flightrecorder.jdk.JdkFilters;
  70 import org.openjdk.jmc.flightrecorder.jdk.JdkQueries;
  71 import org.openjdk.jmc.flightrecorder.jdk.JdkTypeIDs;
  72 import org.openjdk.jmc.flightrecorder.rules.util.JfrRuleTopics;
  73 import org.openjdk.jmc.flightrecorder.ui.FlightRecorderUI;
  74 import org.openjdk.jmc.flightrecorder.ui.IDataPageFactory;
  75 import org.openjdk.jmc.flightrecorder.ui.IDisplayablePage;
  76 import org.openjdk.jmc.flightrecorder.ui.IPageContainer;
  77 import org.openjdk.jmc.flightrecorder.ui.IPageDefinition;
  78 import org.openjdk.jmc.flightrecorder.ui.IPageUI;
  79 import org.openjdk.jmc.flightrecorder.ui.ItemCollectionToolkit;
  80 import org.openjdk.jmc.flightrecorder.ui.StreamModel;
  81 import org.openjdk.jmc.flightrecorder.ui.common.AbstractDataPage;
  82 import org.openjdk.jmc.flightrecorder.ui.common.DataPageToolkit;
  83 import org.openjdk.jmc.flightrecorder.ui.common.FilterComponent;
  84 import org.openjdk.jmc.flightrecorder.ui.common.FlavorSelector;
  85 import org.openjdk.jmc.flightrecorder.ui.common.FlavorSelector.FlavorSelectorState;
  86 import org.openjdk.jmc.flightrecorder.ui.common.ImageConstants;
  87 import org.openjdk.jmc.flightrecorder.ui.common.ItemHistogram;
  88 import org.openjdk.jmc.flightrecorder.ui.common.ItemHistogram.ItemHistogramBuilder;
  89 import org.openjdk.jmc.flightrecorder.ui.common.ItemHistogramWithInput;
  90 import org.openjdk.jmc.flightrecorder.ui.common.ItemList;
  91 import org.openjdk.jmc.flightrecorder.ui.common.ItemList.ItemListBuilder;
  92 import org.openjdk.jmc.flightrecorder.ui.common.TypeLabelProvider;
  93 import org.openjdk.jmc.flightrecorder.ui.messages.internal.Messages;
  94 import org.openjdk.jmc.flightrecorder.ui.selection.SelectionStoreActionToolkit;
  95 import org.openjdk.jmc.ui.charts.IXDataRenderer;
  96 import org.openjdk.jmc.ui.charts.RendererToolkit;
  97 import org.openjdk.jmc.ui.charts.XYChart;
  98 import org.openjdk.jmc.ui.column.ColumnManager.SelectionState;
  99 import org.openjdk.jmc.ui.column.ColumnMenusFactory;
 100 import org.openjdk.jmc.ui.column.TableSettings;
 101 import org.openjdk.jmc.ui.handlers.ActionToolkit;
 102 import org.openjdk.jmc.ui.handlers.MCContextMenuManager;
 103 import org.openjdk.jmc.ui.misc.ActionUiToolkit;
 104 import org.openjdk.jmc.ui.misc.ChartCanvas;
 105 import org.openjdk.jmc.ui.misc.PersistableSashForm;
 106 
 107 public class ClassLoadingPage extends AbstractDataPage {
 108         public static class ClassLoadingPageFactory implements IDataPageFactory {
 109 
 110                 @Override
 111                 public String getName(IState state) {
 112                         return Messages.ClassLoadingPage_PAGE_NAME;
 113                 }
 114 
 115                 @Override
 116                 public ImageDescriptor getImageDescriptor(IState state) {
 117                         return FlightRecorderUI.getDefault().getMCImageDescriptor(ImageConstants.PAGE_CLASSLOADING);
 118                 }
 119 
 120                 @Override
 121                 public String[] getTopics(IState state) {
 122                         return new String[] {JfrRuleTopics.CLASS_LOADING_TOPIC};
 123                 }
 124 
 125                 @Override
 126                 public IDisplayablePage createPage(IPageDefinition dpd, StreamModel items, IPageContainer editor) {
 127                         return new ClassLoadingPage(dpd, items, editor);
 128                 }
 129         }
 130 
 131         private static final IItemFilter TABLE_FILTER = ItemFilters.or(JdkQueries.CLASS_LOAD.getFilter(),
 132                         JdkQueries.CLASS_UNLOAD.getFilter());
 133         private static final ItemHistogramBuilder CLASSLOADER_HISTOGRAM = new ItemHistogramBuilder();
 134         private static final ItemListBuilder CLASS_LOADING_LIST = new ItemListBuilder();
 135         private static final ItemListBuilder CLASS_UNLOADING_LIST = new ItemListBuilder();
 136         private static final ItemListBuilder CLASS_DEFINE_LIST = new ItemListBuilder();
 137         private static final ItemListBuilder CLASS_LOADER_STATISTICS_LIST = new ItemListBuilder();
 138         private static final Map<String, Boolean> LEGEND_ITEMS = new LinkedHashMap<>();
 139         private static final String LOADED_COUNT = "loadedCount"; //$NON-NLS-1$
 140         private static final String UNLOADED_COUNT = "unloadedCount"; //$NON-NLS-1$
 141         private static final String CLASS_LOAD = "classLoad"; //$NON-NLS-1$
 142         private static final String CLASS_UNLOAD = "classUnload"; //$NON-NLS-1$
 143 
 144         static {
 145                 CLASSLOADER_HISTOGRAM.addColumn(LOADED_COUNT,
 146                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_BY_CLASSLOADER,
 147                                                 Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_BY_CLASSLOADER_DESC,
 148                                                 ItemFilters.type(JdkTypeIDs.CLASS_LOAD)));
 149                 CLASSLOADER_HISTOGRAM.addColumn(UNLOADED_COUNT,
 150                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_BY_CLASSLOADER,
 151                                                 Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_BY_CLASSLOADER_DESC,
 152                                                 ItemFilters.type(JdkTypeIDs.CLASS_UNLOAD)));
 153 
 154                 CLASS_LOADING_LIST.addColumn(JdkAttributes.CLASS_LOADED);
 155                 CLASS_LOADING_LIST.addColumn(JdkAttributes.CLASS_DEFINING_CLASSLOADER);
 156                 CLASS_LOADING_LIST.addColumn(JdkAttributes.CLASS_INITIATING_CLASSLOADER);
 157                 CLASS_LOADING_LIST.addColumn(JfrAttributes.START_TIME);
 158                 CLASS_LOADING_LIST.addColumn(JfrAttributes.DURATION);
 159                 CLASS_LOADING_LIST.addColumn(JfrAttributes.END_TIME);
 160                 CLASS_LOADING_LIST.addColumn(JfrAttributes.EVENT_THREAD);
 161                 
 162                 CLASS_UNLOADING_LIST.addColumn(JfrAttributes.EVENT_TIMESTAMP);
 163                 CLASS_UNLOADING_LIST.addColumn(JfrAttributes.EVENT_THREAD);
 164                 CLASS_UNLOADING_LIST.addColumn(JdkAttributes.CLASS_UNLOADED);
 165                 CLASS_UNLOADING_LIST.addColumn(JdkAttributes.CLASS_DEFINING_CLASSLOADER);
 166                 
 167                 CLASS_DEFINE_LIST.addColumn(JfrAttributes.START_TIME);
 168                 CLASS_DEFINE_LIST.addColumn(JdkAttributes.CLASS_DEFINING_CLASSLOADER);
 169                 CLASS_DEFINE_LIST.addColumn(JdkAttributes.CLASS_DEFINED);
 170                 CLASS_DEFINE_LIST.addColumn(JfrAttributes.EVENT_THREAD);
 171                 
 172                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.ANONYMOUS_BLOCK_SIZE);
 173                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.ANONYMOUS_CHUNK_SIZE);
 174                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.ANONYMOUS_CLASS_COUNT);
 175                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.BLOCK_SIZE);
 176                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CHUNK_SIZE);
 177                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CLASS_COUNT);
 178                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CLASS_LOADER_DATA);
 179                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.PARENT_CLASSLOADER);
 180                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CLASSLOADER);
 181                 CLASS_LOADER_STATISTICS_LIST.addColumn(JfrAttributes.START_TIME);
 182                 // FIXME: Need to make a label provider for this
 183                 // FIXME: Want to have this in the same order
 184 
 185                 LEGEND_ITEMS.put(JdkAttributes.CLASSLOADER_LOADED_COUNT.getIdentifier(), Boolean.TRUE);
 186                 LEGEND_ITEMS.put(JdkAttributes.CLASSLOADER_UNLOADED_COUNT.getIdentifier(), Boolean.FALSE);
 187                 LEGEND_ITEMS.put(CLASS_LOAD, Boolean.TRUE);
 188                 LEGEND_ITEMS.put(CLASS_UNLOAD, Boolean.FALSE);
 189         }
 190 
 191         private class ClassLoadingUi implements IPageUI {
 192 
 193                 private final ChartCanvas classLoadingChart;
 194                 private final ItemList classLoadingTable;
 195                 private final ItemList classUnloadingTable;
 196                 private final ItemList classDefineTable;
 197                 private final ItemList classLoaderStatisticsTable;
 198                 private FilterComponent classLoadingFilter;
 199                 private FilterComponent classUnloadingFilter;
 200                 private FilterComponent classDefineFilter;
 201                 private FilterComponent classLoaderStatisticsFilter;
 202                 private final SashForm sash;
 203                 private final IPageContainer pageContainer;
 204                 private IItemCollection selectionItems;
 205                 private ItemHistogram classloaderHistogram;
 206                 private FilterComponent classloaderHistogramFilter;
 207                 private final IAction classLoadAction = DataPageToolkit.createTypeCheckAction(CLASS_LOAD, JdkTypeIDs.CLASS_LOAD,
 208                                 Messages.ClassLoadingPage_CLASS_LOADING_ACTION, Messages.ClassLoadingPage_CLASS_LOADING_ACTION_DESC,
 209                                 b -> updateChart());
 210                 private final IAction classUnloadAction = DataPageToolkit.createTypeCheckAction(CLASS_UNLOAD,
 211                                 JdkTypeIDs.CLASS_UNLOAD, Messages.ClassLoadingPage_CLASS_UNLOADING_ACTION,
 212                                 Messages.ClassLoadingPage_CLASS_UNLOADING_ACTION_DESC, b -> updateChart());
 213                 private final Stream<IAction> statsActions = Stream
 214                                 .of(JdkAttributes.CLASSLOADER_LOADED_COUNT, JdkAttributes.CLASSLOADER_UNLOADED_COUNT)
 215                                 .map(a -> DataPageToolkit.createAttributeCheckAction(a, b -> updateChart()));
 216                 private final List<IAction> allChartSeriesActions = Stream
 217                                 .concat(Stream.of(classLoadAction, classUnloadAction), statsActions).collect(Collectors.toList());
 218                 private CTabFolder tabFolder;
 219                 private CTabFolder classloaderFolder;
 220                 private XYChart chart;
 221                 private IRange<IQuantity> timeRange;
 222                 private FlavorSelector flavorSelector;
 223 
 224                 ClassLoadingUi(Composite parent, FormToolkit toolkit, IPageContainer pageContainer, IState state) {
 225                         this.pageContainer = pageContainer;
 226 
 227                         Form form = DataPageToolkit.createForm(parent, toolkit, getName(), getIcon());
 228 
 229                         sash = new SashForm(form.getBody(), SWT.VERTICAL);
 230 
 231                         Composite chartComp = new Composite(sash, SWT.NONE);
 232                         chartComp.setLayout(new GridLayout());
 233                         Control legend = ActionUiToolkit.buildCheckboxControl(chartComp, allChartSeriesActions.stream(), false);
 234                         legend.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
 235                         ActionToolkit.loadCheckState(state.getChild(CHART), allChartSeriesActions.stream());
 236 
 237                         chart = new XYChart(pageContainer.getRecordingRange(), RendererToolkit.empty(), 180);
 238                         chart.setVisibleRange(timelineRange.getStart(), timelineRange.getEnd());
 239                         chart.addVisibleRangeListener(r -> timelineRange = r);
 240                         classLoadingChart = new ChartCanvas(chartComp);
 241                         classLoadingChart.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 242                         DataPageToolkit.createChartTimestampTooltip(classLoadingChart);
 243                         DataPageToolkit.setChart(classLoadingChart, chart, pageContainer::showSelection, this::onChartSelection);
 244                         SelectionStoreActionToolkit.addSelectionStoreRangeActions(pageContainer.getSelectionStore(), chart,
 245                                         JfrAttributes.LIFETIME, Messages.ClassLoadingPage_CLASS_LOADING_TIMELINE_SELECTION,
 246                                         classLoadingChart.getContextMenu());
 247 
 248                         classloaderFolder = new CTabFolder(sash, SWT.NONE);
 249 
 250                         classloaderHistogram = CLASSLOADER_HISTOGRAM.buildWithoutBorder(classloaderFolder,
 251                                         JdkAttributes.CLASS_DEFINING_CLASSLOADER, TableSettings.forState(state.getChild(HISTOGRAM)));
 252                         classloaderHistogramFilter = FilterComponent.createFilterComponent(classloaderHistogram, null,
 253                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOAD_OR_UNLOAD),
 254                                         pageContainer.getSelectionStore()::getSelections, this::onHistogramFilterChange);
 255                         classloaderHistogram.getManager().getViewer().addSelectionChangedListener(
 256                                         e -> pageContainer.showSelection(classloaderHistogram.getSelection().getItems()));
 257                         MCContextMenuManager classLoaderHistogramMm = MCContextMenuManager
 258                                         .create(classloaderHistogram.getManager().getViewer().getControl());
 259                         ColumnMenusFactory.addDefaultMenus(classloaderHistogram.getManager(), classLoaderHistogramMm);
 260                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(),
 261                                         classloaderHistogram, Messages.ClassLoadingPage_CLASS_LOADING_HISTOGRAM_SELECTION,
 262                                         classLoaderHistogramMm);
 263                         classLoaderHistogramMm.add(classloaderHistogramFilter.getShowFilterAction());
 264                         classLoaderHistogramMm.add(classloaderHistogramFilter.getShowSearchAction());
 265                         classloaderHistogramFilter.loadState(state.getChild(HISTOGRAM_FILTER));
 266                         DataPageToolkit.addTabItem(classloaderFolder, classloaderHistogramFilter.getComponent(),
 267                                         Messages.ClassLoadingPage_CLASS_LOADER_TAB);
 268 
 269                         ItemHistogramWithInput.chain(classloaderHistogram, this::updateTables);
 270 
 271                         classLoaderStatisticsTable = CLASS_LOADER_STATISTICS_LIST.buildWithoutBorder(classloaderFolder,
 272                                         TableSettings.forState(state.getChild(CLASS_LOADER_STATISTICS_TABLE)));
 273                         classLoaderStatisticsTable.getManager().getViewer().addSelectionChangedListener(e -> {
 274                                 // The standard aggregators will skip the null classloader, so we need to do this manually.
 275                                 IItemCollection selection = ItemCollectionToolkit.build(classLoaderStatisticsTable.getSelection().get());
 276                                 Stream<IMCClassLoader> stream = ItemCollectionToolkit.values(selection, JdkAttributes.CLASSLOADER).get().distinct();
 277                                 Set<IMCClassLoader> selected = stream.collect(Collectors.toSet());
 278                                 IItemFilter selectionFilter =  ItemFilters.and(ItemFilters.or(JdkFilters.CLASS_LOAD_OR_UNLOAD,
 279                                 JdkFilters.CLASS_DEFINE), ItemFilters.memberOf(JdkAttributes.CLASS_DEFINING_CLASSLOADER, selected));
 280                                 IItemCollection filteredItems = getDataSource().getItems().apply(selectionFilter);
 281                                 pageContainer.showSelection(filteredItems);
 282                                 updateTables(filteredItems);
 283                         });
 284                         classLoaderStatisticsFilter = FilterComponent.createFilterComponent(classLoaderStatisticsTable, null,
 285                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOADER_STATISTICS),
 286                                         pageContainer.getSelectionStore()::getSelections, this::onClassLoaderStatisticsFilterChange);
 287                         MCContextMenuManager classLoaderStatisticsTableMm = MCContextMenuManager
 288                                         .create(classLoaderStatisticsTable.getManager().getViewer().getControl());
 289                         ColumnMenusFactory.addDefaultMenus(classLoaderStatisticsTable.getManager(), classLoaderStatisticsTableMm);
 290                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classLoaderStatisticsTable,
 291                                         Messages.ClassLoadingPage_CLASS_LOADER_STATISTICS_LIST_SELECTION, classLoaderStatisticsTableMm);
 292                         classLoaderStatisticsTableMm.add(classLoaderStatisticsFilter.getShowFilterAction());
 293                         classLoaderStatisticsTableMm.add(classLoaderStatisticsFilter.getShowSearchAction());
 294                         classLoaderStatisticsFilter.loadState(state.getChild(CLASS_LOADER_STATISTICS_FILTER));
 295                         DataPageToolkit.addTabItem(classloaderFolder, classLoaderStatisticsFilter.getComponent(),
 296                                         Messages.ClassLoadingPage_CLASS_LOADER_STATISTICS_TAB_TITLE);
 297 
 298                         tabFolder = new CTabFolder(sash, SWT.NONE);
 299 
 300                         classLoadingTable = CLASS_LOADING_LIST.buildWithoutBorder(tabFolder,
 301                                         TableSettings.forState(state.getChild(CLASS_LOADING_TABLE)));
 302                         classLoadingTable.getManager().getViewer().addSelectionChangedListener(e -> pageContainer
 303                                         .showSelection(ItemCollectionToolkit.build(classLoadingTable.getSelection().get())));
 304                         classLoadingFilter = FilterComponent.createFilterComponent(classLoadingTable, null,
 305                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOAD),
 306                                         pageContainer.getSelectionStore()::getSelections, this::onClassLoadFilterChange);
 307                         MCContextMenuManager classLoadingTableMm = MCContextMenuManager
 308                                         .create(classLoadingTable.getManager().getViewer().getControl());
 309                         ColumnMenusFactory.addDefaultMenus(classLoadingTable.getManager(), classLoadingTableMm);
 310                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classLoadingTable,
 311                                         Messages.ClassLoadingPage_CLASS_LOADING_LIST_SELECTION, classLoadingTableMm);
 312                         classLoadingTableMm.add(classLoadingFilter.getShowFilterAction());
 313                         classLoadingTableMm.add(classLoadingFilter.getShowSearchAction());
 314                         classLoadingFilter.loadState(state.getChild(CLASS_LOADING_FILTER));
 315                         DataPageToolkit.addTabItem(tabFolder, classLoadingFilter.getComponent(),
 316                                         Messages.ClassLoadingPage_CLASS_LOADING_TAB_TITLE);
 317                         
 318                         classDefineTable = CLASS_DEFINE_LIST.buildWithoutBorder(tabFolder,
 319                                         TableSettings.forState(state.getChild(CLASS_DEFINE_TABLE)));
 320                         classDefineTable.getManager().getViewer().addSelectionChangedListener(e -> pageContainer
 321                                         .showSelection(ItemCollectionToolkit.build(classDefineTable.getSelection().get())));
 322                         classDefineFilter = FilterComponent.createFilterComponent(classDefineTable, null,
 323                                         getDataSource().getItems().apply(JdkFilters.CLASS_DEFINE),
 324                                         pageContainer.getSelectionStore()::getSelections, this::onClassDefineFilterChange);
 325                         MCContextMenuManager classDefineTableMm = MCContextMenuManager
 326                                         .create(classDefineTable.getManager().getViewer().getControl());
 327                         ColumnMenusFactory.addDefaultMenus(classDefineTable.getManager(), classDefineTableMm);
 328                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classDefineTable,
 329                                         Messages.ClassLoadingPage_CLASS_DEFINE_LIST_SELECTION, classDefineTableMm);
 330                         classDefineTableMm.add(classDefineFilter.getShowFilterAction());
 331                         classDefineTableMm.add(classDefineFilter.getShowSearchAction());
 332                         classDefineFilter.loadState(state.getChild(CLASS_DEFINE_FILTER));
 333                         DataPageToolkit.addTabItem(tabFolder, classDefineFilter.getComponent(),
 334                                         Messages.ClassLoadingPage_CLASS_DEFINE_TAB_TITLE);
 335 
 336                         classUnloadingTable = CLASS_UNLOADING_LIST.buildWithoutBorder(tabFolder,
 337                                         TableSettings.forState(state.getChild(CLASS_UNLOADING_TABLE)));
 338                         classUnloadingTable.getManager().getViewer().addSelectionChangedListener(e -> pageContainer
 339                                         .showSelection(ItemCollectionToolkit.build(classUnloadingTable.getSelection().get())));
 340                         classUnloadingFilter = FilterComponent.createFilterComponent(classUnloadingTable, null,
 341                                         getDataSource().getItems().apply(JdkFilters.CLASS_UNLOAD),
 342                                         pageContainer.getSelectionStore()::getSelections, this::onClassUnloadFilterChange);
 343                         MCContextMenuManager classUnloadingTableMm = MCContextMenuManager
 344                                         .create(classUnloadingTable.getManager().getViewer().getControl());
 345                         ColumnMenusFactory.addDefaultMenus(classUnloadingTable.getManager(), classUnloadingTableMm);
 346                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classUnloadingTable,
 347                                         Messages.ClassLoadingPage_CLASS_UNLOADING_LIST_SELECTION, classUnloadingTableMm);
 348                         classUnloadingTableMm.add(classUnloadingFilter.getShowFilterAction());
 349                         classUnloadingTableMm.add(classUnloadingFilter.getShowSearchAction());
 350                         classUnloadingFilter.loadState(state.getChild(CLASS_UNLOADING_FILTER));
 351                         DataPageToolkit.addTabItem(tabFolder, classUnloadingFilter.getComponent(),
 352                                         Messages.ClassLoadingPage_CLASS_UNLOADING_TAB_TITLE);
 353 
 354                         tabFolder.setSelection(tabFolderIndex);
 355                         classloaderFolder.setSelection(tabFolderIndex);
 356 
 357                         PersistableSashForm.loadState(sash, state.getChild(SASH));
 358                         flavorSelector = FlavorSelector.itemsWithTimerange(form, TABLE_FILTER, getDataSource().getItems(),
 359                                         pageContainer, this::onInputSelected, this::onShow, flavorSelectorState);
 360                         addResultActions(form);
 361 
 362                         onHistogramFilterChange(histogramFilter);
 363                         onClassLoadFilterChange(classLoadTableFilter);
 364                         onClassUnloadFilterChange(classUnloadTableFilter);
 365                         onClassDefineFilterChange(classDefineTableFilter);
 366                         onClassLoaderStatisticsFilterChange(classLoaderStatisticsTableFilter);
 367 
 368                         classloaderHistogram.getManager().setSelectionState(histogramSelection);
 369                         classLoadingTable.getManager().setSelectionState(classLoadingTableSelection);
 370                         classUnloadingTable.getManager().setSelectionState(classUnloadingTableSelection);
 371                         classDefineTable.getManager().setSelectionState(classDefineTableSelection);
 372                         classLoaderStatisticsTable.getManager().setSelectionState(classLoaderStatisticsTableSelection);
 373                 }
 374 
 375                 private void onHistogramFilterChange(IItemFilter filter) {
 376                         classloaderHistogramFilter.filterChangeHelper(filter, classloaderHistogram,
 377                                         getDataSource().getItems().apply(ItemFilters.or(JdkFilters.CLASS_LOAD_OR_UNLOAD, JdkFilters.CLASS_DEFINE)));
 378                         if (classLoadingFilter != null) {
 379                                 classLoadingFilter.notifyListener();
 380                         }
 381                         if (classUnloadingFilter != null) {
 382                                 classUnloadingFilter.notifyListener();
 383                         }
 384                         if (classDefineFilter != null) {
 385                                 classDefineFilter.notifyListener();
 386                         }
 387                         if (classLoaderStatisticsFilter != null) {
 388                                 classLoaderStatisticsFilter.notifyListener();
 389                         }
 390                         histogramFilter = filter;
 391                 }
 392 
 393                 private void onClassLoadFilterChange(IItemFilter filter) {
 394                         classLoadingFilter.filterChangeHelper(filter, classLoadingTable,
 395                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOAD));
 396                         classLoadTableFilter = filter;
 397                 }
 398 
 399                 private void onClassUnloadFilterChange(IItemFilter filter) {
 400                         classUnloadingFilter.filterChangeHelper(filter, classUnloadingTable,
 401                                         getDataSource().getItems().apply(JdkFilters.CLASS_UNLOAD));
 402                         classUnloadTableFilter = filter;
 403                 }
 404                 
 405                 private void onClassDefineFilterChange(IItemFilter filter) {
 406                         classDefineFilter.filterChangeHelper(filter, classDefineTable,
 407                                         getDataSource().getItems().apply(JdkFilters.CLASS_DEFINE));
 408                         classDefineTableFilter = filter;
 409                 }
 410                 
 411                 private void onClassLoaderStatisticsFilterChange(IItemFilter filter) {
 412                         classLoaderStatisticsFilter.filterChangeHelper(filter, classLoaderStatisticsTable,
 413                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOADER_STATISTICS));
 414                         classLoaderStatisticsTableFilter = filter;
 415                 }
 416         
 417                 @Override
 418                 public void saveTo(IWritableState state) {
 419                         PersistableSashForm.saveState(sash, state.createChild(SASH));
 420                         classloaderHistogram.getManager().getSettings().saveState(state.createChild(HISTOGRAM));
 421                         classLoadingTable.getManager().getSettings().saveState(state.createChild(CLASS_LOADING_TABLE));
 422                         classUnloadingTable.getManager().getSettings().saveState(state.createChild(CLASS_UNLOADING_TABLE));
 423                         classDefineTable.getManager().getSettings().saveState(state.createChild(CLASS_DEFINE_TABLE));
 424                         classLoaderStatisticsTable.getManager().getSettings().saveState(state.createChild(CLASS_LOADER_STATISTICS_TABLE));
 425                         classloaderHistogramFilter.saveState(state.createChild(HISTOGRAM_FILTER));
 426                         classLoadingFilter.saveState(state.createChild(CLASS_LOADING_FILTER));
 427                         classUnloadingFilter.saveState(state.createChild(CLASS_UNLOADING_FILTER));
 428                         classDefineFilter.saveState(state.createChild(CLASS_DEFINE_FILTER));
 429                         classLoaderStatisticsFilter.saveState(state.createChild(CLASS_LOADER_STATISTICS_FILTER));
 430                         ActionToolkit.saveCheckState(state.createChild(CHART), allChartSeriesActions.stream());
 431 
 432                         saveToLocal();
 433                 }
 434 
 435                 private void saveToLocal() {
 436                         histogramSelection = classloaderHistogram.getManager().getSelectionState();
 437                         classLoadingTableSelection = classLoadingTable.getManager().getSelectionState();
 438                         classUnloadingTableSelection = classUnloadingTable.getManager().getSelectionState();
 439                         classDefineTableSelection = classDefineTable.getManager().getSelectionState();
 440                         classLoaderStatisticsTableSelection = classLoaderStatisticsTable.getManager().getSelectionState();
 441                         tabFolderIndex = tabFolder.getSelectionIndex();
 442                         flavorSelectorState = flavorSelector.getFlavorSelectorState();
 443                 }
 444 
 445                 private void onShow(Boolean show) {
 446                         IRange<IQuantity> range = show ? timeRange : pageContainer.getRecordingRange();
 447                         chart.setVisibleRange(range.getStart(), range.getEnd());
 448                         updateChart();
 449                 }
 450 
 451                 private void onInputSelected(IItemCollection items, IRange<IQuantity> timeRange) {
 452                         selectionItems = items;
 453                         this.timeRange = timeRange;
 454                         updateHistogram(getItems());
 455                         updateTables(getItems());
 456                         updateChart();
 457                 }
 458 
 459                 private IItemCollection getItems() {
 460                         return selectionItems != null ? selectionItems : getDataSource().getItems();
 461                 }
 462 
 463                 private void updateChart() {
 464                         List<IXDataRenderer> rows = new ArrayList<>();
 465 
 466                         DataPageToolkit.buildLinesRow(Messages.ClassLoadingPage_ROW_CLASS_LOADING_STATISTICS,
 467                                         JdkAttributes.CLASSLOADER_LOADED_COUNT.getDescription(), getDataSource().getItems(), false,
 468                                         JdkQueries.CLASS_LOAD_STATISTICS, this::isAttributeEnabled, UnitLookup.NUMBER_UNITY.quantity(0),
 469                                         null).ifPresent(rows::add);
 470 
 471                         if (classLoadAction.isChecked()) {
 472                                 rows.add(DataPageToolkit.buildTimestampHistogram(Messages.ClassLoadingPage_ROW_CLASSES_LOADED,
 473                                                 Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_BY_CLASSLOADER_DESC,
 474                                                 getItems().apply(JdkFilters.CLASS_LOAD),
 475                                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_LOADED,
 476                                                                 Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_DESC, JdkFilters.CLASS_LOAD),
 477                                                 TypeLabelProvider.getColor(JdkTypeIDs.CLASS_LOAD)));
 478                         }
 479                         if (classUnloadAction.isChecked()) {
 480                                 rows.add(DataPageToolkit.buildTimestampHistogram(Messages.ClassLoadingPage_ROW_CLASSES_UNLOADED,
 481                                                 Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_DESC, getItems().apply(JdkFilters.CLASS_UNLOAD),
 482                                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED,
 483                                                                 Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_DESC, JdkFilters.CLASS_UNLOAD),
 484                                                 TypeLabelProvider.getColor(JdkTypeIDs.CLASS_UNLOAD)));
 485                         }
 486                         classLoadingChart.replaceRenderer(RendererToolkit.uniformRows(rows));
 487                 }
 488 
 489                 private boolean isAttributeEnabled(IAttribute<IQuantity> attr) {
 490                         return allChartSeriesActions.stream().filter(a -> attr.getIdentifier().equals(a.getId())).findAny().get()
 491                                         .isChecked();
 492                 }
 493 
 494                 private void updateHistogram(IItemCollection items) {
 495                         if (classloaderHistogram != null) {
 496                                 classloaderHistogram.show(items.apply(JdkFilters.CLASS_LOAD_OR_UNLOAD));
 497                         }
 498                 }
 499 
 500                 private void updateTables(IItemCollection selectedItems) {
 501                         if (classLoadingTable != null && classUnloadingTable != null && classDefineTable != null
 502                                         && classLoaderStatisticsTable != null) {
 503                                 classLoadingTable.show(selectedItems.apply(JdkQueries.CLASS_LOAD.getFilter()));
 504                                 classUnloadingTable.show(selectedItems.apply(JdkQueries.CLASS_UNLOAD.getFilter()));
 505                                 classDefineTable.show(selectedItems.apply(JdkQueries.CLASS_DEFINE.getFilter()));
 506                         }
 507                 }
 508 
 509                 private void onChartSelection(IRange<IQuantity> range) {
 510                         // FIXME: Make this depend on the legend as well? And maybe on which chart row has been selected?
 511                         IItemCollection itemsInRange = range != null ? getItems().apply(ItemFilters
 512                                         .matchRange(RangeMatchPolicy.CENTER_CONTAINED_IN_RIGHT_OPEN, JfrAttributes.LIFETIME, range))
 513                                         : getItems();
 514                         updateTables(itemsInRange);
 515                         updateHistogram(itemsInRange);
 516                 }
 517 
 518         }
 519 
 520         private static final String SASH = "sash"; //$NON-NLS-1$
 521         private static final String HISTOGRAM = "histogram"; //$NON-NLS-1$
 522         private static final String HISTOGRAM_FILTER = "histogramFilter"; //$NON-NLS-1$
 523         private static final String CLASS_LOADING_TABLE = "classLoadingTable"; //$NON-NLS-1$
 524         private static final String CLASS_UNLOADING_TABLE = "classUnloadingTable"; //$NON-NLS-1$
 525         private static final String CLASS_DEFINE_TABLE = "classDefineTable"; //$NON-NLS-1$
 526         private static final String CLASS_LOADER_STATISTICS_TABLE = "classLoaderStatisticsTable"; //$NON-NLS-1$
 527         private static final String CLASS_LOADING_FILTER = "classLoadingFilter"; //$NON-NLS-1$
 528         private static final String CLASS_UNLOADING_FILTER = "classUnloadingFilter"; //$NON-NLS-1$
 529         private static final String CLASS_DEFINE_FILTER = "classDefineFilter"; //$NON-NLS-1$
 530         private static final String CLASS_LOADER_STATISTICS_FILTER = "classLoaderStatisticsFilter"; //$NON-NLS-1$
 531         private static final String CHART = "chart"; //$NON-NLS-1$
 532 
 533         @Override
 534         public IPageUI display(Composite parent, FormToolkit toolkit, IPageContainer pageContainer, IState state) {
 535                 return new ClassLoadingUi(parent, toolkit, pageContainer, state);
 536         }
 537 
 538         private SelectionState histogramSelection;
 539         private SelectionState classLoadingTableSelection;
 540         private SelectionState classUnloadingTableSelection;
 541         private SelectionState classDefineTableSelection;
 542         private SelectionState classLoaderStatisticsTableSelection;
 543         private IItemFilter histogramFilter;
 544         private IItemFilter classLoadTableFilter;
 545         private IItemFilter classUnloadTableFilter;
 546         private IItemFilter classDefineTableFilter;
 547         private IItemFilter classLoaderStatisticsTableFilter;
 548         private int tabFolderIndex = 0;
 549         private IRange<IQuantity> timelineRange;
 550         private FlavorSelectorState flavorSelectorState;
 551 
 552         public ClassLoadingPage(IPageDefinition dpd, StreamModel items, IPageContainer editor) {
 553                 super(dpd, items, editor);
 554                 timelineRange = editor.getRecordingRange();
 555         }
 556 
 557         @Override
 558         public IItemFilter getDefaultSelectionFilter() {
 559                 return ItemFilters.or(TABLE_FILTER, JdkFilters.CLASS_LOAD_STATISTICS);
 560         }
 561 
 562 }