1 18 package org.apache.activemq.tool.reports.plugins; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.activemq.tool.reports.PerformanceStatisticsUtil; 23 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 30 public class CpuReportPlugin implements ReportPlugin { 31 private static final Log log = LogFactory.getLog(CpuReportPlugin.class); 32 33 public static final String NAME_IGNORE_LIST = "$index$timeUnit$r$b$w$swpd$inact$active$free$buff$cache$si$so$in$"; 34 public static final String NAME_BLOCK_RECV = "bi"; 35 public static final String NAME_BLOCK_SENT = "bo"; 36 public static final String NAME_CTX_SWITCH = "cs"; 37 public static final String NAME_USER_TIME = "us"; 38 public static final String NAME_SYS_TIME = "sy"; 39 public static final String NAME_IDLE_TIME = "id"; 40 public static final String NAME_WAIT_TIME = "wa"; 41 42 public static final String KEY_BLOCK_RECV = "BlocksReceived"; 43 public static final String KEY_BLOCK_SENT = "BlocksSent"; 44 public static final String KEY_CTX_SWITCH = "ContextSwitches"; 45 public static final String KEY_USER_TIME = "UserTime"; 46 public static final String KEY_SYS_TIME = "SystemTime"; 47 public static final String KEY_IDLE_TIME = "IdleTime"; 48 public static final String KEY_WAIT_TIME = "WaitingTime"; 49 50 public static final String KEY_AVE_BLOCK_RECV = "AveBlocksReceived"; 51 public static final String KEY_AVE_BLOCK_SENT = "AveBlocksSent"; 52 public static final String KEY_AVE_CTX_SWITCH = "AveContextSwitches"; 53 public static final String KEY_AVE_USER_TIME = "AveUserTime"; 54 public static final String KEY_AVE_SYS_TIME = "AveSystemTime"; 55 public static final String KEY_AVE_IDLE_TIME = "AveIdleTime"; 56 public static final String KEY_AVE_WAIT_TIME = "AveWaitingTime"; 57 58 protected List blockRecv = new ArrayList (); 59 protected List blockSent = new ArrayList (); 60 protected List ctxSwitch = new ArrayList (); 61 protected List userTime = new ArrayList (); 62 protected List sysTime = new ArrayList (); 63 protected List idleTime = new ArrayList (); 64 protected List waitTime = new ArrayList (); 65 66 public void handleCsvData(String csvData) { 67 StringTokenizer tokenizer = new StringTokenizer (csvData, ","); 68 String data, key, val; 69 while (tokenizer.hasMoreTokens()) { 70 data = tokenizer.nextToken(); 71 key = data.substring(0, data.indexOf("=")); 72 val = data.substring(data.indexOf("=") + 1); 73 74 addToCpuList(key, val); 75 } 76 } 77 78 public Map getSummary() { 79 long val; 80 81 Map summary = new HashMap (); 82 83 if (blockRecv.size() > 0) { 84 val = PerformanceStatisticsUtil.getSum(blockRecv); 85 summary.put(KEY_BLOCK_RECV, String.valueOf(val)); 86 summary.put(KEY_AVE_BLOCK_RECV, String.valueOf((double)val / (double)blockRecv.size())); 87 } 88 89 if (blockSent.size() > 0) { 90 val = PerformanceStatisticsUtil.getSum(blockSent); 91 summary.put(KEY_BLOCK_SENT, String.valueOf(val)); 92 summary.put(KEY_AVE_BLOCK_SENT, String.valueOf((double)val / (double)blockSent.size())); 93 } 94 95 if (ctxSwitch.size() > 0) { 96 val = PerformanceStatisticsUtil.getSum(ctxSwitch); 97 summary.put(KEY_CTX_SWITCH, String.valueOf(val)); 98 summary.put(KEY_AVE_CTX_SWITCH, String.valueOf((double)val / (double)ctxSwitch.size())); 99 } 100 101 if (userTime.size() > 0) { 102 val = PerformanceStatisticsUtil.getSum(userTime); 103 summary.put(KEY_USER_TIME, String.valueOf(val)); 104 summary.put(KEY_AVE_USER_TIME, String.valueOf((double)val / (double)userTime.size())); 105 } 106 107 if (sysTime.size() > 0) { 108 val = PerformanceStatisticsUtil.getSum(sysTime); 109 summary.put(KEY_SYS_TIME, String.valueOf(val)); 110 summary.put(KEY_AVE_SYS_TIME, String.valueOf((double)val / (double)sysTime.size())); 111 } 112 113 if (idleTime.size() > 0) { 114 val = PerformanceStatisticsUtil.getSum(idleTime); 115 summary.put(KEY_IDLE_TIME, String.valueOf(val)); 116 summary.put(KEY_AVE_IDLE_TIME, String.valueOf((double)val / (double)idleTime.size())); 117 } 118 119 if (waitTime.size() > 0) { 120 val = PerformanceStatisticsUtil.getSum(waitTime); 121 summary.put(KEY_WAIT_TIME, String.valueOf(val)); 122 summary.put(KEY_AVE_WAIT_TIME, String.valueOf((double)val / (double)waitTime.size())); 123 } 124 125 if (summary.size() > 0) { 126 return summary; 127 } else { 128 return null; 129 } 130 } 131 132 protected void addToCpuList(String key, String val) { 133 if (key.equals(NAME_BLOCK_RECV)) { 134 blockRecv.add(Long.valueOf(val)); 135 } else if (key.equals(NAME_BLOCK_SENT)) { 136 blockSent.add(Long.valueOf(val)); 137 } else if (key.equals(NAME_CTX_SWITCH)) { 138 ctxSwitch.add(Long.valueOf(val)); 139 } else if (key.equals(NAME_USER_TIME)) { 140 userTime.add(Long.valueOf(val)); 141 } else if (key.equals(NAME_SYS_TIME)) { 142 sysTime.add(Long.valueOf(val)); 143 } else if (key.equals(NAME_IDLE_TIME)) { 144 idleTime.add(Long.valueOf(val)); 145 } else if (key.equals(NAME_WAIT_TIME)) { 146 waitTime.add(Long.valueOf(val)); 147 } else if (NAME_IGNORE_LIST.indexOf("$" + key + "$") != -1) { 148 } else { 150 log.warn("Unrecognized CPU data. " + key + "=" + val); 151 } 152 } 153 } 154 | Popular Tags |