119 try (BufferedReader lines = p1.outputReader()) {
120 Duration lastCpu = Duration.ofMillis(0L);
121 for (int j = 0; j < 10; j++) {
122
123 p1.sendAction("cpuloop", cpuLoopTime);
124 p1.sendAction("cputime", "");
125
126 // Read cputime from child
127 Duration childCpuTime = null;
128 // Read lines from the child until the result from cputime is returned
129 String s;
130 while ((s = lines.readLine()) != null) {
131 String[] split = s.trim().split(" ");
132 if (split.length == 3 && split[1].equals("cputime")) {
133 long nanos = Long.valueOf(split[2]);
134 childCpuTime = Duration.ofNanos(nanos);
135 break; // found the result we're looking for
136 }
137 }
138
139
140 ProcessHandle.Info info = p1.info();
141 System.out.printf(" info: %s%n", info);
142
143 if (info.user().isPresent()) {
144 String user = info.user().get();
145 Assert.assertNotNull(user, "User name");
146 Assert.assertEquals(user, whoami, "User name");
147 }
148
149 Optional<String> command = info.command();
150 if (command.isPresent()) {
151 String javaExe = System.getProperty("test.jdk") +
152 File.separator + "bin" + File.separator + "java";
153 String expected = Platform.isWindows() ? javaExe + ".exe" : javaExe;
154 Path expectedPath = Paths.get(expected);
155 Path actualPath = Paths.get(command.get());
156 Assert.assertTrue(Files.isSameFile(expectedPath, actualPath),
157 "Command: expected: " + javaExe + ", actual: " + command.get());
158 }
159
160 if (info.arguments().isPresent()) {
161 String[] args = info.arguments().get();
162
163 if (Platform.isLinux() || Platform.isOSX()) {
164 int offset = args.length - extraArgs.length;
165 for (int i = 0; i < extraArgs.length; i++) {
166 Assert.assertEquals(args[offset + i], extraArgs[i],
167 "Actual argument mismatch, index: " + i);
168 }
169 } else if (Platform.isSolaris()) {
170 Assert.assertEquals(args.length, 1,
171 "Expected argument list length: 1");
172 Assert.assertNotNull(args[0],
173 "Expected an argument");
174 } else {
175 System.out.printf("No argument test for OS: %s%n", Platform.getOsName());
176 }
177
178 // Now check that the first argument is not the same as the executed command
179 if (args.length > 0) {
180 Assert.assertNotEquals(args[0], command,
181 "First argument should not be the executable: args[0]: "
182 + args[0] + ", command: " + command);
183 }
184 }
185
186 if (info.totalCpuDuration().isPresent()) {
187 Duration totalCPU = info.totalCpuDuration().get();
188 Duration epsilon = Duration.ofMillis(200L);
189 if (childCpuTime != null) {
190 System.out.printf(" info.totalCPU: %s, childCpuTime: %s, diff: %s%n",
191 totalCPU.toNanos(), childCpuTime.toNanos(),
192 childCpuTime.toNanos() - totalCPU.toNanos());
193 Assert.assertTrue(checkEpsilon(childCpuTime, totalCPU, epsilon),
194 childCpuTime + " should be within " +
225 @Test
226 public static void test3() {
227 try {
228 for (int sleepTime : Arrays.asList(1, 2)) {
229 Process p = spawn("sleep", String.valueOf(sleepTime));
230 ProcessHandle.Info info = p.info();
231 System.out.printf(" info: %s%n", info);
232
233 if (info.user().isPresent()) {
234 String user = info.user().get();
235 Assert.assertNotNull(user);
236 Assert.assertEquals(user, whoami);
237 }
238 if (info.command().isPresent()) {
239 String command = info.command().get();
240 String expected = Platform.isWindows() ? "sleep.exe" : "sleep";
241 Assert.assertTrue(command.endsWith(expected), "Command: expected: \'" +
242 expected + "\', actual: " + command);
243
244 // Verify the command exists and is executable
245 File exe = new File(command);
246 Assert.assertTrue(exe.exists(), "command must exist: " + exe);
247 Assert.assertTrue(exe.canExecute(), "command must be executable: " + exe);
248 }
249 if (info.arguments().isPresent()) {
250 String[] args = info.arguments().get();
251 if (args.length > 0) {
252 Assert.assertEquals(args[0], String.valueOf(sleepTime));
253 }
254 }
255 Assert.assertTrue(p.waitFor(15, TimeUnit.SECONDS));
256 }
257 } catch (IOException | InterruptedException ex) {
258 ex.printStackTrace(System.out);
259 } finally {
260 // Destroy any children that still exist
261 ProcessUtil.destroyProcessTree(ProcessHandle.current());
262 }
263 }
264
265 /**
266 * Cross check the cputime reported from java.management with that for the current process.
267 */
268 @Test
269 public static void test4() {
270 Duration myCputime1 = ProcessUtil.MXBeanCpuTime();
271
272 Optional<Duration> dur1 = ProcessHandle.current().info().totalCpuDuration();
273
274 Duration myCputime2 = ProcessUtil.MXBeanCpuTime();
275
276 Optional<Duration> dur2 = ProcessHandle.current().info().totalCpuDuration();
277
278 if (dur1.isPresent() && dur2.isPresent()) {
279 Duration total1 = dur1.get();
280 Duration total2 = dur2.get();
281 System.out.printf(" total1 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
282 Objects.toString(total1), myCputime1, myCputime1.minus(total1));
283 System.out.printf(" total2 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
284 Objects.toString(total2), myCputime2, myCputime2.minus(total2));
285
286 Duration epsilon = Duration.ofMillis(200L); // Epsilon is 200ms.
287 Assert.assertTrue(checkEpsilon(myCputime1, myCputime2, epsilon),
288 myCputime1.toNanos() + " should be within " + epsilon
289 + " of " + myCputime2.toNanos());
290 Assert.assertTrue(checkEpsilon(total1, total2, epsilon),
291 total1.toNanos() + " should be within " + epsilon
292 + " of " + total2.toNanos());
293 Assert.assertTrue(checkEpsilon(myCputime1, total1, epsilon),
294 myCputime1.toNanos() + " should be within " + epsilon
295 + " of " + total1.toNanos());
|
119 try (BufferedReader lines = p1.outputReader()) {
120 Duration lastCpu = Duration.ofMillis(0L);
121 for (int j = 0; j < 10; j++) {
122
123 p1.sendAction("cpuloop", cpuLoopTime);
124 p1.sendAction("cputime", "");
125
126 // Read cputime from child
127 Duration childCpuTime = null;
128 // Read lines from the child until the result from cputime is returned
129 String s;
130 while ((s = lines.readLine()) != null) {
131 String[] split = s.trim().split(" ");
132 if (split.length == 3 && split[1].equals("cputime")) {
133 long nanos = Long.valueOf(split[2]);
134 childCpuTime = Duration.ofNanos(nanos);
135 break; // found the result we're looking for
136 }
137 }
138
139 if (Platform.isAix()) {
140 // Unfortunately, on AIX the usr/sys times reported through
141 // /proc/<pid>/status which are used by ProcessHandle.Info
142 // are running slow compared to the corresponding times reported
143 // by the times()/getrusage() system calls which are used by
144 // OperatingSystemMXBean.getProcessCpuTime() and returned by
145 // the JavaChild for the "cputime" command.
146 // So we better wait a little bit to get plausible values here.
147 Thread.sleep(1000);
148 }
149 ProcessHandle.Info info = p1.info();
150 System.out.printf(" info: %s%n", info);
151
152 if (info.user().isPresent()) {
153 String user = info.user().get();
154 Assert.assertNotNull(user, "User name");
155 Assert.assertEquals(user, whoami, "User name");
156 }
157
158 Optional<String> command = info.command();
159 if (command.isPresent()) {
160 String javaExe = System.getProperty("test.jdk") +
161 File.separator + "bin" + File.separator + "java";
162 String expected = Platform.isWindows() ? javaExe + ".exe" : javaExe;
163 Path expectedPath = Paths.get(expected);
164 Path actualPath = Paths.get(command.get());
165 Assert.assertTrue(Files.isSameFile(expectedPath, actualPath),
166 "Command: expected: " + javaExe + ", actual: " + command.get());
167 }
168
169 if (info.arguments().isPresent()) {
170 String[] args = info.arguments().get();
171
172 if (Platform.isLinux() || Platform.isOSX()) {
173 int offset = args.length - extraArgs.length;
174 for (int i = 0; i < extraArgs.length; i++) {
175 Assert.assertEquals(args[offset + i], extraArgs[i],
176 "Actual argument mismatch, index: " + i);
177 }
178 } else {
179 // Arguments on Solaris and AIX come from /proc/pid/psinfo and
180 // are usually truncated to 80 characters so there's not much
181 // we can check here.
182 System.out.printf("No argument test for OS: %s%n", Platform.getOsName());
183 }
184
185 // Now check that the first argument is not the same as the executed command
186 if (args.length > 0) {
187 Assert.assertNotEquals(args[0], command,
188 "First argument should not be the executable: args[0]: "
189 + args[0] + ", command: " + command);
190 }
191 }
192
193 if (info.totalCpuDuration().isPresent()) {
194 Duration totalCPU = info.totalCpuDuration().get();
195 Duration epsilon = Duration.ofMillis(200L);
196 if (childCpuTime != null) {
197 System.out.printf(" info.totalCPU: %s, childCpuTime: %s, diff: %s%n",
198 totalCPU.toNanos(), childCpuTime.toNanos(),
199 childCpuTime.toNanos() - totalCPU.toNanos());
200 Assert.assertTrue(checkEpsilon(childCpuTime, totalCPU, epsilon),
201 childCpuTime + " should be within " +
232 @Test
233 public static void test3() {
234 try {
235 for (int sleepTime : Arrays.asList(1, 2)) {
236 Process p = spawn("sleep", String.valueOf(sleepTime));
237 ProcessHandle.Info info = p.info();
238 System.out.printf(" info: %s%n", info);
239
240 if (info.user().isPresent()) {
241 String user = info.user().get();
242 Assert.assertNotNull(user);
243 Assert.assertEquals(user, whoami);
244 }
245 if (info.command().isPresent()) {
246 String command = info.command().get();
247 String expected = Platform.isWindows() ? "sleep.exe" : "sleep";
248 Assert.assertTrue(command.endsWith(expected), "Command: expected: \'" +
249 expected + "\', actual: " + command);
250
251 // Verify the command exists and is executable
252 if (!Platform.isAix()) {
253 // On Aix, Info.command() only returns arg[0] as command which
254 // doesn't necessarily contains the full path to the executable.
255 File exe = new File(command);
256 Assert.assertTrue(exe.exists(), "command must exist: " + exe);
257 Assert.assertTrue(exe.canExecute(), "command must be executable: " + exe);
258 }
259 }
260 if (info.arguments().isPresent()) {
261 String[] args = info.arguments().get();
262 if (args.length > 0) {
263 Assert.assertEquals(args[0], String.valueOf(sleepTime));
264 }
265 }
266 Assert.assertTrue(p.waitFor(15, TimeUnit.SECONDS));
267 }
268 } catch (IOException | InterruptedException ex) {
269 ex.printStackTrace(System.out);
270 } finally {
271 // Destroy any children that still exist
272 ProcessUtil.destroyProcessTree(ProcessHandle.current());
273 }
274 }
275
276 /**
277 * Cross check the cputime reported from java.management with that for the current process.
278 */
279 @Test
280 public static void test4() {
281 Duration myCputime1 = ProcessUtil.MXBeanCpuTime();
282
283 if (Platform.isAix()) {
284 // Unfortunately, on AIX the usr/sys times reported through
285 // /proc/<pid>/status which are used by ProcessHandle.Info
286 // are running slow compared to the corresponding times reported
287 // by the times()/getrusage() system calls which are used by
288 // OperatingSystemMXBean.getProcessCpuTime() and returned by
289 // the JavaChild for the "cputime" command.
290 // So we better wait a little bit to get plausible values here.
291 try {
292 Thread.sleep(1000);
293 } catch (InterruptedException ex) {}
294 }
295 Optional<Duration> dur1 = ProcessHandle.current().info().totalCpuDuration();
296
297 Duration myCputime2 = ProcessUtil.MXBeanCpuTime();
298
299 if (Platform.isAix()) {
300 try {
301 Thread.sleep(1000);
302 } catch (InterruptedException ex) {}
303 }
304 Optional<Duration> dur2 = ProcessHandle.current().info().totalCpuDuration();
305
306 if (dur1.isPresent() && dur2.isPresent()) {
307 Duration total1 = dur1.get();
308 Duration total2 = dur2.get();
309 System.out.printf(" total1 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
310 Objects.toString(total1), myCputime1, myCputime1.minus(total1));
311 System.out.printf(" total2 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
312 Objects.toString(total2), myCputime2, myCputime2.minus(total2));
313
314 Duration epsilon = Duration.ofMillis(200L); // Epsilon is 200ms.
315 Assert.assertTrue(checkEpsilon(myCputime1, myCputime2, epsilon),
316 myCputime1.toNanos() + " should be within " + epsilon
317 + " of " + myCputime2.toNanos());
318 Assert.assertTrue(checkEpsilon(total1, total2, epsilon),
319 total1.toNanos() + " should be within " + epsilon
320 + " of " + total2.toNanos());
321 Assert.assertTrue(checkEpsilon(myCputime1, total1, epsilon),
322 myCputime1.toNanos() + " should be within " + epsilon
323 + " of " + total1.toNanos());
|