-
Notifications
You must be signed in to change notification settings - Fork 69
/
PerformanceMonitorLinux.java
176 lines (160 loc) · 7.81 KB
/
PerformanceMonitorLinux.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
* Copyright (c) 2003, 2016 IBM Corporation and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors: IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.test.internal.performance;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.StringTokenizer;
class PerformanceMonitorLinux extends PerformanceMonitor {
private static long PAGESIZE = 4096;
private static long JIFFIES = 10L;
private static boolean fgHasElapsedTime = true;
private static long fgStartupTime;
/**
* Write out operating system counters for Linux.
*
* @param scalars
*/
@Override
protected void collectOperatingSystemCounters(Map scalars) {
synchronized (this) {
/**
* The status values for a Linux process, that is the values that come from /proc/self/stat. The names of the variables match
* the man proc page.
*/
StringTokenizer st = readTokens("/proc/self/stat", false); //$NON-NLS-1$
if (st != null) {
st.nextToken(); // int pid; // Process id.
st.nextToken(); // String comm; // The command name.
st.nextToken(); // String state;
st.nextToken(); // int ppid; // Parent process id. */
st.nextToken(); // int pgrp; // Process group. */
st.nextToken(); // int session;
st.nextToken(); // int ttry_nr;
st.nextToken(); // int tpgid;
st.nextToken(); // long flags;
long minflt = Long.parseLong(st.nextToken()); // Minor page faults (didn't need to load a page from disk). */
st.nextToken(); // long cminflt; // Minor page faults for the process and it's children. */
long majflt = Long.parseLong(st.nextToken()); // Major page faults. */
st.nextToken(); // long cmajflt; // Major page faults for the process and it's children. */
long utime = Long.parseLong(st.nextToken()); // User time in jiffies. */
long stime = Long.parseLong(st.nextToken()); // System time in jiffies. */
st.nextToken(); // long cutime; // User time for the process and it's children. */
st.nextToken(); // long cstime; // System time for the process and it's children. */
// addScalar(scalars, InternalDimensions.USER_TIME, utime*JIFFIES);
addScalar(scalars, InternalDimensions.KERNEL_TIME, stime * JIFFIES);
addScalar(scalars, InternalDimensions.CPU_TIME, (utime + stime) * JIFFIES);
addScalar(scalars, InternalDimensions.SOFT_PAGE_FAULTS, minflt);
addScalar(scalars, InternalDimensions.HARD_PAGE_FAULTS, majflt);
}
/**
* The status memory values values for a Linux process, that is the values that come from /proc/self/statm. The names of the
* variables match the man proc page.
*/
st = readTokens("/proc/self/statm", false); //$NON-NLS-1$
if (st != null) {
st.nextToken(); // int size; // Size of the process in pages
int resident = Integer.parseInt(st.nextToken()); // Resident size in pages.
st.nextToken(); // int shared; // Shared size in pages.
int trs = Integer.parseInt(st.nextToken()); // Text (code) size in pages.
int drs = Integer.parseInt(st.nextToken()); // Data/Stack size in pages.
int lrs = Integer.parseInt(st.nextToken()); // Library size in pages.
// st.nextToken(); // int dt; // Dirty pages.
addScalar(scalars, InternalDimensions.WORKING_SET, resident * PAGESIZE);
addScalar(scalars, InternalDimensions.TRS, trs * PAGESIZE);
addScalar(scalars, InternalDimensions.DRS, drs * PAGESIZE);
addScalar(scalars, InternalDimensions.LRS, lrs * PAGESIZE);
}
long currentTime = System.currentTimeMillis();
addScalar(scalars, InternalDimensions.SYSTEM_TIME, currentTime);
if (fgHasElapsedTime) {
if (fgStartupTime == 0) {
String t = System.getProperty("eclipse.startTime"); //$NON-NLS-1$
if (t != null) {
try {
fgStartupTime = Long.parseLong(t);
} catch (NumberFormatException e) {
fgHasElapsedTime = false;
}
} else
fgHasElapsedTime = false;
}
if (fgHasElapsedTime)
addScalar(scalars, InternalDimensions.ELAPSED_PROCESS, currentTime - fgStartupTime);
}
super.collectOperatingSystemCounters(scalars);
}
}
/**
* Write out the global machine counters for Linux.
*
* @param scalars
*/
@Override
protected void collectGlobalPerformanceInfo(Map scalars) {
synchronized (this) {
/**
* The meminfo values for a Linux machine, that is the values that come from /proc/meminfo.
*/
// /proc/meminfo is formatted on linux - use byte-counted output from free
StringTokenizer st = readOutput("free -b", true); //$NON-NLS-1$
if (st != null) {
st.nextToken(); // throw away label
long total = Long.parseLong(st.nextToken());
long used = Long.parseLong(st.nextToken());
long free = Long.parseLong(st.nextToken());
st.nextToken(); // long shared;
long buffers = Long.parseLong(st.nextToken());
long cache = Long.parseLong(st.nextToken());
addScalar(scalars, InternalDimensions.PHYSICAL_TOTAL, total);
addScalar(scalars, InternalDimensions.USED_LINUX_MEM, used);
addScalar(scalars, InternalDimensions.FREE_LINUX_MEM, free);
addScalar(scalars, InternalDimensions.BUFFERS_LINUX, buffers);
addScalar(scalars, InternalDimensions.SYSTEM_CACHE, cache);
}
super.collectGlobalPerformanceInfo(scalars);
}
}
private StringTokenizer readTokens(String procPath, boolean skipFirst) {
try (BufferedReader rdr = new BufferedReader(new FileReader(procPath));){
if (skipFirst)
rdr.readLine(); // throw away the heading line
return new StringTokenizer(rdr.readLine());
} catch (IOException e) {
PerformanceTestPlugin.log(e);
}
return null;
}
private StringTokenizer readOutput(String cmd, boolean skipFirst) {
BufferedReader rdr = null;
try {
Process process = Runtime.getRuntime().exec(cmd);
rdr = new BufferedReader(new InputStreamReader(process.getInputStream()));
if (skipFirst)
rdr.readLine(); // throw away the heading line
return new StringTokenizer(rdr.readLine());
} catch (IOException e) {
PerformanceTestPlugin.log(e);
}
finally {
try {
if (rdr != null)
rdr.close();
} catch (IOException e) {
// silently ignored
}
}
return null;
}
}