Coverage Report - sk.baka.webvm.Memory
 
Classes in this File Line Coverage Branch Coverage Complexity
Memory
0%
0/37
0%
0/10
2.077
Memory$1
0%
0/2
N/A
2.077
Memory$2
0%
0/3
0%
0/2
2.077
Memory$MemoryBeansProducer
0%
0/5
0%
0/2
2.077
Memory$MemoryPoolDetailListView
0%
0/27
0%
0/8
2.077
Memory$MemoryPoolListView
0%
0/20
0%
0/4
2.077
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 18  
  */
 19  
 package sk.baka.webvm;
 20  
 
 21  
 import java.lang.management.GarbageCollectorMXBean;
 22  
 import java.lang.management.ManagementFactory;
 23  
 import java.lang.management.MemoryManagerMXBean;
 24  
 import java.lang.management.MemoryPoolMXBean;
 25  
 import java.lang.management.MemoryUsage;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Arrays;
 28  
 import java.util.Collections;
 29  
 import java.util.List;
 30  
 import org.apache.wicket.markup.html.basic.Label;
 31  
 import org.apache.wicket.markup.html.list.ListItem;
 32  
 import org.apache.wicket.markup.html.list.ListView;
 33  
 import org.apache.wicket.model.IModel;
 34  
 import org.apache.wicket.model.LoadableDetachableModel;
 35  
 import sk.baka.webvm.misc.DivGraph;
 36  
 import sk.baka.webvm.misc.MgmtUtils;
 37  
 import sk.baka.webvm.misc.Producer;
 38  
 
 39  
 /**
 40  
  * Shows detailed memory information.
 41  
  * @author Martin Vysny
 42  
  */
 43  
 public final class Memory extends WebVMPage {
 44  
 
 45  
     private static final long serialVersionUID = 1L;
 46  
     private static final int GRAPH_WIDTH_PIXELS = 300;
 47  
 
 48  
     /**
 49  
      * Creates new object instance
 50  
      */
 51  0
     public Memory() {
 52  0
         MemoryUsage usage = MgmtUtils.getInMB(MgmtUtils.getHeapFromRuntime());
 53  0
         drawMemoryStatus(usage, "heapStatusBar", GRAPH_WIDTH_PIXELS);
 54  0
         border.add(new Label("heapStatusText", MgmtUtils.toString(usage, true)));
 55  0
         usage = MgmtUtils.getInMB(sk.baka.webvm.analyzer.hostos.Memory.getPhysicalMemory());
 56  0
         drawMemoryStatus(usage, "physicalMemoryStatusBar", GRAPH_WIDTH_PIXELS);
 57  0
         border.add(new Label("physicalMemoryStatusText", MgmtUtils.toString(usage, true)));
 58  0
         usage = MgmtUtils.getInMB(sk.baka.webvm.analyzer.hostos.Memory.getSwap());
 59  0
         drawMemoryStatus(usage, "swapStatusBar", GRAPH_WIDTH_PIXELS);
 60  0
         border.add(new Label("swapStatusText", MgmtUtils.toString(usage, true)));
 61  0
         addMemoryPoolInfo(border, new MemoryBeansProducer(false), "memoryManagers", "memManName", "memManValid", "memManProperties");
 62  0
         addMemoryPoolInfo(border, new MemoryBeansProducer(true), "gc", "gcName", "gcValid", "gcProperties");
 63  0
         addDetailedMemoryPoolInfo(border);
 64  0
         addGCStats();
 65  0
     }
 66  
 
 67  
     private void addGCStats() {
 68  
         // garbage collections
 69  0
         int collectors = 0;
 70  0
         long collections = 0;
 71  0
         long collectTime = 0;
 72  0
         final List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans();
 73  0
         if (beans != null) {
 74  0
             for (final GarbageCollectorMXBean bean : beans) {
 75  0
                 if (bean.isValid()) {
 76  0
                     collectors++;
 77  
                 }
 78  0
                 if (bean.getCollectionCount() > 0) {
 79  0
                     collections += bean.getCollectionCount();
 80  
                 }
 81  0
                 if (bean.getCollectionTime() > 0) {
 82  0
                     collectTime += bean.getCollectionTime();
 83  
                 }
 84  
             }
 85  
         }
 86  0
         border.add(new Label("gcCount", Long.toString(collectors)));
 87  0
         border.add(new Label("gcAmount", Long.toString(collections)));
 88  0
         border.add(new Label("gcTime", Long.toString(collectTime)));
 89  0
     }
 90  
 
 91  
     private static void addDetailedMemoryPoolInfo(final AppBorder border) {
 92  0
         final IModel<List<MemoryPoolMXBean>> model = new LoadableDetachableModel<List<MemoryPoolMXBean>>() {
 93  
 
 94  
             private static final long serialVersionUID = 1L;
 95  
 
 96  
             @Override
 97  
             protected List<MemoryPoolMXBean> load() {
 98  0
                 return ManagementFactory.getMemoryPoolMXBeans();
 99  
             }
 100  
         };
 101  0
         border.add(new MemoryPoolDetailListView("memoryPool", model));
 102  0
     }
 103  
 
 104  
     private static void addMemoryPoolInfo(final AppBorder border, final Producer<? extends List<? extends MemoryManagerMXBean>> memoryBeansProducer, final String listId, final String nameId, final String validId, final String propsId) {
 105  0
         final IModel<List<MemoryManagerMXBean>> model = new LoadableDetachableModel<List<MemoryManagerMXBean>>() {
 106  
 
 107  
             private static final long serialVersionUID = 1L;
 108  
 
 109  
             @Override
 110  
             protected List<MemoryManagerMXBean> load() {
 111  0
                 final List<? extends MemoryManagerMXBean> beans = memoryBeansProducer.produce();
 112  0
                 return beans != null ? new ArrayList<MemoryManagerMXBean>(beans) : Collections.<MemoryManagerMXBean>emptyList();
 113  
             }
 114  
         };
 115  0
         border.add(new MemoryPoolListView(listId, model, nameId, validId, propsId));
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Shows a list of detailed information about memory pools.
 120  
      */
 121  
     private static class MemoryPoolDetailListView extends ListView<MemoryPoolMXBean> {
 122  
 
 123  
         public MemoryPoolDetailListView(String id, IModel<? extends List<? extends MemoryPoolMXBean>> model) {
 124  0
             super(id, model);
 125  0
         }
 126  
         private static final int MEMSTAT_GRAPH_WIDTH = 200;
 127  
         private static final long serialVersionUID = 1L;
 128  
 
 129  
         @Override
 130  
         protected void populateItem(ListItem<MemoryPoolMXBean> item) {
 131  0
             final MemoryPoolMXBean bean = item.getModelObject();
 132  0
             item.add(new Label("poolName", bean.getName()));
 133  0
             item.add(new Label("poolType", "" + bean.getType()));
 134  0
             item.add(new Label("poolValid", bean.isValid() ? "Y" : "N"));
 135  0
             MemoryUsage usage = MgmtUtils.getInMB(bean.getCollectionUsage());
 136  0
             add(item, "poolCollects", usage, true);
 137  0
             item.add(new Label("poolCollectsPerc", MgmtUtils.getUsagePerc(usage)));
 138  0
             usage = MgmtUtils.getInMB(bean.getPeakUsage());
 139  0
             add(item, "poolPeak", usage, false);
 140  0
             item.add(new Label("poolPeakPerc", MgmtUtils.getUsagePerc(usage)));
 141  0
             usage = MgmtUtils.getInMB(bean.getUsage());
 142  0
             add(item, "poolUsage", usage, false);
 143  0
             item.add(new Label("poolUsagePerc", MgmtUtils.getUsagePerc(usage)));
 144  0
         }
 145  
 
 146  
         private void add(final ListItem<?> item, final String wid, MemoryUsage usage, final boolean gc) {
 147  0
             if (usage == null && gc) {
 148  0
                 item.add(new Label(wid, "Not collectable"));
 149  0
                 return;
 150  
             }
 151  0
             final StringBuilder sb = new StringBuilder();
 152  0
             if (usage != null) {
 153  0
                 sb.append(DivGraph.drawMemoryStatus(usage, MEMSTAT_GRAPH_WIDTH));
 154  
             }
 155  0
             sb.append(MgmtUtils.toString(usage, true));
 156  0
             final Label label = new Label(wid, sb.toString());
 157  0
             label.setEscapeModelStrings(false);
 158  0
             item.add(label);
 159  0
         }
 160  
     }
 161  
 
 162  0
     private static class MemoryBeansProducer implements Producer<List<? extends MemoryManagerMXBean>> {
 163  
 
 164  
         private final boolean isGcOnly;
 165  
 
 166  0
         public MemoryBeansProducer(boolean isGcOnly) {
 167  0
             this.isGcOnly = isGcOnly;
 168  0
         }
 169  
         private static final long serialVersionUID = 1L;
 170  
 
 171  
         public List<? extends MemoryManagerMXBean> produce() {
 172  0
             return isGcOnly ? ManagementFactory.getGarbageCollectorMXBeans() : ManagementFactory.getMemoryManagerMXBeans();
 173  
         }
 174  
     }
 175  
 
 176  
     /**
 177  
      * Shows a quick memory pool overview.
 178  
      */
 179  
     private static class MemoryPoolListView extends ListView<MemoryManagerMXBean> {
 180  
 
 181  
         private final String nameId;
 182  
         private final String validId;
 183  
         private final String propsId;
 184  
 
 185  
         public MemoryPoolListView(String id, IModel<? extends List<? extends MemoryManagerMXBean>> model, String nameId, String validId, String propsId) {
 186  0
             super(id, model);
 187  0
             this.nameId = nameId;
 188  0
             this.validId = validId;
 189  0
             this.propsId = propsId;
 190  0
         }
 191  
         private static final long serialVersionUID = 1L;
 192  
 
 193  
         @Override
 194  
         protected void populateItem(ListItem<MemoryManagerMXBean> item) {
 195  0
             final MemoryManagerMXBean bean = item.getModelObject();
 196  0
             item.add(new Label(nameId, bean.getName()));
 197  0
             item.add(new Label(validId, bean.isValid() ? "Y" : "N"));
 198  0
             final StringBuilder sb = new StringBuilder();
 199  0
             sb.append("Memory Pools: ");
 200  0
             sb.append(Arrays.toString(bean.getMemoryPoolNames()));
 201  0
             if (bean instanceof GarbageCollectorMXBean) {
 202  0
                 final GarbageCollectorMXBean gb = (GarbageCollectorMXBean) bean;
 203  0
                 sb.append("; Collections: ");
 204  0
                 sb.append(gb.getCollectionCount());
 205  0
                 sb.append("; Time: ");
 206  0
                 sb.append(gb.getCollectionTime());
 207  0
                 sb.append("ms");
 208  
             }
 209  0
             item.add(new Label(propsId, sb.toString()));
 210  0
         }
 211  
     }
 212  
 }
 213