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.sampler.plugins;
018    
019    import java.io.BufferedReader;
020    import java.io.IOException;
021    import java.io.InputStreamReader;
022    import java.util.StringTokenizer;
023    import java.util.concurrent.atomic.AtomicBoolean;
024    
025    public class LinuxCpuSamplerPlugin implements CpuSamplerPlugin, Runnable {
026    
027        private Process vmstatProcess;
028        private String vmstat;
029        private String result = "";
030        private final Object mutex = new Object();
031        private AtomicBoolean stop = new AtomicBoolean(false);
032    
033        public LinuxCpuSamplerPlugin(long intervalInMs) {
034            vmstat = "vmstat -n " + (int)(intervalInMs / 1000);
035        }
036    
037        public void start() {
038            stop.set(false);
039            Thread t = new Thread(this);
040            t.start();
041        }
042    
043        public void stop() {
044            stop.set(true);
045            try {
046                vmstatProcess.waitFor();
047            } catch (InterruptedException e) {
048                e.printStackTrace();
049            }
050        }
051    
052        public void run() {
053    
054            try {
055                vmstatProcess = Runtime.getRuntime().exec(vmstat);
056                BufferedReader br = new BufferedReader(new InputStreamReader(vmstatProcess.getInputStream()), 1024);
057    
058                br.readLine(); // throw away the first line
059    
060                String header = br.readLine();
061                String data;
062    
063                while (!stop.get()) {
064                    data = br.readLine();
065                    if (data != null) {
066                        String csvData = convertToCSV(header, data);
067                        synchronized (mutex) {
068                            result = csvData;
069                        }
070                    }
071                }
072                br.close();
073                vmstatProcess.destroy();
074    
075            } catch (IOException ioe) {
076                ioe.printStackTrace();
077            }
078        }
079    
080        public String getCpuUtilizationStats() {
081            String data;
082            synchronized (mutex) {
083                data = result;
084                result = "";
085            }
086            return data;
087        }
088    
089        public String getVmstat() {
090            return vmstat;
091        }
092    
093        public void setVmstat(String vmstat) {
094            this.vmstat = vmstat;
095        }
096    
097        protected String convertToCSV(String header, String data) {
098            StringTokenizer headerTokens = new StringTokenizer(header, " ");
099            StringTokenizer dataTokens = new StringTokenizer(data, " ");
100    
101            String csv = "";
102            while (headerTokens.hasMoreTokens()) {
103                csv += headerTokens.nextToken() + "=" + dataTokens.nextToken() + ",";
104            }
105    
106            return csv;
107        }
108    }