6

I'm currently testing Process API from java 9 and I have some problem with following code:

Process process = new ProcessBuilder( List.of("ping", "-i", "1", "-c", "4", "google.com")).start();

CompletableFuture<Void> startTimeFuture = process.toHandle().onExit()
            .thenApply(ProcessHandle::info)
            .thenApply(ProcessHandle.Info::startInstant)
            .thenAccept(System.out::println);

startTimeFuture.get();

When I execute this snippet I get Optional.empty in terminal. Javadoc states that info method returns any data if it is available, so I suspect that JVM can't acquire information about spawned process. But when I try to get pid from ProcessHandle in future I get proper value.

To sum up, my question:

Is there any way to get non empty ProcessHandle.Info after calling onExit()?

I'm using Ubuntu 16.04 LTS

Edit - This is the output from terminal when I execute ping -i 1 -c 5 google.com

PING google.com (xxx.xxx.16.46) 56(84) bytes of data.

64 bytes from waw02s14-in-f14.1e100.net (xxx.xxx.16.46): icmp_seq=1 ttl=52 time=6.71 ms

64 bytes from waw02s14-in-f14.1e100.net (xxx.xxx.16.46): icmp_seq=2 ttl=52 time=6.26 ms

64 bytes from waw02s14-in-f14.1e100.net (xxx.xxx.16.46): icmp_seq=3 ttl=52 time=16.6 ms

64 bytes from waw02s14-in-f14.1e100.net (xxx.xxx.16.46): icmp_seq=4 ttl=52 time=10.6 ms

64 bytes from waw02s14-in-f14.1e100.net (xxx.xxx.16.46): icmp_seq=5 ttl=52 time=13.4 ms

--- google.com ping statistics ---

5 packets transmitted, 5 received, 0% packet loss, time 4007ms rtt min/avg/max/mdev = 6.267/10.746/16.667/3.968 ms

Updated use-case:- I want to check if I can, how much time given command was executing, for instance by calling ProcessHandle.Info::totalCpuDuration

Naman
  • 27,789
  • 26
  • 218
  • 353
Maciek Murawski
  • 414
  • 4
  • 15
  • 1
    1. `inheritIO().start();` helps display if the ping is working with the given args. 2. Why is it that you need the startInstant `onExit`? Can you not store the same before calling onExit and use further? – Naman Mar 08 '18 at 04:35
  • Thanks for that `inheritIO().start();`, I see that output is proper (same as in terminal), but still Info object has default data. I don't understand your second point. I'm just messing with API, this code is just my demo – Maciek Murawski Mar 08 '18 at 06:55
  • Well, my second point was mostly onto emphasizing on gathering the `Info` prior to doing `onExit().get()` and then making use of it. Though I am not sure of what you're trying to achieve here. – Naman Mar 08 '18 at 07:26
  • 1
    This is for test purposes. I don't have any specific goal for this code. I wanted to test if I can check how much time given command was executing, for instance by calling `ProcessHandle.Info::totalCpuDuration` – Maciek Murawski Mar 08 '18 at 07:48
  • One way to fetch the `Info` over a `onExit` for a ProcessHandle could have been to make use of **[`complete`](https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/CompletableFuture.html#complete-T-)**, though contradictorily(to `startInstant`) this wouldn't then print out the `totalCpuDuration`... Seem like this might be in effect here - *The attributes of a process vary by operating system and are not available in all implementations. In addition, information about processes is limited by the operating system privileges of the process making the request.* – Naman Mar 08 '18 at 08:47

1 Answers1

2

I think I found cause of this behaviour (at least on linux distribution).

ProcessHandle.Info object is created with following method:

public static ProcessHandle.Info info(long pid, long startTime) {
    Info info = new Info();
    info.info0(pid);
    if (startTime != info.startTime) {
        info.command = null;
        info.arguments = null;
        info.startTime = -1L;
        info.totalTime = -1L;
        info.user = null;
    }
    return info;
}

where info.info0(pid) is call to native method. So I've downloaded openjdk source code and checked this method implementation. On linux JVM retrieves process data by reading /proc/{pid}/stat,/proc/{pid}/cmdline, /proc/{pid}/exe which are no longer available after process termination.

To answer my question:

There is no way to get ProcessHandle.Info for finished process.

Eugene
  • 117,005
  • 15
  • 201
  • 306
Maciek Murawski
  • 414
  • 4
  • 15