134 childCpuTime = Duration.ofNanos(nanos); |
134 childCpuTime = Duration.ofNanos(nanos); |
135 break; // found the result we're looking for |
135 break; // found the result we're looking for |
136 } |
136 } |
137 } |
137 } |
138 |
138 |
139 |
139 if (Platform.isAix()) { |
|
140 // Unfortunately, on AIX the usr/sys times reported through |
|
141 // /proc/<pid>/psinfo 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 // This is because /proc/<pid>/status is only updated once a second. |
|
147 // So we better wait a little bit to get plausible values here. |
|
148 Thread.sleep(1000); |
|
149 } |
140 ProcessHandle.Info info = p1.info(); |
150 ProcessHandle.Info info = p1.info(); |
141 System.out.printf(" info: %s%n", info); |
151 System.out.printf(" info: %s%n", info); |
142 |
152 |
143 if (info.user().isPresent()) { |
153 if (info.user().isPresent()) { |
144 String user = info.user().get(); |
154 String user = info.user().get(); |
158 } |
168 } |
159 |
169 |
160 if (info.arguments().isPresent()) { |
170 if (info.arguments().isPresent()) { |
161 String[] args = info.arguments().get(); |
171 String[] args = info.arguments().get(); |
162 |
172 |
163 if (Platform.isLinux() || Platform.isOSX()) { |
173 int offset = args.length - extraArgs.length; |
164 int offset = args.length - extraArgs.length; |
174 for (int i = 0; i < extraArgs.length; i++) { |
165 for (int i = 0; i < extraArgs.length; i++) { |
175 Assert.assertEquals(args[offset + i], extraArgs[i], |
166 Assert.assertEquals(args[offset + i], extraArgs[i], |
176 "Actual argument mismatch, index: " + 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 } |
177 |
178 |
178 // Now check that the first argument is not the same as the executed command |
179 // Now check that the first argument is not the same as the executed command |
179 if (args.length > 0) { |
180 if (args.length > 0) { |
180 Assert.assertNotEquals(args[0], command, |
181 Assert.assertNotEquals(args[0], command, |
181 "First argument should not be the executable: args[0]: " |
182 "First argument should not be the executable: args[0]: " |
182 + args[0] + ", command: " + command); |
183 + args[0] + ", command: " + command); |
|
184 } |
|
185 } |
|
186 |
|
187 if (command.isPresent() && info.arguments().isPresent()) { |
|
188 // If both, 'command' and 'arguments' are present, |
|
189 // 'commandLine' is just the concatenation of the two. |
|
190 Assert.assertTrue(info.commandLine().isPresent(), |
|
191 "commandLine() must be available"); |
|
192 |
|
193 String javaExe = System.getProperty("test.jdk") + |
|
194 File.separator + "bin" + File.separator + "java"; |
|
195 String expected = Platform.isWindows() ? javaExe + ".exe" : javaExe; |
|
196 Path expectedPath = Paths.get(expected); |
|
197 String commandLine = info.commandLine().get(); |
|
198 String commandLineCmd = commandLine.split(" ")[0]; |
|
199 Path commandLineCmdPath = Paths.get(commandLineCmd); |
|
200 Assert.assertTrue(Files.isSameFile(commandLineCmdPath, expectedPath), |
|
201 "commandLine() should start with: " + expectedPath + |
|
202 " but starts with " + commandLineCmdPath); |
|
203 |
|
204 List<String> allArgs = p1.getArgs(); |
|
205 for (int i = 0; i < allArgs.size(); i++) { |
|
206 Assert.assertTrue(commandLine.contains(allArgs.get(i)), |
|
207 "commandLine() must contain argument: " + allArgs.get(i)); |
|
208 } |
|
209 } else if (info.commandLine().isPresent()) { |
|
210 // If we only have the commandLine() we can only do some basic checks... |
|
211 String commandLine = info.commandLine().get(); |
|
212 String javaExe = "java" + (Platform.isWindows() ? ".exe": ""); |
|
213 int pos = commandLine.indexOf(javaExe); |
|
214 Assert.assertTrue(pos > 0, "commandLine() should at least contain 'java'"); |
|
215 |
|
216 pos += javaExe.length() + 1; // +1 for the space after the command |
|
217 List<String> allArgs = p1.getArgs(); |
|
218 // First argument is the command - skip it here as we've already checked that. |
|
219 for (int i = 1; (i < allArgs.size()) && |
|
220 (pos + allArgs.get(i).length() < commandLine.length()); i++) { |
|
221 Assert.assertTrue(commandLine.contains(allArgs.get(i)), |
|
222 "commandLine() must contain argument: " + allArgs.get(i)); |
|
223 pos += allArgs.get(i).length() + 1; |
183 } |
224 } |
184 } |
225 } |
185 |
226 |
186 if (info.totalCpuDuration().isPresent()) { |
227 if (info.totalCpuDuration().isPresent()) { |
187 Duration totalCPU = info.totalCpuDuration().get(); |
228 Duration totalCPU = info.totalCpuDuration().get(); |
267 */ |
308 */ |
268 @Test |
309 @Test |
269 public static void test4() { |
310 public static void test4() { |
270 Duration myCputime1 = ProcessUtil.MXBeanCpuTime(); |
311 Duration myCputime1 = ProcessUtil.MXBeanCpuTime(); |
271 |
312 |
|
313 if (Platform.isAix()) { |
|
314 // Unfortunately, on AIX the usr/sys times reported through |
|
315 // /proc/<pid>/psinfo which are used by ProcessHandle.Info |
|
316 // are running slow compared to the corresponding times reported |
|
317 // by the times()/getrusage() system calls which are used by |
|
318 // OperatingSystemMXBean.getProcessCpuTime() and returned by |
|
319 // the JavaChild for the "cputime" command. |
|
320 // So we better wait a little bit to get plausible values here. |
|
321 try { |
|
322 Thread.sleep(1000); |
|
323 } catch (InterruptedException ex) {} |
|
324 } |
272 Optional<Duration> dur1 = ProcessHandle.current().info().totalCpuDuration(); |
325 Optional<Duration> dur1 = ProcessHandle.current().info().totalCpuDuration(); |
273 |
326 |
274 Duration myCputime2 = ProcessUtil.MXBeanCpuTime(); |
327 Duration myCputime2 = ProcessUtil.MXBeanCpuTime(); |
275 |
328 |
|
329 if (Platform.isAix()) { |
|
330 try { |
|
331 Thread.sleep(1000); |
|
332 } catch (InterruptedException ex) {} |
|
333 } |
276 Optional<Duration> dur2 = ProcessHandle.current().info().totalCpuDuration(); |
334 Optional<Duration> dur2 = ProcessHandle.current().info().totalCpuDuration(); |
277 |
335 |
278 if (dur1.isPresent() && dur2.isPresent()) { |
336 if (dur1.isPresent() && dur2.isPresent()) { |
279 Duration total1 = dur1.get(); |
337 Duration total1 = dur1.get(); |
280 Duration total2 = dur2.get(); |
338 Duration total2 = dur2.get(); |