001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.tool.reports.plugins;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.StringTokenizer;
024    
025    import org.apache.activemq.tool.reports.PerformanceStatisticsUtil;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    public class CpuReportPlugin implements ReportPlugin {
030    
031        public static final String NAME_IGNORE_LIST = "$index$timeUnit$r$b$w$swpd$inact$active$free$buff$cache$si$so$in$";
032        public static final String NAME_BLOCK_RECV = "bi";
033        public static final String NAME_BLOCK_SENT = "bo";
034        public static final String NAME_CTX_SWITCH = "cs";
035        public static final String NAME_USER_TIME  = "us";
036        public static final String NAME_SYS_TIME   = "sy";
037        public static final String NAME_IDLE_TIME  = "id";
038        public static final String NAME_WAIT_TIME  = "wa";
039    
040        public static final String KEY_BLOCK_RECV = "BlocksReceived";
041        public static final String KEY_BLOCK_SENT = "BlocksSent";
042        public static final String KEY_CTX_SWITCH = "ContextSwitches";
043        public static final String KEY_USER_TIME  = "UserTime";
044        public static final String KEY_SYS_TIME   = "SystemTime";
045        public static final String KEY_IDLE_TIME  = "IdleTime";
046        public static final String KEY_WAIT_TIME  = "WaitingTime";
047    
048        public static final String KEY_AVE_BLOCK_RECV = "AveBlocksReceived";
049        public static final String KEY_AVE_BLOCK_SENT = "AveBlocksSent";
050        public static final String KEY_AVE_CTX_SWITCH = "AveContextSwitches";
051        public static final String KEY_AVE_USER_TIME  = "AveUserTime";
052        public static final String KEY_AVE_SYS_TIME   = "AveSystemTime";
053        public static final String KEY_AVE_IDLE_TIME  = "AveIdleTime";
054        public static final String KEY_AVE_WAIT_TIME  = "AveWaitingTime";
055    
056        private static final Logger LOG = LoggerFactory.getLogger(CpuReportPlugin.class);
057    
058        protected List<Long> blockRecv = new ArrayList<Long>();
059        protected List<Long> blockSent = new ArrayList<Long>();
060        protected List<Long> ctxSwitch = new ArrayList<Long>();
061        protected List<Long> userTime  = new ArrayList<Long>();
062        protected List<Long> sysTime   = new ArrayList<Long>();
063        protected List<Long> idleTime  = new ArrayList<Long>();
064        protected List<Long> waitTime  = new ArrayList<Long>();
065    
066        public void handleCsvData(String csvData) {
067            StringTokenizer tokenizer = new StringTokenizer(csvData, ",");
068            String data;
069            String key;
070            String val;
071            while (tokenizer.hasMoreTokens()) {
072                data = tokenizer.nextToken();
073                key  = data.substring(0, data.indexOf("="));
074                val  = data.substring(data.indexOf("=") + 1);
075    
076                addToCpuList(key, val);
077            }
078        }
079    
080        public Map<String, String> getSummary() {
081            long val;
082    
083            Map<String, String> summary = new HashMap<String, String>();
084    
085            if (blockRecv.size() > 0) {
086                val = PerformanceStatisticsUtil.getSum(blockRecv);
087                summary.put(KEY_BLOCK_RECV, String.valueOf(val));
088                summary.put(KEY_AVE_BLOCK_RECV, String.valueOf((double)val / (double)blockRecv.size()));
089            }
090    
091            if (blockSent.size() > 0) {
092                val = PerformanceStatisticsUtil.getSum(blockSent);
093                summary.put(KEY_BLOCK_SENT, String.valueOf(val));
094                summary.put(KEY_AVE_BLOCK_SENT, String.valueOf((double)val / (double)blockSent.size()));
095            }
096    
097            if (ctxSwitch.size() > 0) {
098                val = PerformanceStatisticsUtil.getSum(ctxSwitch);
099                summary.put(KEY_CTX_SWITCH, String.valueOf(val));
100                summary.put(KEY_AVE_CTX_SWITCH, String.valueOf((double)val / (double)ctxSwitch.size()));
101            }
102    
103            if (userTime.size() > 0) {
104                val = PerformanceStatisticsUtil.getSum(userTime);
105                summary.put(KEY_USER_TIME, String.valueOf(val));
106                summary.put(KEY_AVE_USER_TIME, String.valueOf((double)val / (double)userTime.size()));
107            }
108    
109            if (sysTime.size() > 0) {
110                val = PerformanceStatisticsUtil.getSum(sysTime);
111                summary.put(KEY_SYS_TIME, String.valueOf(val));
112                summary.put(KEY_AVE_SYS_TIME, String.valueOf((double)val / (double)sysTime.size()));
113            }
114    
115            if (idleTime.size() > 0) {
116                val = PerformanceStatisticsUtil.getSum(idleTime);
117                summary.put(KEY_IDLE_TIME, String.valueOf(val));
118                summary.put(KEY_AVE_IDLE_TIME, String.valueOf((double)val / (double)idleTime.size()));
119            }
120    
121            if (waitTime.size() > 0) {
122                val = PerformanceStatisticsUtil.getSum(waitTime);
123                summary.put(KEY_WAIT_TIME, String.valueOf(val));
124                summary.put(KEY_AVE_WAIT_TIME, String.valueOf((double)val / (double)waitTime.size()));
125            }
126    
127            if (summary.size() > 0) {
128                return summary;
129            } else {
130                return null;
131            }
132        }
133    
134        protected void addToCpuList(String key, String val) {
135            if (key.equals(NAME_BLOCK_RECV)) {
136                blockRecv.add(Long.valueOf(val));
137            } else if (key.equals(NAME_BLOCK_SENT)) {
138                blockSent.add(Long.valueOf(val));
139            } else if (key.equals(NAME_CTX_SWITCH)) {
140                ctxSwitch.add(Long.valueOf(val));
141            } else if (key.equals(NAME_USER_TIME)) {
142                userTime.add(Long.valueOf(val));
143            } else if (key.equals(NAME_SYS_TIME)) {
144                sysTime.add(Long.valueOf(val));
145            } else if (key.equals(NAME_IDLE_TIME)) {
146                idleTime.add(Long.valueOf(val));
147            } else if (key.equals(NAME_WAIT_TIME)) {
148                waitTime.add(Long.valueOf(val));
149            } else if (NAME_IGNORE_LIST.indexOf("$" + key + "$") != -1) {
150                // Ignore key
151            } else {
152                LOG.warn("Unrecognized CPU data. " + key + "=" + val);
153            }
154        }
155    }