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 import java.util.ArrayList;
  36 import java.util.LinkedHashMap;
  37 import java.util.List;
  38 import java.util.Map;
  39 import java.util.stream.Collectors;
  40 import java.util.stream.Stream;
  41 
  42 import org.eclipse.jface.action.IAction;
  43 import org.eclipse.jface.resource.ImageDescriptor;
  44 import org.eclipse.swt.SWT;
  45 import org.eclipse.swt.custom.CTabFolder;
  46 import org.eclipse.swt.custom.SashForm;
  47 import org.eclipse.swt.layout.GridData;
  48 import org.eclipse.swt.layout.GridLayout;
  49 import org.eclipse.swt.widgets.Composite;
  50 import org.eclipse.swt.widgets.Control;
  51 import org.eclipse.ui.forms.widgets.Form;
  52 import org.eclipse.ui.forms.widgets.FormToolkit;
  53 
  54 import org.openjdk.jmc.common.IState;
  55 import org.openjdk.jmc.common.IWritableState;
  56 import org.openjdk.jmc.common.item.Aggregators;
  57 import org.openjdk.jmc.common.item.IAttribute;
  58 import org.openjdk.jmc.common.item.IItemCollection;
  59 import org.openjdk.jmc.common.item.IItemFilter;
  60 import org.openjdk.jmc.common.item.ItemFilters;
  61 import org.openjdk.jmc.common.item.RangeMatchPolicy;
  62 import org.openjdk.jmc.common.unit.IQuantity;
  63 import org.openjdk.jmc.common.unit.IRange;
  64 import org.openjdk.jmc.common.unit.UnitLookup;
  65 import org.openjdk.jmc.flightrecorder.JfrAttributes;
  66 import org.openjdk.jmc.flightrecorder.jdk.JdkAttributes;
  67 import org.openjdk.jmc.flightrecorder.jdk.JdkFilters;
  68 import org.openjdk.jmc.flightrecorder.jdk.JdkQueries;
  69 import org.openjdk.jmc.flightrecorder.jdk.JdkTypeIDs;
  70 import org.openjdk.jmc.flightrecorder.rules.util.JfrRuleTopics;
  71 import org.openjdk.jmc.flightrecorder.ui.FlightRecorderUI;
  72 import org.openjdk.jmc.flightrecorder.ui.IDataPageFactory;
  73 import org.openjdk.jmc.flightrecorder.ui.IDisplayablePage;
  74 import org.openjdk.jmc.flightrecorder.ui.IPageContainer;
  75 import org.openjdk.jmc.flightrecorder.ui.IPageDefinition;
  76 import org.openjdk.jmc.flightrecorder.ui.IPageUI;
  77 import org.openjdk.jmc.flightrecorder.ui.ItemCollectionToolkit;
  78 import org.openjdk.jmc.flightrecorder.ui.StreamModel;
  79 import org.openjdk.jmc.flightrecorder.ui.common.AbstractDataPage;
  80 import org.openjdk.jmc.flightrecorder.ui.common.DataPageToolkit;
  81 import org.openjdk.jmc.flightrecorder.ui.common.FilterComponent;
  82 import org.openjdk.jmc.flightrecorder.ui.common.FlavorSelector;
  83 import org.openjdk.jmc.flightrecorder.ui.common.FlavorSelector.FlavorSelectorState;
  84 import org.openjdk.jmc.flightrecorder.ui.common.ImageConstants;
  85 import org.openjdk.jmc.flightrecorder.ui.common.ItemHistogram;
  86 import org.openjdk.jmc.flightrecorder.ui.common.ItemHistogram.ItemHistogramBuilder;
  87 import org.openjdk.jmc.flightrecorder.ui.common.ItemHistogramWithInput;
  88 import org.openjdk.jmc.flightrecorder.ui.common.ItemList;
  89 import org.openjdk.jmc.flightrecorder.ui.common.ItemList.ItemListBuilder;
  90 import org.openjdk.jmc.flightrecorder.ui.common.TypeLabelProvider;
  91 import org.openjdk.jmc.flightrecorder.ui.messages.internal.Messages;
  92 import org.openjdk.jmc.flightrecorder.ui.selection.SelectionStoreActionToolkit;
  93 import org.openjdk.jmc.ui.charts.IXDataRenderer;
  94 import org.openjdk.jmc.ui.charts.RendererToolkit;
  95 import org.openjdk.jmc.ui.charts.XYChart;
  96 import org.openjdk.jmc.ui.column.ColumnManager.SelectionState;
  97 import org.openjdk.jmc.ui.column.ColumnMenusFactory;
  98 import org.openjdk.jmc.ui.column.TableSettings;
  99 import org.openjdk.jmc.ui.handlers.ActionToolkit;
 100 import org.openjdk.jmc.ui.handlers.MCContextMenuManager;
 101 import org.openjdk.jmc.ui.misc.ActionUiToolkit;
 102 import org.openjdk.jmc.ui.misc.ChartCanvas;
 103 import org.openjdk.jmc.ui.misc.PersistableSashForm;
 104 
 105 public class ClassLoadingPage extends AbstractDataPage {
 106         public static class ClassLoadingPageFactory implements IDataPageFactory {
 107 
 108                 @Override
 109                 public String getName(IState state) {
 110                         return Messages.ClassLoadingPage_PAGE_NAME;
 111                 }
 112 
 113                 @Override
 114                 public ImageDescriptor getImageDescriptor(IState state) {
 115                         return FlightRecorderUI.getDefault().getMCImageDescriptor(ImageConstants.PAGE_CLASSLOADING);
 116                 }
 117 
 118                 @Override
 119                 public String[] getTopics(IState state) {
 120                         return new String[] {JfrRuleTopics.CLASS_LOADING_TOPIC};
 121                 }
 122 
 123                 @Override
 124                 public IDisplayablePage createPage(IPageDefinition dpd, StreamModel items, IPageContainer editor) {
 125                         return new ClassLoadingPage(dpd, items, editor);
 126                 }
 127         }
 128 
 129         private static final IItemFilter TABLE_FILTER = ItemFilters.or(JdkQueries.CLASS_LOAD.getFilter(),
 130                         JdkQueries.CLASS_UNLOAD.getFilter());
 131         private static final ItemHistogramBuilder CLASSLOADER_HISTOGRAM = new ItemHistogramBuilder();
 132         private static final ItemListBuilder CLASS_LOADING_LIST = new ItemListBuilder();
 133         private static final ItemListBuilder CLASS_UNLOADING_LIST = new ItemListBuilder();
 134         private static final ItemListBuilder CLASS_DEFINE_LIST = new ItemListBuilder();
 135         private static final ItemListBuilder CLASS_LOADER_STATISTICS_LIST = new ItemListBuilder();
 136         private static final Map<String, Boolean> LEGEND_ITEMS = new LinkedHashMap<>();
 137         private static final String LOADED_COUNT = "loadedCount"; //$NON-NLS-1$
 138         private static final String UNLOADED_COUNT = "unloadedCount"; //$NON-NLS-1$
 139         private static final String CLASS_LOAD = "classLoad"; //$NON-NLS-1$
 140         private static final String CLASS_UNLOAD = "classUnload"; //$NON-NLS-1$
 141 
 142         static {
 143                 CLASSLOADER_HISTOGRAM.addColumn(LOADED_COUNT,
 144                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_BY_CLASSLOADER,
 145                                                 Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_BY_CLASSLOADER_DESC,
 146                                                 ItemFilters.type(JdkTypeIDs.CLASS_LOAD)));
 147                 CLASSLOADER_HISTOGRAM.addColumn(UNLOADED_COUNT,
 148                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_BY_CLASSLOADER,
 149                                                 Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_BY_CLASSLOADER_DESC,
 150                                                 ItemFilters.type(JdkTypeIDs.CLASS_UNLOAD)));
 151 
 152                 CLASS_LOADING_LIST.addColumn(JdkAttributes.CLASS_LOADED);
 153                 CLASS_LOADING_LIST.addColumn(JdkAttributes.CLASS_DEFINING_CLASSLOADER);
 154                 CLASS_LOADING_LIST.addColumn(JdkAttributes.CLASS_INITIATING_CLASSLOADER);
 155                 CLASS_LOADING_LIST.addColumn(JfrAttributes.START_TIME);
 156                 CLASS_LOADING_LIST.addColumn(JfrAttributes.DURATION);
 157                 CLASS_LOADING_LIST.addColumn(JfrAttributes.END_TIME);
 158                 CLASS_LOADING_LIST.addColumn(JfrAttributes.EVENT_THREAD);
 159                 CLASS_UNLOADING_LIST.addColumn(JfrAttributes.EVENT_TIMESTAMP);
 160                 CLASS_UNLOADING_LIST.addColumn(JfrAttributes.EVENT_THREAD);
 161                 CLASS_UNLOADING_LIST.addColumn(JdkAttributes.CLASS_UNLOADED);
 162                 CLASS_UNLOADING_LIST.addColumn(JdkAttributes.CLASS_DEFINING_CLASSLOADER);
 163                 CLASS_DEFINE_LIST.addColumn(JdkAttributes.CLASS_DEFINING_CLASSLOADER);
 164                 CLASS_DEFINE_LIST.addColumn(JdkAttributes.CLASS_DEFINED);
 165                 CLASS_DEFINE_LIST.addColumn(JfrAttributes.EVENT_THREAD);
 166                 CLASS_DEFINE_LIST.addColumn(JfrAttributes.START_TIME);
 167                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.ANONYMOUS_BLOCK_SIZE);
 168                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.ANONYMOUS_CHUNK_SIZE);
 169                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.ANONYMOUS_CLASS_COUNT);
 170                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.BLOCK_SIZE);
 171                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CHUNK_SIZE);
 172                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CLASS_COUNT);
 173                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CLASS_LOADER_DATA);
 174                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.PARENT_CLASSLOADER);
 175                 CLASS_LOADER_STATISTICS_LIST.addColumn(JdkAttributes.CLASSLOADER);
 176                 CLASS_LOADER_STATISTICS_LIST.addColumn(JfrAttributes.START_TIME);
 177                 // FIXME: Need to make a label provider for this
 178                 // FIXME: Want to have this in the same order
 179 
 180                 LEGEND_ITEMS.put(JdkAttributes.CLASSLOADER_LOADED_COUNT.getIdentifier(), Boolean.TRUE);
 181                 LEGEND_ITEMS.put(JdkAttributes.CLASSLOADER_UNLOADED_COUNT.getIdentifier(), Boolean.FALSE);
 182                 LEGEND_ITEMS.put(CLASS_LOAD, Boolean.TRUE);
 183                 LEGEND_ITEMS.put(CLASS_UNLOAD, Boolean.FALSE);
 184         }
 185 
 186         private class ClassLoadingUi implements IPageUI {
 187 
 188                 private final ChartCanvas classLoadingChart;
 189                 private final ItemList classLoadingTable;
 190                 private final ItemList classUnloadingTable;
 191                 private final ItemList classDefineTable;
 192                 private final ItemList classLoaderStatisticsTable;
 193                 private FilterComponent classLoadingFilter;
 194                 private FilterComponent classUnloadingFilter;
 195                 private FilterComponent classDefineFilter;
 196                 private FilterComponent classLoaderStatisticsFilter;
 197                 private final SashForm sash;
 198                 private final IPageContainer pageContainer;
 199                 private IItemCollection selectionItems;
 200                 private ItemHistogram classloaderHistogram;
 201                 private FilterComponent classloaderHistogramFilter;
 202                 private final IAction classLoadAction = DataPageToolkit.createTypeCheckAction(CLASS_LOAD, JdkTypeIDs.CLASS_LOAD,
 203                                 Messages.ClassLoadingPage_CLASS_LOADING_ACTION, Messages.ClassLoadingPage_CLASS_LOADING_ACTION_DESC,
 204                                 b -> updateChart());
 205                 private final IAction classUnloadAction = DataPageToolkit.createTypeCheckAction(CLASS_UNLOAD,
 206                                 JdkTypeIDs.CLASS_UNLOAD, Messages.ClassLoadingPage_CLASS_UNLOADING_ACTION,
 207                                 Messages.ClassLoadingPage_CLASS_UNLOADING_ACTION_DESC, b -> updateChart());
 208                 private final Stream<IAction> statsActions = Stream
 209                                 .of(JdkAttributes.CLASSLOADER_LOADED_COUNT, JdkAttributes.CLASSLOADER_UNLOADED_COUNT)
 210                                 .map(a -> DataPageToolkit.createAttributeCheckAction(a, b -> updateChart()));
 211                 private final List<IAction> allChartSeriesActions = Stream
 212                                 .concat(Stream.of(classLoadAction, classUnloadAction), statsActions).collect(Collectors.toList());
 213                 private CTabFolder tabFolder;
 214                 private XYChart chart;
 215                 private IRange<IQuantity> timeRange;
 216                 private FlavorSelector flavorSelector;
 217 
 218                 ClassLoadingUi(Composite parent, FormToolkit toolkit, IPageContainer pageContainer, IState state) {
 219                         this.pageContainer = pageContainer;
 220 
 221                         Form form = DataPageToolkit.createForm(parent, toolkit, getName(), getIcon());
 222 
 223                         sash = new SashForm(form.getBody(), SWT.VERTICAL);
 224 
 225                         Composite chartComp = new Composite(sash, SWT.NONE);
 226                         chartComp.setLayout(new GridLayout());
 227                         Control legend = ActionUiToolkit.buildCheckboxControl(chartComp, allChartSeriesActions.stream(), false);
 228                         legend.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
 229                         ActionToolkit.loadCheckState(state.getChild(CHART), allChartSeriesActions.stream());
 230 
 231                         chart = new XYChart(pageContainer.getRecordingRange(), RendererToolkit.empty(), 180);
 232                         chart.setVisibleRange(timelineRange.getStart(), timelineRange.getEnd());
 233                         chart.addVisibleRangeListener(r -> timelineRange = r);
 234                         classLoadingChart = new ChartCanvas(chartComp);
 235                         classLoadingChart.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 236                         DataPageToolkit.createChartTimestampTooltip(classLoadingChart);
 237                         DataPageToolkit.setChart(classLoadingChart, chart, pageContainer::showSelection, this::onChartSelection);
 238                         SelectionStoreActionToolkit.addSelectionStoreRangeActions(pageContainer.getSelectionStore(), chart,
 239                                         JfrAttributes.LIFETIME, Messages.ClassLoadingPage_CLASS_LOADING_TIMELINE_SELECTION,
 240                                         classLoadingChart.getContextMenu());
 241 
 242                         classloaderHistogram = CLASSLOADER_HISTOGRAM.buildWithoutBorder(sash,
 243                                         JdkAttributes.CLASS_DEFINING_CLASSLOADER, TableSettings.forState(state.getChild(HISTOGRAM)));
 244                         classloaderHistogramFilter = FilterComponent.createFilterComponent(classloaderHistogram, null,
 245                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOAD_OR_UNLOAD),
 246                                         pageContainer.getSelectionStore()::getSelections, this::onHistogramFilterChange);
 247                         classloaderHistogram.getManager().getViewer().addSelectionChangedListener(
 248                                         e -> pageContainer.showSelection(classloaderHistogram.getSelection().getItems()));
 249                         MCContextMenuManager classLoaderHistogramMm = MCContextMenuManager
 250                                         .create(classloaderHistogram.getManager().getViewer().getControl());
 251                         ColumnMenusFactory.addDefaultMenus(classloaderHistogram.getManager(), classLoaderHistogramMm);
 252                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(),
 253                                         classloaderHistogram, Messages.ClassLoadingPage_CLASS_LOADING_HISTOGRAM_SELECTION,
 254                                         classLoaderHistogramMm);
 255                         classLoaderHistogramMm.add(classloaderHistogramFilter.getShowFilterAction());
 256                         classLoaderHistogramMm.add(classloaderHistogramFilter.getShowSearchAction());
 257                         classloaderHistogramFilter.loadState(state.getChild(HISTOGRAM_FILTER));
 258 
 259                         ItemHistogramWithInput.chain(classloaderHistogram, this::updateTables);
 260 
 261                         tabFolder = new CTabFolder(sash, SWT.NONE);
 262 
 263                         classLoadingTable = CLASS_LOADING_LIST.buildWithoutBorder(tabFolder,
 264                                         TableSettings.forState(state.getChild(CLASS_LOADING_TABLE)));
 265                         classLoadingTable.getManager().getViewer().addSelectionChangedListener(e -> pageContainer
 266                                         .showSelection(ItemCollectionToolkit.build(classLoadingTable.getSelection().get())));
 267                         classLoadingFilter = FilterComponent.createFilterComponent(classLoadingTable, null,
 268                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOAD),
 269                                         pageContainer.getSelectionStore()::getSelections, this::onClassLoadFilterChange);
 270                         MCContextMenuManager classLoadingTableMm = MCContextMenuManager
 271                                         .create(classLoadingTable.getManager().getViewer().getControl());
 272                         ColumnMenusFactory.addDefaultMenus(classLoadingTable.getManager(), classLoadingTableMm);
 273                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classLoadingTable,
 274                                         Messages.ClassLoadingPage_CLASS_LOADING_LIST_SELECTION, classLoadingTableMm);
 275                         classLoadingTableMm.add(classLoadingFilter.getShowFilterAction());
 276                         classLoadingTableMm.add(classLoadingFilter.getShowSearchAction());
 277                         classLoadingFilter.loadState(state.getChild(CLASS_LOADING_FILTER));
 278                         DataPageToolkit.addTabItem(tabFolder, classLoadingFilter.getComponent(),
 279                                         Messages.ClassLoadingPage_CLASS_LOADING_TAB_TITLE);
 280                         
 281                         classLoaderStatisticsTable = CLASS_LOADER_STATISTICS_LIST.buildWithoutBorder(tabFolder,
 282                                         TableSettings.forState(state.getChild(CLASS_LOADER_STATISTICS_TABLE)));
 283                         classLoaderStatisticsTable.getManager().getViewer().addSelectionChangedListener(e -> pageContainer
 284                                         .showSelection(ItemCollectionToolkit.build(classLoaderStatisticsTable.getSelection().get())));
 285                         classLoaderStatisticsFilter = FilterComponent.createFilterComponent(classLoaderStatisticsTable, null,
 286                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOADER_STATISTICS),
 287                                         pageContainer.getSelectionStore()::getSelections, this::onClassLoadFilterChange);
 288                         MCContextMenuManager classLoaderStatisticsTableMm = MCContextMenuManager
 289                                         .create(classLoaderStatisticsTable.getManager().getViewer().getControl());
 290                         ColumnMenusFactory.addDefaultMenus(classLoaderStatisticsTable.getManager(), classLoaderStatisticsTableMm);
 291                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classLoaderStatisticsTable,
 292                                         Messages.ClassLoadingPage_CLASS_LOADER_STATISTICS_LIST_SELECTION, classLoaderStatisticsTableMm);
 293                         classLoaderStatisticsTableMm.add(classLoaderStatisticsFilter.getShowFilterAction());
 294                         classLoaderStatisticsTableMm.add(classLoaderStatisticsFilter.getShowSearchAction());
 295                         classLoaderStatisticsFilter.loadState(state.getChild(CLASS_LOADER_STATISTICS_FILTER));
 296                         DataPageToolkit.addTabItem(tabFolder, classLoaderStatisticsFilter.getComponent(),
 297                                         Messages.ClassLoadingPage_CLASS_LOADER_STATISTICS_TAB_TITLE);
 298                         
 299                         classDefineTable = CLASS_DEFINE_LIST.buildWithoutBorder(tabFolder,
 300                                         TableSettings.forState(state.getChild(CLASS_DEFINE_TABLE)));
 301                         classDefineTable.getManager().getViewer().addSelectionChangedListener(e -> pageContainer
 302                                         .showSelection(ItemCollectionToolkit.build(classDefineTable.getSelection().get())));
 303                         classDefineFilter = FilterComponent.createFilterComponent(classDefineTable, null,
 304                                         getDataSource().getItems().apply(JdkFilters.CLASS_DEFINE),
 305                                         pageContainer.getSelectionStore()::getSelections, this::onClassLoadFilterChange);
 306                         MCContextMenuManager classDefineTableMm = MCContextMenuManager
 307                                         .create(classDefineTable.getManager().getViewer().getControl());
 308                         ColumnMenusFactory.addDefaultMenus(classDefineTable.getManager(), classDefineTableMm);
 309                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classDefineTable,
 310                                         Messages.ClassLoadingPage_CLASS_DEFINE_LIST_SELECTION, classDefineTableMm);
 311                         classDefineTableMm.add(classDefineFilter.getShowFilterAction());
 312                         classDefineTableMm.add(classDefineFilter.getShowSearchAction());
 313                         classDefineFilter.loadState(state.getChild(CLASS_DEFINE_FILTER));
 314                         DataPageToolkit.addTabItem(tabFolder, classDefineFilter.getComponent(),
 315                                         Messages.ClassLoadingPage_CLASS_DEFINE_TAB_TITLE);
 316 
 317                         classUnloadingTable = CLASS_UNLOADING_LIST.buildWithoutBorder(tabFolder,
 318                                         TableSettings.forState(state.getChild(CLASS_UNLOADING_TABLE)));
 319                         classUnloadingTable.getManager().getViewer().addSelectionChangedListener(e -> pageContainer
 320                                         .showSelection(ItemCollectionToolkit.build(classUnloadingTable.getSelection().get())));
 321                         classUnloadingFilter = FilterComponent.createFilterComponent(classUnloadingTable, null,
 322                                         getDataSource().getItems().apply(JdkFilters.CLASS_UNLOAD),
 323                                         pageContainer.getSelectionStore()::getSelections, this::onClassUnloadFilterChange);
 324                         MCContextMenuManager classUnloadingTableMm = MCContextMenuManager
 325                                         .create(classUnloadingTable.getManager().getViewer().getControl());
 326                         ColumnMenusFactory.addDefaultMenus(classUnloadingTable.getManager(), classUnloadingTableMm);
 327                         SelectionStoreActionToolkit.addSelectionStoreActions(pageContainer.getSelectionStore(), classUnloadingTable,
 328                                         Messages.ClassLoadingPage_CLASS_UNLOADING_LIST_SELECTION, classUnloadingTableMm);
 329                         classUnloadingTableMm.add(classUnloadingFilter.getShowFilterAction());
 330                         classUnloadingTableMm.add(classUnloadingFilter.getShowSearchAction());
 331                         classUnloadingFilter.loadState(state.getChild(CLASS_UNLOADING_FILTER));
 332                         DataPageToolkit.addTabItem(tabFolder, classUnloadingFilter.getComponent(),
 333                                         Messages.ClassLoadingPage_CLASS_UNLOADING_TAB_TITLE);
 334 
 335                         tabFolder.setSelection(tabFolderIndex);
 336 
 337                         PersistableSashForm.loadState(sash, state.getChild(SASH));
 338                         flavorSelector = FlavorSelector.itemsWithTimerange(form, TABLE_FILTER, getDataSource().getItems(),
 339                                         pageContainer, this::onInputSelected, this::onShow, flavorSelectorState);
 340                         addResultActions(form);
 341 
 342                         onHistogramFilterChange(histogramFilter);
 343                         onClassLoadFilterChange(classLoadTableFilter);
 344                         onClassUnloadFilterChange(classUnloadTableFilter);
 345                         onClassDefineFilterChange(classDefineTableFilter);
 346                         onClassLoaderStatisticsFilterChange(classLoaderStatisticsTableFilter);
 347 
 348                         classloaderHistogram.getManager().setSelectionState(histogramSelection);
 349                         classLoadingTable.getManager().setSelectionState(classLoadingTableSelection);
 350                         classUnloadingTable.getManager().setSelectionState(classUnloadingTableSelection);
 351                         classDefineTable.getManager().setSelectionState(classDefineTableSelection);
 352                         classLoaderStatisticsTable.getManager().setSelectionState(classLoaderStatisticsTableSelection);
 353                 }
 354 
 355                 private void onHistogramFilterChange(IItemFilter filter) {
 356                         classloaderHistogramFilter.filterChangeHelper(filter, classloaderHistogram,
 357                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOAD_OR_UNLOAD));
 358                         if (classLoadingFilter != null) {
 359                                 classLoadingFilter.notifyListener();
 360                         }
 361                         if (classUnloadingFilter != null) {
 362                                 classUnloadingFilter.notifyListener();
 363                         }
 364                         if (classDefineFilter != null) {
 365                                 classDefineFilter.notifyListener();
 366                         }
 367                         if (classLoaderStatisticsFilter != null) {
 368                                 classLoaderStatisticsFilter.notifyListener();
 369                         }
 370                         histogramFilter = filter;
 371                 }
 372 
 373                 private void onClassLoadFilterChange(IItemFilter filter) {
 374                         classLoadingFilter.filterChangeHelper(filter, classLoadingTable,
 375                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOAD));
 376                         classLoadTableFilter = filter;
 377                 }
 378 
 379                 private void onClassUnloadFilterChange(IItemFilter filter) {
 380                         classUnloadingFilter.filterChangeHelper(filter, classUnloadingTable,
 381                                         getDataSource().getItems().apply(JdkFilters.CLASS_UNLOAD));
 382                         classUnloadTableFilter = filter;
 383                 }
 384                 
 385                 private void onClassDefineFilterChange(IItemFilter filter) {
 386                         classDefineFilter.filterChangeHelper(filter, classDefineTable,
 387                                         getDataSource().getItems().apply(JdkFilters.CLASS_DEFINE));
 388                         classDefineTableFilter = filter;
 389                 }
 390                 
 391                 private void onClassLoaderStatisticsFilterChange(IItemFilter filter) {
 392                         classLoaderStatisticsFilter.filterChangeHelper(filter, classLoaderStatisticsTable,
 393                                         getDataSource().getItems().apply(JdkFilters.CLASS_LOADER_STATISTICS));
 394                         classLoaderStatisticsTableFilter = filter;
 395                 }
 396 
 397                 @Override
 398                 public void saveTo(IWritableState state) {
 399                         PersistableSashForm.saveState(sash, state.createChild(SASH));
 400                         classloaderHistogram.getManager().getSettings().saveState(state.createChild(HISTOGRAM));
 401                         classLoadingTable.getManager().getSettings().saveState(state.createChild(CLASS_LOADING_TABLE));
 402                         classUnloadingTable.getManager().getSettings().saveState(state.createChild(CLASS_UNLOADING_TABLE));
 403                         classDefineTable.getManager().getSettings().saveState(state.createChild(CLASS_DEFINE_TABLE));
 404                         classLoaderStatisticsTable.getManager().getSettings().saveState(state.createChild(CLASS_LOADER_STATISTICS_TABLE));
 405                         classloaderHistogramFilter.saveState(state.createChild(HISTOGRAM_FILTER));
 406                         classLoadingFilter.saveState(state.createChild(CLASS_LOADING_FILTER));
 407                         classUnloadingFilter.saveState(state.createChild(CLASS_UNLOADING_FILTER));
 408                         classDefineFilter.saveState(state.createChild(CLASS_DEFINE_FILTER));
 409                         classLoaderStatisticsFilter.saveState(state.createChild(CLASS_LOADER_STATISTICS_FILTER));
 410                         ActionToolkit.saveCheckState(state.createChild(CHART), allChartSeriesActions.stream());
 411 
 412                         saveToLocal();
 413                 }
 414 
 415                 private void saveToLocal() {
 416                         histogramSelection = classloaderHistogram.getManager().getSelectionState();
 417                         classLoadingTableSelection = classLoadingTable.getManager().getSelectionState();
 418                         classUnloadingTableSelection = classUnloadingTable.getManager().getSelectionState();
 419                         classDefineTableSelection = classDefineTable.getManager().getSelectionState();
 420                         classLoaderStatisticsTableSelection = classLoaderStatisticsTable.getManager().getSelectionState();
 421                         tabFolderIndex = tabFolder.getSelectionIndex();
 422                         flavorSelectorState = flavorSelector.getFlavorSelectorState();
 423                 }
 424 
 425                 private void onShow(Boolean show) {
 426                         IRange<IQuantity> range = show ? timeRange : pageContainer.getRecordingRange();
 427                         chart.setVisibleRange(range.getStart(), range.getEnd());
 428                         updateChart();
 429                 }
 430 
 431                 private void onInputSelected(IItemCollection items, IRange<IQuantity> timeRange) {
 432                         selectionItems = items;
 433                         this.timeRange = timeRange;
 434                         updateHistogram(getItems());
 435                         updateTables(getItems());
 436                         updateChart();
 437                 }
 438 
 439                 private IItemCollection getItems() {
 440                         return selectionItems != null ? selectionItems : getDataSource().getItems();
 441                 }
 442 
 443                 private void updateChart() {
 444                         List<IXDataRenderer> rows = new ArrayList<>();
 445 
 446                         DataPageToolkit.buildLinesRow(Messages.ClassLoadingPage_ROW_CLASS_LOADING_STATISTICS,
 447                                         JdkAttributes.CLASSLOADER_LOADED_COUNT.getDescription(), getDataSource().getItems(), false,
 448                                         JdkQueries.CLASS_LOAD_STATISTICS, this::isAttributeEnabled, UnitLookup.NUMBER_UNITY.quantity(0),
 449                                         null).ifPresent(rows::add);
 450 
 451                         if (classLoadAction.isChecked()) {
 452                                 rows.add(DataPageToolkit.buildTimestampHistogram(Messages.ClassLoadingPage_ROW_CLASSES_LOADED,
 453                                                 Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_BY_CLASSLOADER_DESC,
 454                                                 getItems().apply(JdkFilters.CLASS_LOAD),
 455                                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_LOADED,
 456                                                                 Messages.ClassLoadingPage_AGGR_CLASSES_LOADED_DESC, JdkFilters.CLASS_LOAD),
 457                                                 TypeLabelProvider.getColor(JdkTypeIDs.CLASS_LOAD)));
 458                         }
 459                         if (classUnloadAction.isChecked()) {
 460                                 rows.add(DataPageToolkit.buildTimestampHistogram(Messages.ClassLoadingPage_ROW_CLASSES_UNLOADED,
 461                                                 Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_DESC, getItems().apply(JdkFilters.CLASS_UNLOAD),
 462                                                 Aggregators.count(Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED,
 463                                                                 Messages.ClassLoadingPage_AGGR_CLASSES_UNLOADED_DESC, JdkFilters.CLASS_UNLOAD),
 464                                                 TypeLabelProvider.getColor(JdkTypeIDs.CLASS_UNLOAD)));
 465                         }
 466                         classLoadingChart.replaceRenderer(RendererToolkit.uniformRows(rows));
 467                 }
 468 
 469                 private boolean isAttributeEnabled(IAttribute<IQuantity> attr) {
 470                         return allChartSeriesActions.stream().filter(a -> attr.getIdentifier().equals(a.getId())).findAny().get()
 471                                         .isChecked();
 472                 }
 473 
 474                 private void updateHistogram(IItemCollection items) {
 475                         if (classloaderHistogram != null) {
 476                                 classloaderHistogram.show(items.apply(JdkFilters.CLASS_LOAD_OR_UNLOAD));
 477                         }
 478                 }
 479 
 480                 private void updateTables(IItemCollection selectedItems) {
 481                         if (classLoadingTable != null && classUnloadingTable != null) {
 482                                 classLoadingTable.show(selectedItems.apply(JdkQueries.CLASS_LOAD.getFilter()));
 483                                 classUnloadingTable.show(selectedItems.apply(JdkQueries.CLASS_UNLOAD.getFilter()));
 484                         }
 485                 }
 486 
 487                 private void onChartSelection(IRange<IQuantity> range) {
 488                         // FIXME: Make this depend on the legend as well? And maybe on which chart row has been selected?
 489                         IItemCollection itemsInRange = range != null ? getItems().apply(ItemFilters
 490                                         .matchRange(RangeMatchPolicy.CENTER_CONTAINED_IN_RIGHT_OPEN, JfrAttributes.LIFETIME, range))
 491                                         : getItems();
 492                         updateTables(itemsInRange);
 493                         updateHistogram(itemsInRange);
 494                 }
 495 
 496         }
 497 
 498         private static final String SASH = "sash"; //$NON-NLS-1$
 499         private static final String HISTOGRAM = "histogram"; //$NON-NLS-1$
 500         private static final String HISTOGRAM_FILTER = "histogramFilter"; //$NON-NLS-1$
 501         private static final String CLASS_LOADING_TABLE = "classLoadingTable"; //$NON-NLS-1$
 502         private static final String CLASS_UNLOADING_TABLE = "classUnloadingTable"; //$NON-NLS-1$
 503         private static final String CLASS_DEFINE_TABLE = "classDefineTable"; //$NON-NLS-1$
 504         private static final String CLASS_LOADER_STATISTICS_TABLE = "classLoaderStatisticsTable"; //$NON-NLS-1$
 505         private static final String CLASS_LOADING_FILTER = "classLoadingFilter"; //$NON-NLS-1$
 506         private static final String CLASS_UNLOADING_FILTER = "classUnloadingFilter"; //$NON-NLS-1$
 507         private static final String CLASS_DEFINE_FILTER = "classDefineFilter"; //$NON-NLS-1$
 508         private static final String CLASS_LOADER_STATISTICS_FILTER = "classLoaderStatisticsFilter"; //$NON-NLS-1$
 509         private static final String CHART = "chart"; //$NON-NLS-1$
 510 
 511         @Override
 512         public IPageUI display(Composite parent, FormToolkit toolkit, IPageContainer pageContainer, IState state) {
 513                 return new ClassLoadingUi(parent, toolkit, pageContainer, state);
 514         }
 515 
 516         private SelectionState histogramSelection;
 517         private SelectionState classLoadingTableSelection;
 518         private SelectionState classUnloadingTableSelection;
 519         private SelectionState classDefineTableSelection;
 520         private SelectionState classLoaderStatisticsTableSelection;
 521         private IItemFilter histogramFilter;
 522         private IItemFilter classLoadTableFilter;
 523         private IItemFilter classUnloadTableFilter;
 524         private IItemFilter classDefineTableFilter;
 525         private IItemFilter classLoaderStatisticsTableFilter;
 526         private int tabFolderIndex = 0;
 527         private IRange<IQuantity> timelineRange;
 528         private FlavorSelectorState flavorSelectorState;
 529 
 530         public ClassLoadingPage(IPageDefinition dpd, StreamModel items, IPageContainer editor) {
 531                 super(dpd, items, editor);
 532                 timelineRange = editor.getRecordingRange();
 533         }
 534 
 535         @Override
 536         public IItemFilter getDefaultSelectionFilter() {
 537                 return ItemFilters.or(TABLE_FILTER, JdkFilters.CLASS_LOAD_STATISTICS);
 538         }
 539 
 540 }