KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > PerformanceMonitorLinux


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.internal.performance;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.FileReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 class PerformanceMonitorLinux extends PerformanceMonitor {
21
22     private static long PAGESIZE= 4096;
23     private static long JIFFIES= 10L;
24     private static boolean fgHasElapsedTime= true;
25     private static long fgStartupTime;
26     
27     /**
28      * Write out operating system counters for Linux.
29      * @param scalars
30      */

31     protected void collectOperatingSystemCounters(Map JavaDoc scalars) {
32         synchronized(this) {
33             /**
34              * The status values for a Linux process, that is the values that come from /proc/self/stat.
35              * The names of the variables match the man proc page.
36              */

37             StringTokenizer JavaDoc st= readTokens("/proc/self/stat", false); //$NON-NLS-1$
38
if (st != null) {
39                 st.nextToken(); // int pid; // Process id.
40
st.nextToken(); // String comm; // The command name.
41
st.nextToken(); // String state;
42
st.nextToken(); // int ppid; // Parent process id. */
43
st.nextToken(); // int pgrp; // Process group. */
44
st.nextToken(); // int session;
45
st.nextToken(); // int ttry_nr;
46
st.nextToken(); // int tpgid;
47
st.nextToken(); // long flags;
48
long minflt = Long.parseLong(st.nextToken()); // Minor page faults (didn't need to load a page from disk). */
49
st.nextToken(); // long cminflt; // Minor page faults for the process and it's children. */
50
long majflt = Long.parseLong(st.nextToken()); // Major page faults. */
51
st.nextToken(); // long cmajflt; // Major page faults for the process and it's children. */
52
long utime = Long.parseLong(st.nextToken()); // User time in jiffies. */
53
long stime = Long.parseLong(st.nextToken()); // System time in jiffies. */
54
st.nextToken(); // long cutime; // User time for the process and it's children. */
55
st.nextToken(); // long cstime; // System time for the process and it's children. */
56

57                 //addScalar(scalars, InternalDimensions.USER_TIME, utime*JIFFIES);
58
addScalar(scalars, InternalDimensions.KERNEL_TIME, stime*JIFFIES);
59                 addScalar(scalars, InternalDimensions.CPU_TIME, (utime+stime)*JIFFIES);
60                 addScalar(scalars, InternalDimensions.SOFT_PAGE_FAULTS, minflt);
61                 addScalar(scalars, InternalDimensions.HARD_PAGE_FAULTS, majflt);
62             }
63
64             /**
65              * The status memory values values for a Linux process, that is the values that come from /proc/self/statm.
66              * The names of the variables match the man proc page.
67              */

68             st= readTokens("/proc/self/statm", false); //$NON-NLS-1$
69
if (st != null) {
70                 st.nextToken(); // int size; // Size of the process in pages
71
int resident= Integer.parseInt(st.nextToken()); // Resident size in pages.
72
st.nextToken(); // int shared; // Shared size in pages.
73
int trs= Integer.parseInt(st.nextToken()); // Text (code) size in pages.
74
int drs= Integer.parseInt(st.nextToken()); // Data/Stack size in pages.
75
int lrs= Integer.parseInt(st.nextToken()); // Library size in pages.
76
// st.nextToken(); // int dt; // Dirty pages.
77

78                 addScalar(scalars, InternalDimensions.WORKING_SET, resident*PAGESIZE);
79                 addScalar(scalars, InternalDimensions.TRS, trs*PAGESIZE);
80                 addScalar(scalars, InternalDimensions.DRS, drs*PAGESIZE);
81                 addScalar(scalars, InternalDimensions.LRS, lrs*PAGESIZE);
82             }
83             
84             long currentTime= System.currentTimeMillis();
85             if (!PerformanceTestPlugin.isOldDB())
86                 addScalar(scalars, InternalDimensions.SYSTEM_TIME, currentTime);
87             
88             if (fgHasElapsedTime) {
89                 if (fgStartupTime == 0) {
90                     String JavaDoc t= System.getProperty("eclipse.startTime"); //$NON-NLS-1$
91
if (t != null) {
92                         try {
93                             fgStartupTime= Long.parseLong(t);
94                         } catch (NumberFormatException JavaDoc e) {
95                             fgHasElapsedTime= false;
96                         }
97                     } else
98                         fgHasElapsedTime= false;
99                 }
100                 if (fgHasElapsedTime)
101                     addScalar(scalars, InternalDimensions.ELAPSED_PROCESS, currentTime-fgStartupTime);
102             }
103             
104             super.collectOperatingSystemCounters(scalars);
105         }
106     }
107     
108     /**
109      * Write out the global machine counters for Linux.
110      * @param scalars
111      */

112     protected void collectGlobalPerformanceInfo(Map JavaDoc scalars) {
113         synchronized(this) {
114             /**
115              * The meminfo values for a Linux machine, that is the values that come from /proc/meminfo.
116              */

117             // /proc/meminfo is formatted on linux - use byte-counted output from free
118
StringTokenizer JavaDoc st= readOutput("free -b", true); //$NON-NLS-1$
119
if (st != null) {
120                 st.nextToken(); // throw away label
121
long total= Long.parseLong(st.nextToken());
122                 long used= Long.parseLong(st.nextToken());
123                 long free= Long.parseLong(st.nextToken());
124                 st.nextToken(); // long shared;
125
long buffers= Long.parseLong(st.nextToken());
126                 long cache= Long.parseLong(st.nextToken());
127         
128                 addScalar(scalars, InternalDimensions.PHYSICAL_TOTAL, total);
129                 addScalar(scalars, InternalDimensions.USED_LINUX_MEM, used);
130                 addScalar(scalars, InternalDimensions.FREE_LINUX_MEM, free);
131                 addScalar(scalars, InternalDimensions.BUFFERS_LINUX, buffers);
132                 addScalar(scalars, InternalDimensions.SYSTEM_CACHE, cache);
133             }
134             super.collectGlobalPerformanceInfo(scalars);
135         }
136     }
137
138     private StringTokenizer JavaDoc readTokens(String JavaDoc procPath, boolean skipFirst) {
139         BufferedReader JavaDoc rdr= null;
140         try {
141             rdr= new BufferedReader JavaDoc(new FileReader JavaDoc(procPath));
142             if (skipFirst)
143                 rdr.readLine(); // throw away the heading line
144
return new StringTokenizer JavaDoc(rdr.readLine());
145         } catch (IOException JavaDoc e) {
146             PerformanceTestPlugin.log(e);
147         } finally {
148             try {
149                 if (rdr != null)
150                     rdr.close();
151             } catch (IOException JavaDoc e) {
152                 // silently ignored
153
}
154         }
155         return null;
156     }
157     
158     private StringTokenizer JavaDoc readOutput(String JavaDoc cmd, boolean skipFirst) {
159         BufferedReader JavaDoc rdr= null;
160         try {
161             Process JavaDoc process= Runtime.getRuntime().exec(cmd);
162             rdr= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(process.getInputStream()));
163             if (skipFirst)
164                 rdr.readLine(); // throw away the heading line
165
return new StringTokenizer JavaDoc(rdr.readLine());
166         } catch (IOException JavaDoc e) {
167             PerformanceTestPlugin.log(e);
168         } finally {
169             try {
170                 if (rdr != null)
171                     rdr.close();
172             } catch (IOException JavaDoc e) {
173                 // silently ignored
174
}
175         }
176         return null;
177     }
178 }
179
Popular Tags