Search code examples
javajava-9processhandle

ProcessHandle onExit has empty data


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


Solution

  • 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.