KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > sampler > plugins > LinuxCpuSamplerPlugin


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.tool.sampler.plugins;
18
19 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 public class LinuxCpuSamplerPlugin implements CpuSamplerPlugin, Runnable JavaDoc {
27
28     private Process JavaDoc vmstatProcess;
29     private String JavaDoc vmstat;
30     private String JavaDoc result = "";
31     private final Object JavaDoc mutex = new Object JavaDoc();
32     private AtomicBoolean JavaDoc stop = new AtomicBoolean JavaDoc(false);
33
34     public LinuxCpuSamplerPlugin(long intervalInMs) {
35         vmstat = "vmstat -n " + (int)(intervalInMs / 1000);
36     }
37
38     public void start() {
39         stop.set(false);
40         Thread JavaDoc t = new Thread JavaDoc(this);
41         t.start();
42     }
43
44     public void stop() {
45         stop.set(true);
46         try {
47             vmstatProcess.waitFor();
48         } catch (InterruptedException JavaDoc e) {
49             e.printStackTrace();
50         }
51     }
52
53     public void run() {
54
55         try {
56             vmstatProcess = Runtime.getRuntime().exec(vmstat);
57             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(vmstatProcess.getInputStream()), 1024);
58
59             br.readLine(); // throw away the first line
60

61             String JavaDoc header = br.readLine();
62             String JavaDoc data;
63
64             while (!stop.get()) {
65                 data = br.readLine();
66                 if (data != null) {
67                     String JavaDoc csvData = convertToCSV(header, data);
68                     synchronized (mutex) {
69                         result = csvData;
70                     }
71                 }
72             }
73             br.close();
74             vmstatProcess.destroy();
75
76         } catch (IOException JavaDoc ioe) {
77             ioe.printStackTrace();
78         }
79     }
80
81     public String JavaDoc getCpuUtilizationStats() {
82         String JavaDoc data;
83         synchronized (mutex) {
84             data = result;
85             result = "";
86         }
87         return data;
88     }
89
90     public String JavaDoc getVmstat() {
91         return vmstat;
92     }
93
94     public void setVmstat(String JavaDoc vmstat) {
95         this.vmstat = vmstat;
96     }
97
98     protected String JavaDoc convertToCSV(String JavaDoc header, String JavaDoc data) {
99         StringTokenizer JavaDoc headerTokens = new StringTokenizer JavaDoc(header, " ");
100         StringTokenizer JavaDoc dataTokens = new StringTokenizer JavaDoc(data, " ");
101
102         String JavaDoc csv = "";
103         while (headerTokens.hasMoreTokens()) {
104             csv += (headerTokens.nextToken() + "=" + dataTokens.nextToken() + ",");
105         }
106
107         return csv;
108     }
109 }
110
Popular Tags